@arolariu/components
Version:
🎨 70+ beautiful, accessible React components built on Base UI. TypeScript-first, CSS Modules styling, tree-shakeable, SSR-ready. Perfect for modern web apps, design systems & rapid prototyping. Zero config, maximum flexibility! ⚡
29 lines (28 loc) • 1.08 kB
JavaScript
"use client";
import * as __rspack_external_react from "react";
function useControllableState(options) {
const { controlled, defaultValue, onChange } = options;
const [uncontrolledState, setUncontrolledState] = __rspack_external_react.useState(defaultValue);
const isControlled = void 0 !== controlled;
const value = isControlled ? controlled : uncontrolledState;
const setValue = __rspack_external_react.useCallback((nextValue)=>{
if (isControlled) {
const resolvedValue = "function" == typeof nextValue ? nextValue(controlled) : nextValue;
onChange?.(resolvedValue);
} else setUncontrolledState((currentValue)=>{
const resolvedValue = "function" == typeof nextValue ? nextValue(currentValue) : nextValue;
onChange?.(resolvedValue);
return resolvedValue;
});
}, [
isControlled,
onChange,
controlled
]);
return [
value,
setValue
];
}
export { useControllableState };
//# sourceMappingURL=useControllableState.js.map