Code Block

Syntax-highlighted code block component with integrated copy button and standalone or attached card variants.

// Plain markup
import { CodeBlock } from "@shavin/ui";

Import snippet

import { Button } from "@shavin/ui";
Component preview
import { CodeBlock } from "@shavin/ui";

<CodeBlock code={source} />

Multi-line Code Block

function Welcome({ name }: { name: string }) {
  return (
    <Card>
      <Heading>Hello, {name}</Heading>
      <Text>Welcome back to the dashboard.</Text>
    </Card>
  );
}
Component preview
import { CodeBlock } from "@shavin/ui";

<CodeBlock code={source} />

Wrapping long lines

const message = "This is a deliberately long single line of code that would otherwise scroll horizontally, but with the wrap prop it breaks onto multiple lines instead.";
Component preview
<CodeBlock wrap code={longLine} />

Without the Copy button

export const version = "1.0.0";
Component preview
<CodeBlock copyable={false} code={source} />

2-Column Side-by-Side Split

Horizontal split displaying live rendered preview on one side and syntax-highlighted code on the other.

<Card className="p-4 flex flex-col gap-2">
  <span className="text-xs font-semibold text-fg">Active Session</span>
  <span className="text-xs text-fg-subtle">Last synced 2m ago</span>
</Card>
Active SessionLast synced 2m ago
Horizontal split displaying live rendered preview on one side and syntax-highlighted code on the other.
<CodeBlock
  split="horizontal"
  previewPosition="right"
  code={`<Card className="p-4 flex flex-col gap-2">
  <span className="text-xs font-semibold text-fg">Active Session</span>
  <span className="text-xs text-fg-subtle">Last synced 2m ago</span>
</Card>`}
>
  <Card className="p-4 flex flex-col gap-2 w-full max-w-xs shadow-sm">
    <span className="text-xs font-semibold text-fg">Active Session</span>
    <span className="text-xs text-fg-subtle">Last synced 2m ago</span>
  </Card>
</CodeBlock>

Collapsible Preview Drawer

Component preview card with a description footer and an animated source code drawer.

Publish controls with split preview and primary action
<Flex gap="2" align="center">
  <Button variant="outline" size="sm">Preview</Button>
  <Button variant="solid" size="sm">Publish</Button>
</Flex>
Component preview card with a description footer and an animated source code drawer.
<CodeBlock
  collapsible
  description="Publish controls with split preview and primary action"
  code={`<Flex gap="2" align="center">
  <Button variant="outline" size="sm">Preview</Button>
  <Button variant="solid" size="sm">Publish</Button>
</Flex>`}
>
  <Flex gap="2" align="center">
    <Button variant="outline" size="sm">Preview</Button>
    <Button variant="solid" size="sm">Publish</Button>
  </Flex>
</CodeBlock>

Props

PropTypeDefaultDescription
code*stringSource code string to highlight.
variant"standalone" | "attached"Layout mode: standalone (independent box) or attached (anchored below component preview). Default 'standalone'.
split"vertical" | "horizontal"Split orientation when pairing code with a live preview children: 'vertical' (stacked) or 'horizontal' (2-column side-by-side). Default 'vertical'.
previewPosition"left" | "right" | "top" | "bottom"Position of the preview relative to the code block: 'left' | 'right' | 'top' | 'bottom'. Default 'right' when split='horizontal', 'top' when split='vertical'.
childrenReactNodeOptional live preview component rendered alongside the code block.
previewClassNamestringOptional class name for the preview container.
wrapbooleanWhether code wraps lines automatically (whitespace-pre-wrap). Default false.
copyablebooleanShow one-click Copy button overlay. Default true.
collapsiblebooleanWhether the code drawer is collapsible with a View/Hide code button below the preview. Default false.
defaultOpenbooleanDefault open state when collapsible is enabled. Default false.
openbooleanControlled open state when collapsible is enabled.
onOpenChange(open: boolean) => voidCallback fired when collapsible open state changes.
descriptionstringOptional label or description displayed in the collapsible footer bar.
titlestringOptional title displayed in the collapsible footer bar (alias for description).
classNamestringCustom wrapper class name.

* required

Configure Theme
Theme
Accent Color
Custom
Gray Family
Appearance
Radius
Scaling
Panel Style
Heading Font
Body Font