UNPKG

@loke/ui

Version:
2 lines (1 loc) 3.54 kB
var __create=Object.create;var{getPrototypeOf:__getProtoOf,defineProperty:__defProp,getOwnPropertyNames:__getOwnPropNames,getOwnPropertyDescriptor:__getOwnPropDesc}=Object,__hasOwnProp=Object.prototype.hasOwnProperty;function __accessProp(key){return this[key]}var __toESMCache_node,__toESMCache_esm,__toESM=(mod,isNodeMode,target)=>{var canCache=mod!=null&&typeof mod==="object";if(canCache){var cache=isNodeMode?__toESMCache_node??=new WeakMap:__toESMCache_esm??=new WeakMap,cached=cache.get(mod);if(cached)return cached}target=mod!=null?__create(__getProtoOf(mod)):{};let to=isNodeMode||!mod||!mod.__esModule?__defProp(target,"default",{value:mod,enumerable:!0}):target;for(let key of __getOwnPropNames(mod))if(!__hasOwnProp.call(to,key))__defProp(to,key,{get:__accessProp.bind(mod,key),enumerable:!0});if(canCache)cache.set(mod,to);return to},__toCommonJS=(from)=>{var entry=(__moduleCache??=new WeakMap).get(from),desc;if(entry)return entry;if(entry=__defProp({},"__esModule",{value:!0}),from&&typeof from==="object"||typeof from==="function"){for(var key of __getOwnPropNames(from))if(!__hasOwnProp.call(entry,key))__defProp(entry,key,{get:__accessProp.bind(from,key),enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return __moduleCache.set(from,entry),entry},__moduleCache;var __returnValue=(v)=>v;function __exportSetter(name,newValue){this[name]=__returnValue.bind(null,newValue)}var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0,configurable:!0,set:__exportSetter.bind(all,name)})};var exports_use_controllable_state={};__export(exports_use_controllable_state,{useControllableState:()=>useControllableState});module.exports=__toCommonJS(exports_use_controllable_state);var import_use_layout_effect=require("@loke/ui/use-layout-effect"),import_react=__toESM(require("react")),useInsertionEffect=import_react.default[" useInsertionEffect ".trim().toString()]||import_use_layout_effect.useLayoutEffect;function useControllableState({prop,defaultProp,onChange=()=>{},caller}){let[uncontrolledProp,setUncontrolledProp,onChangeRef]=useUncontrolledState({defaultProp,onChange}),isControlled=prop!==void 0,value=isControlled?prop:uncontrolledProp;if(process.env.NODE_ENV!=="production"){let isControlledRef=import_react.useRef(prop!==void 0);import_react.useEffect(()=>{let wasControlled=isControlledRef.current;if(wasControlled!==isControlled)console.warn(`${caller} is changing from ${wasControlled?"controlled":"uncontrolled"} to ${isControlled?"controlled":"uncontrolled"}. 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])}let setValue=import_react.useCallback((nextValue)=>{if(isControlled){let v=isFunction(nextValue)?nextValue(prop):nextValue;if(v!==prop)onChangeRef.current?.(v)}else setUncontrolledProp(nextValue)},[isControlled,prop,setUncontrolledProp,onChangeRef]);return[value,setValue]}function useUncontrolledState({defaultProp,onChange}){let[value,setValue]=import_react.useState(defaultProp),prevValueRef=import_react.useRef(value),onChangeRef=import_react.useRef(onChange);return useInsertionEffect(()=>{onChangeRef.current=onChange},[onChange]),import_react.useEffect(()=>{if(prevValueRef.current!==value)onChangeRef.current?.(value),prevValueRef.current=value},[value,prevValueRef]),[value,setValue,onChangeRef]}function isFunction(value){return typeof value==="function"}