UNPKG

@pdf-viewer/react

Version:

A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.

266 lines (265 loc) 6.69 kB
import { jsx as i, jsxs as w, Fragment as U } from "react/jsx-runtime"; import * as s from "react"; import { c as z, u as B, P as E, a as S, b as G, d as K, e as X } from "../../index-35c7f4a2.js"; import { CheckIcon as J } from "../icons/CheckIcon.js"; import "react-dom"; function Q(e) { const r = s.useRef({ value: e, previous: e }); return s.useMemo(() => (r.current.value !== e && (r.current.previous = r.current.value, r.current.value = e), r.current.previous), [e]); } var g = "Checkbox", [V, oe] = z(g), [W, P] = V(g); function Y(e) { const { __scopeCheckbox: r, checked: o, children: d, defaultChecked: n, disabled: t, form: h, name: f, onCheckedChange: u, required: k, value: C = "on", // @ts-expect-error internal_do_not_use_render: l } = e, [p, v] = X({ prop: o, defaultProp: n ?? !1, onChange: u, caller: g }), [m, x] = s.useState(null), [_, c] = s.useState(null), a = s.useRef(!1), I = m ? !!h || !!m.closest("form") : ( // We set this to true by default so that events bubble to forms without JS (SSR) !0 ), R = { checked: p, disabled: t, setChecked: v, control: m, setControl: x, name: f, form: h, value: C, hasConsumerStoppedPropagationRef: a, required: k, defaultChecked: b(n) ? !1 : n, isFormControl: I, bubbleInput: _, setBubbleInput: c }; return /* @__PURE__ */ i( W, { scope: r, ...R, children: Z(l) ? l(R) : d } ); } var M = "CheckboxTrigger", T = s.forwardRef( ({ __scopeCheckbox: e, onKeyDown: r, onClick: o, ...d }, n) => { const { control: t, value: h, disabled: f, checked: u, required: k, setControl: C, setChecked: l, hasConsumerStoppedPropagationRef: p, isFormControl: v, bubbleInput: m } = P(M, e), x = B(n, C), _ = s.useRef(u); return s.useEffect(() => { const c = t == null ? void 0 : t.form; if (c) { const a = () => l(_.current); return c.addEventListener("reset", a), () => c.removeEventListener("reset", a); } }, [t, l]), /* @__PURE__ */ i( E.button, { type: "button", role: "checkbox", "aria-checked": b(u) ? "mixed" : u, "aria-required": k, "data-state": O(u), "data-disabled": f ? "" : void 0, disabled: f, value: h, ...d, ref: x, onKeyDown: S(r, (c) => { c.key === "Enter" && c.preventDefault(); }), onClick: S(o, (c) => { l((a) => b(a) ? !0 : !a), m && v && (p.current = c.isPropagationStopped(), p.current || c.stopPropagation()); }) } ); } ); T.displayName = M; var q = s.forwardRef( (e, r) => { const { __scopeCheckbox: o, name: d, checked: n, defaultChecked: t, required: h, disabled: f, value: u, onCheckedChange: k, form: C, ...l } = e; return /* @__PURE__ */ i( Y, { __scopeCheckbox: o, checked: n, defaultChecked: t, disabled: f, required: h, onCheckedChange: k, name: d, form: C, value: u, internal_do_not_use_render: ({ isFormControl: p }) => /* @__PURE__ */ w(U, { children: [ /* @__PURE__ */ i( T, { ...l, ref: r, __scopeCheckbox: o } ), p && /* @__PURE__ */ i( L, { __scopeCheckbox: o } ) ] }) } ); } ); q.displayName = g; var A = "CheckboxIndicator", F = s.forwardRef( (e, r) => { const { __scopeCheckbox: o, forceMount: d, ...n } = e, t = P(A, o); return /* @__PURE__ */ i( G, { present: d || b(t.checked) || t.checked === !0, children: /* @__PURE__ */ i( E.span, { "data-state": O(t.checked), "data-disabled": t.disabled ? "" : void 0, ...n, ref: r, style: { pointerEvents: "none", ...e.style } } ) } ); } ); F.displayName = A; var D = "CheckboxBubbleInput", L = s.forwardRef( ({ __scopeCheckbox: e, ...r }, o) => { const { control: d, hasConsumerStoppedPropagationRef: n, checked: t, defaultChecked: h, required: f, disabled: u, name: k, value: C, form: l, bubbleInput: p, setBubbleInput: v } = P(D, e), m = B(o, v), x = Q(t), _ = K(d); s.useEffect(() => { const a = p; if (!a) return; const I = window.HTMLInputElement.prototype, N = Object.getOwnPropertyDescriptor( I, "checked" ).set, j = !n.current; if (x !== t && N) { const H = new Event("click", { bubbles: j }); a.indeterminate = b(t), N.call(a, b(t) ? !1 : t), a.dispatchEvent(H); } }, [p, x, t, n]); const c = s.useRef(b(t) ? !1 : t); return /* @__PURE__ */ i( E.input, { type: "checkbox", "aria-hidden": !0, defaultChecked: h ?? c.current, required: f, disabled: u, name: k, value: C, form: l, ...r, tabIndex: -1, ref: m, style: { ...r.style, ..._, position: "absolute", pointerEvents: "none", opacity: 0, margin: 0, // We transform because the input is absolutely positioned but we have // rendered it **after** the button. This pulls it back to sit on top // of the button. transform: "translateX(-100%)" } } ); } ); L.displayName = D; function Z(e) { return typeof e == "function"; } function b(e) { return e === "indeterminate"; } function O(e) { return b(e) ? "indeterminate" : e ? "checked" : "unchecked"; } const y = { "rp-checkbox": "_rp-checkbox_1vwg8_1", "rp-checkbox-indicator": "_rp-checkbox-indicator_1vwg8_25" }, ne = ({ children: e, value: r, name: o, onChange: d, tabIndex: n }) => /* @__PURE__ */ w("div", { style: { display: "flex", alignItems: "center", position: "relative" }, children: [ /* @__PURE__ */ i( q, { tabIndex: n, className: y["rp-checkbox"], checked: r, id: o, onCheckedChange: d, children: /* @__PURE__ */ i(F, { className: y["rp-checkbox-indicator"], children: /* @__PURE__ */ i(J, {}) }) } ), /* @__PURE__ */ i("label", { className: y["rp-checkbox-label"], htmlFor: o, children: e }) ] }); export { ne as UICheckbox };