UNPKG

@toolpad/utils

Version:

Shared utilities used by Toolpad packages.

72 lines (69 loc) 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _events = require("../events"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } // storage events only work across windows, we'll use an event emitter to announce within the window const emitter = new _events.Emitter(); function subscribe(area, key, cb) { const storageHandler = event => { if (event.storageArea === area && event.key === key) { cb(); } }; window.addEventListener('storage', storageHandler); emitter.on(key, cb); return () => { window.removeEventListener('storage', storageHandler); emitter.off(key, cb); }; } function getSnapshot(area, key) { return area.getItem(key); } function setValue(area, key, value) { if (typeof window !== 'undefined') { if (value === null) { area.removeItem(key); } else { area.setItem(key, String(value)); } emitter.emit(key, null); } } function useStorageStateServer(kind, key, initializer = null) { const [initialValue] = React.useState(initializer); return [initialValue, () => {}]; } /** * Sync state to local/session storage so that it persists through a page refresh. Usage is * similar to useState except we pass in a storage key so that we can default * to that value on page load instead of the specified initial value. * * Since the storage API isn't available in server-rendering environments, we * return initialValue during SSR and hydration. * * Things this hook does different from existing solutions: * - SSR-capable: it shows initial value during SSR and hydration, but immediately * initializes when clientside mounted. * - Sync state across tabs: When another tab changes the value in the storage area, the * current tab follows suit. */ function useStorageStateBrowser(kind, key, initializer = null) { const [initialValue] = React.useState(initializer); const area = kind === 'session' ? window.sessionStorage : window.localStorage; const subscribeKey = React.useCallback(cb => subscribe(area, key, cb), [area, key]); const getKeySnapshot = React.useCallback(() => getSnapshot(area, key) ?? initialValue, [area, initialValue, key]); const getKeyServerSnapshot = React.useCallback(() => initialValue, [initialValue]); const storedValue = React.useSyncExternalStore(subscribeKey, getKeySnapshot, getKeyServerSnapshot); const setStoredValue = React.useCallback(value => { const valueToStore = value instanceof Function ? value(storedValue) : value; setValue(area, key, valueToStore); }, [area, key, storedValue]); return [storedValue, setStoredValue]; } var _default = exports.default = typeof window === 'undefined' ? useStorageStateServer : useStorageStateBrowser;