{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"motion-radio-group","type":"registry:component","title":"Radio Group (Motion)","description":"RadioGroup with motion: the dot springs into the radio you pick and shrinks out of the one you left, and with cards the picked frame glides from card to card.","dependencies":["motion","radix-ui"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/motion-field.json","https://dashral.com/r/motion-hover.json","https://dashral.com/r/motion-reduced.json","https://dashral.com/r/motion-tokens.json","https://dashral.com/r/field.json","https://dashral.com/r/radio-group.json"],"files":[{"path":"registry/dashral/motion/radio-group.tsx","type":"registry:file","target":"components/dashral/motion/radio-group.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport { RadioGroup as RadioGroupPrimitive } from \"radix-ui\"\nimport { animate, motion, useMotionValue } from \"motion/react\"\nimport { ChoiceField, FieldLabel, FieldWrapper, splitFieldProps, useFieldControl, type FieldOwnProps } from \"../field\"\nimport type { RadioGroupProps } from \"../radio-group\"\nimport { cn } from \"../lib/utils\"\n\nimport { useHoverPill } from \"../lib/motion-hover\"\nimport { motionMessages } from \"../lib/motion-field\"\nimport { useReducedMotion } from \"../lib/motion-reduced\"\nimport { duration, ease, spring } from \"../lib/motion-tokens\"\n\nexport type { RadioGroupProps, RadioOption } from \"../radio-group\"\n\ntype ControlColor = NonNullable<RadioGroupProps[\"color\"]>\n\n// The dot and border when picked, as the kit's RadioGroup has them. Every class is written out in full so Tailwind finds it.\nconst radioColors: Record<ControlColor, string> = {\n  primary: \"text-primary data-[state=checked]:border-primary\",\n  neutral: \"text-foreground data-[state=checked]:border-foreground\",\n  info: \"text-info data-[state=checked]:border-info\",\n  success: \"text-success data-[state=checked]:border-success\",\n  warning: \"text-warning data-[state=checked]:border-warning\",\n  destructive: \"text-destructive data-[state=checked]:border-destructive\",\n  highlight: \"text-highlight data-[state=checked]:border-highlight\",\n}\n\n// The picked card's frame: its border and a tint in the color.\nconst frameColors: Record<ControlColor, string> = {\n  primary: \"border-primary bg-primary/5\",\n  neutral: \"border-foreground bg-foreground/5\",\n  info: \"border-info bg-info/5\",\n  success: \"border-success bg-success/5\",\n  warning: \"border-warning bg-warning/5\",\n  destructive: \"border-destructive bg-destructive/5\",\n  highlight: \"border-highlight bg-highlight/5\",\n}\n\n/**\n * The kit's RadioGroup with motion: the same options, variants, colors and props. The picked radio's dot springs in\n * from nothing while the one you left shrinks away; with variant=\"cards\" the picked card's frame and tint glide from\n * card to card (one frame over the group, measured from the cards' layout), a soft hover glides after the pointer\n * from card to card without a blink, and a card presses in while held. With reduced motion the dot and frame only fade.\n */\nexport function RadioGroup(props: RadioGroupProps) {\n  const [field, control] = splitFieldProps(props)\n  return (\n    <FieldWrapper {...field} group renderMessages={motionMessages}>\n      <RadioGroupControl {...control} />\n    </FieldWrapper>\n  )\n}\n\nfunction RadioGroupControl({\n  options,\n  variant = \"default\",\n  indicator = true,\n  color = \"primary\",\n  value,\n  defaultValue,\n  onValueChange,\n  className,\n  classNames,\n  ...rest\n}: Omit<RadioGroupProps, keyof FieldOwnProps>) {\n  const props = useFieldControl(rest)\n  const reduced = useReducedMotion()\n  const groupId = React.useId()\n  const [own, setOwn] = React.useState(defaultValue ?? \"\")\n  const picked = value ?? own\n\n  // The picked card's frame: one element over the group, gliding to that card's place. Measured from the cards' own\n  // layout (offsetTop, offsetLeft and their size), which a card pressed in mid-click doesn't skew, so it always lands\n  // exactly on the card's edge, whether the cards stack or sit side by side in columns.\n  const root = React.useRef<HTMLDivElement>(null)\n  const top = useMotionValue(0)\n  const height = useMotionValue(0)\n  const left = useMotionValue(0)\n  const width = useMotionValue(0)\n  const shown = useMotionValue(0)\n  const measured = React.useRef(false)\n  React.useLayoutEffect(() => {\n    const node = root.current\n    if (variant !== \"cards\" || !node) return\n    const place = (glide: boolean) => {\n      const card = Array.from(node.querySelectorAll<HTMLElement>(\"[data-slot=radio-card]\")).find((el) => el.dataset.value === picked)\n      if (!card) return shown.set(0)\n      if (!glide || !measured.current || reduced) {\n        top.jump(card.offsetTop)\n        height.jump(card.offsetHeight)\n        left.jump(card.offsetLeft)\n        width.jump(card.offsetWidth)\n      } else {\n        animate(top, card.offsetTop, spring.move)\n        animate(height, card.offsetHeight, spring.move)\n        animate(left, card.offsetLeft, spring.move)\n        animate(width, card.offsetWidth, spring.move)\n      }\n      measured.current = true\n      shown.set(1)\n    }\n    place(true)\n    // A card growing or the group rewrapping: follow at once. Only on a real change of size: an observer also calls\n    // back as soon as it starts, which would cut the glide just begun short.\n    let size = `${node.offsetWidth}x${node.offsetHeight}`\n    const observer = new ResizeObserver(() => {\n      const now = `${node.offsetWidth}x${node.offsetHeight}`\n      if (now === size) return\n      size = now\n      place(false)\n    })\n    observer.observe(node)\n    return () => observer.disconnect()\n  }, [picked, variant, reduced, top, height, left, width, shown])\n\n  // The hover: one soft layer under the cards, gliding from card to card without going out between them; it fades\n  // where it is when the pointer leaves the group.\n  const hover = useHoverPill<string>(reduced)\n  const hoverTop = useMotionValue(0)\n  const hoverHeight = useMotionValue(0)\n  const hoverLeft = useMotionValue(0)\n  const hoverWidth = useMotionValue(0)\n  const hoverCard = (card: HTMLElement, key: string) => {\n    // Showing already, it glides to the new card; coming back after it faded, it starts right there.\n    if (hover.opacity.get() < 0.05 || reduced) {\n      hoverTop.jump(card.offsetTop)\n      hoverHeight.jump(card.offsetHeight)\n      hoverLeft.jump(card.offsetLeft)\n      hoverWidth.jump(card.offsetWidth)\n    } else {\n      animate(hoverTop, card.offsetTop, spring.move)\n      animate(hoverHeight, card.offsetHeight, spring.move)\n      animate(hoverLeft, card.offsetLeft, spring.move)\n      animate(hoverWidth, card.offsetWidth, spring.move)\n    }\n    hover.enter(key)\n  }\n\n  return (\n    <RadioGroupPrimitive.Root\n      ref={root}\n      onPointerLeave={variant === \"cards\" ? hover.leave : undefined}\n      data-slot=\"radio-group\"\n      value={picked}\n      onValueChange={(next) => {\n        if (value === undefined) setOwn(next)\n        onValueChange?.(next)\n      }}\n      className={cn(\"relative isolate grid gap-3\", className, classNames?.group)}\n      {...props}\n    >\n      {variant === \"cards\" && (\n        // Under the cards' content, over the group's background.\n        <motion.span\n          aria-hidden\n          className=\"pointer-events-none absolute -z-10 rounded-lg bg-accent/50\"\n          style={{ top: hoverTop, height: hoverHeight, left: hoverLeft, width: hoverWidth, opacity: hover.opacity }}\n        />\n      )}\n      {variant === \"cards\" && (\n        // Over the cards, so it covers their borders; its tint is faint enough to leave their text clear.\n        <motion.span\n          aria-hidden\n          className={cn(\"pointer-events-none absolute z-10 rounded-lg border\", frameColors[color])}\n          style={{ top, height, left, width, opacity: shown }}\n        />\n      )}\n      {options.map((option) => {\n        const { value: itemValue, label = itemValue, description, icon, disabled } = typeof option === \"string\" ? { value: option } : option\n        const id = `${groupId}-${itemValue}`\n        const radio = (extra?: string, describedBy?: string) => (\n          <Radio\n            id={id}\n            value={itemValue}\n            disabled={disabled}\n            picked={picked === itemValue}\n            reduced={reduced}\n            aria-describedby={describedBy}\n            className={cn(radioColors[color], extra, classNames?.radio)}\n          />\n        )\n\n        if (variant === \"cards\") {\n          const descriptionId = description ? `${id}-description` : undefined\n          return (\n            <FieldLabel\n              key={itemValue}\n              htmlFor={id}\n              data-slot=\"radio-card\"\n              data-value={itemValue}\n              onPointerEnter={(event: React.PointerEvent<HTMLLabelElement>) => !disabled && hoverCard(event.currentTarget, itemValue)}\n              asChild\n              // On the label, which merges them with its own; the motion element below only takes them.\n              className={cn(\n                \"relative flex items-start gap-3 rounded-lg border p-4 font-normal transition-colors\",\n                \"has-focus-visible:ring-2 has-focus-visible:ring-ring/50 has-disabled:cursor-not-allowed has-disabled:opacity-50\",\n                classNames?.item\n              )}\n            >\n              <motion.label whileTap={reduced || disabled ? undefined : { scale: 0.985 }} transition={spring.press}>\n                {icon && <span className=\"flex shrink-0 text-muted-foreground [&_svg]:size-5\">{icon}</span>}\n                <span className=\"grid min-w-0 flex-1 gap-1\">\n                  <span className=\"leading-4 font-medium\">{label}</span>\n                  {description && (\n                    <span id={descriptionId} className=\"text-xs text-muted-foreground\">\n                      {description}\n                    </span>\n                  )}\n                </span>\n                {/* Hidden, the radio still takes focus and the arrow keys. */}\n                {radio(indicator ? undefined : \"sr-only\", descriptionId)}\n              </motion.label>\n            </FieldLabel>\n          )\n        }\n\n        return (\n          <ChoiceField renderMessages={motionMessages} key={itemValue} id={id} label={label} description={description} disabled={disabled} leading=\"leading-4\" classNames={{ root: classNames?.item }}>\n            {(choice) => radio(undefined, choice[\"aria-describedby\"])}\n          </ChoiceField>\n        )\n      })}\n    </RadioGroupPrimitive.Root>\n  )\n}\n\nfunction Radio({\n  className,\n  picked,\n  reduced,\n  ...props\n}: React.ComponentProps<typeof RadioGroupPrimitive.Item> & { picked: boolean; reduced: boolean }) {\n  return (\n    <RadioGroupPrimitive.Item\n      data-slot=\"radio-group-item\"\n      className={cn(\n        \"grid aspect-square size-4 shrink-0 place-items-center rounded-full border border-input shadow-xs transition-[color,border-color,box-shadow] outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 in-[[aria-invalid=true]]:border-destructive\",\n        className\n      )}\n      {...props}\n    >\n      {/* Always there, so the dot can shrink away as well as spring in. */}\n      <RadioGroupPrimitive.Indicator forceMount data-slot=\"radio-group-indicator\" asChild>\n        <motion.span\n          className=\"size-2 rounded-full bg-current\"\n          initial={false}\n          animate={picked ? { scale: 1, opacity: 1 } : { scale: reduced ? 1 : 0, opacity: 0 }}\n          transition={\n            reduced\n              ? { duration: duration.fast }\n              : picked\n                ? { ...spring.pop, opacity: { duration: duration.fast, ease: ease.enter } }\n                : { duration: duration.fast, ease: ease.exit }\n          }\n        />\n      </RadioGroupPrimitive.Indicator>\n    </RadioGroupPrimitive.Item>\n  )\n}\n"}],"cssVars":{"theme":{"color-info":"var(--info)","color-info-foreground":"var(--info-foreground)","color-info-text":"var(--info-text)","color-success":"var(--success)","color-success-foreground":"var(--success-foreground)","color-success-text":"var(--success-text)","color-warning":"var(--warning)","color-warning-foreground":"var(--warning-foreground)","color-warning-text":"var(--warning-text)","color-highlight":"var(--highlight)","color-highlight-foreground":"var(--highlight-foreground)","color-highlight-text":"var(--highlight-text)"},"light":{"info":"oklch(0.5 0.134 242.749)","info-foreground":"oklch(0.985 0 0)","info-text":"oklch(0.443 0.11 240.79)","success":"oklch(0.508 0.118 165.612)","success-foreground":"oklch(0.985 0 0)","success-text":"oklch(0.432 0.095 166.913)","warning":"#f59200","warning-foreground":"oklch(0.3 0.07 60)","warning-text":"#964e00","highlight":"#b8008c","highlight-foreground":"oklch(0.985 0 0)","highlight-text":"#a3007b"},"dark":{"info":"oklch(0.685 0.169 237.323)","info-foreground":"oklch(0.2 0 0)","info-text":"oklch(0.746 0.16 232.661)","success":"oklch(0.696 0.17 162.48)","success-foreground":"oklch(0.2 0.04 160)","success-text":"oklch(0.765 0.177 163.223)","warning":"#f59200","warning-foreground":"oklch(0.25 0.06 60)","warning-text":"#ffba42","highlight":"#ff2ecf","highlight-foreground":"oklch(0.2 0 0)","highlight-text":"#ff75df"}}}