176 lines
4.9 KiB
JavaScript
176 lines
4.9 KiB
JavaScript
"use client";
|
|
import {
|
|
useSize
|
|
} from "./chunk-KFN67LD4.js";
|
|
import {
|
|
Primitive,
|
|
composeEventHandlers,
|
|
createContextScope,
|
|
useControllableState
|
|
} from "./chunk-77KTLLK5.js";
|
|
import "./chunk-KDCVS43I.js";
|
|
import {
|
|
useComposedRefs
|
|
} from "./chunk-YVE7F5OV.js";
|
|
import {
|
|
require_jsx_runtime
|
|
} from "./chunk-S725DACQ.js";
|
|
import {
|
|
require_react
|
|
} from "./chunk-RLJ2RCJQ.js";
|
|
import {
|
|
__toESM
|
|
} from "./chunk-DC5AMYBS.js";
|
|
|
|
// node_modules/@radix-ui/react-switch/dist/index.mjs
|
|
var React2 = __toESM(require_react(), 1);
|
|
|
|
// node_modules/@radix-ui/react-use-previous/dist/index.mjs
|
|
var React = __toESM(require_react(), 1);
|
|
function usePrevious(value) {
|
|
const ref = React.useRef({ value, previous: value });
|
|
return React.useMemo(() => {
|
|
if (ref.current.value !== value) {
|
|
ref.current.previous = ref.current.value;
|
|
ref.current.value = value;
|
|
}
|
|
return ref.current.previous;
|
|
}, [value]);
|
|
}
|
|
|
|
// node_modules/@radix-ui/react-switch/dist/index.mjs
|
|
var import_jsx_runtime = __toESM(require_jsx_runtime(), 1);
|
|
var SWITCH_NAME = "Switch";
|
|
var [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);
|
|
var [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
var Switch = React2.forwardRef(
|
|
(props, forwardedRef) => {
|
|
const {
|
|
__scopeSwitch,
|
|
name,
|
|
checked: checkedProp,
|
|
defaultChecked,
|
|
required,
|
|
disabled,
|
|
value = "on",
|
|
onCheckedChange,
|
|
form,
|
|
...switchProps
|
|
} = props;
|
|
const [button, setButton] = React2.useState(null);
|
|
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
const hasConsumerStoppedPropagationRef = React2.useRef(false);
|
|
const isFormControl = button ? form || !!button.closest("form") : true;
|
|
const [checked = false, setChecked] = useControllableState({
|
|
prop: checkedProp,
|
|
defaultProp: defaultChecked,
|
|
onChange: onCheckedChange
|
|
});
|
|
return (0, import_jsx_runtime.jsxs)(SwitchProvider, { scope: __scopeSwitch, checked, disabled, children: [
|
|
(0, import_jsx_runtime.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 && (0, import_jsx_runtime.jsx)(
|
|
BubbleInput,
|
|
{
|
|
control: button,
|
|
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
name,
|
|
value,
|
|
checked,
|
|
required,
|
|
disabled,
|
|
form,
|
|
style: { transform: "translateX(-100%)" }
|
|
}
|
|
)
|
|
] });
|
|
}
|
|
);
|
|
Switch.displayName = SWITCH_NAME;
|
|
var THUMB_NAME = "SwitchThumb";
|
|
var SwitchThumb = React2.forwardRef(
|
|
(props, forwardedRef) => {
|
|
const { __scopeSwitch, ...thumbProps } = props;
|
|
const context = useSwitchContext(THUMB_NAME, __scopeSwitch);
|
|
return (0, import_jsx_runtime.jsx)(
|
|
Primitive.span,
|
|
{
|
|
"data-state": getState(context.checked),
|
|
"data-disabled": context.disabled ? "" : void 0,
|
|
...thumbProps,
|
|
ref: forwardedRef
|
|
}
|
|
);
|
|
}
|
|
);
|
|
SwitchThumb.displayName = THUMB_NAME;
|
|
var BubbleInput = (props) => {
|
|
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
const ref = React2.useRef(null);
|
|
const prevChecked = usePrevious(checked);
|
|
const controlSize = useSize(control);
|
|
React2.useEffect(() => {
|
|
const input = ref.current;
|
|
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 (0, import_jsx_runtime.jsx)(
|
|
"input",
|
|
{
|
|
type: "checkbox",
|
|
"aria-hidden": true,
|
|
defaultChecked: checked,
|
|
...inputProps,
|
|
tabIndex: -1,
|
|
ref,
|
|
style: {
|
|
...props.style,
|
|
...controlSize,
|
|
position: "absolute",
|
|
pointerEvents: "none",
|
|
opacity: 0,
|
|
margin: 0
|
|
}
|
|
}
|
|
);
|
|
};
|
|
function getState(checked) {
|
|
return checked ? "checked" : "unchecked";
|
|
}
|
|
var Root = Switch;
|
|
var Thumb = SwitchThumb;
|
|
export {
|
|
Root,
|
|
Switch,
|
|
SwitchThumb,
|
|
Thumb,
|
|
createSwitchScope
|
|
};
|
|
//# sourceMappingURL=@radix-ui_react-switch.js.map
|