quintodrome/ui/src/dialogs/useDialog.jsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

31 lines
689 B
React
Raw Normal View History

2023-01-19 18:52:55 -09:00
import { useCallback, useMemo, useState } from 'react'
// Idea from https://blog.bitsrc.io/new-react-design-pattern-return-component-from-hooks-79215c3eac00
export const useDialog = () => {
const [anchorEl, setAnchorEl] = useState(null)
const openDialog = useCallback((event) => {
2023-01-19 18:52:55 -09:00
event?.stopPropagation()
setAnchorEl(event.currentTarget)
}, [])
const closeDialog = useCallback((event) => {
2023-01-19 18:52:55 -09:00
event?.stopPropagation()
setAnchorEl(null)
}, [])
const props = useMemo(() => {
return {
anchorEl,
open: Boolean(anchorEl),
onClose: closeDialog,
2023-01-19 18:52:55 -09:00
}
}, [anchorEl, closeDialog])
2023-01-19 18:52:55 -09:00
return {
openDialog,
closeDialog,
2023-01-19 18:52:55 -09:00
props,
}
}