# Animated Toolbar for UI SDK (/docs/components/toolbar)

One highlight springs between the toolbar's buttons, and presses pop back.

```tsx title="example.tsx"
import * as React from "react"

import {
  Toolbar,
  ToolbarButton,
  ToolbarSeparator,
  ToolbarToggle,
  ToolbarToggleGroup,
} from "@/components/keyframery/toolbar"
import { cn } from "@/lib/utils"

function Icon({ d }: { d: string }) {
  return (
    <svg
      viewBox="0 0 16 16"
      fill="none"
      stroke="currentColor"
      strokeWidth="1.5"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden
    >
      <path d={d} />
    </svg>
  )
}

const alignments = [
  { value: "left", label: "Align left", d: "M2.75 4h10.5M2.75 8h6.5M2.75 12h8.5" },
  { value: "center", label: "Align center", d: "M2.75 4h10.5M4.75 8h6.5M3.75 12h8.5" },
  { value: "right", label: "Align right", d: "M2.75 4h10.5M6.75 8h6.5M4.75 12h8.5" },
]

export default function ToolbarDemo() {
  const [styles, setStyles] = React.useState(["bold"])
  const [align, setAlign] = React.useState("left")

  return (
    <div className="relative mb-5 w-[400px]">
      <article className="rounded-[14px] bg-card px-6 pt-5 pb-10 text-card-foreground shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_1px_2px_rgb(0_0_0/0.04)] dark:shadow-[0_0_0_1px_rgb(255_255_255/0.07)]">
        <h3 className="text-[15px] font-semibold tracking-tight">Allotment, Saturday</h3>
        <p className="mt-1 text-[12px] text-muted-foreground">Edited just now</p>
        <p
          className={cn(
            "mt-4 text-[14px] leading-[1.7]",
            align === "center" && "text-center",
            align === "right" && "text-right",
          )}
        >
          Seed potatoes in by ten.{" "}
          <span
            className={cn(
              "rounded-[3px] bg-ring/20 box-decoration-clone px-0.5 py-0.5",
              styles.includes("bold") && "font-semibold",
              styles.includes("italic") && "italic",
              styles.includes("underline") && "underline decoration-[1.5px] underline-offset-[3px]",
            )}
          >
            Net the brassicas before the pigeons find them.
          </span>{" "}
          Take the long twine.
        </p>
      </article>

      <Toolbar aria-label="Formatting" className="absolute -bottom-5 left-1/2 -translate-x-1/2">
        <ToolbarButton aria-label="Undo">
          <Icon d="M5.75 3.75l-3 3 3 3M3 6.75h6.5a3.25 3.25 0 010 6.5h-2" />
        </ToolbarButton>
        <ToolbarButton aria-label="Redo" disabled>
          <Icon d="M10.25 3.75l3 3-3 3M13 6.75H6.5a3.25 3.25 0 000 6.5h2" />
        </ToolbarButton>
        <ToolbarSeparator />
        <ToolbarToggleGroup multiple value={styles} onValueChange={setStyles} aria-label="Text style">
          <ToolbarToggle value="bold" aria-label="Bold">
            <span className="font-bold">B</span>
          </ToolbarToggle>
          <ToolbarToggle value="italic" aria-label="Italic">
            <span className="italic">I</span>
          </ToolbarToggle>
          <ToolbarToggle value="underline" aria-label="Underline">
            <span className="underline decoration-[1.5px] underline-offset-[3px]">U</span>
          </ToolbarToggle>
        </ToolbarToggleGroup>
        <ToolbarSeparator />
        <ToolbarToggleGroup
          value={[align]}
          onValueChange={([next]) => next && setAlign(next)}
          deselectable={false}
          aria-label="Alignment"
        >
          {alignments.map((a) => (
            <ToolbarToggle key={a.value} value={a.value} aria-label={a.label}>
              <Icon d={a.d} />
            </ToolbarToggle>
          ))}
        </ToolbarToggleGroup>
        <ToolbarSeparator />
        <ToolbarButton className="px-2" data-play="link">
          <Icon d="M6.9 9.1a2.6 2.6 0 003.7 0l1.9-1.9a2.6 2.6 0 00-3.7-3.7l-.7.7M9.1 6.9a2.6 2.6 0 00-3.7 0L3.5 8.8a2.6 2.6 0 003.7 3.7l.7-.7" />
          Link
        </ToolbarButton>
      </Toolbar>
    </div>
  )
}
```

