-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDrawer.tsx
More file actions
51 lines (46 loc) · 1.38 KB
/
Drawer.tsx
File metadata and controls
51 lines (46 loc) · 1.38 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
45
46
47
48
49
50
51
/** @jsxImportSource @emotion/react */
import { filterComponent } from '@jdesignlab/react-utils';
import { useId, useMemo } from 'react';
import { DrawerTrigger } from './DrawerTrigger';
import { DrawerPortal } from './DrawerPortal';
import { useToggleLayer } from '../hooks/useToggleLayer';
import { DrawerContext } from '../context';
import type { DrawerProps } from '../types';
export const Drawer = (props: DrawerProps) => {
const {
children,
open: openProp,
onOpen: onOpenProp,
onClose: onCloseProp,
hasCloseIcon = true,
disableOverlayClose = false,
placement = 'right',
full = false
} = props;
const { isOpen, onOpen, onClose } = useToggleLayer(openProp, onOpenProp, onCloseProp);
const id = useId();
const triggerComponent = filterComponent(children, DrawerTrigger, true);
const portalComponent = filterComponent(children, DrawerPortal, true);
const providerValue = useMemo(
() => ({
id,
isOpen,
onOpen,
onClose,
hasCloseIcon,
disableOverlayClose,
placement,
full
}),
[id, isOpen, onOpen, onClose, hasCloseIcon, disableOverlayClose, placement, full]
);
return (
<DrawerContext.Provider value={providerValue}>
{triggerComponent}
{portalComponent}
</DrawerContext.Provider>
);
};
Drawer.displayName = 'Drawer';
Drawer.Trigger = DrawerTrigger;
Drawer.Portal = DrawerPortal;