One selection ring glides to the chosen option and the dot pops in.
Part of Keyframery, the open-source animated components for UI SDK
The demo plays by itself. Point at it or press Tab to take over.
Installation
npx shadcn@latest add @keyframery/radioThis 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
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.
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 you set, so this component moves like the rest of your app.
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.
// Before
import { … } from "@ui-sdk/react/radio"
// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/radio"Parts
The finished component exports 3 parts:
Radio, RadioGroup, RadioIndicator
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.
Source
The code that lands in your project. It is yours to change.
"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 }"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.