# Animated Radio for UI SDK (/docs/components/radio)

One selection ring glides to the chosen option and the dot pops in.

```tsx title="example.tsx"
import { Radio, RadioGroup } from "@/components/keyframery/radio"

const speeds = [
  { value: "standard", label: "Standard", when: "Arrives Thu 21 – Mon 25 Nov", price: "Free" },
  { value: "express", label: "Express", when: "Arrives Tue 19 Nov", price: "€6.90" },
  { value: "next-day", label: "Next day", when: "Order in 2 h 14 min for tomorrow", price: "€12.50" },
  { value: "pickup", label: "Pick up in store", when: "Not available for this order", price: "Free", disabled: true },
]

export default function RadioDemo() {
  return (
    <div className="w-[380px]">
      <h3 id="delivery" className="text-[15px] font-semibold tracking-tight">Delivery</h3>
      <p className="mt-1 text-[12px] text-muted-foreground">Pour-over set and a linen apron</p>
      <RadioGroup
        aria-labelledby="delivery"
        defaultValue="express"
        className="mt-4 gap-2 [--kf-radio-pad:0px] before:z-10 before:rounded-[12px] before:shadow-[0_0_0_1.5px_var(--primary),0_2px_8px_-2px_rgb(0_0_0/0.14)] dark:before:shadow-[0_0_0_1.5px_var(--primary)]"
      >
        {speeds.map((speed) => (
          <label
            key={speed.value}
            className={[
              "flex cursor-pointer items-center gap-3 rounded-[12px] bg-background px-3.5 py-3",
              "shadow-[0_0_0_1px_rgb(0_0_0/0.08),0_1px_2px_rgb(0_0_0/0.04)] dark:shadow-[0_0_0_1px_rgb(255_255_255/0.08)]",
              "transition-shadow duration-150 ease-[cubic-bezier(0.22,1,0.36,1)] motion-reduce:transition-none",
              "hover:shadow-[0_0_0_1px_rgb(0_0_0/0.14),0_1px_2px_rgb(0_0_0/0.05)] dark:hover:shadow-[0_0_0_1px_rgb(255_255_255/0.14)]",
              /* The card of the checked option is what the selection ring glides to. */
              "has-data-checked:[anchor-name:--kf-radio]",
              "has-data-disabled:cursor-not-allowed has-data-disabled:opacity-60 has-data-disabled:hover:shadow-[0_0_0_1px_rgb(0_0_0/0.08)]",
            ].join(" ")}
          >
            <Radio value={speed.value} disabled={speed.disabled} className="data-checked:[anchor-name:none]" />
            <span className="flex min-w-0 flex-1 flex-col gap-1">
              <span className="text-[13px] leading-none font-medium">{speed.label}</span>
              <span className="text-[12px] leading-none text-muted-foreground">{speed.when}</span>
            </span>
            <span className="text-[13px] font-medium tabular-nums">{speed.price}</span>
          </label>
        ))}
      </RadioGroup>
    </div>
  )
}
```

## Installation [#installation]

```bash
npx shadcn@latest add @keyframery/radio
```

This adds `components/keyframery/radio.tsx` and its primitive, `components/keyframery/primitives/radio.tsx`, to your project, and installs `@ui-sdk/react`. The motion tokens are added to your global CSS the first time you install a component.

## Usage [#usage]

```tsx
import { Radio, RadioGroup, RadioIndicator } from "@/components/keyframery/radio"
```

The parts and props are the same as UI SDK's Radio. For every prop, see the [UI SDK radio reference](https://ui-sdk.dev/docs/components/radio).

## What moves [#what-moves]

| Move      | What you see                                    |
| --------- | ----------------------------------------------- |
| **Glide** | One selection ring glides to the chosen option. |
| **Pop**   | The dot pops in.                                |
| **Press** | The control shrinks slightly while pressed.     |

Timing and springiness follow the [motion style](/docs/motion-styles) you set, so this component moves like the rest of your app.

## Swap the import [#swap-the-import]

Already using UI SDK's radio with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.

```tsx
// Before
import { … } from "@ui-sdk/react/radio"

// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/radio"
```

## Parts [#parts]

The finished component exports 3 parts:

`Radio`, `RadioGroup`, `RadioIndicator`

## Accessibility [#accessibility]

Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the radio moves: changes happen at once or with a short fade. More in [Accessibility](/docs/accessibility).

## Source [#source]

The code that lands in your project. It is yours to change.

**components/keyframery/radio.tsx**

