({
storageKey,
title,
actions,
sidebar,
children,
defaultWidth = 260,
}: PageSidebarLayoutProps)
| 65 | * local navigator, and owns its width + mobile drawer behavior. |
| 66 | */ |
| 67 | export function PageSidebarLayout({ |
| 68 | storageKey, |
| 69 | title, |
| 70 | actions, |
| 71 | sidebar, |
| 72 | children, |
| 73 | defaultWidth = 260, |
| 74 | }: PageSidebarLayoutProps) { |
| 75 | const isDesktop = useIsDesktop() |
| 76 | const rootRef = useRef<HTMLDivElement | null>(null) |
| 77 | const [drawerOpen, setDrawerOpen] = useState(false) |
| 78 | const [preferredWidth, setPreferredWidth] = useState(() => |
| 79 | readStoredWidth(storageKey, clampWidth(defaultWidth, defaultWidth)), |
| 80 | ) |
| 81 | const [containerWidth, setContainerWidth] = useState(() => |
| 82 | typeof window !== 'undefined' ? window.innerWidth : 0, |
| 83 | ) |
| 84 | const maxWidth = responsiveMaxWidth(containerWidth) |
| 85 | const width = Math.min(preferredWidth, maxWidth) |
| 86 | |
| 87 | const persistWidth = useCallback((next: number) => { |
| 88 | window.localStorage.setItem(storageName(storageKey), String(next)) |
| 89 | }, [storageKey]) |
| 90 | |
| 91 | const beginResize = useCallback((event: ReactPointerEvent<HTMLDivElement>) => { |
| 92 | event.preventDefault() |
| 93 | event.stopPropagation() |
| 94 | |
| 95 | const startX = event.clientX |
| 96 | const startWidth = width |
| 97 | let nextWidth = startWidth |
| 98 | |
| 99 | const onPointerMove = (moveEvent: PointerEvent) => { |
| 100 | nextWidth = Math.max(MIN_WIDTH, Math.min(maxWidth, Math.round(startWidth + moveEvent.clientX - startX))) |
| 101 | setPreferredWidth(nextWidth) |
| 102 | } |
| 103 | |
| 104 | const onPointerUp = () => { |
| 105 | persistWidth(nextWidth) |
| 106 | document.body.style.cursor = '' |
| 107 | document.body.style.userSelect = '' |
| 108 | window.removeEventListener('pointermove', onPointerMove) |
| 109 | window.removeEventListener('pointerup', onPointerUp) |
| 110 | window.removeEventListener('pointercancel', onPointerUp) |
| 111 | } |
| 112 | |
| 113 | document.body.style.cursor = 'col-resize' |
| 114 | document.body.style.userSelect = 'none' |
| 115 | window.addEventListener('pointermove', onPointerMove) |
| 116 | window.addEventListener('pointerup', onPointerUp) |
| 117 | window.addEventListener('pointercancel', onPointerUp) |
| 118 | }, [maxWidth, persistWidth, width]) |
| 119 | |
| 120 | useEffect(() => { |
| 121 | if (isDesktop) setDrawerOpen(false) |
| 122 | }, [isDesktop]) |
| 123 | |
| 124 | useEffect(() => { |
nothing calls this directly
no test coverage detected