@loke/ui
Version:
To install dependencies:
157 lines (156 loc) • 6.26 kB
JavaScript
// src/components/checkbox/checkbox.tsx
import { composeEventHandlers } from "@loke/ui/compose-events";
import { useComposedRefs } from "@loke/ui/compose-refs";
import { createContextScope } from "@loke/ui/context";
import { Presence } from "@loke/ui/presence";
import { Primitive } from "@loke/ui/primitive";
import { useControllableState } from "@loke/ui/use-controllable-state";
import { useSize } from "@loke/ui/use-size";
import {
forwardRef,
useEffect,
useReducer,
useRef,
useState
} from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var CHECKBOX_NAME = "Checkbox", [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME), [CheckboxProvider, useCheckboxContext] = createCheckboxContext(CHECKBOX_NAME), Checkbox = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let {
__scopeCheckbox,
name,
checked: checkedProp,
defaultChecked,
required,
disabled,
value = "on",
onCheckedChange,
form,
...checkboxProps
} = props, [button, setButton] = useState(null), composedRefs = useComposedRefs(forwardedRef, setButton), hasConsumerStoppedPropagationRef = useRef(!1), [userInteractionCount, onUserInteraction] = useReducer((count) => count + 1, 0), isFormControl = button ? form || !!button.closest("form") : !0, [checked, setChecked] = useControllableState({
caller: CHECKBOX_NAME,
defaultProp: defaultChecked ?? !1,
onChange: onCheckedChange,
prop: checkedProp
}), initialCheckedStateRef = useRef(checked);
return useEffect(() => {
let buttonForm = button?.form;
if (buttonForm) {
let reset = () => setChecked(initialCheckedStateRef.current);
return buttonForm.addEventListener("reset", reset), () => buttonForm.removeEventListener("reset", reset);
}
}, [button, setChecked]), /* @__PURE__ */ jsxs(CheckboxProvider, {
disabled,
scope: __scopeCheckbox,
state: checked,
children: [
/* @__PURE__ */ jsx(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: composeEventHandlers(props.onClick, (event) => {
if (onUserInteraction(), setChecked((prevChecked) => isIndeterminate(prevChecked) ? !0 : !prevChecked), isFormControl) {
if (hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(), !hasConsumerStoppedPropagationRef.current)
event.stopPropagation();
}
}),
onKeyDown: composeEventHandlers(props.onKeyDown, (event) => {
if (event.key === "Enter")
event.preventDefault();
}),
ref: composedRefs
}),
isFormControl && /* @__PURE__ */ 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__ */ forwardRef((props, forwardedRef) => {
let { __scopeCheckbox, forceMount, ...indicatorProps } = props, context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);
return /* @__PURE__ */ jsx(Presence, {
present: forceMount || isIndeterminate(context.state) || context.state === !0,
children: /* @__PURE__ */ jsx(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 = useRef(null), controlSize = useSize(control), shouldStopClickPropagationRef = useRef(!1), prevCheckedRef = useRef(checked), prevUserInteractionCountRef = useRef(userInteractionCount);
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 = useRef(isIndeterminate(checked) ? !1 : checked);
return /* @__PURE__ */ 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;
export {
Checkbox,
CheckboxIndicator,
Indicator,
Root,
createCheckboxScope
};