UNPKG

@loke/ui

Version:
248 lines (246 loc) • 10.3 kB
"use client"; // src/components/radio-group/radio.tsx import { composeEventHandlers } from "@loke/ui/compose-events"; import { useComposedRefs } from "@loke/ui/compose-refs"; import { createContextScope } from "@loke/ui/context"; import { Presence } from "@loke/ui/presence"; import { Primitive } from "@loke/ui/primitive"; import { useSize } from "@loke/ui/use-size"; import { forwardRef, useEffect, useReducer, useRef, useState } from "react"; import { jsx, jsxs } from "react/jsx-runtime"; var RADIO_NAME = "Radio", [createRadioContext, createRadioScope] = createContextScope(RADIO_NAME), [RadioProvider, useRadioContext] = createRadioContext(RADIO_NAME), Radio = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeRadio, name, checked = !1, required, disabled, value = "on", onCheck, form, ...radioProps } = props, [button, setButton] = useState(null), composedRefs = useComposedRefs(forwardedRef, setButton), hasConsumerStoppedPropagationRef = useRef(!1), [userInteractionCount, onUserInteraction] = useReducer((count) => count + 1, 0), isFormControl = button ? form || !!button.closest("form") : !0; return /* @__PURE__ */ jsxs(RadioProvider, { checked, disabled, scope: __scopeRadio, children: [ /* @__PURE__ */ jsx(Primitive.button, { "aria-checked": checked, "data-disabled": disabled ? "" : void 0, "data-state": getState(checked), disabled, role: "radio", type: "button", value, ...radioProps, onClick: composeEventHandlers(props.onClick, (event) => { if (!checked) onUserInteraction(), onCheck?.(); if (isFormControl) { if (hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(), !hasConsumerStoppedPropagationRef.current) event.stopPropagation(); } }), ref: composedRefs }), isFormControl && /* @__PURE__ */ 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__ */ forwardRef((props, forwardedRef) => { let { __scopeRadio, forceMount, ...indicatorProps } = props, context = useRadioContext(INDICATOR_NAME, __scopeRadio); return /* @__PURE__ */ jsx(Presence, { present: forceMount || context.checked, children: /* @__PURE__ */ jsx(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__ */ forwardRef(({ __scopeRadio, control, checked, hasConsumerStoppedPropagationRef, userInteractionCount, ...props }, forwardedRef) => { let ref = useRef(null), composedRefs = useComposedRefs(ref, forwardedRef), controlSize = useSize(control), shouldStopClickPropagationRef = useRef(!1), prevCheckedRef = useRef(checked), prevUserInteractionCountRef = useRef(userInteractionCount); return 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("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 import { composeEventHandlers as composeEventHandlers2 } from "@loke/ui/compose-events"; import { useComposedRefs as useComposedRefs2 } from "@loke/ui/compose-refs"; import { createContextScope as createContextScope2 } from "@loke/ui/context"; import { Primitive as Primitive2 } from "@loke/ui/primitive"; import * as RovingFocusGroup from "@loke/ui/roving-focus"; import { createRovingFocusGroupScope } from "@loke/ui/roving-focus"; import { useControllableState } from "@loke/ui/use-controllable-state"; import { useDirection } from "@loke/ui/use-direction"; import { forwardRef as forwardRef2, useEffect as useEffect2, useRef as useRef2, useState as useState2 } from "react"; import { jsx as jsx2 } from "react/jsx-runtime"; var ARROW_KEYS = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"], RADIO_GROUP_NAME = "RadioGroup", [createRadioGroupContext, createRadioGroupScope] = createContextScope2(RADIO_GROUP_NAME, [createRovingFocusGroupScope, createRadioScope]), useRovingFocusGroupScope = createRovingFocusGroupScope(), useRadioScope = createRadioScope(), [RadioGroupProvider, useRadioGroupContext] = createRadioGroupContext(RADIO_GROUP_NAME), RadioGroup = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef2((props, forwardedRef) => { let { __scopeRadioGroup, name, form, defaultValue, value: valueProp, required = !1, disabled = !1, orientation, dir, loop = !0, onValueChange, ...groupProps } = props, rovingFocusGroupScope = useRovingFocusGroupScope(__scopeRadioGroup), direction = useDirection(dir), [value, setValue] = useControllableState({ caller: RADIO_GROUP_NAME, defaultProp: defaultValue ?? null, onChange: onValueChange, prop: valueProp }), [control, setControl] = useState2(null), composedRefs = useComposedRefs2(forwardedRef, setControl), initialValueRef = useRef2(value); return useEffect2(() => { 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__ */ jsx2(RadioGroupProvider, { disabled, form, name, onValueChange: setValue, required, scope: __scopeRadioGroup, value, children: /* @__PURE__ */ jsx2(RovingFocusGroup.Root, { asChild: !0, ...rovingFocusGroupScope, dir: direction, loop, orientation, children: /* @__PURE__ */ jsx2(Primitive2.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__ */ forwardRef2((props, forwardedRef) => { let { __scopeRadioGroup, disabled, ...itemProps } = props, context = useRadioGroupContext(ITEM_NAME, __scopeRadioGroup), isDisabled = context.disabled || disabled, rovingFocusGroupScope = useRovingFocusGroupScope(__scopeRadioGroup), radioScope = useRadioScope(__scopeRadioGroup), ref = useRef2(null), composedRefs = useComposedRefs2(forwardedRef, ref), checked = context.value === itemProps.value, isArrowKeyPressedRef = useRef2(!1); return useEffect2(() => { 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__ */ jsx2(RovingFocusGroup.Item, { asChild: !0, ...rovingFocusGroupScope, active: checked, focusable: !isDisabled, children: /* @__PURE__ */ jsx2(Radio, { checked, disabled: isDisabled, form: context.form, required: context.required, ...radioScope, ...itemProps, name: context.name, onCheck: () => context.onValueChange(itemProps.value), onFocus: composeEventHandlers2(itemProps.onFocus, () => { if (isArrowKeyPressedRef.current) ref.current?.click(); }), onKeyDown: composeEventHandlers2((event) => { if (event.key === "Enter") event.preventDefault(); }), ref: composedRefs }) }); }), { displayName: ITEM_NAME }), INDICATOR_NAME2 = "RadioGroupIndicator", RadioGroupIndicator = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef2((props, forwardedRef) => { let { __scopeRadioGroup, ...indicatorProps } = props, radioScope = useRadioScope(__scopeRadioGroup); return /* @__PURE__ */ jsx2(RadioIndicator, { ...radioScope, ...indicatorProps, ref: forwardedRef }); }), { displayName: INDICATOR_NAME2 }), Root2 = RadioGroup, Item2 = RadioGroupItem, Indicator = RadioGroupIndicator; export { Indicator, Item2 as Item, Radio, RadioGroup, RadioGroupIndicator, RadioGroupItem, RadioIndicator, Root2 as Root, createRadioGroupScope, createRadioScope };