@@ -4,31 +4,16 @@ import {DrawerItem, Drawer as GravityDrawer} from '@gravity-ui/navigation';
44
55import { cn } from '../../utils/cn' ;
66
7+ import { useDrawerContext } from './DrawerContext' ;
8+
79const DEFAULT_DRAWER_WIDTH_PERCENTS = 60 ;
810const DEFAULT_DRAWER_WIDTH = 600 ;
911const DRAWER_WIDTH_KEY = 'drawer-width' ;
1012const b = cn ( 'ydb-drawer' ) ;
1113
1214import './Drawer.scss' ;
1315
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 {
16+ interface DrawerPaneContentWrapperProps {
3217 isVisible : boolean ;
3318 onClose : ( ) => void ;
3419 children : React . ReactNode ;
@@ -41,7 +26,7 @@ interface ContentWrapperProps {
4126 isPercentageWidth ?: boolean ;
4227}
4328
44- const ContentWrapper = ( {
29+ const DrawerPaneContentWrapper = ( {
4530 isVisible,
4631 onClose,
4732 children,
@@ -52,7 +37,7 @@ const ContentWrapper = ({
5237 className,
5338 detectClickOutside = false ,
5439 isPercentageWidth,
55- } : ContentWrapperProps ) => {
40+ } : DrawerPaneContentWrapperProps ) => {
5641 const [ drawerWidth , setDrawerWidth ] = React . useState ( ( ) => {
5742 const savedWidth = localStorage . getItem ( storageKey ) ;
5843 return savedWidth ? Number ( savedWidth ) : defaultWidth ;
@@ -126,12 +111,7 @@ const ContentWrapper = ({
126111 ) ;
127112} ;
128113
129- interface ContainerProps {
130- children : React . ReactNode ;
131- className ?: string ;
132- }
133-
134- interface ItemWrapperProps {
114+ interface DrawerPaneProps {
135115 children : React . ReactNode ;
136116 renderDrawerContent : ( ) => React . ReactNode ;
137117 isDrawerVisible : boolean ;
@@ -145,80 +125,40 @@ interface ItemWrapperProps {
145125 isPercentageWidth ?: boolean ;
146126}
147127
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- } ,
128+ export const DrawerWrapper = ( {
129+ children,
130+ renderDrawerContent,
131+ isDrawerVisible,
132+ onCloseDrawer,
133+ drawerId,
134+ storageKey,
135+ defaultWidth,
136+ direction,
137+ className,
138+ detectClickOutside,
139+ isPercentageWidth,
140+ } : DrawerPaneProps ) => {
141+ React . useEffect ( ( ) => {
142+ return ( ) => {
143+ onCloseDrawer ( ) ;
144+ } ;
145+ } , [ onCloseDrawer ] ) ;
146+ return (
147+ < React . Fragment >
148+ { children }
149+ < DrawerPaneContentWrapper
150+ isVisible = { isDrawerVisible }
151+ onClose = { onCloseDrawer }
152+ drawerId = { drawerId }
153+ storageKey = { storageKey }
154+ defaultWidth = { defaultWidth }
155+ direction = { direction }
156+ className = { className }
157+ detectClickOutside = { detectClickOutside }
158+ isPercentageWidth = { isPercentageWidth }
159+ >
160+ { renderDrawerContent ( ) }
161+ </ DrawerPaneContentWrapper >
162+ </ React . Fragment >
163+ ) ;
224164} ;
0 commit comments