0f9f08b1ef
* Improve a11y on ios * Format * Remove android * Fix android * Revert some changes * use sharedvalue for `importantForAccessibility` * add back `isOpen` * fix some more types --------- Co-authored-by: Eric Bailey <git@esb.lol>
48 lines
1.1 KiB
TypeScript
48 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],
|
|
)
|
|
}
|