UNPKG

@loke/ui

Version:
184 lines (181 loc) • 8.19 kB
"use client"; 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/switch/index.ts var exports_switch = {}; __export(exports_switch, { Root: () => Root, Switch: () => Switch, SwitchThumb: () => SwitchThumb, Thumb: () => Thumb, createSwitchScope: () => createSwitchScope }); module.exports = __toCommonJS(exports_switch); // src/components/switch/switch.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_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"), SWITCH_NAME = "Switch", [createSwitchContext, createSwitchScope] = import_context.createContextScope(SWITCH_NAME), [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME), Switch = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => { let { __scopeSwitch, name, checked: checkedProp, defaultChecked, required, disabled, value = "on", onCheckedChange, form, ...switchProps } = 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: SWITCH_NAME, defaultProp: defaultChecked ?? !1, onChange: onCheckedChange, prop: checkedProp }), initialCheckedRef = import_react.useRef(checked); return import_react.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__ */ jsx_runtime.jsxs(SwitchProvider, { checked, disabled, scope: __scopeSwitch, children: [ /* @__PURE__ */ jsx_runtime.jsx(import_primitive.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: import_compose_events.composeEventHandlers(props.onClick, (event) => { if (onUserInteraction(), setChecked((prevChecked) => !prevChecked), isFormControl) { if (hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(), !hasConsumerStoppedPropagationRef.current) event.stopPropagation(); } }), ref: composedRefs }), isFormControl && /* @__PURE__ */ jsx_runtime.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__ */ import_react.forwardRef((props, forwardedRef) => { let { __scopeSwitch, ...thumbProps } = props, context = useSwitchContext(THUMB_NAME, __scopeSwitch); return /* @__PURE__ */ jsx_runtime.jsx(import_primitive.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__ */ import_react.forwardRef(({ __scopeSwitch, control, checked, hasConsumerStoppedPropagationRef, userInteractionCount, ...props }, forwardedRef) => { let ref = import_react.useRef(null), composedRefs = import_compose_refs.useComposedRefs(ref, forwardedRef), controlSize = import_use_size.useSize(control), shouldStopClickPropagationRef = import_react.useRef(!1), prevCheckedRef = import_react.useRef(checked), prevUserInteractionCountRef = import_react.useRef(userInteractionCount); return 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) }); setChecked.call(input, checked), input.dispatchEvent(event), shouldStopClickPropagationRef.current = !1; } }, [checked, hasConsumerStoppedPropagationRef, userInteractionCount]), /* @__PURE__ */ jsx_runtime.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;