Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useResizableWidth.ts

97 lines3.2 KB

1import * as Schema from "effect/Schema";
2import {
3 type PointerEvent as ReactPointerEvent,
4 useCallback,
5 useLayoutEffect,
6 useRef,
7 useState,
8} from "react";
9​
10import { getLocalStorageItem, setLocalStorageItem } from "./useLocalStorage";
11import { useResizeDrag } from "./useResizeDrag";
12​
13const WidthSchema = Schema.Finite;
14​
15export interface UseResizableWidthOptions {
16 /** localStorage key the persisted width is stored under. */
17 readonly storageKey: string;
18 readonly defaultWidth: number;
19 readonly minWidth: number;
20 readonly maxWidth: number;
21 /**
22 * Which edge of the host element carries the drag handle:
23 * - "left" → panel grows leftward (right-anchored panels)
24 * - "right" → panel grows rightward (left-anchored panels)
25 */
26 readonly edge: "left" | "right";
28​
29export interface ResizableWidthHandlers {
30 readonly onPointerDown: (event: ReactPointerEvent<HTMLElement>) => void;
31 readonly onPointerMove: (event: ReactPointerEvent<HTMLElement>) => void;
32 readonly onPointerUp: (event: ReactPointerEvent<HTMLElement>) => void;
33 readonly onPointerCancel: (event: ReactPointerEvent<HTMLElement>) => void;
34 readonly onLostPointerCapture: (event: ReactPointerEvent<HTMLElement>) => void;
36​
37/**
38 * Width state for a side-anchored panel resized via a drag handle on the
39 * specified edge. Width is read from localStorage on mount and persisted on
40 * drag-end (not on every rAF tick — would otherwise be ~60 writes/sec).
41 *
42 * The hook updates an internal `width` state during drag (so the panel
43 * follows the cursor live) and only commits to localStorage when the user
44 * lifts the pointer or the drag is interrupted.
45 */
46export function useResizableWidth(options: UseResizableWidthOptions): {
47 readonly width: number;
48 readonly handlers: ResizableWidthHandlers;
49} {
50 const { storageKey, defaultWidth, minWidth, maxWidth, edge } = options;
51​
52 const clamp = useCallback(
53 (value: number): number => {
54 if (!Number.isFinite(value)) return defaultWidth;
55 return Math.max(minWidth, Math.min(maxWidth, value));
56 },
57 [defaultWidth, maxWidth, minWidth],
58 );
59​
60 // No cross-tab subscription: panel width is per-window state.
61 const [width, setWidth] = useState<number>(() => {
62 if (typeof window === "undefined") return defaultWidth;
63 try {
64 const stored = getLocalStorageItem(storageKey, WidthSchema);
65 return clamp(stored ?? defaultWidth);
66 } catch (error) {
67 console.error("Could not read persisted panel width.", error);
68 return defaultWidth;
69 }
70 });
71​
72 const clampedWidth = clamp(width);
73 const latestOptions = useRef({ clamp, storageKey });
74 useLayoutEffect(() => {
75 latestOptions.current = { clamp, storageKey };
76 }, [clamp, storageKey]);
77​
78 const handlers = useResizeDrag<HTMLElement>(() => ({
79 width: clampedWidth,
80 edge,
81 resize(value) {
82 const nextWidth = latestOptions.current.clamp(value);
83 setWidth(nextWidth);
84 return nextWidth;
85 },
86 finish(finalWidth) {
87 // Commit once at drag-end to avoid 60Hz localStorage writes.
88 try {
89 setLocalStorageItem(latestOptions.current.storageKey, finalWidth, WidthSchema);
90 } catch (error) {
91 console.error("Could not persist panel width.", error);
92 }
93 },
94 }));
95​
96 return { width: clampedWidth, handlers };