Back to Base UIYou are on Code Block.

Code Block

A code surface that types itself in, copies cleanly and follows the caret.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
code-block.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/code-block.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/code-block.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
code-block.tsx
"use client";

import * as React from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { Check, Copy } from "lucide-react";
import { cn } from "@/lib/utils";

export type CodeBlockProps = {
  code: string;
  filename?: string;
  language?: string;
  typing?: boolean;
  className?: string;
};

export function CodeBlock({
  code,
  filename = "example.ts",
  language = "ts",
  typing = true,
  className,
}: CodeBlockProps) {
  const reduce = useReducedMotion();
  const [length, setLength] = React.useState(typing && !reduce ? 0 : code.length);
  const [copied, setCopied] = React.useState(false);

  React.useEffect(() => {
    if (!typing || reduce) {
      setLength(code.length);
      return;
    }
    setLength(0);
    const timer = window.setInterval(() => {
      setLength((value) => {
        if (value >= code.length) {
          window.clearInterval(timer);
          return value;
        }
        return value + 1;
      });
    }, 14);
    return () => window.clearInterval(timer);
  }, [code, reduce, typing]);

  React.useEffect(() => {
    if (!copied) return;
    const timer = window.setTimeout(() => setCopied(false), 2000);
    return () => window.clearTimeout(timer);
  }, [copied]);

  async function copy() {
    try {
      await navigator.clipboard.writeText(code);
    } catch {
      const area = document.createElement("textarea");
      area.value = code;
      document.body.appendChild(area);
      area.select();
      document.execCommand("copy");
      area.remove();
    }
    setCopied(true);
  }

  return (
    <div className={cn("overflow-hidden rounded-xl border bg-code", className)}>
      <div className="flex items-center justify-between border-b px-4 py-2">
        <span className="font-mono text-[13px] text-muted-foreground">{filename}</span>
        <button type="button" onClick={copy} aria-label={copied ? "Copied" : "Copy code"} className="relative flex size-7 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground">
          <AnimatePresence mode="popLayout" initial={false}>
            <motion.span key={copied ? "check" : "copy"} initial={{ opacity: 0, scale: 0.5, filter: "blur(2px)" }} animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }} exit={{ opacity: 0, scale: 0.5, filter: "blur(2px)" }} transition={{ duration: 0.15, ease: [0.23, 1, 0.32, 1] }} className="flex items-center justify-center">
              {copied ? <Check className="size-3.5" /> : <Copy className="size-3.5" />}
            </motion.span>
          </AnimatePresence>
        </button>
      </div>
      <pre className="overflow-auto p-4 font-mono text-[13px] leading-6">
        <code>
          {code.slice(0, length)}
          {length < code.length && <span className="animate-pulse">▍</span>}
        </code>
      </pre>
      <span className="sr-only">{language}</span>
    </div>
  );
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/code-block.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

code-block.tsx
import { CodeBlock } from "@/components/prequelui/code-block";

export default function Demo() {
  return (
    <CodeBlock>
      {/* compose the parts exactly like the underlying primitive */}
    </CodeBlock>
  );
}
import { CodeBlock } from "@/components/prequelui/code-block";

export function Variant() {
  return (
    <CodeBlock tempo={1.2}>
      …
    </CodeBlock>
  );
}
03

Animation

This primitive keeps the API and semantics of the underlying component and adds motion at the surface edges. Entrances are slower and more settled than exits, so closing never feels like waiting.

  • Springs for position, scale and size; tweens for colour and opacity.
  • The exit is shorter than the entrance, and keeps the current position as its starting point.
  • All measurements are taken again when content or font metrics change.
  • With reduced motion, the surface fades in place and never travels.
spring 300 / 30exit 150 msblur 4 pxheight 450 ms
04

API Reference

Every part takes exactly the props of the Code Block primitive. The animated surface adds only motion props, so the accessibility tree and keyboard behaviour stay the same.

Code Block

PropTypeDefault
childrenReactNode—
disabledbooleanfalse
transitionTransitionspring.press
classNamestring—

Next component

Collapsible

A single panel that opens from a separate trigger and closes its parent gap.