UNPKG

@coveord/plasma-mantine

Version:

A Plasma flavoured Mantine theme

146 lines (145 loc) 6.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "useUrlSyncedState", { enumerable: true, get: function() { return useUrlSyncedState; } }); var _instanceof = require("@swc/helpers/_/_instanceof"); var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array"); var _react = require("react"); var slice = Function.prototype.call.bind(Array.prototype.slice); /** * Split a url into its parts. * * @param href The url to extract the parts from. * @returns The separate parts, all are an empty string if not present. */ var extractParts = function(href) { return slice(/^([^?#]*)(\?[^#]*|)(#[^?]*|)(\?.*|)$/.exec(href !== null && href !== void 0 ? href : ''), 1, 5); }; /** * The index of the search parameter to use, e.g. hashSearch for hash routes (hash starts with '#/'). * * @param parts: The url parts, as returned by `extractParts`. * @returns The index of the search parameter to use (1 or 3). */ var searchIndex = function(parts) { return /^#\//.test(parts[2]) ? 3 : 1; }; /** * Read the **current** search params from `window.location`, with support for detecting React's HashRouter. * Also returns a method that will yield the href (string) value, after any changes made on the params object. * * @returns The `URLSearchParams` instance, and a function that can be used to get an updated href. */ var getSearchParams = function() { var parts = extractParts(window.location.href); return new URLSearchParams(parts[searchIndex(parts)]); }; /** * Apply the search params to the current location, using `replaceState` (no navigation history). * Note that only parameters in the `params` argument will be set, any other current params will be removed. * * @param params The parameters to apply. */ var applySearchParams = function(params) { var currentHref = window.location.href; var parts = extractParts(currentHref); var search = params.size > 0 ? "?".concat(params.toString()) : ''; var index = searchIndex(parts); if (parts[index] !== search) { parts[index] = search; window.history.replaceState(null, '', parts.join('')); } }; var getInitialState = function(options) { return _instanceof._(options.initialState, Function) ? options.initialState() : options.initialState; }; var useUrlSyncedState = function(options) { var sync = options.sync !== false; var _useState = _sliced_to_array._((0, _react.useState)(function() { var initialState = getInitialState(options); return sync ? options.deserializer(getSearchParams(), initialState) : initialState; }), 2), state = _useState[0], setState = _useState[1]; // Capture the initial state as a map (first render only!), to compare values and see if they should be set to the params. var initialStateSerialized = (0, _react.useMemo)(function() { var stateMap = new Map(); var initialize = null; var needsApply = false; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = options.serializer(getInitialState(options))[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var _step_value = _sliced_to_array._(_step.value, 3), key = _step_value[0], value = _step_value[1], alwaysEmit = _step_value[2]; stateMap.set(key, value); if (sync && alwaysEmit && value) { initialize !== null && initialize !== void 0 ? initialize : initialize = getSearchParams(); if (!initialize.has(key)) { needsApply = true; initialize.set(key, value); } } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } if (needsApply) { applySearchParams(initialize); } return stateMap; }, []); var enhancedSetState = (0, _react.useMemo)(function() { if (!sync) { return setState; } return function(updater) { setState(function(old) { var newValue = _instanceof._(updater, Function) ? updater(old) : updater; var search = getSearchParams(); var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = options.serializer(newValue)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var _step_value = _sliced_to_array._(_step.value, 3), key = _step_value[0], value = _step_value[1], alwaysEmit = _step_value[2]; if (value && (alwaysEmit || !Object.is(initialStateSerialized.get(key), value))) { search.set(key, value); } else { search.delete(key); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } applySearchParams(search); return newValue; }); }; }, [ sync ]); return [ state, enhancedSetState ]; }; //# sourceMappingURL=use-url-synced-state.js.map