import { i as __toESM, r as __exportAll, t as require_react } from "./react-DMmEJb6B.js"; import { t as require_jsx_runtime } from "./react_jsx-runtime.js"; import { C as getWindow, l as mergeProps, r as EMPTY_OBJECT, s as useMergedRefs, t as useRenderElement, u as useIsoLayoutEffect } from "./useRenderElement-3Cwvu1gO.js"; import { t as useButton } from "./useButton-CBy-cFDk.js"; import { a as fieldValidityMapping, i as useFieldRootContext, n as useLabelableId, r as useLabelableContext, t as useRegisterFieldControl } from "./useRegisterFieldControl-CPlvTQh-.js"; import { t as useFormContext } from "./FormContext-D8skpAva.js"; import { _ as useBaseUiId, l as none, t as createChangeEventDetails } from "./createBaseUIEventDetails-CJxlTiBH.js"; import { t as useControlled } from "./useControlled-B7gU8wdF.js"; import { n as visuallyHidden, r as visuallyHiddenInput, t as useValueChanged } from "./useValueChanged-C6oHa1CC.js"; //#region ../node_modules/@base-ui/react/esm/internals/labelable-provider/useAriaLabelledBy.js var import_react = /* @__PURE__ */ __toESM(require_react(), 1); /** * @internal */ function useAriaLabelledBy(explicitAriaLabelledBy, labelId, labelSourceRef, enableFallback = true, labelSourceId) { const [fallbackAriaLabelledBy, setFallbackAriaLabelledBy] = import_react.useState(); const generatedLabelId = useBaseUiId(labelSourceId ? `${labelSourceId}-label` : void 0); const ariaLabelledBy = explicitAriaLabelledBy ?? labelId ?? fallbackAriaLabelledBy; useIsoLayoutEffect(() => { const nextAriaLabelledBy = explicitAriaLabelledBy || labelId || !enableFallback ? void 0 : getAriaLabelledBy(labelSourceRef.current, generatedLabelId); if (fallbackAriaLabelledBy !== nextAriaLabelledBy) setFallbackAriaLabelledBy(nextAriaLabelledBy); }); return ariaLabelledBy; } function getAriaLabelledBy(labelSource, generatedLabelId) { const label = findAssociatedLabel(labelSource); if (!label) return; if (!label.id && generatedLabelId) label.id = generatedLabelId; return label.id || void 0; } function findAssociatedLabel(labelSource) { if (!labelSource) return; const parent = labelSource.parentElement; if (parent && parent.tagName === "LABEL") return parent; const controlId = labelSource.id; if (controlId) { const nextSibling = labelSource.nextElementSibling; if (nextSibling && nextSibling.htmlFor === controlId) return nextSibling; } const labels = labelSource.labels; return labels && labels[0]; } //#endregion //#region ../node_modules/@base-ui/react/esm/switch/root/SwitchRootContext.js var SwitchRootContext = /* @__PURE__ */ import_react.createContext(void 0); SwitchRootContext.displayName = "SwitchRootContext"; function useSwitchRootContext() { const context = import_react.useContext(SwitchRootContext); if (context === void 0) throw new Error("Base UI: SwitchRootContext is missing. Switch parts must be placed within ."); return context; } //#endregion //#region ../node_modules/@base-ui/react/esm/switch/root/SwitchRootDataAttributes.js var SwitchRootDataAttributes = /* @__PURE__ */ function(SwitchRootDataAttributes) { /** * Present when the switch is checked. */ SwitchRootDataAttributes["checked"] = "data-checked"; /** * Present when the switch is not checked. */ SwitchRootDataAttributes["unchecked"] = "data-unchecked"; /** * Present when the switch is disabled. */ SwitchRootDataAttributes["disabled"] = "data-disabled"; /** * Present when the switch is readonly. */ SwitchRootDataAttributes["readonly"] = "data-readonly"; /** * Present when the switch is required. */ SwitchRootDataAttributes["required"] = "data-required"; /** * Present when the switch is in a valid state (when wrapped in Field.Root). */ SwitchRootDataAttributes["valid"] = "data-valid"; /** * Present when the switch is in an invalid state (when wrapped in Field.Root). */ SwitchRootDataAttributes["invalid"] = "data-invalid"; /** * Present when the switch has been touched (when wrapped in Field.Root). */ SwitchRootDataAttributes["touched"] = "data-touched"; /** * Present when the switch's value has changed (when wrapped in Field.Root). */ SwitchRootDataAttributes["dirty"] = "data-dirty"; /** * Present when the switch is active (when wrapped in Field.Root). */ SwitchRootDataAttributes["filled"] = "data-filled"; /** * Present when the switch is focused (when wrapped in Field.Root). */ SwitchRootDataAttributes["focused"] = "data-focused"; return SwitchRootDataAttributes; }({}); //#endregion //#region ../node_modules/@base-ui/react/esm/switch/stateAttributesMapping.js var stateAttributesMapping = { ...fieldValidityMapping, checked(value) { if (value) return { [SwitchRootDataAttributes.checked]: "" }; return { [SwitchRootDataAttributes.unchecked]: "" }; } }; //#endregion //#region ../node_modules/@base-ui/react/esm/switch/root/SwitchRoot.js var import_jsx_runtime = require_jsx_runtime(); var SwitchRoot = /* @__PURE__ */ import_react.forwardRef(function SwitchRoot(componentProps, forwardedRef) { const { checked: checkedProp, className, defaultChecked, "aria-labelledby": ariaLabelledByProp, form, id: idProp, inputRef: externalInputRef, name: nameProp, nativeButton = false, onCheckedChange, readOnly = false, required = false, disabled: disabledProp = false, render, uncheckedValue, value, style, ...elementProps } = componentProps; const { clearErrors } = useFormContext(); const { state: fieldState, setTouched, setDirty, validityData, setFilled, setFocused, shouldValidateOnChange, validationMode, disabled: fieldDisabled, name: fieldName, validation } = useFieldRootContext(); const { labelId } = useLabelableContext(); const disabled = fieldDisabled || disabledProp; const name = fieldName ?? nameProp; const inputRef = import_react.useRef(null); const handleInputRef = useMergedRefs(inputRef, externalInputRef, validation.inputRef); const switchRef = import_react.useRef(null); const id = useBaseUiId(); const controlId = useLabelableId({ id: idProp, implicit: false, controlRef: switchRef }); const hiddenInputId = nativeButton ? void 0 : controlId; const [checked, setCheckedState] = useControlled({ controlled: checkedProp, default: Boolean(defaultChecked), name: "Switch", state: "checked" }); useRegisterFieldControl(switchRef, id, checked); useIsoLayoutEffect(() => { if (inputRef.current) setFilled(inputRef.current.checked); }, [inputRef, setFilled]); useValueChanged(checked, () => { clearErrors(name); setDirty(checked !== validityData.initialValue); setFilled(checked); if (shouldValidateOnChange()) validation.commit(checked); else validation.commit(checked, true); }); const { getButtonProps, buttonRef } = useButton({ disabled, native: nativeButton }); const ariaLabelledBy = useAriaLabelledBy(ariaLabelledByProp, labelId, inputRef, !nativeButton, hiddenInputId); const rootProps = { id: nativeButton ? controlId : id, role: "switch", "aria-checked": checked, "aria-readonly": readOnly || void 0, "aria-required": required || void 0, "aria-labelledby": ariaLabelledBy, onFocus() { if (!disabled) setFocused(true); }, onBlur() { const element = inputRef.current; if (!element || disabled) return; setTouched(true); setFocused(false); if (validationMode === "onBlur") validation.commit(element.checked); }, onClick(event) { if (readOnly || disabled) return; event.preventDefault(); const input = inputRef.current; if (!input) return; input.dispatchEvent(new (getWindow(input)).PointerEvent("click", { bubbles: true, shiftKey: event.shiftKey, ctrlKey: event.ctrlKey, altKey: event.altKey, metaKey: event.metaKey })); } }; const inputProps = mergeProps({ checked, disabled, form, id: hiddenInputId, name, required, style: name ? visuallyHiddenInput : visuallyHidden, tabIndex: -1, type: "checkbox", "aria-hidden": true, ref: handleInputRef, onChange(event) { if (event.nativeEvent.defaultPrevented) return; if (readOnly) { event.preventDefault(); return; } const nextChecked = event.currentTarget.checked; const eventDetails = createChangeEventDetails(none, event.nativeEvent); onCheckedChange?.(nextChecked, eventDetails); if (eventDetails.isCanceled) return; setCheckedState(nextChecked); }, onFocus() { switchRef.current?.focus(); } }, validation.getInputValidationProps, value !== void 0 ? { value } : EMPTY_OBJECT); const state = import_react.useMemo(() => ({ ...fieldState, checked, disabled, readOnly, required }), [ fieldState, checked, disabled, readOnly, required ]); const element = useRenderElement("span", componentProps, { state, ref: [ forwardedRef, switchRef, buttonRef ], props: [ rootProps, validation.getValidationProps, elementProps, getButtonProps ], stateAttributesMapping }); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SwitchRootContext.Provider, { value: state, children: [ element, !checked && name && uncheckedValue !== void 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { type: "hidden", form, name, value: uncheckedValue }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { ...inputProps, suppressHydrationWarning: true }) ] }); }); SwitchRoot.displayName = "SwitchRoot"; //#endregion //#region ../node_modules/@base-ui/react/esm/switch/thumb/SwitchThumb.js /** * The movable part of the switch that indicates whether the switch is on or off. * Renders a ``. * * Documentation: [Base UI Switch](https://base-ui.com/react/components/switch) */ var SwitchThumb = /* @__PURE__ */ import_react.forwardRef(function SwitchThumb(componentProps, forwardedRef) { const { render, className, style, ...elementProps } = componentProps; return useRenderElement("span", componentProps, { state: useSwitchRootContext(), ref: forwardedRef, stateAttributesMapping, props: elementProps }); }); SwitchThumb.displayName = "SwitchThumb"; //#endregion //#region ../node_modules/@base-ui/react/esm/switch/index.parts.js var index_parts_exports = /* @__PURE__ */ __exportAll({ Root: () => SwitchRoot, Thumb: () => SwitchThumb }); //#endregion export { index_parts_exports as Switch }; //# sourceMappingURL=@base-ui_react_switch.js.map