@@ -2,25 +2,43 @@ import { useEffect } from 'react';
22
33export const InteractivesScrollbarWidth = ( ) => {
44 useEffect ( ( ) => {
5- updateScrollbarWidth ( ) ;
6- window . addEventListener ( 'resize' , updateScrollbarWidth ) ;
7- return ( ) => {
8- window . removeEventListener ( 'resize' , updateScrollbarWidth ) ;
5+ const updateScrollbarWidth = ( ) => {
6+ const documentWidth = document . documentElement . clientWidth ;
7+ if ( documentWidth <= 0 ) return ;
8+
9+ const scrollbarWidth = window . innerWidth - documentWidth ;
10+ const root = document . documentElement ;
11+
12+ root . style . setProperty ( '--scrollbar-width' , `${ scrollbarWidth } px` ) ;
13+ root . style . setProperty (
14+ '--half-scrollbar-width' ,
15+ `${ scrollbarWidth / 2 } px` ,
16+ ) ;
917 } ;
10- } , [ ] ) ;
11- return null ;
12- } ;
1318
14- const updateScrollbarWidth = ( ) => {
15- const documentWidth = document . documentElement . clientWidth ;
16- if ( documentWidth <= 0 ) {
17- return ;
18- }
19+ let timeoutId : ReturnType < typeof setTimeout > | null = null ;
20+
21+ const debouncedResize = ( ) => {
22+ if ( timeoutId ) {
23+ clearTimeout ( timeoutId ) ;
24+ }
1925
20- const scrollbarWidth = window . innerWidth - documentWidth ;
26+ timeoutId = setTimeout ( ( ) => {
27+ updateScrollbarWidth ( ) ;
28+ } , 150 ) ;
29+ } ;
30+
31+ updateScrollbarWidth ( ) ;
32+
33+ window . addEventListener ( 'resize' , debouncedResize ) ;
2134
22- const root = document . documentElement ;
35+ return ( ) => {
36+ if ( timeoutId ) {
37+ clearTimeout ( timeoutId ) ;
38+ }
39+ window . removeEventListener ( 'resize' , debouncedResize ) ;
40+ } ;
41+ } , [ ] ) ;
2342
24- root . style . setProperty ( '--scrollbar-width' , `${ scrollbarWidth } px` ) ;
25- root . style . setProperty ( '--half-scrollbar-width' , `${ scrollbarWidth / 2 } px` ) ;
43+ return null ;
2644} ;
0 commit comments