|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {DrawerItem, Drawer as GravityDrawer} from '@gravity-ui/navigation'; |
| 4 | + |
| 5 | +import {cn} from '../../utils/cn'; |
| 6 | +import {isNumeric} from '../../utils/utils'; |
| 7 | + |
| 8 | +import {useDrawerContext} from './DrawerContext'; |
| 9 | + |
| 10 | +const DEFAULT_DRAWER_WIDTH_PERCENTS = 60; |
| 11 | +const DEFAULT_DRAWER_WIDTH = 600; |
| 12 | +const DRAWER_WIDTH_KEY = 'drawer-width'; |
| 13 | +const b = cn('ydb-drawer'); |
| 14 | + |
| 15 | +import './Drawer.scss'; |
| 16 | + |
| 17 | +interface DrawerPaneContentWrapperProps { |
| 18 | + isVisible: boolean; |
| 19 | + onClose: () => void; |
| 20 | + children: React.ReactNode; |
| 21 | + drawerId?: string; |
| 22 | + storageKey?: string; |
| 23 | + direction?: 'left' | 'right'; |
| 24 | + className?: string; |
| 25 | + detectClickOutside?: boolean; |
| 26 | + defaultWidth?: number; |
| 27 | + isPercentageWidth?: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +const DrawerPaneContentWrapper = ({ |
| 31 | + isVisible, |
| 32 | + onClose, |
| 33 | + children, |
| 34 | + drawerId = 'drawer', |
| 35 | + storageKey = DRAWER_WIDTH_KEY, |
| 36 | + defaultWidth, |
| 37 | + direction = 'right', |
| 38 | + className, |
| 39 | + detectClickOutside = false, |
| 40 | + isPercentageWidth, |
| 41 | +}: DrawerPaneContentWrapperProps) => { |
| 42 | + const [drawerWidth, setDrawerWidth] = React.useState(() => { |
| 43 | + const savedWidth = localStorage.getItem(storageKey); |
| 44 | + return isNumeric(savedWidth) ? Number(savedWidth) : defaultWidth; |
| 45 | + }); |
| 46 | + |
| 47 | + const drawerRef = React.useRef<HTMLDivElement>(null); |
| 48 | + const {containerWidth} = useDrawerContext(); |
| 49 | + // Calculate drawer width based on container width percentage if specified |
| 50 | + const calculatedWidth = React.useMemo(() => { |
| 51 | + if (isPercentageWidth && containerWidth > 0) { |
| 52 | + return Math.round( |
| 53 | + (containerWidth * (drawerWidth || DEFAULT_DRAWER_WIDTH_PERCENTS)) / 100, |
| 54 | + ); |
| 55 | + } |
| 56 | + return drawerWidth || DEFAULT_DRAWER_WIDTH; |
| 57 | + }, [containerWidth, isPercentageWidth, drawerWidth]); |
| 58 | + |
| 59 | + React.useEffect(() => { |
| 60 | + if (!detectClickOutside) { |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + |
| 64 | + const handleClickOutside = (event: MouseEvent) => { |
| 65 | + if ( |
| 66 | + isVisible && |
| 67 | + drawerRef.current && |
| 68 | + !drawerRef.current.contains(event.target as Node) |
| 69 | + ) { |
| 70 | + onClose(); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + document.addEventListener('click', handleClickOutside); |
| 75 | + return () => { |
| 76 | + document.removeEventListener('click', handleClickOutside); |
| 77 | + }; |
| 78 | + }, [isVisible, onClose, detectClickOutside]); |
| 79 | + |
| 80 | + const handleResizeDrawer = (width: number) => { |
| 81 | + if (isPercentageWidth && containerWidth > 0) { |
| 82 | + const percentageWidth = Math.round((width / containerWidth) * 100); |
| 83 | + setDrawerWidth(percentageWidth); |
| 84 | + localStorage.setItem(storageKey, percentageWidth.toString()); |
| 85 | + } else { |
| 86 | + setDrawerWidth(width); |
| 87 | + localStorage.setItem(storageKey, width.toString()); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <GravityDrawer |
| 93 | + onEscape={onClose} |
| 94 | + onVeilClick={onClose} |
| 95 | + hideVeil |
| 96 | + className={b('container', className)} |
| 97 | + > |
| 98 | + <DrawerItem |
| 99 | + id={drawerId} |
| 100 | + visible={isVisible} |
| 101 | + resizable |
| 102 | + maxResizeWidth={containerWidth} |
| 103 | + width={isPercentageWidth ? calculatedWidth : drawerWidth} |
| 104 | + onResize={handleResizeDrawer} |
| 105 | + direction={direction} |
| 106 | + className={b('item')} |
| 107 | + ref={detectClickOutside ? drawerRef : undefined} |
| 108 | + > |
| 109 | + {children} |
| 110 | + </DrawerItem> |
| 111 | + </GravityDrawer> |
| 112 | + ); |
| 113 | +}; |
| 114 | + |
| 115 | +interface DrawerPaneProps { |
| 116 | + children: React.ReactNode; |
| 117 | + renderDrawerContent: () => React.ReactNode; |
| 118 | + isDrawerVisible: boolean; |
| 119 | + onCloseDrawer: () => void; |
| 120 | + drawerId?: string; |
| 121 | + storageKey?: string; |
| 122 | + defaultWidth?: number; |
| 123 | + direction?: 'left' | 'right'; |
| 124 | + className?: string; |
| 125 | + detectClickOutside?: boolean; |
| 126 | + isPercentageWidth?: boolean; |
| 127 | +} |
| 128 | + |
| 129 | +export const DrawerWrapper = ({ |
| 130 | + children, |
| 131 | + renderDrawerContent, |
| 132 | + isDrawerVisible, |
| 133 | + onCloseDrawer, |
| 134 | + drawerId, |
| 135 | + storageKey, |
| 136 | + defaultWidth, |
| 137 | + direction, |
| 138 | + className, |
| 139 | + detectClickOutside, |
| 140 | + isPercentageWidth, |
| 141 | +}: DrawerPaneProps) => { |
| 142 | + React.useEffect(() => { |
| 143 | + return () => { |
| 144 | + onCloseDrawer(); |
| 145 | + }; |
| 146 | + }, [onCloseDrawer]); |
| 147 | + return ( |
| 148 | + <React.Fragment> |
| 149 | + {children} |
| 150 | + <DrawerPaneContentWrapper |
| 151 | + isVisible={isDrawerVisible} |
| 152 | + onClose={onCloseDrawer} |
| 153 | + drawerId={drawerId} |
| 154 | + storageKey={storageKey} |
| 155 | + defaultWidth={defaultWidth} |
| 156 | + direction={direction} |
| 157 | + className={className} |
| 158 | + detectClickOutside={detectClickOutside} |
| 159 | + isPercentageWidth={isPercentageWidth} |
| 160 | + > |
| 161 | + {renderDrawerContent()} |
| 162 | + </DrawerPaneContentWrapper> |
| 163 | + </React.Fragment> |
| 164 | + ); |
| 165 | +}; |
0 commit comments