-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuseDialog.tsx
More file actions
44 lines (37 loc) · 1.2 KB
/
useDialog.tsx
File metadata and controls
44 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { useCallback, useMemo, useState } from 'react';
import { InternalDialogProps } from './index';
export type UseDialogReturnType = {
/** Props meant to pass to a {@link Dialog} component */
dialogProps: InternalDialogProps;
/** Function to show the dialog */
show: () => void;
/** Function to close the dialog */
close: () => void;
/** Boolean indicating wether the dialog is currently open */
isOpen: boolean;
};
/** Sets up state, and functions to use with a {@link Dialog} */
export const useDialog = (): UseDialogReturnType => {
const [showDialog, setShowDialog] = useState(false);
const [visible, setVisible] = useState(false);
const show = useCallback(() => {
setShowDialog(true);
setVisible(true);
}, []);
const close = useCallback(() => {
setShowDialog(false);
}, []);
const handleClosed = useCallback(() => {
setVisible(false);
}, []);
/** Props that should be passed to a {@link Dialog} component. */
const dialogProps = useMemo<InternalDialogProps>(
() => ({
show: showDialog,
onClose: close,
onClosed: handleClosed,
}),
[showDialog, close, handleClosed],
);
return { dialogProps, show, close, isOpen: visible };
};