@loke/ui
Version:
To install dependencies:
276 lines (273 loc) • 13.3 kB
JavaScript
"use client";
var __create = Object.create;
var { getPrototypeOf: __getProtoOf, defineProperty: __defProp, getOwnPropertyNames: __getOwnPropNames, getOwnPropertyDescriptor: __getOwnPropDesc } = Object, __hasOwnProp = Object.prototype.hasOwnProperty;
function __accessProp(key) {
return this[key];
}
var __toESMCache_node, __toESMCache_esm, __toESM = (mod, isNodeMode, target) => {
var canCache = mod != null && typeof mod === "object";
if (canCache) {
var cache = isNodeMode ? __toESMCache_node ??= /* @__PURE__ */ new WeakMap : __toESMCache_esm ??= /* @__PURE__ */ new WeakMap, cached = cache.get(mod);
if (cached)
return cached;
}
target = mod != null ? __create(__getProtoOf(mod)) : {};
let to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target;
if (mod && typeof mod === "object" || typeof mod === "function") {
for (let key of __getOwnPropNames(mod))
if (!__hasOwnProp.call(to, key))
__defProp(to, key, {
get: __accessProp.bind(mod, key),
enumerable: !0
});
}
if (canCache)
cache.set(mod, to);
return to;
}, __toCommonJS = (from) => {
var entry = (__moduleCache ??= /* @__PURE__ */ new WeakMap).get(from), desc;
if (entry)
return entry;
if (entry = __defProp({}, "__esModule", { value: !0 }), from && typeof from === "object" || typeof from === "function") {
for (var key of __getOwnPropNames(from))
if (!__hasOwnProp.call(entry, key))
__defProp(entry, key, {
get: __accessProp.bind(from, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return __moduleCache.set(from, entry), entry;
}, __moduleCache;
var __returnValue = (v) => v;
function __exportSetter(name, newValue) {
this[name] = __returnValue.bind(null, newValue);
}
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: !0,
configurable: !0,
set: __exportSetter.bind(all, name)
});
};
// src/components/radio-group/index.ts
var exports_radio_group = {};
__export(exports_radio_group, {
Indicator: () => Indicator,
Item: () => Item2,
Radio: () => Radio,
RadioGroup: () => RadioGroup,
RadioGroupIndicator: () => RadioGroupIndicator,
RadioGroupItem: () => RadioGroupItem,
RadioIndicator: () => RadioIndicator,
Root: () => Root2,
createRadioGroupScope: () => createRadioGroupScope,
createRadioScope: () => createRadioScope
});
module.exports = __toCommonJS(exports_radio_group);
// src/components/radio-group/radio.tsx
var import_compose_events = require("@loke/ui/compose-events"), import_compose_refs = require("@loke/ui/compose-refs"), import_context = require("@loke/ui/context"), import_presence = require("@loke/ui/presence"), import_primitive = require("@loke/ui/primitive"), import_use_size = require("@loke/ui/use-size"), import_react = require("react"), jsx_runtime = require("react/jsx-runtime"), RADIO_NAME = "Radio", [createRadioContext, createRadioScope] = import_context.createContextScope(RADIO_NAME), [RadioProvider, useRadioContext] = createRadioContext(RADIO_NAME), Radio = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => {
let {
__scopeRadio,
name,
checked = !1,
required,
disabled,
value = "on",
onCheck,
form,
...radioProps
} = props, [button, setButton] = import_react.useState(null), composedRefs = import_compose_refs.useComposedRefs(forwardedRef, setButton), hasConsumerStoppedPropagationRef = import_react.useRef(!1), [userInteractionCount, onUserInteraction] = import_react.useReducer((count) => count + 1, 0), isFormControl = button ? form || !!button.closest("form") : !0;
return /* @__PURE__ */ jsx_runtime.jsxs(RadioProvider, {
checked,
disabled,
scope: __scopeRadio,
children: [
/* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.button, {
"aria-checked": checked,
"data-disabled": disabled ? "" : void 0,
"data-state": getState(checked),
disabled,
role: "radio",
type: "button",
value,
...radioProps,
onClick: import_compose_events.composeEventHandlers(props.onClick, (event) => {
if (!checked)
onUserInteraction(), onCheck?.();
if (isFormControl) {
if (hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(), !hasConsumerStoppedPropagationRef.current)
event.stopPropagation();
}
}),
ref: composedRefs
}),
isFormControl && /* @__PURE__ */ jsx_runtime.jsx(RadioBubbleInput, {
checked,
control: button,
disabled,
form,
hasConsumerStoppedPropagationRef,
name,
required,
style: { transform: "translateX(-100%)" },
userInteractionCount,
value
})
]
});
}), { displayName: RADIO_NAME }), INDICATOR_NAME = "RadioIndicator", RadioIndicator = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef((props, forwardedRef) => {
let { __scopeRadio, forceMount, ...indicatorProps } = props, context = useRadioContext(INDICATOR_NAME, __scopeRadio);
return /* @__PURE__ */ jsx_runtime.jsx(import_presence.Presence, {
present: forceMount || context.checked,
children: /* @__PURE__ */ jsx_runtime.jsx(import_primitive.Primitive.span, {
"data-disabled": context.disabled ? "" : void 0,
"data-state": getState(context.checked),
...indicatorProps,
ref: forwardedRef
})
});
}), { displayName: INDICATOR_NAME }), BUBBLE_INPUT_NAME = "RadioBubbleInput", RadioBubbleInput = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react.forwardRef(({
__scopeRadio,
control,
checked,
hasConsumerStoppedPropagationRef,
userInteractionCount,
...props
}, forwardedRef) => {
let ref = import_react.useRef(null), composedRefs = import_compose_refs.useComposedRefs(ref, forwardedRef), controlSize = import_use_size.useSize(control), shouldStopClickPropagationRef = import_react.useRef(!1), prevCheckedRef = import_react.useRef(checked), prevUserInteractionCountRef = import_react.useRef(userInteractionCount);
return import_react.useEffect(() => {
let input = ref.current;
if (!input)
return;
let inputProto = window.HTMLInputElement.prototype, setChecked = Object.getOwnPropertyDescriptor(inputProto, "checked").set, isUserInteraction = userInteractionCount !== prevUserInteractionCountRef.current;
prevUserInteractionCountRef.current = userInteractionCount;
let checkedChanged = prevCheckedRef.current !== checked;
if (prevCheckedRef.current = checked, checkedChanged && setChecked) {
shouldStopClickPropagationRef.current = !isUserInteraction;
let event = new Event("click", {
bubbles: !(isUserInteraction && hasConsumerStoppedPropagationRef.current)
});
setChecked.call(input, checked), input.dispatchEvent(event), shouldStopClickPropagationRef.current = !1;
}
}, [checked, hasConsumerStoppedPropagationRef, userInteractionCount]), /* @__PURE__ */ jsx_runtime.jsx("input", {
"aria-hidden": !0,
defaultChecked: checked,
type: "radio",
...props,
onClick: (event) => {
if (shouldStopClickPropagationRef.current)
event.stopPropagation();
},
ref: composedRefs,
style: {
...props.style,
...controlSize,
margin: 0,
opacity: 0,
pointerEvents: "none",
position: "absolute"
},
tabIndex: -1
});
}), { displayName: BUBBLE_INPUT_NAME });
function getState(checked) {
return checked ? "checked" : "unchecked";
}
// src/components/radio-group/radio-group.tsx
var import_compose_events2 = require("@loke/ui/compose-events"), import_compose_refs2 = require("@loke/ui/compose-refs"), import_context2 = require("@loke/ui/context"), import_primitive2 = require("@loke/ui/primitive"), RovingFocusGroup = __toESM(require("@loke/ui/roving-focus"), 1), import_roving_focus = require("@loke/ui/roving-focus"), import_use_controllable_state = require("@loke/ui/use-controllable-state"), import_use_direction = require("@loke/ui/use-direction"), import_react2 = require("react");
var jsx_runtime2 = require("react/jsx-runtime"), ARROW_KEYS = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"], RADIO_GROUP_NAME = "RadioGroup", [createRadioGroupContext, createRadioGroupScope] = import_context2.createContextScope(RADIO_GROUP_NAME, [import_roving_focus.createRovingFocusGroupScope, createRadioScope]), useRovingFocusGroupScope = import_roving_focus.createRovingFocusGroupScope(), useRadioScope = createRadioScope(), [RadioGroupProvider, useRadioGroupContext] = createRadioGroupContext(RADIO_GROUP_NAME), RadioGroup = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react2.forwardRef((props, forwardedRef) => {
let {
__scopeRadioGroup,
name,
form,
defaultValue,
value: valueProp,
required = !1,
disabled = !1,
orientation,
dir,
loop = !0,
onValueChange,
...groupProps
} = props, rovingFocusGroupScope = useRovingFocusGroupScope(__scopeRadioGroup), direction = import_use_direction.useDirection(dir), [value, setValue] = import_use_controllable_state.useControllableState({
caller: RADIO_GROUP_NAME,
defaultProp: defaultValue ?? null,
onChange: onValueChange,
prop: valueProp
}), [control, setControl] = import_react2.useState(null), composedRefs = import_compose_refs2.useComposedRefs(forwardedRef, setControl), initialValueRef = import_react2.useRef(value);
return import_react2.useEffect(() => {
let associatedForm = form ? control?.ownerDocument.getElementById(form) : control?.closest("form");
if (!(associatedForm instanceof HTMLFormElement))
return;
let reset = () => setValue(initialValueRef.current);
return associatedForm.addEventListener("reset", reset), () => associatedForm.removeEventListener("reset", reset);
}, [control, form, setValue]), /* @__PURE__ */ jsx_runtime2.jsx(RadioGroupProvider, {
disabled,
form,
name,
onValueChange: setValue,
required,
scope: __scopeRadioGroup,
value,
children: /* @__PURE__ */ jsx_runtime2.jsx(RovingFocusGroup.Root, {
asChild: !0,
...rovingFocusGroupScope,
dir: direction,
loop,
orientation,
children: /* @__PURE__ */ jsx_runtime2.jsx(import_primitive2.Primitive.div, {
"aria-orientation": orientation,
"aria-required": required,
"data-disabled": disabled ? "" : void 0,
dir: direction,
role: "radiogroup",
...groupProps,
ref: composedRefs
})
})
});
}), { displayName: RADIO_GROUP_NAME }), ITEM_NAME = "RadioGroupItem", RadioGroupItem = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react2.forwardRef((props, forwardedRef) => {
let { __scopeRadioGroup, disabled, ...itemProps } = props, context = useRadioGroupContext(ITEM_NAME, __scopeRadioGroup), isDisabled = context.disabled || disabled, rovingFocusGroupScope = useRovingFocusGroupScope(__scopeRadioGroup), radioScope = useRadioScope(__scopeRadioGroup), ref = import_react2.useRef(null), composedRefs = import_compose_refs2.useComposedRefs(forwardedRef, ref), checked = context.value === itemProps.value, isArrowKeyPressedRef = import_react2.useRef(!1);
return import_react2.useEffect(() => {
let handleKeyDown = (event) => {
if (ARROW_KEYS.includes(event.key))
isArrowKeyPressedRef.current = !0;
}, handleKeyUp = () => isArrowKeyPressedRef.current = !1;
return document.addEventListener("keydown", handleKeyDown), document.addEventListener("keyup", handleKeyUp), () => {
document.removeEventListener("keydown", handleKeyDown), document.removeEventListener("keyup", handleKeyUp);
};
}, []), /* @__PURE__ */ jsx_runtime2.jsx(RovingFocusGroup.Item, {
asChild: !0,
...rovingFocusGroupScope,
active: checked,
focusable: !isDisabled,
children: /* @__PURE__ */ jsx_runtime2.jsx(Radio, {
checked,
disabled: isDisabled,
form: context.form,
required: context.required,
...radioScope,
...itemProps,
name: context.name,
onCheck: () => context.onValueChange(itemProps.value),
onFocus: import_compose_events2.composeEventHandlers(itemProps.onFocus, () => {
if (isArrowKeyPressedRef.current)
ref.current?.click();
}),
onKeyDown: import_compose_events2.composeEventHandlers((event) => {
if (event.key === "Enter")
event.preventDefault();
}),
ref: composedRefs
})
});
}), { displayName: ITEM_NAME }), INDICATOR_NAME2 = "RadioGroupIndicator", RadioGroupIndicator = /* @__PURE__ */ Object.assign(/* @__PURE__ */ import_react2.forwardRef((props, forwardedRef) => {
let { __scopeRadioGroup, ...indicatorProps } = props, radioScope = useRadioScope(__scopeRadioGroup);
return /* @__PURE__ */ jsx_runtime2.jsx(RadioIndicator, {
...radioScope,
...indicatorProps,
ref: forwardedRef
});
}), { displayName: INDICATOR_NAME2 }), Root2 = RadioGroup, Item2 = RadioGroupItem, Indicator = RadioGroupIndicator;