@@ -17,6 +17,7 @@ import {
1717} from "~/components/ui/sheet" ;
1818import { Skeleton } from "~/components/ui/skeleton" ;
1919import { Tooltip , TooltipPopup , TooltipTrigger } from "~/components/ui/tooltip" ;
20+ import { useResizeDrag } from "~/hooks/useResizeDrag" ;
2021import { useIsMobile } from "~/hooks/useMediaQuery" ;
2122import { getLocalStorageItem , setLocalStorageItem } from "~/hooks/useLocalStorage" ;
2223import { resolveSidebarState , type ResponsiveSidebarState } from "./sidebarState" ;
@@ -353,6 +354,7 @@ function SidebarRail({
353354 className,
354355 onClick,
355356 onPointerCancel,
357+ onLostPointerCapture,
356358 onPointerDown,
357359 onPointerMove,
358360 onPointerUp,
@@ -362,182 +364,79 @@ function SidebarRail({
362364 const sidebarInstance = React . use ( SidebarInstanceContext ) ;
363365 const railRef = React . useRef < HTMLButtonElement | null > ( null ) ;
364366 const suppressClickRef = React . useRef ( false ) ;
365- const resizeStateRef = React . useRef < {
366- moved : boolean ;
367- pointerId : number ;
368- pendingWidth : number ;
369- rail : HTMLButtonElement ;
370- rafId : number | null ;
371- sidebarRoot : HTMLElement ;
372- side : "left" | "right" ;
373- startWidth : number ;
374- startX : number ;
375- transitionTargets : HTMLElement [ ] ;
376- width : number ;
377- wrapper : HTMLElement ;
378- } | null > ( null ) ;
379367 const resolvedResizable = sidebarInstance ?. resizable ?? null ;
368+ const latestResizable = React . useRef ( resolvedResizable ) ;
369+ React . useLayoutEffect ( ( ) => {
370+ latestResizable . current = resolvedResizable ;
371+ } , [ resolvedResizable ] ) ;
380372 const canResize = resolvedResizable !== null && open ;
381373 const railLabel = canResize ? "Resize Sidebar" : "Toggle Sidebar" ;
382374 const railTitle = canResize ? "Drag to resize sidebar" : "Toggle Sidebar" ;
375+ const resize = useResizeDrag < HTMLButtonElement > ( ( event ) => {
376+ if ( ! resolvedResizable || ! open ) return null ;
377+ const rail = event . currentTarget ;
378+ const wrapper = rail . closest < HTMLElement > ( "[data-slot='sidebar-wrapper']" ) ;
379+ const sidebarRoot = rail . closest < HTMLElement > ( "[data-slot='sidebar']" ) ;
380+ const sidebarContainer = sidebarRoot ?. querySelector < HTMLElement > (
381+ "[data-slot='sidebar-container']" ,
382+ ) ;
383+ if ( ! wrapper || ! sidebarRoot || ! sidebarContainer ) return null ;
383384
384- const stopResize = React . useCallback (
385- ( pointerId : number ) => {
386- const resizeState = resizeStateRef . current ;
387- if ( ! resizeState ) {
388- return ;
389- }
390- if ( resizeState . rafId !== null ) {
391- window . cancelAnimationFrame ( resizeState . rafId ) ;
392- }
393- resizeState . transitionTargets . forEach ( ( element ) => {
394- element . style . removeProperty ( "transition-duration" ) ;
395- } ) ;
396- if ( resolvedResizable ?. storageKey && typeof window !== "undefined" ) {
397- setLocalStorageItem ( resolvedResizable . storageKey , resizeState . width , Schema . Finite ) ;
398- }
399- resolvedResizable ?. onResize ?.( resizeState . width ) ;
400- resizeStateRef . current = null ;
401- if ( resizeState . rail . hasPointerCapture ( pointerId ) ) {
402- resizeState . rail . releasePointerCapture ( pointerId ) ;
403- }
404- document . body . style . removeProperty ( "cursor" ) ;
405- document . body . style . removeProperty ( "user-select" ) ;
406- } ,
407- [ resolvedResizable ] ,
408- ) ;
409-
410- const handlePointerDown = React . useCallback (
411- ( event : React . PointerEvent < HTMLButtonElement > ) => {
412- onPointerDown ?.( event ) ;
413- if ( event . defaultPrevented ) return ;
414- if ( ! resolvedResizable || ! open || event . button !== 0 ) return ;
415-
416- const wrapper = event . currentTarget . closest < HTMLElement > ( "[data-slot='sidebar-wrapper']" ) ;
417- const sidebarRoot = event . currentTarget . closest < HTMLElement > ( "[data-slot='sidebar']" ) ;
418- if ( ! wrapper || ! sidebarRoot ) {
419- return ;
420- }
421-
422- const sidebarContainer = sidebarRoot . querySelector < HTMLElement > (
423- "[data-slot='sidebar-container']" ,
424- ) ;
425- if ( ! sidebarContainer ) {
426- return ;
427- }
428-
429- const startWidth = sidebarContainer . getBoundingClientRect ( ) . width ;
430- const initialWidth = clampSidebarWidth ( startWidth , resolvedResizable ) ;
431- const transitionTargets = [
432- sidebarRoot . querySelector < HTMLElement > ( "[data-slot='sidebar-gap']" ) ,
433- sidebarRoot . querySelector < HTMLElement > ( "[data-slot='sidebar-container']" ) ,
434- ] . filter ( ( element ) : element is HTMLElement => element !== null ) ;
435- transitionTargets . forEach ( ( element ) => {
436- element . style . setProperty ( "transition-duration" , "0ms" ) ;
437- } ) ;
438-
439- event . preventDefault ( ) ;
440- event . stopPropagation ( ) ;
441- resizeStateRef . current = {
442- moved : false ,
443- pointerId : event . pointerId ,
444- pendingWidth : initialWidth ,
445- rail : event . currentTarget ,
446- rafId : null ,
447- sidebarRoot,
448- side : sidebarInstance ?. side ?? "left" ,
449- startWidth : initialWidth ,
450- startX : event . clientX ,
451- transitionTargets,
452- width : initialWidth ,
453- wrapper,
454- } ;
455- wrapper . style . setProperty ( "--sidebar-width" , `${ initialWidth } px` ) ;
456- event . currentTarget . setPointerCapture ( event . pointerId ) ;
457- document . body . style . cursor = "col-resize" ;
458- document . body . style . userSelect = "none" ;
459- } ,
460- [ onPointerDown , open , resolvedResizable , sidebarInstance ?. side ] ,
461- ) ;
462-
463- const handlePointerMove = React . useCallback (
464- ( event : React . PointerEvent < HTMLButtonElement > ) => {
465- onPointerMove ?.( event ) ;
466- if ( event . defaultPrevented ) return ;
467- const resizeState = resizeStateRef . current ;
468- if ( ! resizeState || resizeState . pointerId !== event . pointerId || ! resolvedResizable ) return ;
469-
470- event . preventDefault ( ) ;
471- const delta =
472- resizeState . side === "right"
473- ? resizeState . startX - event . clientX
474- : event . clientX - resizeState . startX ;
475- if ( Math . abs ( delta ) > 2 ) {
476- resizeState . moved = true ;
477- }
478- resizeState . pendingWidth = clampSidebarWidth (
479- resizeState . startWidth + delta ,
480- resolvedResizable ,
481- ) ;
482- if ( resizeState . rafId !== null ) {
483- return ;
484- }
485-
486- resizeState . rafId = window . requestAnimationFrame ( ( ) => {
487- const activeResizeState = resizeStateRef . current ;
488- if ( ! activeResizeState || ! resolvedResizable ) return ;
385+ const side = sidebarInstance ?. side ?? "left" ;
386+ let width = clampSidebarWidth (
387+ sidebarContainer . getBoundingClientRect ( ) . width ,
388+ resolvedResizable ,
389+ ) ;
390+ const transitionTargets = [
391+ sidebarRoot . querySelector < HTMLElement > ( "[data-slot='sidebar-gap']" ) ,
392+ sidebarContainer ,
393+ ] . filter ( ( element ) : element is HTMLElement => element !== null ) ;
394+ transitionTargets . forEach ( ( element ) => {
395+ element . style . setProperty ( "transition-duration" , "0ms" ) ;
396+ } ) ;
397+ wrapper . style . setProperty ( "--sidebar-width" , `${ width } px` ) ;
489398
490- activeResizeState . rafId = null ;
491- const nextWidth = activeResizeState . pendingWidth ;
399+ return {
400+ width,
401+ edge : side === "left" ? "right" : "left" ,
402+ resize ( value ) {
403+ const options = latestResizable . current ;
404+ if ( ! options ) return width ;
405+ const nextWidth = clampSidebarWidth ( value , options ) ;
492406 const accepted =
493- resolvedResizable . shouldAcceptWidth ?.( {
494- currentWidth : activeResizeState . width ,
407+ options . shouldAcceptWidth ?.( {
408+ currentWidth : width ,
495409 nextWidth,
496- rail : activeResizeState . rail ,
497- side : activeResizeState . side ,
498- sidebarRoot : activeResizeState . sidebarRoot ,
499- wrapper : activeResizeState . wrapper ,
410+ rail,
411+ side,
412+ sidebarRoot,
413+ wrapper,
500414 } ) ?? true ;
501- if ( ! accepted ) {
502- return ;
415+ if ( accepted ) {
416+ wrapper . style . setProperty ( "--sidebar-width" , `${ nextWidth } px` ) ;
417+ width = nextWidth ;
503418 }
504-
505- activeResizeState . wrapper . style . setProperty ( "--sidebar-width" , `${ nextWidth } px` ) ;
506- activeResizeState . width = nextWidth ;
507- } ) ;
508- } ,
509- [ onPointerMove , resolvedResizable ] ,
510- ) ;
511-
512- const endResizeInteraction = React . useCallback (
513- ( event : React . PointerEvent < HTMLButtonElement > ) => {
514- const resizeState = resizeStateRef . current ;
515- if ( ! resizeState || resizeState . pointerId !== event . pointerId ) return ;
516-
517- event . preventDefault ( ) ;
518- suppressClickRef . current = resizeState . moved ;
519- stopResize ( event . pointerId ) ;
520- } ,
521- [ stopResize ] ,
522- ) ;
523-
524- const handlePointerUp = React . useCallback (
525- ( event : React . PointerEvent < HTMLButtonElement > ) => {
526- onPointerUp ?.( event ) ;
527- if ( event . defaultPrevented ) return ;
528- endResizeInteraction ( event ) ;
529- } ,
530- [ endResizeInteraction , onPointerUp ] ,
531- ) ;
532-
533- const handlePointerCancel = React . useCallback (
534- ( event : React . PointerEvent < HTMLButtonElement > ) => {
535- onPointerCancel ?.( event ) ;
536- if ( event . defaultPrevented ) return ;
537- endResizeInteraction ( event ) ;
538- } ,
539- [ endResizeInteraction , onPointerCancel ] ,
540- ) ;
419+ return width ;
420+ } ,
421+ finish ( finalWidth , moved ) {
422+ suppressClickRef . current = moved ;
423+ const options = latestResizable . current ;
424+ if ( options ?. storageKey ) {
425+ try {
426+ setLocalStorageItem ( options . storageKey , finalWidth , Schema . Finite ) ;
427+ } catch ( error ) {
428+ console . error ( "Could not persist sidebar width." , error ) ;
429+ }
430+ }
431+ options ?. onResize ?.( finalWidth ) ;
432+ } ,
433+ cleanup ( ) {
434+ transitionTargets . forEach ( ( element ) => {
435+ element . style . removeProperty ( "transition-duration" ) ;
436+ } ) ;
437+ } ,
438+ } ;
439+ } ) ;
541440
542441 const handleClick = React . useCallback (
543442 ( event : React . MouseEvent < HTMLButtonElement > ) => {
@@ -579,20 +478,6 @@ function SidebarRail({
579478 resolvedResizable . onResize ?.( clampedWidth ) ;
580479 } , [ resolvedResizable ] ) ;
581480
582- React . useEffect ( ( ) => {
583- return ( ) => {
584- const resizeState = resizeStateRef . current ;
585- if ( resizeState ?. rafId != null ) {
586- window . cancelAnimationFrame ( resizeState . rafId ) ;
587- }
588- resizeState ?. transitionTargets . forEach ( ( element ) => {
589- element . style . removeProperty ( "transition-duration" ) ;
590- } ) ;
591- document . body . style . removeProperty ( "cursor" ) ;
592- document . body . style . removeProperty ( "user-select" ) ;
593- } ;
594- } , [ ] ) ;
595-
596481 return (
597482 < Tooltip >
598483 < TooltipTrigger
@@ -613,10 +498,26 @@ function SidebarRail({
613498 data-sidebar = "rail"
614499 data-slot = "sidebar-rail"
615500 onClick = { handleClick }
616- onPointerCancel = { handlePointerCancel }
617- onPointerDown = { handlePointerDown }
618- onPointerMove = { handlePointerMove }
619- onPointerUp = { handlePointerUp }
501+ onLostPointerCapture = { ( event ) => {
502+ onLostPointerCapture ?.( event ) ;
503+ if ( ! event . defaultPrevented ) resize . onLostPointerCapture ( event ) ;
504+ } }
505+ onPointerCancel = { ( event ) => {
506+ onPointerCancel ?.( event ) ;
507+ if ( ! event . defaultPrevented ) resize . onPointerCancel ( event ) ;
508+ } }
509+ onPointerDown = { ( event ) => {
510+ onPointerDown ?.( event ) ;
511+ if ( ! event . defaultPrevented ) resize . onPointerDown ( event ) ;
512+ } }
513+ onPointerMove = { ( event ) => {
514+ onPointerMove ?.( event ) ;
515+ if ( ! event . defaultPrevented ) resize . onPointerMove ( event ) ;
516+ } }
517+ onPointerUp = { ( event ) => {
518+ onPointerUp ?.( event ) ;
519+ if ( ! event . defaultPrevented ) resize . onPointerUp ( event ) ;
520+ } }
620521 ref = { railRef }
621522 tabIndex = { - 1 }
622523 type = "button"
0 commit comments