* Small logic cleanups * Small logic cleanups (#3451) * remove a few things * oops * stop swallowing the error * queue callbacks * oops * log error if caught * no need to be nullable * move isClosing=true up * reset `isClosing` and `closeCallbacks` on close completion and open * run queued callbacks on `open` if there are any pending * rm unnecessary ref and check * ensure order of calls is always correct * call `snapToIndex()` on open * add tester to storybook --------- Co-authored-by: Hailey <me@haileyok.com>
47 lines
1.1 KiB
TypeScript
47 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} = useDialogStateContext()
|
|
|
|
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,
|
|
open: () => {
|
|
control.current.open()
|
|
},
|
|
close: cb => {
|
|
control.current.close(cb)
|
|
},
|
|
}),
|
|
[id, control],
|
|
)
|
|
}
|