UNPKG

@loke/ui

Version:
199 lines (196 loc) • 8.95 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 ??= /* @__PURE__ */ new WeakMap : __toESMCache_esm ??= /* @__PURE__ */ new WeakMap, cached = cache.get(mod); if (cached) return cached; } target = mod != null ? __create(__getProtoOf(mod)) : {}; let to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target; if (mod && typeof mod === "object" || typeof mod === "function") { 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 ??= /* @__PURE__ */ 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) }); }; // src/components/checkbox/index.ts var exports_checkbox = {}; __export(exports_checkbox, { Checkbox: () => Checkbox, CheckboxIndicator: () => CheckboxIndicator, Indicator: () => Indicator, Root: () => Root, createCheckboxScope: () => createCheckboxScope }); module.exports = __toCommonJS(exports_checkbox); // src/components/checkbox/checkbox.tsx var import_compose_events = require("@loke/ui/compose-events"), import_compose_refs = require("@loke/ui/compose-refs"), import_context = require("@loke/ui/context"), import_presence = require("@loke/ui/presence"), import_primitive = require("@loke/ui/primitive"), import_use_controllable_state = require("@loke/ui/use-controllable-state"), import_use_size = require("@loke/ui/use-size"), import_react = require("react"), jsx_runtime = require("react/jsx-runtime"), CHECKBOX_NAME = "Checkbox", [createCheckboxContext, createCheckboxScope] = import_context.createContextScope(CHECKBOX_NAME), [CheckboxProvider, useCheckboxContext] = createCheckboxContext(CHECKBOX_NAME), Checkbox = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => { let { __scopeCheckbox, name, checked: checkedProp, defaultChecked, required, disabled, value = "on", onCheckedChange, form, ...checkboxProps } = props, [button, setButton] = import_react.useState(null), composedRefs = import_compose_refs.useComposedRefs(forwardedRef, setButton), hasConsumerStoppedPropagationRef = import_react.useRef(!1), [userInteractionCount, onUserInteraction] = import_react.useReducer((count) => count + 1, 0), isFormControl = button ? form || !!button.closest("form") : !0, [checked, setChecked] = import_use_controllable_state.useControllableState({ caller: CHECKBOX_NAME, defaultProp: defaultChecked ?? !1, onChange: onCheckedChange, prop: checkedProp }), initialCheckedStateRef = import_react.useRef(checked); return import_react.useEffect(() => { let buttonForm = button?.form; if (buttonForm) { let reset = () => setChecked(initialCheckedStateRef.current); return buttonForm.addEventListener("reset", reset), () => buttonForm.removeEventListener("reset", reset); } }, [button, setChecked]), /* @__PURE__ */ jsx_runtime.jsxs(CheckboxProvider, { disabled, scope: __scopeCheckbox, state: checked, children: [ /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.button, { "aria-checked": isIndeterminate(checked) ? "mixed" : checked, "aria-required": required, "data-disabled": disabled ? "" : void 0, "data-state": getState(checked), disabled, role: "checkbox", type: "button", value, ...checkboxProps, onClick: import_compose_events.composeEventHandlers(props.onClick, (event) => { if (onUserInteraction(), setChecked((prevChecked) => isIndeterminate(prevChecked) ? !0 : !prevChecked), isFormControl) { if (hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(), !hasConsumerStoppedPropagationRef.current) event.stopPropagation(); } }), onKeyDown: import_compose_events.composeEventHandlers(props.onKeyDown, (event) => { if (event.key === "Enter") event.preventDefault(); }), ref: composedRefs }), isFormControl && /* @__PURE__ */ jsx_runtime.jsx(BubbleInput, { checked, control: button, defaultChecked: isIndeterminate(defaultChecked) ? !1 : defaultChecked, disabled, form, hasConsumerStoppedPropagationRef, name, required, style: { transform: "translateX(-100%)" }, userInteractionCount, value }) ] }); }), { displayName: CHECKBOX_NAME }), INDICATOR_NAME = "CheckboxIndicator", CheckboxIndicator = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => { let { __scopeCheckbox, forceMount, ...indicatorProps } = props, context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox); return /* @__PURE__ */ jsx_runtime.jsx(import_presence.Presence, { present: forceMount || isIndeterminate(context.state) || context.state === !0, children: /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.span, { "data-disabled": context.disabled ? "" : void 0, "data-state": getState(context.state), ...indicatorProps, ref: forwardedRef, style: { pointerEvents: "none", ...props.style } }) }); }), { displayName: INDICATOR_NAME }), BubbleInput = (props) => { let { control, checked, defaultChecked, hasConsumerStoppedPropagationRef, userInteractionCount, ...inputProps } = props, ref = import_react.useRef(null), controlSize = import_use_size.useSize(control), shouldStopClickPropagationRef = import_react.useRef(!1), prevCheckedRef = import_react.useRef(checked), prevUserInteractionCountRef = import_react.useRef(userInteractionCount); import_react.useEffect(() => { let input = ref.current; if (!input) return; let inputProto = window.HTMLInputElement.prototype, setChecked = Object.getOwnPropertyDescriptor(inputProto, "checked").set, isUserInteraction = userInteractionCount !== prevUserInteractionCountRef.current; prevUserInteractionCountRef.current = userInteractionCount; let checkedChanged = prevCheckedRef.current !== checked; if (prevCheckedRef.current = checked, checkedChanged && setChecked) { shouldStopClickPropagationRef.current = !isUserInteraction; let event = new Event("click", { bubbles: !(isUserInteraction && hasConsumerStoppedPropagationRef.current) }); input.indeterminate = isIndeterminate(checked), setChecked.call(input, isIndeterminate(checked) ? !1 : checked), input.dispatchEvent(event), shouldStopClickPropagationRef.current = !1; } }, [checked, hasConsumerStoppedPropagationRef, userInteractionCount]); let defaultCheckedRef = import_react.useRef(isIndeterminate(checked) ? !1 : checked); return /* @__PURE__ */ jsx_runtime.jsx("input", { "aria-hidden": !0, defaultChecked: defaultChecked ?? defaultCheckedRef.current, type: "checkbox", ...inputProps, onClick: (event) => { if (shouldStopClickPropagationRef.current) event.stopPropagation(); }, ref, style: { ...props.style, ...controlSize, margin: 0, opacity: 0, pointerEvents: "none", position: "absolute" }, tabIndex: -1 }); }; function isIndeterminate(checked) { return checked === "indeterminate"; } function getState(checked) { if (isIndeterminate(checked)) return "indeterminate"; return checked ? "checked" : "unchecked"; } var Root = Checkbox, Indicator = CheckboxIndicator;