Skip to content

Commit

Permalink
[field] Fix nesting of fields in change list
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent b87453b commit 0bd7850
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/@sanity/field/src/utils/useHover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {useState, useRef, useEffect} from 'react'

export function useHover<T extends HTMLElement>(): [React.MutableRefObject<T | null>, boolean] {
const [value, setValue] = useState(false)

const ref = useRef<T | null>(null)

const handleMouseOver = () => setValue(true)
const handleMouseOut = () => setValue(false)

useEffect(() => {
const node = ref.current
if (!node) {
return () => undefined
}

node.addEventListener('mouseover', handleMouseOver)
node.addEventListener('mouseout', handleMouseOut)

return () => {
node.removeEventListener('mouseover', handleMouseOver)
node.removeEventListener('mouseout', handleMouseOut)
}
}, [ref.current])

return [ref, value]
}

0 comments on commit 0bd7850

Please sign in to comment.