-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDrawer.tsx
96 lines (88 loc) · 2.24 KB
/
AppDrawer.tsx
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Drawer } from '@mui/material';
import type { PropsWithChildren } from 'react';
import {
forwardRef,
useCallback,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
import type { WidgetDrawerContext } from './AppDrawerContext.js';
import { DrawerContext } from './AppDrawerContext.js';
import type { WidgetDrawerProps } from './types/widget.js';
export interface WidgetDrawer {
isOpen(): void;
toggleDrawer(): void;
openDrawer(): void;
closeDrawer(): void;
}
export const AppDrawer = forwardRef<
WidgetDrawer,
PropsWithChildren<WidgetDrawerProps>
>(({ elementRef, open, onClose, config, children }, ref) => {
const openRef = useRef(Boolean(open));
const [drawerOpen, setDrawerOpen] = useState(Boolean(open));
const toggleDrawer = useCallback(() => {
setDrawerOpen((open) => {
openRef.current = !open;
return openRef.current;
});
if (!openRef.current) {
onClose?.();
}
}, [onClose]);
const openDrawer = useCallback(() => {
setDrawerOpen(true);
openRef.current = true;
}, []);
const closeDrawer = useCallback(() => {
setDrawerOpen(false);
openRef.current = false;
onClose?.();
}, [onClose]);
useImperativeHandle(
ref,
() => ({
isOpen: () => openRef.current,
toggleDrawer,
openDrawer,
closeDrawer,
}),
[closeDrawer, openDrawer, toggleDrawer],
);
const drawerContext: WidgetDrawerContext = useMemo(
() => ({
closeDrawer,
}),
[closeDrawer],
);
return (
<DrawerContext.Provider value={drawerContext}>
<Drawer
ref={elementRef}
anchor="right"
open={drawerOpen}
onClose={closeDrawer}
slotProps={{
backdrop: {
sx: {
backgroundColor: 'rgb(0 0 0 / 48%)',
backdropFilter: 'blur(3px)',
},
},
}}
PaperProps={{
sx: (theme) => ({
width: theme?.container?.width ?? '100%',
minWidth: theme?.container?.minWidth ?? theme.breakpoints.values.xs,
maxWidth: theme?.container?.maxWidth ?? theme.breakpoints.values.sm,
}),
}}
keepMounted
>
{children}
</Drawer>
</DrawerContext.Provider>
);
});