ivt
Version:
Ivt Components Library
269 lines (265 loc) • 10.8 kB
JavaScript
import * as React from 'react';
import { c as composeEventHandlers } from '../chunks/index-Bl-WJHvp.mjs';
import { u as useComposedRefs } from '../chunks/index-1tQVI0Jh.mjs';
import { c as createContextScope } from '../chunks/index-DT8WgpCS.mjs';
import { P as Primitive } from '../chunks/index-DgKlJYZP.mjs';
import { c as createRovingFocusGroupScope, R as Root, I as Item } from '../chunks/index-BsG_UW8k.mjs';
import { u as useControllableState } from '../chunks/index-D4FMFHi9.mjs';
import { u as useDirection } from '../chunks/index-CGLjQEjG.mjs';
import { u as useSize } from '../chunks/index-DmY774z-.mjs';
import { u as usePrevious } from '../chunks/index-FL3PKmOS.mjs';
import { P as Presence } from '../chunks/index-C6s8KI_8.mjs';
import { jsx, jsxs } from 'react/jsx-runtime';
import { c as cn } from '../chunks/utils-05LlW3Cl.mjs';
import { C as Check } from '../chunks/check-BBGTedl-.mjs';
import 'react-dom';
import '@radix-ui/react-slot';
import '@radix-ui/react-collection';
import '../chunks/index-tkRL9Tft.mjs';
import '../chunks/index-DKOlG3mh.mjs';
import '../chunks/index-Cbm3--wc.mjs';
import '../chunks/bundle-mjs-BYcyWisL.mjs';
import '../chunks/createLucideIcon-DLrNgMqk.mjs';
var RADIO_NAME = "Radio";
var [createRadioContext, createRadioScope] = createContextScope(RADIO_NAME);
var [RadioProvider, useRadioContext] = createRadioContext(RADIO_NAME);
var Radio = React.forwardRef((props, forwardedRef)=>{
const { __scopeRadio, name, checked = false, required, disabled, value = "on", onCheck, form, ...radioProps } = props;
const [button, setButton] = React.useState(null);
const composedRefs = useComposedRefs(forwardedRef, (node)=>setButton(node));
const hasConsumerStoppedPropagationRef = React.useRef(false);
const isFormControl = button ? form || !!button.closest("form") : true;
return /* @__PURE__ */ jsxs(RadioProvider, {
scope: __scopeRadio,
checked,
disabled,
children: [
/* @__PURE__ */ jsx(Primitive.button, {
type: "button",
role: "radio",
"aria-checked": checked,
"data-state": getState(checked),
"data-disabled": disabled ? "" : void 0,
disabled,
value,
...radioProps,
ref: composedRefs,
onClick: composeEventHandlers(props.onClick, (event)=>{
if (!checked) onCheck?.();
if (isFormControl) {
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
}
})
}),
isFormControl && /* @__PURE__ */ jsx(RadioBubbleInput, {
control: button,
bubbles: !hasConsumerStoppedPropagationRef.current,
name,
value,
checked,
required,
disabled,
form,
style: {
transform: "translateX(-100%)"
}
})
]
});
});
Radio.displayName = RADIO_NAME;
var INDICATOR_NAME = "RadioIndicator";
var RadioIndicator = React.forwardRef((props, forwardedRef)=>{
const { __scopeRadio, forceMount, ...indicatorProps } = props;
const context = useRadioContext(INDICATOR_NAME, __scopeRadio);
return /* @__PURE__ */ jsx(Presence, {
present: forceMount || context.checked,
children: /* @__PURE__ */ jsx(Primitive.span, {
"data-state": getState(context.checked),
"data-disabled": context.disabled ? "" : void 0,
...indicatorProps,
ref: forwardedRef
})
});
});
RadioIndicator.displayName = INDICATOR_NAME;
var BUBBLE_INPUT_NAME = "RadioBubbleInput";
var RadioBubbleInput = React.forwardRef(({ __scopeRadio, control, checked, bubbles = true, ...props }, forwardedRef)=>{
const ref = React.useRef(null);
const composedRefs = useComposedRefs(ref, forwardedRef);
const prevChecked = usePrevious(checked);
const controlSize = useSize(control);
React.useEffect(()=>{
const input = ref.current;
if (!input) return;
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 /* @__PURE__ */ jsx(Primitive.input, {
type: "radio",
"aria-hidden": true,
defaultChecked: checked,
...props,
tabIndex: -1,
ref: composedRefs,
style: {
...props.style,
...controlSize,
position: "absolute",
pointerEvents: "none",
opacity: 0,
margin: 0
}
});
});
RadioBubbleInput.displayName = BUBBLE_INPUT_NAME;
function getState(checked) {
return checked ? "checked" : "unchecked";
}
var ARROW_KEYS = [
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight"
];
var RADIO_GROUP_NAME = "RadioGroup";
var [createRadioGroupContext] = createContextScope(RADIO_GROUP_NAME, [
createRovingFocusGroupScope,
createRadioScope
]);
var useRovingFocusGroupScope = createRovingFocusGroupScope();
var useRadioScope = createRadioScope();
var [RadioGroupProvider, useRadioGroupContext] = createRadioGroupContext(RADIO_GROUP_NAME);
var RadioGroup$1 = React.forwardRef((props, forwardedRef)=>{
const { __scopeRadioGroup, name, defaultValue, value: valueProp, required = false, disabled = false, orientation, dir, loop = true, onValueChange, ...groupProps } = props;
const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeRadioGroup);
const direction = useDirection(dir);
const [value, setValue] = useControllableState({
prop: valueProp,
defaultProp: defaultValue ?? null,
onChange: onValueChange,
caller: RADIO_GROUP_NAME
});
return /* @__PURE__ */ jsx(RadioGroupProvider, {
scope: __scopeRadioGroup,
name,
required,
disabled,
value,
onValueChange: setValue,
children: /* @__PURE__ */ jsx(Root, {
asChild: true,
...rovingFocusGroupScope,
orientation,
dir: direction,
loop,
children: /* @__PURE__ */ jsx(Primitive.div, {
role: "radiogroup",
"aria-required": required,
"aria-orientation": orientation,
"data-disabled": disabled ? "" : void 0,
dir: direction,
...groupProps,
ref: forwardedRef
})
})
});
});
RadioGroup$1.displayName = RADIO_GROUP_NAME;
var ITEM_NAME = "RadioGroupItem";
var RadioGroupItem$1 = React.forwardRef((props, forwardedRef)=>{
const { __scopeRadioGroup, disabled, ...itemProps } = props;
const context = useRadioGroupContext(ITEM_NAME, __scopeRadioGroup);
const isDisabled = context.disabled || disabled;
const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeRadioGroup);
const radioScope = useRadioScope(__scopeRadioGroup);
const ref = React.useRef(null);
const composedRefs = useComposedRefs(forwardedRef, ref);
const checked = context.value === itemProps.value;
const isArrowKeyPressedRef = React.useRef(false);
React.useEffect(()=>{
const handleKeyDown = (event)=>{
if (ARROW_KEYS.includes(event.key)) {
isArrowKeyPressedRef.current = true;
}
};
const handleKeyUp = ()=>isArrowKeyPressedRef.current = false;
document.addEventListener("keydown", handleKeyDown);
document.addEventListener("keyup", handleKeyUp);
return ()=>{
document.removeEventListener("keydown", handleKeyDown);
document.removeEventListener("keyup", handleKeyUp);
};
}, []);
return /* @__PURE__ */ jsx(Item, {
asChild: true,
...rovingFocusGroupScope,
focusable: !isDisabled,
active: checked,
children: /* @__PURE__ */ jsx(Radio, {
disabled: isDisabled,
required: context.required,
checked,
...radioScope,
...itemProps,
name: context.name,
ref: composedRefs,
onCheck: ()=>context.onValueChange(itemProps.value),
onKeyDown: composeEventHandlers((event)=>{
if (event.key === "Enter") event.preventDefault();
}),
onFocus: composeEventHandlers(itemProps.onFocus, ()=>{
if (isArrowKeyPressedRef.current) ref.current?.click();
})
})
});
});
RadioGroupItem$1.displayName = ITEM_NAME;
var INDICATOR_NAME2 = "RadioGroupIndicator";
var RadioGroupIndicator = React.forwardRef((props, forwardedRef)=>{
const { __scopeRadioGroup, ...indicatorProps } = props;
const radioScope = useRadioScope(__scopeRadioGroup);
return /* @__PURE__ */ jsx(RadioIndicator, {
...radioScope,
...indicatorProps,
ref: forwardedRef
});
});
RadioGroupIndicator.displayName = INDICATOR_NAME2;
var Root2 = RadioGroup$1;
var Item2 = RadioGroupItem$1;
var Indicator = RadioGroupIndicator;
function RadioGroup({ className, ...props }) {
return /*#__PURE__*/ React.createElement(Root2, {
"data-slot": "radio-group",
className: cn("grid gap-3", className),
...props
});
}
function RadioGroupItem({ className, ...props }) {
return /*#__PURE__*/ React.createElement(Item2, {
"data-slot": "radio-group-item",
className: cn("border-primary text-primary focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:bg-input/30 aspect-square size-4 shrink-0 rounded-full border shadow-xs transition-[color,box-shadow] outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50", className),
...props
}, /*#__PURE__*/ React.createElement(Indicator, {
"data-slot": "radio-group-indicator",
className: "relative flex items-center justify-center"
}, /*#__PURE__*/ React.createElement(Check, {
className: "size-3 text-current"
})));
}
export { RadioGroup, RadioGroupItem };
//# sourceMappingURL=index.mjs.map