```tsx
"use client"

/* Styled radio: the animated primitive plus the Keyframery look. */

import * as React from "react"

import {
  Radio as RadioPrimitive,
  RadioGroup as RadioGroupPrimitive,
  RadioIndicator as RadioIndicatorPrimitive,
} from "@/components/keyframery/primitives/radio"
import { cn } from "@/lib/utils"

/* The gliding selection ring: a thin primary ring 4px outside the checked radio. */
function RadioGroup({ className, ...props }: React.ComponentProps<typeof RadioGroupPrimitive>) {
  return (
    <RadioGroupPrimitive
      className={cn(
        "flex flex-col gap-2.5 outline-none [--kf-radio-pad:4px]",
        "before:bg-transparent before:shadow-[inset_0_0_0_1.5px_color-mix(in_oklab,var(--primary)_28%,transparent)]",
        className as string,
      )}
      {...props}
    />
  )
}

function Radio({
  className,
  children,
  ...props
}: React.ComponentProps<typeof RadioPrimitive>) {
  return (
    <RadioPrimitive
      className={cn(
        "relative inline-grid size-[18px] shrink-0 cursor-pointer place-items-center rounded-full outline-none select-none",
        /* A small round field; the primary key is the indicator, which pops in on top. */
        "bg-background shadow-[0_0_0_1px_rgb(0_0_0/0.14),0_1px_2px_rgb(0_0_0/0.05)]",
        "dark:bg-accent/40 dark:shadow-[0_0_0_1px_rgb(255_255_255/0.14)]",
        "not-data-disabled:not-data-readonly:hover:shadow-[0_0_0_1px_rgb(0_0_0/0.24),0_1px_2px_rgb(0_0_0/0.06)]",
        "dark:not-data-disabled:not-data-readonly:hover:shadow-[0_0_0_1px_rgb(255_255_255/0.24)]",
        "transition-[box-shadow,scale] motion-reduce:transition-none!",
        "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "data-disabled:cursor-not-allowed data-disabled:opacity-50 data-readonly:cursor-default",
        className as string,
      )}
      {...props}
    >
      {children ?? <RadioIndicator />}
    </RadioPrimitive>
  )
}

/* A primary disc holding a dot. */
function RadioIndicator({ className, ...props }: React.ComponentProps<typeof RadioIndicatorPrimitive>) {
  return (
    <RadioIndicatorPrimitive
      aria-hidden
      className={cn(
        "pointer-events-none absolute inset-0 grid place-items-center rounded-full",
        "bg-primary shadow-[inset_0_1px_0_rgb(255_255_255/0.14),0_1px_2px_rgb(0_0_0/0.25)]",
        "dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.7),0_1px_2px_rgb(0_0_0/0.5)]",
        "after:size-[7px] after:rounded-full after:bg-primary-foreground after:content-['']",
        className as string,
      )}
      {...props}
    />
  )
}

export { Radio, RadioGroup, RadioIndicator }
```

**components/keyframery/primitives/radio.tsx**

```tsx
"use client"

/* Animated drop-in for `@ui-sdk/react/radio`: same parts, same props. Swap the import and keep your code.
   This file adds motion only. Colours, radii, shadows and spacing stay yours. */

import {
  Radio as RadioPart,
  RadioGroup as RadioGroupPart,
  RadioIndicator as RadioIndicatorPart,
} from "@ui-sdk/react/radio"
import * as React from "react"

import { cn } from "@/lib/utils"

export * from "@ui-sdk/react/radio"

/* UI SDK accepts a class name or a function of state; keep both working. */
function withClass<S>(base: string, className: string | ((state: S) => string | undefined) | undefined) {
  return typeof className === "function" ? (state: S) => cn(base, className(state)) : cn(base, className)
}

/* Glide: one selection ring follows the checked radio from option to option, positioned with
   CSS anchors. By default it is a soft halo around the checked radio, `--kf-radio-pad` outside
   it; restyle it with `before:` classes. To ring a whole row instead, give the row
   `has-data-checked:[anchor-name:--kf-radio]` and the radio `data-checked:[anchor-name:none]`. */
function RadioGroup({ className, ...props }: React.ComponentProps<typeof RadioGroupPart>) {
  return (
    <RadioGroupPart
      className={withClass(
        cn(
          "relative isolate [anchor-scope:--kf-radio] [--kf-radio-pad:5px]",
          "before:pointer-events-none before:absolute before:-z-10 before:rounded-full before:content-['']",
          "before:bg-[color-mix(in_oklch,currentColor_9%,transparent)]",
          "before:[position-anchor:--kf-radio]",
          "before:top-[calc(anchor(top)-var(--kf-radio-pad))] before:right-[calc(anchor(right)-var(--kf-radio-pad))]",
          "before:bottom-[calc(anchor(bottom)-var(--kf-radio-pad))] before:left-[calc(anchor(left)-var(--kf-radio-pad))]",
          "before:transition-[top,right,bottom,left,opacity] before:duration-[calc(460ms*var(--kf-speed,1))] before:ease-spring",
          "not-has-data-checked:before:opacity-0",
          "motion-reduce:before:transition-none!",
        ),
        className,
      )}
      {...props}
    />
  )
}

/* Press: gives under the finger, springs back. The checked radio is the anchor the ring follows. */
function Radio({ className, ...props }: React.ComponentProps<typeof RadioPart>) {
  return (
    <RadioPart
      className={withClass(
        cn(
          "data-checked:[anchor-name:--kf-radio]",
          "transition-[scale] duration-[calc(200ms*var(--kf-speed,1))] ease-pop motion-reduce:transition-none!",
          "not-data-disabled:not-data-readonly:active:scale-[0.86]",
        ),
        className,
      )}
      {...props}
    />
  )
}

/* The dot pops in from 0.3 with an overshoot and shrinks away on the way out. */
function RadioIndicator({ className, ...props }: React.ComponentProps<typeof RadioIndicatorPart>) {
  return (
    <RadioIndicatorPart
      className={withClass(
        cn(
          "transition-[scale,opacity] duration-[calc(480ms*var(--kf-speed,1))] ease-pop",
          "data-starting-style:scale-[0.3] data-starting-style:opacity-0",
          "data-ending-style:scale-[0.4] data-ending-style:opacity-0 data-ending-style:duration-[calc(150ms*var(--kf-speed,1))] data-ending-style:ease-house",
          "motion-reduce:transition-none!",
        ),
        className,
      )}
      {...props}
    />
  )
}

export { Radio, RadioGroup, RadioIndicator }
```

## Questions

### How does the animated Radio for UI SDK move?

Glide: One selection ring glides to the chosen option. Pop: The dot pops in. Press: The control shrinks slightly while pressed.

### Do I need Motion or Framer Motion for the animated Radio?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

### Can I keep my own styling and only take the motion?

Yes. Import from `@/components/keyframery/primitives/radio` instead. It has the same parts and props as `@ui-sdk/react/radio` and adds motion only.

### What happens to the Radio when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.
