@anoki/fse-marche-ui
Version:
FSE UI components library
120 lines (119 loc) • 3.15 kB
JavaScript
import * as s from "react";
import { composeEventHandlers as T } from "./index.es235.js";
import { useComposedRefs as _ } from "./index.es236.js";
import { createContextScope as H } from "./index.es237.js";
import { useControllableState as I } from "./index.es238.js";
import { usePrevious as M } from "./index.es245.js";
import { useSize as N } from "./index.es246.js";
import { Primitive as w } from "./index.es239.js";
import { j as p } from "./index.es137.js";
var S = "Switch", [B, J] = H(S), [q, z] = B(S), C = s.forwardRef(
(e, n) => {
const {
__scopeSwitch: t,
name: o,
checked: r,
defaultChecked: u,
required: a,
disabled: c,
value: i = "on",
onCheckedChange: f,
form: b,
...l
} = e, [d, R] = s.useState(null), E = _(n, (m) => R(m)), v = s.useRef(!1), k = d ? b || !!d.closest("form") : !0, [h = !1, g] = I({
prop: r,
defaultProp: u,
onChange: f
});
return /* @__PURE__ */ p.jsxs(q, { scope: t, checked: h, disabled: c, children: [
/* @__PURE__ */ p.jsx(
w.button,
{
type: "button",
role: "switch",
"aria-checked": h,
"aria-required": a,
"data-state": y(h),
"data-disabled": c ? "" : void 0,
disabled: c,
value: i,
...l,
ref: E,
onClick: T(e.onClick, (m) => {
g((j) => !j), k && (v.current = m.isPropagationStopped(), v.current || m.stopPropagation());
})
}
),
k && /* @__PURE__ */ p.jsx(
A,
{
control: d,
bubbles: !v.current,
name: o,
value: i,
checked: h,
required: a,
disabled: c,
form: b,
style: { transform: "translateX(-100%)" }
}
)
] });
}
);
C.displayName = S;
var x = "SwitchThumb", P = s.forwardRef(
(e, n) => {
const { __scopeSwitch: t, ...o } = e, r = z(x, t);
return /* @__PURE__ */ p.jsx(
w.span,
{
"data-state": y(r.checked),
"data-disabled": r.disabled ? "" : void 0,
...o,
ref: n
}
);
}
);
P.displayName = x;
var A = (e) => {
const { control: n, checked: t, bubbles: o = !0, ...r } = e, u = s.useRef(null), a = M(t), c = N(n);
return s.useEffect(() => {
const i = u.current, f = window.HTMLInputElement.prototype, l = Object.getOwnPropertyDescriptor(f, "checked").set;
if (a !== t && l) {
const d = new Event("click", { bubbles: o });
l.call(i, t), i.dispatchEvent(d);
}
}, [a, t, o]), /* @__PURE__ */ p.jsx(
"input",
{
type: "checkbox",
"aria-hidden": !0,
defaultChecked: t,
...r,
tabIndex: -1,
ref: u,
style: {
...e.style,
...c,
position: "absolute",
pointerEvents: "none",
opacity: 0,
margin: 0
}
}
);
};
function y(e) {
return e ? "checked" : "unchecked";
}
var K = C, Q = P;
export {
K as Root,
C as Switch,
P as SwitchThumb,
Q as Thumb,
J as createSwitchScope
};
//# sourceMappingURL=index.es208.js.map