* Add POC menu abstraction * Better platform handling * Remove ignore * Add some menu items * Add controlled dropdown * Pass through a11y props * Ignore uninitialized context * Tweaks * Usability improvements * Rename handlers to props * Add radix comment * Ignore known type * Remove todo * Move storybook item * Improve Group matching * Adjust theming
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import React from 'react'
|
|
|
|
import {useDialogStateContext} from '#/state/dialogs'
|
|
import {
|
|
DialogContextProps,
|
|
DialogControlRefProps,
|
|
DialogOuterProps,
|
|
} from '#/components/Dialog/types'
|
|
|
|
export const Context = React.createContext<DialogContextProps>({
|
|
close: () => {},
|
|
})
|
|
|
|
export function useDialogContext() {
|
|
return React.useContext(Context)
|
|
}
|
|
|
|
export function useDialogControl(): DialogOuterProps['control'] {
|
|
const id = React.useId()
|
|
const control = React.useRef<DialogControlRefProps>({
|
|
open: () => {},
|
|
close: () => {},
|
|
})
|
|
const {activeDialogs, openDialogs} = useDialogStateContext()
|
|
const isOpen = openDialogs.includes(id)
|
|
|
|
React.useEffect(() => {
|
|
activeDialogs.current.set(id, control)
|
|
return () => {
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
activeDialogs.current.delete(id)
|
|
}
|
|
}, [id, activeDialogs])
|
|
|
|
return React.useMemo<DialogOuterProps['control']>(
|
|
() => ({
|
|
id,
|
|
ref: control,
|
|
isOpen,
|
|
open: () => {
|
|
control.current.open()
|
|
},
|
|
close: cb => {
|
|
control.current.close(cb)
|
|
},
|
|
}),
|
|
[id, control, isOpen],
|
|
)
|
|
}
|