The selected key stretches across toward the new option, then closes up.
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/segmented-controlThis adds components/keyframery/segmented-control.tsx and its primitive, components/keyframery/primitives/segmented-control.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 {
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@/components/keyframery/segmented-control"The parts and props are the same as UI SDK's Segmented Control. For every prop, see the UI SDK segmented control reference.
What moves
| Move | What you see |
|---|---|
| Stretch | The selected key stretches across toward the new option, then closes up. |
| Fade | Label colours flip exactly at the key's edge as it passes. |
| Press | Options press in. |
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 segmented control with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.
// Before
import { … } from "@ui-sdk/react/segmented-control"
// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/segmented-control"Parts
The finished component exports 3 parts:
SegmentedControl, SegmentedControlIndicator, SegmentedControlItem
Accessibility
Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the segmented control 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 segmented control: the animated primitive plus the Keyframery look. */
import * as React from "react"
import {
SegmentedControl as SegmentedControlRoot,
SegmentedControlIndicator as SegmentedControlIndicatorPrimitive,
SegmentedControlItem as SegmentedControlItemPrimitive,
} from "@/components/keyframery/primitives/segmented-control"
import { cn } from "@/lib/utils"
/* One box shared by the real item and its copy inside the key, so both lay out identically. */
const itemBox =
"inline-flex h-8 shrink-0 items-center justify-center gap-1.5 rounded-[9px] px-3 text-[13px] leading-none font-medium whitespace-nowrap [&_svg]:size-3.5 [&_svg]:shrink-0"
const rowLayout = "flex items-stretch gap-0.5 p-1 data-[orientation=vertical]:flex-col"
/* The control renders its own key (the indicator), like the tabs list does, so don't add a
SegmentedControlIndicator inside it. */
function SegmentedControl({
className,
children,
orientation,
...props
}: Omit<React.ComponentProps<typeof SegmentedControlRoot>, "children"> & {
children?: React.ReactNode
}) {
return (
<SegmentedControlRoot
orientation={orientation}
className={cn(
rowLayout,
"inline-flex w-fit rounded-[12px] bg-muted outline-none",
"shadow-[inset_0_1px_2px_rgb(0_0_0/0.07),inset_0_0_0_1px_rgb(0_0_0/0.035)]",
"dark:shadow-[inset_0_1px_2px_rgb(0_0_0/0.5),inset_0_0_0_1px_rgb(255_255_255/0.04)]",
"data-disabled:opacity-60",
className as string,
)}
{...props}
>
{children}
{/* The key: a raised surface that stretches toward the item you pick. */}
<SegmentedControlIndicator>
{/* A copy of the labels in the selected colour, clipped to the key. It is offset by the
key's own moving edges, so it stays still while the key passes over it. */}
<span className="absolute inset-0 overflow-hidden rounded-[inherit]">
<span
data-orientation={orientation ?? "horizontal"}
className={cn(
rowLayout,
"absolute text-foreground",
"top-[calc(min(var(--kf-ta),var(--kf-tb))*-1)] left-[calc(min(var(--kf-la),var(--kf-lb))*-1)]",
"h-[calc(var(--segmented-control-indicator-top,0px)+var(--segmented-control-indicator-height,0px)+var(--segmented-control-indicator-bottom,0px))]",
"w-[calc(var(--segmented-control-indicator-left,0px)+var(--segmented-control-indicator-width,0px)+var(--segmented-control-indicator-right,0px))]",
)}
>
{React.Children.map(children, (child) =>
React.isValidElement<{ children?: React.ReactNode }>(child) ? (
<span className={itemBox}>{child.props.children}</span>
) : null,
)}
</span>
</span>
</SegmentedControlIndicator>
</SegmentedControlRoot>
)
}
function SegmentedControlIndicator({
className,
...props
}: React.ComponentProps<typeof SegmentedControlIndicatorPrimitive>) {
return (
<SegmentedControlIndicatorPrimitive
aria-hidden
className={cn(
"z-10 rounded-[9px] bg-background",
"shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_1px_1px_rgb(0_0_0/0.05),0_3px_6px_-2px_rgb(0_0_0/0.1)]",
"dark:bg-accent dark:shadow-[0_0_0_1px_rgb(255_255_255/0.07),inset_0_1px_0_rgb(255_255_255/0.08),0_2px_6px_-1px_rgb(0_0_0/0.6)]",
className as string,
)}
{...props}
/>
)
}
function SegmentedControlItem({
className,
...props
}: React.ComponentProps<typeof SegmentedControlItemPrimitive>) {
return (
<SegmentedControlItemPrimitive
className={cn(
itemBox,
"relative cursor-pointer text-muted-foreground outline-none select-none",
"transition-[color,background-color,scale] motion-reduce:transition-none! hover:bg-foreground/[0.045] hover:text-foreground",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-muted",
"data-disabled:pointer-events-none data-disabled:opacity-40 data-readonly:cursor-default",
className as string,
)}
{...props}
/>
)
}
export { SegmentedControl, SegmentedControlIndicator, SegmentedControlItem }"use client"
/* Animated drop-in for `@ui-sdk/react/segmented-control`: same parts, same props. Swap the import and
keep your code. This file adds motion only. Colours, radii, shadows and spacing stay yours. */
import {
SegmentedControl as SegmentedControlRoot,
SegmentedControlIndicator as SegmentedControlIndicatorPart,
SegmentedControlItem as SegmentedControlItemPart,
} from "@ui-sdk/react/segmented-control"
import * as React from "react"
import { cn } from "@/lib/utils"
export * from "@ui-sdk/react/segmented-control"
/* 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)
}
function SegmentedControl({ className, ...props }: React.ComponentProps<typeof SegmentedControlRoot>) {
return <SegmentedControlRoot className={withClass("relative isolate", className)} {...props} />
}
function SegmentedControlItem({ className, ...props }: React.ComponentProps<typeof SegmentedControlItemPart>) {
return (
<SegmentedControlItemPart
className={withClass(
"transition-[scale] duration-[calc(200ms*var(--kf-speed,1))] ease-pop not-data-disabled:not-data-readonly:active:scale-[0.96] motion-reduce:transition-none!",
className,
)}
{...props}
/>
)
}
/* Stretch, the same recipe as the tabs primitive: every edge of the marker is tracked twice,
once quickly and once slowly with an 80ms lag, and each edge uses whichever of the two is
further out. The edge facing the direction of travel leads, the other follows, so the
marker stretches toward the item you pick and closes up there. The registered properties
are inherited, so anything inside the indicator can read the same moving edges. */
const stretchProperties = ["la", "lb", "ra", "rb", "ta", "tb", "ba", "bb"]
.map((n) => `@property --kf-${n}{syntax:"<length>";inherits:true;initial-value:0px}`)
.join("")
function SegmentedControlIndicator({
className,
...props
}: React.ComponentProps<typeof SegmentedControlIndicatorPart>) {
/* Hold the transitions until UI SDK has measured the checked item, so the marker is
simply there on load instead of sliding in from the corner. */
const [ready, setReady] = React.useState(false)
React.useEffect(() => {
const timer = setTimeout(() => setReady(true), 150)
return () => clearTimeout(timer)
}, [])
return (
<>
<style href="kf-stretch" precedence="default">
{stretchProperties}
</style>
<SegmentedControlIndicatorPart
data-ready={ready || undefined}
className={withClass(
cn(
"pointer-events-none absolute",
/* No box while nothing is checked. */
"[[data-slot=segmented-control]:not(:has([data-checked]))>&]:invisible",
"[--kf-la:var(--segmented-control-indicator-left,0px)] [--kf-lb:var(--segmented-control-indicator-left,0px)]",
"[--kf-ra:var(--segmented-control-indicator-right,0px)] [--kf-rb:var(--segmented-control-indicator-right,0px)]",
"[--kf-ta:var(--segmented-control-indicator-top,0px)] [--kf-tb:var(--segmented-control-indicator-top,0px)]",
"[--kf-ba:var(--segmented-control-indicator-bottom,0px)] [--kf-bb:var(--segmented-control-indicator-bottom,0px)]",
"top-[min(var(--kf-ta),var(--kf-tb))] right-[min(var(--kf-ra),var(--kf-rb))] bottom-[min(var(--kf-ba),var(--kf-bb))] left-[min(var(--kf-la),var(--kf-lb))]",
"motion-reduce:transition-none! data-ready:[transition:--kf-la_calc(260ms*var(--kf-speed,1))_var(--ease-house),--kf-ra_calc(260ms*var(--kf-speed,1))_var(--ease-house),--kf-ta_calc(260ms*var(--kf-speed,1))_var(--ease-house),--kf-ba_calc(260ms*var(--kf-speed,1))_var(--ease-house),--kf-lb_calc(380ms*var(--kf-speed,1))_var(--ease-house)_calc(80ms*var(--kf-speed,1)),--kf-rb_calc(380ms*var(--kf-speed,1))_var(--ease-house)_calc(80ms*var(--kf-speed,1)),--kf-tb_calc(380ms*var(--kf-speed,1))_var(--ease-house)_calc(80ms*var(--kf-speed,1)),--kf-bb_calc(380ms*var(--kf-speed,1))_var(--ease-house)_calc(80ms*var(--kf-speed,1))]",
"motion-reduce:[transition:none]!",
),
className,
)}
{...props}
/>
</>
)
}
export { SegmentedControl, SegmentedControlIndicator, SegmentedControlItem }Questions
How does the animated Segmented Control for UI SDK move?
Stretch: The selected key stretches across toward the new option, then closes up. Fade: Label colours flip exactly at the key's edge as it passes. Press: Options press in.
Do I need Motion or Framer Motion for the animated Segmented Control?
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/segmented-control instead. It has the same parts and props as @ui-sdk/react/segmented-control and adds motion only.
What happens to the Segmented Control when someone turns on reduced motion?
Nothing moves. State changes happen at once, or with a short fade.