## Installation [#installation]

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

This adds `components/keyframery/toolbar.tsx` and its primitive, `components/keyframery/primitives/toolbar.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 {
  Toolbar,
  ToolbarButton,
  ToolbarGroup,
  ToolbarInput,
  ToolbarLink,
  ToolbarSeparator,
  ToolbarToggle,
  ToolbarToggleGroup,
} from "@/components/keyframery/toolbar"
```

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

## What moves [#what-moves]

| Move      | What you see                                                                         |
| --------- | ------------------------------------------------------------------------------------ |
| **Glide** | One highlight springs between the toolbar's buttons, following pointer and keyboard. |
| **Press** | Buttons press in with a pop.                                                         |

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 toolbar 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/toolbar"

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

## Parts [#parts]

The finished component exports 8 parts:

`Toolbar`, `ToolbarButton`, `ToolbarGroup`, `ToolbarInput`, `ToolbarLink`, `ToolbarSeparator`, `ToolbarToggle`, `ToolbarToggleGroup`

## Accessibility [#accessibility]

Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the toolbar 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/toolbar.tsx**

```tsx
"use client"

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

import * as React from "react"

import {
  Toolbar as ToolbarRoot,
  ToolbarButton as ToolbarButtonPart,
  ToolbarGroup as ToolbarGroupPart,
  ToolbarInput as ToolbarInputPart,
  ToolbarLink as ToolbarLinkPart,
  ToolbarSeparator as ToolbarSeparatorPart,
  ToolbarToggle as TogglePart,
  ToolbarToggleGroup as ToggleGroupPart,
} from "@/components/keyframery/primitives/toolbar"
import { cn } from "@/lib/utils"

/* One box for everything you can press in the bar: buttons, links and toggles. */
const item = cn(
  "relative inline-flex h-7 min-w-7 shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-[8px] px-1.5",
  "text-[13px] leading-none font-medium whitespace-nowrap text-muted-foreground outline-none select-none",
  "[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
  "not-data-disabled:hover:text-foreground data-kf-toolbar-hl:not-data-disabled:text-foreground",
  "focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-popover",
  "data-disabled:cursor-not-allowed data-disabled:opacity-40",
)

const row =
  "flex items-center gap-0.5 data-[orientation=vertical]:flex-col data-[orientation=vertical]:items-stretch"

function Toolbar({
  className,
  ...props
}: React.ComponentProps<typeof ToolbarRoot>) {
  return (
    <ToolbarRoot
      className={cn(
        row,
        "w-fit rounded-[12px] bg-popover p-1 text-popover-foreground",
        /* The gliding highlight. */
        "before:rounded-[8px] before:bg-accent dark:before:bg-accent/70",
        "shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_2px_6px_-2px_rgb(0_0_0/0.08),0_12px_32px_-8px_rgb(0_0_0/0.18)]",
        "dark:shadow-[0_0_0_1px_rgb(255_255_255/0.08),0_12px_32px_-8px_rgb(0_0_0/0.7)]",
        className,
      )}
      {...props}
    />
  )
}

function ToolbarButton({
  className,
  ...props
}: React.ComponentProps<typeof ToolbarButtonPart>) {
  return (
    <ToolbarButtonPart
      className={cn(item, className)}
      {...props}
    />
  )
}

function ToolbarLink({
  className,
  ...props
}: React.ComponentProps<typeof ToolbarLinkPart>) {
  return (
    <ToolbarLinkPart
      className={cn(item, "px-2", className)}
      {...props}
    />
  )
}

/* A toggle joins the bar's arrow keys on its own. Pressed, it becomes a key. */
function ToolbarToggle({
  className,
  ...props
}: React.ComponentProps<typeof TogglePart>) {
  return (
    <TogglePart
      className={cn(
        item,
        "data-pressed:bg-background data-pressed:text-foreground",
        "data-pressed: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:data-pressed:bg-accent dark:data-pressed: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,
      )}
      {...props}
    />
  )
}

function ToolbarToggleGroup<T extends string = string>({
  className,
  ...props
}: React.ComponentProps<typeof ToggleGroupPart<T>>) {
  return <ToggleGroupPart className={cn(row, className)} {...props} />
}

function ToolbarGroup({
  className,
  ...props
}: React.ComponentProps<typeof ToolbarGroupPart>) {
  return <ToolbarGroupPart className={cn(row, className)} {...props} />
}

function ToolbarInput({
  className,
  ...props
}: React.ComponentProps<typeof ToolbarInputPart>) {
  return (
    <ToolbarInputPart
      className={cn(
        "h-7 min-w-0 rounded-[8px] bg-background px-2 text-[13px] text-foreground outline-none placeholder:text-muted-foreground",
        "shadow-[0_0_0_1px_rgb(0_0_0/0.09),0_1px_2px_rgb(0_0_0/0.05)] dark:shadow-[0_0_0_1px_rgb(255_255_255/0.1)]",
        "transition-shadow duration-[calc(150ms*var(--kf-speed,1))] motion-reduce:transition-none!",
        "not-data-disabled:hover:shadow-[0_0_0_1px_rgb(0_0_0/0.16),0_1px_2px_rgb(0_0_0/0.05)] dark:not-data-disabled:hover:shadow-[0_0_0_1px_rgb(255_255_255/0.18)]",
        "focus-visible:shadow-[0_0_0_1px_var(--ring)] focus-visible:ring-[3px] focus-visible:ring-ring/25",
        "dark:focus-visible:shadow-[0_0_0_1px_var(--ring)]",
        "data-disabled:cursor-not-allowed data-disabled:opacity-50",
        className,
      )}
      {...props}
    />
  )
}

/* The line runs across the bar, so it stands upright in a horizontal one. */
function ToolbarSeparator({
  className,
  ...props
}: React.ComponentProps<typeof ToolbarSeparatorPart>) {
  return (
    <ToolbarSeparatorPart
      className={cn(
        "shrink-0 bg-border",
        "data-[orientation=vertical]:mx-1 data-[orientation=vertical]:h-4 data-[orientation=vertical]:w-px",
        "data-[orientation=horizontal]:my-1 data-[orientation=horizontal]:h-px data-[orientation=horizontal]:w-4 data-[orientation=horizontal]:self-center",
        className,
      )}
      {...props}
    />
  )
}

export {
  Toolbar,
  ToolbarButton,
  ToolbarGroup,
  ToolbarInput,
  ToolbarLink,
  ToolbarSeparator,
  ToolbarToggle,
  ToolbarToggleGroup,
}
```

