@loke/ui
Version:
To install dependencies:
142 lines (140 loc) • 5.55 kB
JavaScript
"use client";
// src/components/switch/switch.tsx
import { composeEventHandlers } from "@loke/ui/compose-events";
import { useComposedRefs } from "@loke/ui/compose-refs";
import { createContextScope } from "@loke/ui/context";
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 SWITCH_NAME = "Switch", [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME), [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME), Switch = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let {
__scopeSwitch,
name,
checked: checkedProp,
defaultChecked,
required,
disabled,
value = "on",
onCheckedChange,
form,
...switchProps
} = 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: SWITCH_NAME,
defaultProp: defaultChecked ?? !1,
onChange: onCheckedChange,
prop: checkedProp
}), initialCheckedRef = useRef(checked);
return useEffect(() => {
let associatedForm = form ? button?.ownerDocument.getElementById(form) : button?.form;
if (!(associatedForm instanceof HTMLFormElement))
return;
let reset = () => setChecked(initialCheckedRef.current);
return associatedForm.addEventListener("reset", reset), () => associatedForm.removeEventListener("reset", reset);
}, [button, form, setChecked]), /* @__PURE__ */ jsxs(SwitchProvider, {
checked,
disabled,
scope: __scopeSwitch,
children: [
/* @__PURE__ */ jsx(Primitive.button, {
"aria-checked": checked,
"aria-required": required,
"data-disabled": disabled ? "" : void 0,
"data-state": getState(checked),
disabled,
role: "switch",
type: "button",
value,
...switchProps,
onClick: composeEventHandlers(props.onClick, (event) => {
if (onUserInteraction(), setChecked((prevChecked) => !prevChecked), isFormControl) {
if (hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(), !hasConsumerStoppedPropagationRef.current)
event.stopPropagation();
}
}),
ref: composedRefs
}),
isFormControl && /* @__PURE__ */ jsx(SwitchBubbleInput, {
checked,
control: button,
disabled,
form,
hasConsumerStoppedPropagationRef,
name,
required,
style: { transform: "translateX(-100%)" },
userInteractionCount,
value
})
]
});
}), { displayName: SWITCH_NAME }), THUMB_NAME = "SwitchThumb", SwitchThumb = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => {
let { __scopeSwitch, ...thumbProps } = props, context = useSwitchContext(THUMB_NAME, __scopeSwitch);
return /* @__PURE__ */ jsx(Primitive.span, {
"data-disabled": context.disabled ? "" : void 0,
"data-state": getState(context.checked),
...thumbProps,
ref: forwardedRef
});
}), { displayName: THUMB_NAME }), BUBBLE_INPUT_NAME = "SwitchBubbleInput", SwitchBubbleInput = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef(({
__scopeSwitch,
control,
checked,
hasConsumerStoppedPropagationRef,
userInteractionCount,
...props
}, forwardedRef) => {
let ref = useRef(null), composedRefs = useComposedRefs(ref, forwardedRef), controlSize = useSize(control), shouldStopClickPropagationRef = useRef(!1), prevCheckedRef = useRef(checked), prevUserInteractionCountRef = useRef(userInteractionCount);
return 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)
});
setChecked.call(input, checked), input.dispatchEvent(event), shouldStopClickPropagationRef.current = !1;
}
}, [checked, hasConsumerStoppedPropagationRef, userInteractionCount]), /* @__PURE__ */ jsx("input", {
"aria-hidden": !0,
defaultChecked: checked,
type: "checkbox",
...props,
onClick: (event) => {
if (shouldStopClickPropagationRef.current)
event.stopPropagation();
},
ref: composedRefs,
style: {
...props.style,
...controlSize,
margin: 0,
opacity: 0,
pointerEvents: "none",
position: "absolute"
},
tabIndex: -1
});
}), { displayName: BUBBLE_INPUT_NAME });
function getState(checked) {
return checked ? "checked" : "unchecked";
}
var Root = Switch, Thumb = SwitchThumb;
export {
Root,
Switch,
SwitchThumb,
Thumb,
createSwitchScope
};