|
1 | 1 | import * as Schema from "effect/Schema"; |
2 | 2 | import { type PointerEvent as ReactPointerEvent, useLayoutEffect, useState } from "react"; |
3 | 3 |
|
4 | | -import { getLocalStorageItem, setLocalStorageItem } from "./useLocalStorage"; |
| 4 | +import { useLocalStorage, writeLocalStorageValue } from "./useLocalStorage"; |
5 | 5 | import { useResizeDrag } from "./useResizeDrag"; |
6 | 6 | import { useStableCallback } from "./useStableCallback"; |
7 | 7 |
|
8 | | -const WidthSchema = Schema.Finite; |
| 8 | +// Null while no width is saved, so an unsaved width survives a responsive default changing. |
| 9 | +const StoredWidthSchema = Schema.NullOr(Schema.Finite); |
9 | 10 |
|
10 | 11 | /** The host must size itself from this custom property. */ |
11 | 12 | export const RESIZABLE_WIDTH_PROPERTY = "--resizable-width"; |
@@ -40,34 +41,26 @@ export function useResizableWidth(options: UseResizableWidthOptions): { |
40 | 41 | return Math.max(minWidth, Math.min(maxWidth, value)); |
41 | 42 | }; |
42 | 43 |
|
43 | | - const readWidth = (): number | null => { |
44 | | - if (typeof window === "undefined") return null; |
45 | | - try { |
46 | | - const stored = getLocalStorageItem(storageKey, WidthSchema); |
47 | | - return stored === null ? null : clamp(stored); |
48 | | - } catch (error) { |
49 | | - console.error("Could not read persisted panel width.", error); |
50 | | - return null; |
51 | | - } |
52 | | - }; |
53 | | - const [widthState, setWidthState] = useState(() => ({ storageKey, width: readWidth() })); |
54 | | - if (widthState.storageKey !== storageKey) { |
55 | | - setWidthState({ storageKey, width: readWidth() }); |
56 | | - } |
57 | | - |
58 | | - const clampedWidth = clamp(widthState.width ?? defaultWidth); |
| 44 | + // Panels that share a key follow each other's drags, in this tab and in others. |
| 45 | + const [storedWidth] = useLocalStorage(storageKey, null, StoredWidthSchema); |
| 46 | + // Resizing remains usable for this session when browser storage is unavailable. |
| 47 | + const [unsaved, setUnsaved] = useState<{ |
| 48 | + readonly storageKey: string; |
| 49 | + readonly storedWidth: number | null; |
| 50 | + readonly width: number; |
| 51 | + } | null>(null); |
| 52 | + const clampedWidth = clamp( |
| 53 | + unsaved?.storageKey === storageKey && unsaved.storedWidth === storedWidth |
| 54 | + ? unsaved.width |
| 55 | + : (storedWidth ?? defaultWidth), |
| 56 | + ); |
59 | 57 | const restoreWidth = useStableCallback((host: HTMLElement | null) => { |
60 | 58 | host?.style.setProperty(RESIZABLE_WIDTH_PROPERTY, `${clampedWidth}px`); |
61 | 59 | }); |
62 | 60 |
|
63 | 61 | const commitWidth = (nextStorageKey: string, nextWidth: number) => { |
64 | | - // Resizing remains usable for this session when browser storage is unavailable. |
65 | | - setWidthState({ storageKey: nextStorageKey, width: nextWidth }); |
66 | | - try { |
67 | | - setLocalStorageItem(nextStorageKey, nextWidth, WidthSchema); |
68 | | - } catch (error) { |
69 | | - console.error("Could not persist panel width.", error); |
70 | | - } |
| 62 | + const saved = writeLocalStorageValue(nextStorageKey, nextWidth, null, StoredWidthSchema); |
| 63 | + setUnsaved(saved ? null : { storageKey: nextStorageKey, storedWidth, width: nextWidth }); |
71 | 64 | }; |
72 | 65 |
|
73 | 66 | const drag = useResizeDrag<HTMLElement>((event) => { |
|
0 commit comments