Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { type PointerEvent as ReactPointerEvent, useLayoutEffect, useRef, 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 onPointerDown: (event: ReactPointerEvent<HTMLElement>) => void;
22 readonly onPointerMove: (event: ReactPointerEvent<HTMLElement>) => void;
23 readonly onPointerUp: (event: ReactPointerEvent<HTMLElement>) => void;
24 readonly onPointerCancel: (event: ReactPointerEvent<HTMLElement>) => void;
25 readonly onLostPointerCapture: (event: ReactPointerEvent<HTMLElement>) => void;
26}
27
28/** Persist on drag end to avoid a storage write on every animation frame. */
29export function useResizableWidth(options: UseResizableWidthOptions): {
30 readonly width: number;
31 readonly handlers: ResizableWidthHandlers;
32} {
33 const { storageKey, defaultWidth, minWidth, maxWidth, edge } = options;
34
35 const clamp = (value: number): number => {
36 if (!Number.isFinite(value)) return defaultWidth;
37 return Math.max(minWidth, Math.min(maxWidth, value));
38 };
39
40 const readWidth = () => {
41 if (typeof window === "undefined") return defaultWidth;
42 try {
43 const stored = getLocalStorageItem(storageKey, WidthSchema);
44 return clamp(stored ?? defaultWidth);
45 } catch (error) {
46 console.error("Could not read persisted panel width.", error);
47 return defaultWidth;
48 }
49 };
50 const [widthState, setWidthState] = useState(() => ({ storageKey, width: readWidth() }));
51 if (widthState.storageKey !== storageKey) {
52 setWidthState({ storageKey, width: readWidth() });
53 }
54
55 const clampedWidth = clamp(widthState.width);
56 const latestOptions = useRef({ clamp, storageKey, width: clampedWidth });
57 useLayoutEffect(() => {
58 latestOptions.current = { clamp, storageKey, width: clampedWidth };
59 }, [clamp, clampedWidth, storageKey]);
60
61 const { refresh, ...handlers } = useResizeDrag<HTMLElement>((event) => {
62 const host = event.currentTarget.parentElement;
63 // Disable drag transitions through transition-property; the host renders transition-duration.
64 host?.style.setProperty("transition-property", "none");
65 return {
66 width: clampedWidth,
67 edge,
68 resize(value) {
69 const nextWidth = latestOptions.current.clamp(value);
70 host?.style.setProperty(RESIZABLE_WIDTH_PROPERTY, `${nextWidth}px`);
71 return nextWidth;
72 },
73 finish(finalWidth) {
74 setWidthState({ storageKey, width: finalWidth });
75 try {
76 setLocalStorageItem(latestOptions.current.storageKey, finalWidth, WidthSchema);
77 } catch (error) {
78 console.error("Could not persist panel width.", error);
79 }
80 },
81 cleanup(committed) {
82 host?.style.removeProperty("transition-property");
83 if (!committed) {
84 host?.style.setProperty(RESIZABLE_WIDTH_PROPERTY, `${latestOptions.current.width}px`);
85 }
86 },
87 };
88 }, storageKey);
89
90 // Mid-drag bound changes rewrite committed width; restore the live pointer position.
91 useLayoutEffect(refresh, [clamp, clampedWidth, refresh]);
92
93 return { width: clampedWidth, handlers };
94}