-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDrawer.tsx
More file actions
213 lines (194 loc) · 6.41 KB
/
Drawer.tsx
File metadata and controls
213 lines (194 loc) · 6.41 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React from 'react';
import {Xmark} from '@gravity-ui/icons';
import {DrawerItem, Drawer as GravityDrawer} from '@gravity-ui/navigation';
import {ActionTooltip, Button, Flex, Icon} from '@gravity-ui/uikit';
import {cn} from '../../utils/cn';
import {isNumeric} from '../../utils/utils';
import {CopyLinkButton} from '../CopyLinkButton/CopyLinkButton';
import {useDrawerContext} from './DrawerContext';
const DEFAULT_DRAWER_WIDTH_PERCENTS = 60;
const DEFAULT_DRAWER_WIDTH = 600;
const DRAWER_WIDTH_KEY = 'drawer-width';
const b = cn('ydb-drawer');
import './Drawer.scss';
interface DrawerPaneContentWrapperProps {
isVisible: boolean;
onClose: () => void;
children: React.ReactNode;
drawerId?: string;
storageKey?: string;
direction?: 'left' | 'right';
className?: string;
detectClickOutside?: boolean;
defaultWidth?: number;
isPercentageWidth?: boolean;
}
const DrawerPaneContentWrapper = ({
isVisible,
onClose,
children,
drawerId = 'drawer',
storageKey = DRAWER_WIDTH_KEY,
defaultWidth,
direction = 'right',
className,
detectClickOutside = false,
isPercentageWidth,
}: DrawerPaneContentWrapperProps) => {
const [drawerWidth, setDrawerWidth] = React.useState(() => {
const savedWidth = localStorage.getItem(storageKey);
return isNumeric(savedWidth) ? Number(savedWidth) : defaultWidth;
});
const drawerRef = React.useRef<HTMLDivElement>(null);
const {containerWidth} = useDrawerContext();
// Calculate drawer width based on container width percentage if specified
const calculatedWidth = React.useMemo(() => {
if (isPercentageWidth && containerWidth > 0) {
return Math.round(
(containerWidth * (drawerWidth || DEFAULT_DRAWER_WIDTH_PERCENTS)) / 100,
);
}
return drawerWidth || DEFAULT_DRAWER_WIDTH;
}, [containerWidth, isPercentageWidth, drawerWidth]);
React.useEffect(() => {
if (!detectClickOutside) {
return undefined;
}
const handleClickOutside = (event: MouseEvent) => {
if (
isVisible &&
drawerRef.current &&
!drawerRef.current.contains(event.target as Node)
) {
onClose();
}
};
document.addEventListener('click', handleClickOutside);
return () => {
document.removeEventListener('click', handleClickOutside);
};
}, [isVisible, onClose, detectClickOutside]);
const handleResizeDrawer = (width: number) => {
if (isPercentageWidth && containerWidth > 0) {
const percentageWidth = Math.round((width / containerWidth) * 100);
setDrawerWidth(percentageWidth);
localStorage.setItem(storageKey, percentageWidth.toString());
} else {
setDrawerWidth(width);
localStorage.setItem(storageKey, width.toString());
}
};
return (
<GravityDrawer
onEscape={onClose}
onVeilClick={onClose}
hideVeil
className={b('container', className)}
>
<DrawerItem
id={drawerId}
visible={isVisible}
resizable
maxResizeWidth={containerWidth}
width={isPercentageWidth ? calculatedWidth : drawerWidth}
onResize={handleResizeDrawer}
direction={direction}
className={b('item')}
ref={detectClickOutside ? drawerRef : undefined}
>
{children}
</DrawerItem>
</GravityDrawer>
);
};
type DrawerControl =
| {type: 'close'}
| {type: 'copyLink'; link: string}
| {type: 'custom'; node: React.ReactNode; key: string};
interface DrawerPaneProps {
children: React.ReactNode;
renderDrawerContent: () => React.ReactNode;
isDrawerVisible: boolean;
onCloseDrawer: () => void;
drawerId?: string;
storageKey?: string;
defaultWidth?: number;
direction?: 'left' | 'right';
className?: string;
detectClickOutside?: boolean;
isPercentageWidth?: boolean;
drawerControls?: DrawerControl[];
title?: React.ReactNode;
headerClassName?: string;
}
export const DrawerWrapper = ({
children,
renderDrawerContent,
isDrawerVisible,
onCloseDrawer,
drawerId,
storageKey,
defaultWidth,
direction,
className,
detectClickOutside,
isPercentageWidth,
drawerControls = [],
title,
headerClassName,
}: DrawerPaneProps) => {
React.useEffect(() => {
return () => {
onCloseDrawer();
};
}, [onCloseDrawer]);
const renderDrawerHeader = () => {
const controls = [];
for (const control of drawerControls) {
switch (control.type) {
case 'close':
controls.push(
<ActionTooltip title="Close" key="close">
<Button view="flat" onClick={onCloseDrawer}>
<Icon data={Xmark} size={16} />
</Button>
</ActionTooltip>,
);
break;
case 'copyLink':
controls.push(<CopyLinkButton text={control.link} key="copyLink" />);
break;
case 'custom':
controls.push(
<React.Fragment key={control.key}>{control.node}</React.Fragment>,
);
break;
}
}
return (
<Flex justifyContent="space-between" className={headerClassName}>
{title}
<Flex className={b('controls')}>{controls}</Flex>
</Flex>
);
};
return (
<React.Fragment>
{children}
<DrawerPaneContentWrapper
isVisible={isDrawerVisible}
onClose={onCloseDrawer}
drawerId={drawerId}
storageKey={storageKey}
defaultWidth={defaultWidth}
direction={direction}
className={className}
detectClickOutside={detectClickOutside}
isPercentageWidth={isPercentageWidth}
>
{renderDrawerHeader()}
{renderDrawerContent()}
</DrawerPaneContentWrapper>
</React.Fragment>
);
};