|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {DrawerItem, Drawer as GravityDrawer} from '@gravity-ui/navigation'; |
| 4 | + |
| 5 | +import {cn} from '../../utils/cn'; |
| 6 | + |
| 7 | +const DEFAULT_DRAWER_WIDTH_PERCENTS = 60; |
| 8 | +const DEFAULT_DRAWER_WIDTH = 600; |
| 9 | +const DRAWER_WIDTH_KEY = 'drawer-width'; |
| 10 | +const b = cn('ydb-drawer'); |
| 11 | + |
| 12 | +import './Drawer.scss'; |
| 13 | + |
| 14 | +// Create a context for sharing container dimensions |
| 15 | +interface DrawerContextType { |
| 16 | + containerWidth: number; |
| 17 | + setContainerWidth: React.Dispatch<React.SetStateAction<number>>; |
| 18 | +} |
| 19 | + |
| 20 | +const DrawerContext = React.createContext<DrawerContextType | undefined>(undefined); |
| 21 | + |
| 22 | +// Custom hook to use the drawer context |
| 23 | +const useDrawerContext = () => { |
| 24 | + const context = React.useContext(DrawerContext); |
| 25 | + if (context === undefined) { |
| 26 | + return {containerWidth: 0, setContainerWidth: () => {}}; |
| 27 | + } |
| 28 | + return context; |
| 29 | +}; |
| 30 | + |
| 31 | +interface ContentWrapperProps { |
| 32 | + isVisible: boolean; |
| 33 | + onClose: () => void; |
| 34 | + children: React.ReactNode; |
| 35 | + drawerId?: string; |
| 36 | + storageKey?: string; |
| 37 | + direction?: 'left' | 'right'; |
| 38 | + className?: string; |
| 39 | + detectClickOutside?: boolean; |
| 40 | + defaultWidth?: number; |
| 41 | + isPercentageWidth?: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +const ContentWrapper = ({ |
| 45 | + isVisible, |
| 46 | + onClose, |
| 47 | + children, |
| 48 | + drawerId = 'drawer', |
| 49 | + storageKey = DRAWER_WIDTH_KEY, |
| 50 | + defaultWidth, |
| 51 | + direction = 'right', |
| 52 | + className, |
| 53 | + detectClickOutside = false, |
| 54 | + isPercentageWidth, |
| 55 | +}: ContentWrapperProps) => { |
| 56 | + const [drawerWidth, setDrawerWidth] = React.useState(() => { |
| 57 | + const savedWidth = localStorage.getItem(storageKey); |
| 58 | + return savedWidth ? Number(savedWidth) : defaultWidth; |
| 59 | + }); |
| 60 | + |
| 61 | + const drawerRef = React.useRef<HTMLDivElement>(null); |
| 62 | + const {containerWidth} = useDrawerContext(); |
| 63 | + // Calculate drawer width based on container width percentage if specified |
| 64 | + const calculatedWidth = React.useMemo(() => { |
| 65 | + if (isPercentageWidth && containerWidth > 0) { |
| 66 | + return Math.round( |
| 67 | + (containerWidth * (drawerWidth || DEFAULT_DRAWER_WIDTH_PERCENTS)) / 100, |
| 68 | + ); |
| 69 | + } |
| 70 | + return drawerWidth || DEFAULT_DRAWER_WIDTH; |
| 71 | + }, [containerWidth, isPercentageWidth, drawerWidth]); |
| 72 | + |
| 73 | + React.useEffect(() => { |
| 74 | + if (!detectClickOutside) { |
| 75 | + return undefined; |
| 76 | + } |
| 77 | + |
| 78 | + const handleClickOutside = (event: MouseEvent) => { |
| 79 | + if ( |
| 80 | + isVisible && |
| 81 | + drawerRef.current && |
| 82 | + !drawerRef.current.contains(event.target as Node) |
| 83 | + ) { |
| 84 | + onClose(); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + document.addEventListener('click', handleClickOutside); |
| 89 | + return () => { |
| 90 | + document.removeEventListener('click', handleClickOutside); |
| 91 | + }; |
| 92 | + }, [isVisible, onClose, detectClickOutside]); |
| 93 | + |
| 94 | + const handleResizeDrawer = (width: number) => { |
| 95 | + if (isPercentageWidth && containerWidth > 0) { |
| 96 | + const percentageWidth = Math.round((width / containerWidth) * 100); |
| 97 | + setDrawerWidth(percentageWidth); |
| 98 | + localStorage.setItem(storageKey, percentageWidth.toString()); |
| 99 | + } else { |
| 100 | + setDrawerWidth(width); |
| 101 | + localStorage.setItem(storageKey, width.toString()); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + return ( |
| 106 | + <GravityDrawer |
| 107 | + onEscape={onClose} |
| 108 | + onVeilClick={onClose} |
| 109 | + hideVeil |
| 110 | + className={b('container', className)} |
| 111 | + > |
| 112 | + <DrawerItem |
| 113 | + id={drawerId} |
| 114 | + visible={isVisible} |
| 115 | + resizable |
| 116 | + maxResizeWidth={containerWidth} |
| 117 | + width={isPercentageWidth ? calculatedWidth : drawerWidth} |
| 118 | + onResize={handleResizeDrawer} |
| 119 | + direction={direction} |
| 120 | + className={b('item')} |
| 121 | + ref={detectClickOutside ? drawerRef : undefined} |
| 122 | + > |
| 123 | + {children} |
| 124 | + </DrawerItem> |
| 125 | + </GravityDrawer> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +interface ContainerProps { |
| 130 | + children: React.ReactNode; |
| 131 | + className?: string; |
| 132 | +} |
| 133 | + |
| 134 | +interface ItemWrapperProps { |
| 135 | + children: React.ReactNode; |
| 136 | + renderDrawerContent: () => React.ReactNode; |
| 137 | + isDrawerVisible: boolean; |
| 138 | + onCloseDrawer: () => void; |
| 139 | + drawerId?: string; |
| 140 | + storageKey?: string; |
| 141 | + defaultWidth?: number; |
| 142 | + direction?: 'left' | 'right'; |
| 143 | + className?: string; |
| 144 | + detectClickOutside?: boolean; |
| 145 | + isPercentageWidth?: boolean; |
| 146 | +} |
| 147 | + |
| 148 | +export const Drawer = { |
| 149 | + Container: ({children, className}: ContainerProps) => { |
| 150 | + const [containerWidth, setContainerWidth] = React.useState(0); |
| 151 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 152 | + |
| 153 | + React.useEffect(() => { |
| 154 | + if (!containerRef.current) { |
| 155 | + return undefined; |
| 156 | + } |
| 157 | + |
| 158 | + const updateWidth = () => { |
| 159 | + if (containerRef.current) { |
| 160 | + setContainerWidth(containerRef.current.clientWidth); |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + // Set initial width |
| 165 | + updateWidth(); |
| 166 | + |
| 167 | + // Update width on resize |
| 168 | + const resizeObserver = new ResizeObserver(updateWidth); |
| 169 | + resizeObserver.observe(containerRef.current); |
| 170 | + |
| 171 | + return () => { |
| 172 | + if (containerRef.current) { |
| 173 | + resizeObserver.disconnect(); |
| 174 | + } |
| 175 | + }; |
| 176 | + }, []); |
| 177 | + |
| 178 | + return ( |
| 179 | + <DrawerContext.Provider value={{containerWidth, setContainerWidth}}> |
| 180 | + <div ref={containerRef} className={b('drawer-container', className)}> |
| 181 | + {children} |
| 182 | + </div> |
| 183 | + </DrawerContext.Provider> |
| 184 | + ); |
| 185 | + }, |
| 186 | + |
| 187 | + ItemWrapper: ({ |
| 188 | + children, |
| 189 | + renderDrawerContent, |
| 190 | + isDrawerVisible, |
| 191 | + onCloseDrawer, |
| 192 | + drawerId, |
| 193 | + storageKey, |
| 194 | + defaultWidth, |
| 195 | + direction, |
| 196 | + className, |
| 197 | + detectClickOutside, |
| 198 | + isPercentageWidth, |
| 199 | + }: ItemWrapperProps) => { |
| 200 | + React.useEffect(() => { |
| 201 | + return () => { |
| 202 | + onCloseDrawer(); |
| 203 | + }; |
| 204 | + }, [onCloseDrawer]); |
| 205 | + return ( |
| 206 | + <React.Fragment> |
| 207 | + {children} |
| 208 | + <ContentWrapper |
| 209 | + isVisible={isDrawerVisible} |
| 210 | + onClose={onCloseDrawer} |
| 211 | + drawerId={drawerId} |
| 212 | + storageKey={storageKey} |
| 213 | + defaultWidth={defaultWidth} |
| 214 | + direction={direction} |
| 215 | + className={className} |
| 216 | + detectClickOutside={detectClickOutside} |
| 217 | + isPercentageWidth={isPercentageWidth} |
| 218 | + > |
| 219 | + {renderDrawerContent()} |
| 220 | + </ContentWrapper> |
| 221 | + </React.Fragment> |
| 222 | + ); |
| 223 | + }, |
| 224 | +}; |
0 commit comments