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.See this component the way an AI agent does.
Installation
npx shadcn@latest add @keyframery/code-blockThis 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
└── CodeBlockContentExamples
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
| Move | What you see |
|---|---|
| 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. |
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.
| Variable | What it changes | Default |
|---|---|---|
--code-block-face | Code sheet | bg-card |
--code-block-ink | Code text | text-card-foreground |
--code-block-muted | Comments and hints | text-muted-foreground |
--code-block-radius | Sheet corners | rounded-[14px] |
--code-block-font | Code size | text-[12.5px] |
--code-block-inset-x | Line padding | px-3.5 |
--code-block-inset-y | Code padding | py-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
| Key | What it does |
|---|---|
| Tab | Moves focus to the code, then to the copy button. |
| ← → ↑ ↓ | Scroll the code while it has focus. |
| Enter or Space | Copies 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.