Bounds Persistence
Simple persistence
Section titled “Simple persistence”Add persistBounds with a unique key. Bounds save automatically to localStorage and restore on next open.
<Window open={show} persistBounds="settings" defaultWidth={600} defaultHeight={400}> <Settings /></Window>First open uses the defaults. After the user resizes or moves the window, those bounds are saved. Next open restores them.
The key should be unique per window. Use something stable and descriptive like "settings" or "editor-main".
Manual control with usePersistedBounds
Section titled “Manual control with usePersistedBounds”For cases where you need to read bounds before rendering, reset them programmatically, or integrate with your own storage:
import { usePersistedBounds } from "@loc/electron-window";
function PersistentWindow({ children }) { const { bounds, save, clear } = usePersistedBounds("my-window", { defaultWidth: 800, defaultHeight: 600, });
return ( <Window open {...bounds} onBoundsChange={save}> {children} <button onClick={clear}>Reset Position</button> </Window> );}Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
defaultWidth | number | — | Width when nothing is saved |
defaultHeight | number | — | Height when nothing is saved |
defaultX | number | — | X position when nothing is saved |
defaultY | number | — | Y position when nothing is saved |
saveDebounceMs | number | 500 | Debounce for save() — writes coalesce |
Return values
Section titled “Return values”| Value | Type | Description |
|---|---|---|
bounds | { defaultWidth?, defaultHeight?, defaultX?, defaultY? } | Saved bounds mapped to default* prop names, or the defaults you passed. Spread directly into <Window {...bounds}>. |
save | (bounds: Bounds) => void | Persist new bounds (pass directly to onBoundsChange). Debounced. |
clear | () => void | Remove saved bounds, revert to defaults |
The bounds keys are defaultWidth/defaultHeight/defaultX/defaultY — not width/height/x/y — so spreading them doesn’t accidentally create a controlled window.
Clearing bounds without the hook
Section titled “Clearing bounds without the hook”For “reset all settings” or logout flows where you don’t have the hook mounted:
import { clearPersistedBounds, clearAllPersistedBounds } from "@loc/electron-window";
clearPersistedBounds("settings"); // one keyclearAllPersistedBounds(); // every key this library has storedStorage
Section titled “Storage”Bounds are stored in localStorage under a namespaced key (electron-window:bounds:<your-key>). The stored value includes x, y, width, and height.