ivt
Version:
Ivt Components Library
220 lines (216 loc) • 9.16 kB
JavaScript
import * as React from 'react';
import React__default from 'react';
import { u as useComposedRefs } from './index-1tQVI0Jh.mjs';
import { c as createContextScope } from './index-DT8WgpCS.mjs';
import { c as composeEventHandlers } from './index-Bl-WJHvp.mjs';
import { u as useControllableState } from './index-D4FMFHi9.mjs';
import { u as usePrevious } from './index-FL3PKmOS.mjs';
import { u as useSize } from './index-DmY774z-.mjs';
import { P as Presence } from './index-C6s8KI_8.mjs';
import { P as Primitive } from './index-DgKlJYZP.mjs';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { c as cn } from './utils-05LlW3Cl.mjs';
import { C as Check } from './check-BBGTedl-.mjs';
var CHECKBOX_NAME = "Checkbox";
var [createCheckboxContext] = createContextScope(CHECKBOX_NAME);
var [CheckboxProviderImpl, useCheckboxContext] = createCheckboxContext(CHECKBOX_NAME);
function CheckboxProvider(props) {
const { __scopeCheckbox, checked: checkedProp, children, defaultChecked, disabled, form, name, onCheckedChange, required, value = "on", // @ts-expect-error
internal_do_not_use_render } = props;
const [checked, setChecked] = useControllableState({
prop: checkedProp,
defaultProp: defaultChecked ?? false,
onChange: onCheckedChange,
caller: CHECKBOX_NAME
});
const [control, setControl] = React.useState(null);
const [bubbleInput, setBubbleInput] = React.useState(null);
const hasConsumerStoppedPropagationRef = React.useRef(false);
const isFormControl = control ? !!form || !!control.closest("form") : // We set this to true by default so that events bubble to forms without JS (SSR)
true;
const context = {
checked,
disabled,
setChecked,
control,
setControl,
name,
form,
value,
hasConsumerStoppedPropagationRef,
required,
defaultChecked: isIndeterminate(defaultChecked) ? false : defaultChecked,
isFormControl,
bubbleInput,
setBubbleInput
};
return /* @__PURE__ */ jsx(CheckboxProviderImpl, {
scope: __scopeCheckbox,
...context,
children: isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children
});
}
var TRIGGER_NAME = "CheckboxTrigger";
var CheckboxTrigger = React.forwardRef(({ __scopeCheckbox, onKeyDown, onClick, ...checkboxProps }, forwardedRef)=>{
const { control, value, disabled, checked, required, setControl, setChecked, hasConsumerStoppedPropagationRef, isFormControl, bubbleInput } = useCheckboxContext(TRIGGER_NAME, __scopeCheckbox);
const composedRefs = useComposedRefs(forwardedRef, setControl);
const initialCheckedStateRef = React.useRef(checked);
React.useEffect(()=>{
const form = control?.form;
if (form) {
const reset = ()=>setChecked(initialCheckedStateRef.current);
form.addEventListener("reset", reset);
return ()=>form.removeEventListener("reset", reset);
}
}, [
control,
setChecked
]);
return /* @__PURE__ */ jsx(Primitive.button, {
type: "button",
role: "checkbox",
"aria-checked": isIndeterminate(checked) ? "mixed" : checked,
"aria-required": required,
"data-state": getState(checked),
"data-disabled": disabled ? "" : void 0,
disabled,
value,
...checkboxProps,
ref: composedRefs,
onKeyDown: composeEventHandlers(onKeyDown, (event)=>{
if (event.key === "Enter") event.preventDefault();
}),
onClick: composeEventHandlers(onClick, (event)=>{
setChecked((prevChecked)=>isIndeterminate(prevChecked) ? true : !prevChecked);
if (bubbleInput && isFormControl) {
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
}
})
});
});
CheckboxTrigger.displayName = TRIGGER_NAME;
var Checkbox$1 = React.forwardRef((props, forwardedRef)=>{
const { __scopeCheckbox, name, checked, defaultChecked, required, disabled, value, onCheckedChange, form, ...checkboxProps } = props;
return /* @__PURE__ */ jsx(CheckboxProvider, {
__scopeCheckbox,
checked,
defaultChecked,
disabled,
required,
onCheckedChange,
name,
form,
value,
internal_do_not_use_render: ({ isFormControl })=>/* @__PURE__ */ jsxs(Fragment, {
children: [
/* @__PURE__ */ jsx(CheckboxTrigger, {
...checkboxProps,
ref: forwardedRef,
__scopeCheckbox
}),
isFormControl && /* @__PURE__ */ jsx(CheckboxBubbleInput, {
__scopeCheckbox
})
]
})
});
});
Checkbox$1.displayName = CHECKBOX_NAME;
var INDICATOR_NAME = "CheckboxIndicator";
var CheckboxIndicator = React.forwardRef((props, forwardedRef)=>{
const { __scopeCheckbox, forceMount, ...indicatorProps } = props;
const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);
return /* @__PURE__ */ jsx(Presence, {
present: forceMount || isIndeterminate(context.checked) || context.checked === true,
children: /* @__PURE__ */ jsx(Primitive.span, {
"data-state": getState(context.checked),
"data-disabled": context.disabled ? "" : void 0,
...indicatorProps,
ref: forwardedRef,
style: {
pointerEvents: "none",
...props.style
}
})
});
});
CheckboxIndicator.displayName = INDICATOR_NAME;
var BUBBLE_INPUT_NAME = "CheckboxBubbleInput";
var CheckboxBubbleInput = React.forwardRef(({ __scopeCheckbox, ...props }, forwardedRef)=>{
const { control, hasConsumerStoppedPropagationRef, checked, defaultChecked, required, disabled, name, value, form, bubbleInput, setBubbleInput } = useCheckboxContext(BUBBLE_INPUT_NAME, __scopeCheckbox);
const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);
const prevChecked = usePrevious(checked);
const controlSize = useSize(control);
React.useEffect(()=>{
const input = bubbleInput;
if (!input) return;
const inputProto = window.HTMLInputElement.prototype;
const descriptor = Object.getOwnPropertyDescriptor(inputProto, "checked");
const setChecked = descriptor.set;
const bubbles = !hasConsumerStoppedPropagationRef.current;
if (prevChecked !== checked && setChecked) {
const event = new Event("click", {
bubbles
});
input.indeterminate = isIndeterminate(checked);
setChecked.call(input, isIndeterminate(checked) ? false : checked);
input.dispatchEvent(event);
}
}, [
bubbleInput,
prevChecked,
checked,
hasConsumerStoppedPropagationRef
]);
const defaultCheckedRef = React.useRef(isIndeterminate(checked) ? false : checked);
return /* @__PURE__ */ jsx(Primitive.input, {
type: "checkbox",
"aria-hidden": true,
defaultChecked: defaultChecked ?? defaultCheckedRef.current,
required,
disabled,
name,
value,
form,
...props,
tabIndex: -1,
ref: composedRefs,
style: {
...props.style,
...controlSize,
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%)"
}
});
});
CheckboxBubbleInput.displayName = BUBBLE_INPUT_NAME;
function isFunction(value) {
return typeof value === "function";
}
function isIndeterminate(checked) {
return checked === "indeterminate";
}
function getState(checked) {
return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
}
function Checkbox({ className, ...props }) {
return /*#__PURE__*/ React__default.createElement(Checkbox$1, {
"data-slot": "checkbox",
className: cn("peer border-primary focus-visible:ring-ring data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground h-4 w-4 shrink-0 rounded-sm border shadow focus-visible:ring-1 focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50", className),
...props
}, /*#__PURE__*/ React__default.createElement(CheckboxIndicator, {
"data-slot": "checkbox-indicator",
className: cn("flex items-center justify-center text-current")
}, props.children ? props.children : /*#__PURE__*/ React__default.createElement(Check, {
className: "h-4 w-4"
})));
}
export { Checkbox as C };
//# sourceMappingURL=checkbox-D9LLByPZ.mjs.map