UNPKG

@liveblocks/react-ui

Version:

A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.

35 lines (31 loc) 1.15 kB
'use strict'; var core = require('@liveblocks/core'); var react = require('react'); function useControllableState(value, onChange, defaultValue) { const [uncontrolledValue, setUncontrolledValue] = react.useState(defaultValue); const isControlled = value !== void 0; const wasControlled = react.useRef(isControlled); react.useEffect(() => { if (process.env.NODE_ENV !== "production" && wasControlled.current !== isControlled) { core.console.warn( `A component is changing from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.` ); } wasControlled.current = isControlled; }, [isControlled]); const currentValue = isControlled ? value : uncontrolledValue; const setValue = react.useCallback( (value2) => { if (isControlled) { return onChange?.(value2); } else { setUncontrolledValue(value2); return onChange?.(value2); } }, [isControlled, onChange] ); return [currentValue, setValue]; } exports.useControllableState = useControllableState; //# sourceMappingURL=use-controllable-state.cjs.map