Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { type PointerEvent as ReactPointerEvent, useLayoutEffect, useState } from "react";
3
4import { getLocalStorageItem, setLocalStorageItem } from "./useLocalStorage";
5import { useResizeDrag } from "./useResizeDrag";
6
7const WidthSchema = Schema.Finite;
8
9/** The host must size itself from this custom property. */
10export const RESIZABLE_WIDTH_PROPERTY = "--resizable-width";
11
12export interface UseResizableWidthOptions {
13 readonly storageKey: string;
14 readonly defaultWidth: number;
15 readonly minWidth: number;
16 readonly maxWidth: number;
17 readonly edge: "left" | "right";
18}
19
20export interface ResizableWidthHandlers {
21 readonly ref: (node: HTMLElement | null) => (() => void) | undefined;
22 readonly onPointerDown: (event: ReactPointerEvent<HTMLElement>) => void;
23 readonly onPointerMove: (event: ReactPointerEvent<HTMLElement>) => void;
24 readonly onPointerUp: (event: ReactPointerEvent<HTMLElement>) => void;
25 readonly onPointerCancel: (event: ReactPointerEvent<HTMLElement>) => void;
26 readonly onLostPointerCapture: (event: ReactPointerEvent<HTMLElement>) => void;
27}
28
29/** Persist on drag end to avoid a storage write on every animation frame. */
30export function useResizableWidth(options: UseResizableWidthOptions): {
31 readonly width: number;
32 readonly handlers: ResizableWidthHandlers;
33} {
34 const { storageKey, defaultWidth, minWidth, maxWidth, edge } = options;
35
36 const clamp = (value: number): number => {
37 if (!Number.isFinite(value)) return defaultWidth;
38 return Math.max(minWidth, Math.min(maxWidth, value));
39 };
40
41 const readWidth = () => {
42 if (typeof window === "undefined") return defaultWidth;
43 try {
44 const stored = getLocalStorageItem(storageKey, WidthSchema);
45 return clamp(stored ?? defaultWidth);
46 } catch (error) {
47 console.error("Could not read persisted panel width.", error);
48 return defaultWidth;
49 }
50 };
51 const [widthState, setWidthState] = useState(() => ({ storageKey, width: readWidth() }));
52 if (widthState.storageKey !== storageKey) {
53 setWidthState({ storageKey, width: readWidth() });
54 }
55
56 const clampedWidth = clamp(widthState.width);
57
58 const drag = useResizeDrag<HTMLElement>((event) => {
59 const host = event.currentTarget.parentElement;
60 // Disable drag transitions through transition-property; the host renders transition-duration.
61 host?.style.setProperty("transition-property", "none");
62 // The drag commits to the key and width it started from, even if the panel
63 // switches keys before the pointer is released.
64 const startStorageKey = storageKey;
65 const startWidth = clampedWidth;
66 return {
67 width: startWidth,
68 edge,
69 bounds: { min: minWidth, max: maxWidth },
70 resize(nextWidth) {
71 host?.style.setProperty(RESIZABLE_WIDTH_PROPERTY, `${nextWidth}px`);
72 return nextWidth;
73 },
74 finish(finalWidth) {
75 setWidthState({ storageKey: startStorageKey, width: finalWidth });
76 try {
77 setLocalStorageItem(startStorageKey, finalWidth, WidthSchema);
78 } catch (error) {
79 console.error("Could not persist panel width.", error);
80 }
81 },
82 cleanup(committed) {
83 host?.style.removeProperty("transition-property");
84 if (!committed) host?.style.setProperty(RESIZABLE_WIDTH_PROPERTY, `${startWidth}px`);
85 },
86 };
87 });
88
89 // Bounds can change mid-drag (sidebar opens, window narrows), and the render
90 // that carries them rewrites the committed width, so re-apply the live pointer position.
91 const { refresh } = drag;
92 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reapplies the live drag pointer position to the DOM before paint when the bounds change
93 useLayoutEffect(() => refresh({ min: minWidth, max: maxWidth }), [refresh, minWidth, maxWidth]);
94
95 return {
96 width: clampedWidth,
97 handlers: {
98 ref: drag.ref,
99 onPointerDown: drag.onPointerDown,
100 onPointerMove: drag.onPointerMove,
101 onPointerUp: drag.onPointerUp,
102 onPointerCancel: drag.onPointerCancel,
103 onLostPointerCapture: drag.onLostPointerCapture,
104 },
105 };
106}