UNPKG

toggles

Version:

A collection of React hooks for toggles with dynamic noun and verb APIs

77 lines (76 loc) 5.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toggle = exports.unsubscribe = exports.subscribe = exports.unlock = exports.lock = exports.dismiss = exports.display = exports.conceal = exports.reveal = exports.unmount = exports.mount = exports.blur = exports.focus = exports.disconnect = exports.connect = exports.end = exports.start = exports.deactivate = exports.activate = exports.collapse = exports.expand = exports.disable = exports.enable = exports.uncheck = exports.check = exports.turnOff = exports.turnOn = exports.hide = exports.show = exports.close = exports.open = exports.verbs = exports.useToggle = exports.useToggles = void 0; var react_1 = require("react"); var globalNouns_1 = require("./src/globalNouns"); var nouns_1 = require("./src/nouns"); var verbs_1 = require("./src/verbs"); function useToggles() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var namespace = typeof args[0] === 'string' && args[0] ? args[0] : undefined; var initialValues = (namespace ? args.slice(1) : args); var rerender = (0, react_1.useReducer)(function () { return ({}); }, 0)[1]; var nounOrder = (0, react_1.useRef)([]); var unsubscribers = (0, react_1.useRef)(new Map()); (0, react_1.useLayoutEffect)(function () { return function () { unsubscribers.current.forEach(function (unsub, key) { unsub(); if (namespace) globalNouns_1.globalNouns.release(key); }); unsubscribers.current.clear(); }; }, [namespace]); var localNouns = (0, react_1.useRef)({}); var nouns = (0, react_1.useMemo)(function () { return new Proxy({}, { get: function (_, prop) { var _a, _b; var _c; if (typeof prop !== 'string') return undefined; // Check for verb name conflicts if (prop in verbs_1.verbs) { var message = "Invalid noun name \"".concat(prop, "\": noun names must not conflict with verb names"); if (process.env.NODE_ENV !== 'production') throw new Error(message); console.warn(message); } // Track order of noun access for initial values if (!nounOrder.current.includes(prop)) nounOrder.current.push(prop); var nounIndex = nounOrder.current.indexOf(prop); var initialValue = (_a = initialValues[nounIndex]) !== null && _a !== void 0 ? _a : false; // Global noun - use global registry with namespace prefix if (namespace) { var key = "".concat(namespace, ":").concat(prop); var noun = globalNouns_1.globalNouns.get(key, initialValue); if (!unsubscribers.current.has(key)) { globalNouns_1.globalNouns.acquire(key); unsubscribers.current.set(key, globalNouns_1.globalNouns.subscribe(key, rerender)); } return noun; } // Local noun - use local state (_b = (_c = localNouns.current)[prop]) !== null && _b !== void 0 ? _b : (_c[prop] = (function () { var entry = { state: initialValue, noun: null }; entry.noun = (0, nouns_1.createNounFromState)(prop, function () { return entry.state; }, function (val) { entry.state = val; rerender(); }); return entry; })()); return localNouns.current[prop].noun; } }); }, [initialValues, namespace, rerender]); return nouns; } exports.useToggles = useToggles; function useToggle(initial) { var nouns = useToggles(initial); var key = (0, react_1.useMemo)(function () { return Math.random().toString(36).substring(2, 8); }, []); return nouns[key]; } exports.useToggle = useToggle; var verbs_2 = require("./src/verbs"); Object.defineProperty(exports, "verbs", { enumerable: true, get: function () { return verbs_2.verbs; } }); exports.open = verbs_1.verbs.open, exports.close = verbs_1.verbs.close, exports.show = verbs_1.verbs.show, exports.hide = verbs_1.verbs.hide, exports.turnOn = verbs_1.verbs.turnOn, exports.turnOff = verbs_1.verbs.turnOff, exports.check = verbs_1.verbs.check, exports.uncheck = verbs_1.verbs.uncheck, exports.enable = verbs_1.verbs.enable, exports.disable = verbs_1.verbs.disable, exports.expand = verbs_1.verbs.expand, exports.collapse = verbs_1.verbs.collapse, exports.activate = verbs_1.verbs.activate, exports.deactivate = verbs_1.verbs.deactivate, exports.start = verbs_1.verbs.start, exports.end = verbs_1.verbs.end, exports.connect = verbs_1.verbs.connect, exports.disconnect = verbs_1.verbs.disconnect, exports.focus = verbs_1.verbs.focus, exports.blur = verbs_1.verbs.blur, exports.mount = verbs_1.verbs.mount, exports.unmount = verbs_1.verbs.unmount, exports.reveal = verbs_1.verbs.reveal, exports.conceal = verbs_1.verbs.conceal, exports.display = verbs_1.verbs.display, exports.dismiss = verbs_1.verbs.dismiss, exports.lock = verbs_1.verbs.lock, exports.unlock = verbs_1.verbs.unlock, exports.subscribe = verbs_1.verbs.subscribe, exports.unsubscribe = verbs_1.verbs.unsubscribe, exports.toggle = verbs_1.verbs.toggle; exports.default = useToggles;