UNPKG

@ryvora/react-use-controllable-state

Version:

🎮🔄 Controlled/uncontrolled state hook for React. Build flexible, predictable components!

170 lines (166 loc) • 6.48 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { useControllableState: () => useControllableState, useControllableStateReducer: () => useControllableStateReducer }); module.exports = __toCommonJS(index_exports); // src/use-controllable-state.tsx var React = __toESM(require("react")); var import_react_use_layout_effect = require("@ryvora/react-use-layout-effect"); var useInsertionEffect = React[" useInsertionEffect ".trim().toString()] || import_react_use_layout_effect.useLayoutEffect; function useControllableState({ prop, defaultProp, onChange = () => { }, caller }) { const [uncontrolledProp, setUncontrolledProp, onChangeRef] = useUncontrolledState({ defaultProp, onChange }); const isControlled = prop !== void 0; const value = isControlled ? prop : uncontrolledProp; if (true) { const isControlledRef = React.useRef(prop !== void 0); React.useEffect(() => { const wasControlled = isControlledRef.current; if (wasControlled !== isControlled) { const from = wasControlled ? "controlled" : "uncontrolled"; const to = isControlled ? "controlled" : "uncontrolled"; console.warn( `${caller} is changing from ${from} to ${to}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.` ); } isControlledRef.current = isControlled; }, [isControlled, caller]); } const setValue = React.useCallback( (nextValue) => { if (isControlled) { const value2 = isFunction(nextValue) ? nextValue(prop) : nextValue; if (value2 !== prop) { onChangeRef.current?.(value2); } } else { setUncontrolledProp(nextValue); } }, [isControlled, prop, setUncontrolledProp, onChangeRef] ); return [value, setValue]; } function useUncontrolledState({ defaultProp, onChange }) { const [value, setValue] = React.useState(defaultProp); const prevValueRef = React.useRef(value); const onChangeRef = React.useRef(onChange); useInsertionEffect(() => { onChangeRef.current = onChange; }, [onChange]); React.useEffect(() => { if (prevValueRef.current !== value) { onChangeRef.current?.(value); prevValueRef.current = value; } }, [value, prevValueRef]); return [value, setValue, onChangeRef]; } function isFunction(value) { return typeof value === "function"; } // src/use-controllable-state-reducer.tsx var React2 = __toESM(require("react")); var import_react_use_effect_event = require("@ryvora/react-use-effect-event"); var SYNC_STATE = Symbol("RYVORA:SYNC_STATE"); function useControllableStateReducer(reducer, userArgs, initialArg, init) { const { prop: controlledState, defaultProp, onChange: onChangeProp, caller } = userArgs; const isControlled = controlledState !== void 0; const onChange = (0, import_react_use_effect_event.useEffectEvent)(onChangeProp); if (true) { const isControlledRef = React2.useRef(controlledState !== void 0); React2.useEffect(() => { const wasControlled = isControlledRef.current; if (wasControlled !== isControlled) { const from = wasControlled ? "controlled" : "uncontrolled"; const to = isControlled ? "controlled" : "uncontrolled"; console.warn( `${caller} is changing from ${from} to ${to}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.` ); } isControlledRef.current = isControlled; }, [isControlled, caller]); } const args = [{ ...initialArg, state: defaultProp }]; if (init) { args.push(init); } const [internalState, dispatch] = React2.useReducer( (state2, action) => { if (action.type === SYNC_STATE) { return { ...state2, state: action.state }; } const next = reducer(state2, action); if (isControlled && !Object.is(next.state, state2.state)) { onChange(next.state); } return next; }, ...args ); const uncontrolledState = internalState.state; const prevValueRef = React2.useRef(uncontrolledState); React2.useEffect(() => { if (prevValueRef.current !== uncontrolledState) { prevValueRef.current = uncontrolledState; if (!isControlled) { onChange(uncontrolledState); } } }, [onChange, uncontrolledState, prevValueRef, isControlled]); const state = React2.useMemo(() => { const isControlled2 = controlledState !== void 0; if (isControlled2) { return { ...internalState, state: controlledState }; } return internalState; }, [internalState, controlledState]); React2.useEffect(() => { if (isControlled && !Object.is(controlledState, internalState.state)) { dispatch({ type: SYNC_STATE, state: controlledState }); } }, [controlledState, internalState.state, isControlled]); return [state, dispatch]; } //# sourceMappingURL=index.js.map