@@ -20,78 +20,77 @@ export interface CubeGridProviderProps {
2020 initialWidth ?: Styles [ 'width' ] ;
2121}
2222
23- export const GridProvider = forwardRef ( function GridProvider (
24- props : CubeGridProviderProps ,
25- outerRef ,
26- ) {
27- let ref = useCombinedRefs ( outerRef ) ;
28-
29- let {
30- children,
31- columns = 2 ,
32- gap = '0' ,
33- width : forcedWidth ,
34- initialWidth,
35- } = props ;
36-
37- let [ width , setWidth ] = useState < Styles [ 'width' ] > (
38- forcedWidth || initialWidth || '100vw' ,
39- ) ;
40-
41- const resizeCallback = useCallback ( ( ) => {
42- const el = ref ?. current ?. parentElement ;
43-
44- if ( ! el ) return ;
45-
46- const computedStyle = getComputedStyle ( el ) ;
47- const containerWidth =
48- el . clientWidth -
49- parseFloat ( computedStyle . paddingLeft ) -
50- parseFloat ( computedStyle . paddingRight ) ;
51-
52- setWidth ( `${ containerWidth } px` ) ;
53- } , [ ref , columns , gap ] ) ;
54-
55- useEffect ( ( ) => {
56- if ( forcedWidth ) return ;
57-
58- const el = ref && ref . current && ref . current . parentNode ;
59-
60- if ( ! el ) return ;
61-
62- let sensor ;
63-
64- import ( '../utils/ResizeSensor' )
65- . then ( ( module ) => module . ResizeSensor )
66- . then ( ( ResizeSensor ) => {
67- sensor = new ResizeSensor ( el , resizeCallback ) ;
68- } ) ;
69-
70- return ( ) => {
71- if ( sensor ) {
72- sensor . detach ( ) ;
73- }
74- } ;
75- } , [ resizeCallback ] ) ;
76-
77- useEffect ( ( ) => {
78- if ( forcedWidth ) return ;
79-
80- resizeCallback ( ) ;
81- } , [ resizeCallback ] ) ;
82-
83- return (
84- < GridElement
85- { ...filterBaseProps ( props , { eventProps : true } ) }
86- ref = { ref }
87- styles = { {
88- '--grid-width' : width ,
89- '--columns-amount' : columns ,
90- '--column-gap' : gap ,
91- '--column-width' : COLUMN_WIDTH ,
92- } }
93- >
94- { children }
95- </ GridElement >
96- ) ;
97- } ) ;
23+ export const GridProvider = forwardRef < HTMLDivElement , CubeGridProviderProps > (
24+ function GridProvider ( props , outerRef ) {
25+ let ref = useCombinedRefs ( outerRef ) ;
26+
27+ let {
28+ children,
29+ columns = 2 ,
30+ gap = '0' ,
31+ width : forcedWidth ,
32+ initialWidth,
33+ } = props ;
34+
35+ let [ width , setWidth ] = useState < Styles [ 'width' ] > (
36+ forcedWidth || initialWidth || '100vw' ,
37+ ) ;
38+
39+ const resizeCallback = useCallback ( ( ) => {
40+ const el = ref ?. current ?. parentElement ;
41+
42+ if ( ! el ) return ;
43+
44+ const computedStyle = getComputedStyle ( el ) ;
45+ const containerWidth =
46+ el . clientWidth -
47+ parseFloat ( computedStyle . paddingLeft ) -
48+ parseFloat ( computedStyle . paddingRight ) ;
49+
50+ setWidth ( `${ containerWidth } px` ) ;
51+ } , [ ref , columns , gap ] ) ;
52+
53+ useEffect ( ( ) => {
54+ if ( forcedWidth ) return ;
55+
56+ const el = ref && ref . current && ref . current . parentNode ;
57+
58+ if ( ! el ) return ;
59+
60+ let sensor ;
61+
62+ import ( '../utils/ResizeSensor' )
63+ . then ( ( module ) => module . ResizeSensor )
64+ . then ( ( ResizeSensor ) => {
65+ sensor = new ResizeSensor ( el , resizeCallback ) ;
66+ } ) ;
67+
68+ return ( ) => {
69+ if ( sensor ) {
70+ sensor . detach ( ) ;
71+ }
72+ } ;
73+ } , [ resizeCallback ] ) ;
74+
75+ useEffect ( ( ) => {
76+ if ( forcedWidth ) return ;
77+
78+ resizeCallback ( ) ;
79+ } , [ resizeCallback ] ) ;
80+
81+ return (
82+ < GridElement
83+ { ...filterBaseProps ( props , { eventProps : true } ) }
84+ ref = { ref }
85+ styles = { {
86+ '--grid-width' : width ,
87+ '--columns-amount' : columns ,
88+ '--column-gap' : gap ,
89+ '--column-width' : COLUMN_WIDTH ,
90+ } }
91+ >
92+ { children }
93+ </ GridElement >
94+ ) ;
95+ } ,
96+ ) ;
0 commit comments