diff options
Diffstat (limited to 'src/components/Dialog/context.ts')
-rw-r--r-- | src/components/Dialog/context.ts | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/components/Dialog/context.ts b/src/components/Dialog/context.ts index b28b9f5a2..859f8edd7 100644 --- a/src/components/Dialog/context.ts +++ b/src/components/Dialog/context.ts @@ -1,7 +1,11 @@ import React from 'react' import {useDialogStateContext} from '#/state/dialogs' -import {DialogContextProps, DialogControlProps} from '#/components/Dialog/types' +import { + DialogContextProps, + DialogControlRefProps, + DialogOuterProps, +} from '#/components/Dialog/types' export const Context = React.createContext<DialogContextProps>({ close: () => {}, @@ -11,9 +15,9 @@ export function useDialogContext() { return React.useContext(Context) } -export function useDialogControl() { +export function useDialogControl(): DialogOuterProps['control'] { const id = React.useId() - const control = React.useRef<DialogControlProps>({ + const control = React.useRef<DialogControlRefProps>({ open: () => {}, close: () => {}, }) @@ -27,9 +31,17 @@ export function useDialogControl() { } }, [id, activeDialogs]) - return { - ref: control, - open: () => control.current.open(), - close: () => control.current.close(), - } + return React.useMemo<DialogOuterProps['control']>( + () => ({ + id, + ref: control, + open: () => { + control.current.open() + }, + close: cb => { + control.current.close(cb) + }, + }), + [id, control], + ) } |