UNPKG

ivt

Version:

Ivt Components Library

145 lines (141 loc) 6.03 kB
import * as React from 'react'; import { c as composeEventHandlers } from '../chunks/index-Bl-WJHvp.mjs'; import { u as useComposedRefs } from '../chunks/index-1tQVI0Jh.mjs'; import { c as createContextScope } from '../chunks/index-DT8WgpCS.mjs'; import { u as useControllableState } from '../chunks/index-D4FMFHi9.mjs'; import { u as usePrevious } from '../chunks/index-FL3PKmOS.mjs'; import { u as useSize } from '../chunks/index-DmY774z-.mjs'; import { P as Primitive } from '../chunks/index-DgKlJYZP.mjs'; import { jsxs, jsx } from 'react/jsx-runtime'; import { c as cn } from '../chunks/utils-05LlW3Cl.mjs'; import '../chunks/index-DKOlG3mh.mjs'; import 'react-dom'; import '@radix-ui/react-slot'; import '../chunks/bundle-mjs-BYcyWisL.mjs'; var SWITCH_NAME = "Switch"; var [createSwitchContext] = createContextScope(SWITCH_NAME); var [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME); var Switch$1 = React.forwardRef((props, forwardedRef)=>{ const { __scopeSwitch, name, checked: checkedProp, defaultChecked, required, disabled, value = "on", onCheckedChange, form, ...switchProps } = props; const [button, setButton] = React.useState(null); const composedRefs = useComposedRefs(forwardedRef, (node)=>setButton(node)); const hasConsumerStoppedPropagationRef = React.useRef(false); const isFormControl = button ? form || !!button.closest("form") : true; const [checked, setChecked] = useControllableState({ prop: checkedProp, defaultProp: defaultChecked ?? false, onChange: onCheckedChange, caller: SWITCH_NAME }); return /* @__PURE__ */ jsxs(SwitchProvider, { scope: __scopeSwitch, checked, disabled, children: [ /* @__PURE__ */ jsx(Primitive.button, { type: "button", role: "switch", "aria-checked": checked, "aria-required": required, "data-state": getState(checked), "data-disabled": disabled ? "" : void 0, disabled, value, ...switchProps, ref: composedRefs, onClick: composeEventHandlers(props.onClick, (event)=>{ setChecked((prevChecked)=>!prevChecked); if (isFormControl) { hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(); if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation(); } }) }), isFormControl && /* @__PURE__ */ jsx(SwitchBubbleInput, { control: button, bubbles: !hasConsumerStoppedPropagationRef.current, name, value, checked, required, disabled, form, style: { transform: "translateX(-100%)" } }) ] }); }); Switch$1.displayName = SWITCH_NAME; var THUMB_NAME = "SwitchThumb"; var SwitchThumb = React.forwardRef((props, forwardedRef)=>{ const { __scopeSwitch, ...thumbProps } = props; const context = useSwitchContext(THUMB_NAME, __scopeSwitch); return /* @__PURE__ */ jsx(Primitive.span, { "data-state": getState(context.checked), "data-disabled": context.disabled ? "" : void 0, ...thumbProps, ref: forwardedRef }); }); SwitchThumb.displayName = THUMB_NAME; var BUBBLE_INPUT_NAME = "SwitchBubbleInput"; var SwitchBubbleInput = React.forwardRef(({ __scopeSwitch, control, checked, bubbles = true, ...props }, forwardedRef)=>{ const ref = React.useRef(null); const composedRefs = useComposedRefs(ref, forwardedRef); const prevChecked = usePrevious(checked); const controlSize = useSize(control); React.useEffect(()=>{ const input = ref.current; if (!input) return; const inputProto = window.HTMLInputElement.prototype; const descriptor = Object.getOwnPropertyDescriptor(inputProto, "checked"); const setChecked = descriptor.set; if (prevChecked !== checked && setChecked) { const event = new Event("click", { bubbles }); setChecked.call(input, checked); input.dispatchEvent(event); } }, [ prevChecked, checked, bubbles ]); return /* @__PURE__ */ jsx("input", { type: "checkbox", "aria-hidden": true, defaultChecked: checked, ...props, tabIndex: -1, ref: composedRefs, style: { ...props.style, ...controlSize, position: "absolute", pointerEvents: "none", opacity: 0, margin: 0 } }); }); SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME; function getState(checked) { return checked ? "checked" : "unchecked"; } var Root = Switch$1; var Thumb = SwitchThumb; function Switch({ className, ...props }) { return /*#__PURE__*/ React.createElement(Root, { "data-slot": "switch", className: cn("peer data-[state=checked]:bg-primary data-[state=unchecked]:bg-input focus-visible:border-ring focus-visible:ring-ring/50 dark:data-[state=unchecked]:bg-input/80 inline-flex h-[1.15rem] w-8 shrink-0 items-center rounded-full border border-transparent shadow-xs transition-all outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50", className), ...props }, /*#__PURE__*/ React.createElement(Thumb, { "data-slot": "switch-thumb", className: cn("bg-background dark:data-[state=unchecked]:bg-foreground dark:data-[state=checked]:bg-primary-foreground pointer-events-none block size-4 rounded-full ring-0 transition-transform data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:translate-x-0") })); } export { Switch }; //# sourceMappingURL=index.mjs.map