Keyframery

Code Block

Lines cascade in from the left, a highlighted line's band sweeps across and follows the highlight when it moves, and copying pops a tick that draws itself.

See it as an AI agent does.

Resize preview. Focus the handle and use the left or right arrow keys. Hold Shift for larger steps.— px
Customize1 controls
Animation speed1×

Installation

npx shadcn@latest add @keyframery/code-block

This adds components/keyframery/code-block.tsx and its primitive to your project, and installs UI SDK. The motion tokens are added to your global CSS the first time you install a component.

Usage

import { CodeBlock, CodeBlockContent, CodeBlockCopyButton, CodeBlockHeader, CodeBlockTitle } from "@/components/keyframery/code-block"

export default function Usage() {
  return (
    <CodeBlock code={`npm install @ui-sdk/react @ui-sdk/core`} language="bash">
      <CodeBlockHeader>
        <CodeBlockTitle>Terminal</CodeBlockTitle>
        <CodeBlockCopyButton className="ms-auto" />
      </CodeBlockHeader>
      <CodeBlockContent />
    </CodeBlock>
  )
}

Anatomy

How the parts fit together:

CodeBlock
├── CodeBlockHeader
│   ├── CodeBlockTitle
│   └── CodeBlockCopyButton
└── CodeBlockContent

Examples

Line numbers and highlighted lines

lineNumbers numbers each line; highlight takes line numbers, and their bands sweep in from the left.

A diff

Mark lines with added and removed. Each gets a tint and a + or − in the gutter, so the change never rests on colour alone.

Floating copy button

Put CodeBlockActions straight inside CodeBlock and the copy key floats over the corner, showing on hover or focus.

Motion

MoveWhat you see
CascadeThe lines arrive one after another from the left.
DrawA highlighted line's band sweeps in from the left; the old band shrinks back as the highlight moves.
PopOn copy, the copy icon blurs away and a tick pops in and draws itself.
PressThe copy key sinks and springs back.

Timing and springiness follow the motion style you set. To change how fast this code block moves, pass speed:

<CodeBlock speed="fast">
  …
</CodeBlock>

"fast" is 0.6×, "slow" 1.4×, and a number sets the multiplier. With reduced motion turned on, nothing moves: changes happen at once or with a short fade.

Theming

Set these CSS variables on the component or a parent. Leave them unset to follow the light and dark theme. The open Customize panel below the preview lets you try these settings.

VariableWhat it changesDefault
--code-block-faceCode sheetbg-card
--code-block-inkCode texttext-card-foreground
--code-block-mutedComments and hintstext-muted-foreground
--code-block-radiusSheet cornersrounded-[14px]
--code-block-fontCode sizetext-[12.5px]
--code-block-inset-xLine paddingpx-3.5
--code-block-inset-yCode paddingpy-3

API reference

Every part takes className and children, along with the props of the element it renders. The parts are rendered by UI SDK, so they also take render, to draw your own element, and a function of their state for className and style.

CodeBlock

Renders a <div>, and takes its props too.

Prop

Type

CodeBlockLine

Renders a <span>, and takes its props too.

Prop

Type

Other parts

These take the props of the element they render, and nothing else:

  • CodeBlockHeader: a <div>
  • CodeBlockTitle: a <div>
  • CodeBlockCopyButton: a <button>
  • CodeBlockContent: a <pre>
  • CodeBlockActions: a <div>
  • CodeBlockFooter: a <div>

Keyboard

KeyWhat it does
TabMoves focus to the code, then to the copy button.
← → ↑ ↓Scroll the code while it has focus.
Enter or SpaceCopies the code from the copy button.

Accessibility

Roles, keyboard and focus are declared on the component's UI SDK primitive, and state is announced through the matching ARIA attributes. More in Accessibility.

Built on UI SDK's core

UI SDK doesn't ship a code block, so this one is defined with its core: one definePrimitive in components/keyframery/core/code-block-core.ts, with no React in it. UI SDK's adapters can render that same definition in Vue, Svelte, Solid and web components. To keep your own styling and take only the motion, import from @/components/keyframery/primitives/code-block.

Questions

How does the animated Code Block for UI SDK move?

Cascade: The lines arrive one after another from the left. Draw: A highlighted line's band sweeps in from the left; the old band shrinks back as the highlight moves. Pop: On copy, the copy icon blurs away and a tick pops in and draws itself. Press: The copy key sinks and springs back.

Do I need Motion or Framer Motion for the animated Code Block?

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

UI SDK has no code block. How is this one built on UI SDK?

Its behaviour is one definition written with UI SDK's core (definePrimitive), in components/keyframery/core/code-block-core.ts. Its parts are rendered by UI SDK's adapter, so they take render, state-based class names and data-slot, like every UI SDK component.

What happens to the Code Block when someone turns on reduced motion?

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

All components

On this page