**components/keyframery/primitives/toolbar.tsx**

```tsx
"use client"

/* Animated drop-in for `@ui-sdk/react/toolbar`: same parts, same props. Swap the import and keep your code.
   This file adds motion only. Colours, radii, shadows and spacing stay yours.
   - Glide: one highlight springs from item to item under the pointer or keyboard focus, instead of
     each item lighting up on its own. It is the toolbar's `::before`; give it a colour and radius with
     `before:` classes. The default follows the text colour.
   - Press: items give under the pointer and spring back with a small pop.
   `ToolbarToggle` and `ToolbarToggleGroup` are extra: `@ui-sdk/react/toggle` parts with the same press. */

import { Toggle as TogglePart, ToggleGroup as ToggleGroupPart } from "@ui-sdk/react/toggle"
import {
  Toolbar as ToolbarRoot,
  ToolbarButton as ToolbarButtonPart,
  ToolbarLink as ToolbarLinkPart,
} from "@ui-sdk/react/toolbar"
import * as React from "react"

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

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

/* Everything the highlight can rest on. Inputs keep their own field look. */
const ITEM =
  '[data-slot="toolbar-button"],[data-slot="toolbar-link"],[data-slot="toggle"]'
const MARK = "data-kf-toolbar-hl"

/* Colours and shadows ease as the pointer arrives; the press springs back with a pop. */
const press = cn(
  "[transition:color_calc(150ms*var(--kf-speed,1))_var(--ease-house),background-color_calc(150ms*var(--kf-speed,1))_var(--ease-house),box-shadow_calc(150ms*var(--kf-speed,1))_var(--ease-house),scale_calc(260ms*var(--kf-speed,1))_var(--ease-pop)]",
  "not-data-disabled:active:scale-[0.92] not-data-disabled:active:[transition-duration:calc(100ms*var(--kf-speed,1))]",
  "motion-reduce:[transition:none]!",
)

/* Moves the mark to `item`, or hides the highlight where it is (so it can glide on from there). */
function mark(root: HTMLElement, item: Element | null) {
  if (!item || item.hasAttribute("data-disabled")) {
    root.setAttribute("data-kf-idle", "")
    return
  }
  for (const old of root.querySelectorAll(`[${MARK}]`)) if (old !== item) old.removeAttribute(MARK)
  item.setAttribute(MARK, "")
  root.removeAttribute("data-kf-idle")
  /* The first time, the highlight appears in place; from then on it glides. */
  if (!root.hasAttribute("data-kf-glide")) setTimeout(() => root.setAttribute("data-kf-glide", ""), 40)
}

function itemFrom(root: HTMLElement, target: EventTarget | null) {
  const item = target instanceof Element ? target.closest(ITEM) : null
  return item && root.contains(item) ? item : null
}

function Toolbar({
  className,
  onPointerOver,
  onPointerLeave,
  onFocus,
  onBlur,
  onClick,
  ...props
}: React.ComponentProps<typeof ToolbarRoot>) {
  const hovering = React.useRef(false)
  return (
    <ToolbarRoot
      className={cn(
        "relative isolate [anchor-scope:--kf-toolbar-hl]",
        "[&_[data-kf-toolbar-hl]]:[anchor-name:--kf-toolbar-hl]",
        "before:pointer-events-none before:absolute before:-z-10 before:bg-[color-mix(in_oklch,currentColor_9%,transparent)]",
        "before:[position-anchor:--kf-toolbar-hl] before:top-[anchor(top)] before:right-[anchor(right)] before:bottom-[anchor(bottom)] before:left-[anchor(left)]",
        "before:scale-100 before:opacity-100 before:transition-[opacity,scale] before:duration-[calc(200ms*var(--kf-speed,1))] before:ease-house",
        "data-kf-glide:before:[transition:top_calc(360ms*var(--kf-speed,1))_var(--ease-spring),right_calc(360ms*var(--kf-speed,1))_var(--ease-spring),bottom_calc(360ms*var(--kf-speed,1))_var(--ease-spring),left_calc(360ms*var(--kf-speed,1))_var(--ease-spring),opacity_calc(200ms*var(--kf-speed,1))_var(--ease-house),scale_calc(200ms*var(--kf-speed,1))_var(--ease-house)]",
        "data-kf-idle:before:scale-[0.92] data-kf-idle:before:opacity-0 not-has-[[data-kf-toolbar-hl]]:before:opacity-0",
        "motion-reduce:before:[transition:none]!",
        className,
      )}
      onPointerOver={(event) => {
        onPointerOver?.(event)
        hovering.current = true
        const item = itemFrom(event.currentTarget, event.target)
        if (item) mark(event.currentTarget, item)
      }}
      onPointerLeave={(event) => {
        onPointerLeave?.(event)
        hovering.current = false
        const root = event.currentTarget
        mark(root, itemFrom(root, root.querySelector(":focus-visible")))
      }}
      onFocus={(event) => {
        onFocus?.(event)
        const target = event.target as Element
        if (target.matches(":focus-visible")) mark(event.currentTarget, itemFrom(event.currentTarget, target))
      }}
      onBlur={(event) => {
        onBlur?.(event)
        const root = event.currentTarget
        if (!hovering.current && !root.contains(event.relatedTarget as Node | null)) mark(root, null)
      }}
      onClick={(event) => {
        onClick?.(event)
        const item = itemFrom(event.currentTarget, event.target)
        if (item) mark(event.currentTarget, item)
      }}
      {...props}
    />
  )
}

function ToolbarButton({ className, ...props }: React.ComponentProps<typeof ToolbarButtonPart>) {
  return <ToolbarButtonPart className={cn(press, className)} {...props} />
}

function ToolbarLink({ className, ...props }: React.ComponentProps<typeof ToolbarLinkPart>) {
  return <ToolbarLinkPart className={cn(press, className)} {...props} />
}

/* A toggle joins the toolbar's arrow keys on its own. Pressed, its surface pops in. */
function ToolbarToggle({ className, ...props }: React.ComponentProps<typeof TogglePart>) {
  return <TogglePart className={cn(press, className)} {...props} />
}

function ToolbarToggleGroup<T extends string = string>({
  className,
  ...props
}: React.ComponentProps<typeof ToggleGroupPart<T>>) {
  return <ToggleGroupPart className={cn(className)} {...props} />
}

export { Toolbar, ToolbarButton, ToolbarLink, ToolbarToggle, ToolbarToggleGroup }
```

## Questions

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

Glide: One highlight springs between the toolbar's buttons, following pointer and keyboard. Press: Buttons press in with a pop.

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

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/toolbar` instead. It has the same parts and props as `@ui-sdk/react/toolbar` and adds motion only.

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

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