UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

38 lines (37 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSessionStorageValue = void 0; var useStorageValue_1 = require("../useStorageValue/useStorageValue"); var const_1 = require("../util/const"); var IS_SESSION_STORAGE_AVAILABLE = false; try { IS_SESSION_STORAGE_AVAILABLE = const_1.isBrowser && !!window.sessionStorage; } catch (_a) { // no need to test this flag leads to noop behaviour /* istanbul ignore next */ IS_SESSION_STORAGE_AVAILABLE = false; } /** * Manages a single sessionStorage key. * * @param key Storage key to manage * @param defaultValue Default value to yield in case the key is not in storage * @param options */ exports.useSessionStorageValue = IS_SESSION_STORAGE_AVAILABLE ? function (key, defaultValue, options) { if (defaultValue === void 0) { defaultValue = null; } if (options === void 0) { options = {}; } return (0, useStorageValue_1.useStorageValue)(sessionStorage, key, defaultValue, options); } : function (key, defaultValue, options) { if (defaultValue === void 0) { defaultValue = null; } if (options === void 0) { options = {}; } /* istanbul ignore next */ if (const_1.isBrowser && process.env.NODE_ENV === 'development') { // eslint-disable-next-line no-console console.warn('SessionStorage is not available in this environment'); } return [undefined, const_1.noop, const_1.noop, const_1.noop]; };