mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at rn-stack-repro 49 lines 1.2 kB view raw
1import React from 'react' 2 3import {useDialogStateContext} from '#/state/dialogs' 4import { 5 DialogContextProps, 6 DialogControlRefProps, 7 DialogOuterProps, 8} from '#/components/Dialog/types' 9 10export const Context = React.createContext<DialogContextProps>({ 11 close: () => {}, 12}) 13 14export function useDialogContext() { 15 return React.useContext(Context) 16} 17 18export function useDialogControl(): DialogOuterProps['control'] { 19 const id = React.useId() 20 const control = React.useRef<DialogControlRefProps>({ 21 open: () => {}, 22 close: () => {}, 23 }) 24 const {activeDialogs, openDialogs} = useDialogStateContext() 25 const isOpen = openDialogs.includes(id) 26 27 React.useEffect(() => { 28 activeDialogs.current.set(id, control) 29 return () => { 30 // eslint-disable-next-line react-hooks/exhaustive-deps 31 activeDialogs.current.delete(id) 32 } 33 }, [id, activeDialogs]) 34 35 return React.useMemo<DialogOuterProps['control']>( 36 () => ({ 37 id, 38 ref: control, 39 isOpen, 40 open: () => { 41 control.current.open() 42 }, 43 close: cb => { 44 control.current.close(cb) 45 }, 46 }), 47 [id, control, isOpen], 48 ) 49}