Code Block
Code with a file name, a copy button and optional line numbers.
Concept, not a real feature. Designed from Linear's code blocks in issue descriptions, its card ring and the round ghost icon button. Linear's own syntax colours weren't measured, so the token colours are muted guesses in its hues.
import { CodeBlock, Tooltip } from "@replicas/ui";
const code = `export async function POST(request: Request) {
const { messages } = await request.json();
const result = streamText({ model, messages });
return result.toUIMessageStreamResponse();
}`;
export default function CodeBlockDemo() {
return (
<CodeBlock.Root language="ts" style={{ width: "100%", maxWidth: 520 }}>
<CodeBlock.Header>
<CodeBlock.Title>app/api/chat/route.ts</CodeBlock.Title>
<CodeBlock.Actions>
<Tooltip content="Copy" side="top">
<CodeBlock.Copy value={code} />
</Tooltip>
</CodeBlock.Actions>
</CodeBlock.Header>
<CodeBlock.Content lineNumbers highlight>
{code}
</CodeBlock.Content>
</CodeBlock.Root>
);
}Prompt
Copy Prompt hands your agent everything it needs to build the Code Block in the Linear style: the measured tokens, the CSS and the structure.
Show Prompt
# Linear Code Block
Code with a file name, a copy button and optional line numbers.
Build a Code Block in this project that looks and behaves like Linear's. Measured from the running Linear app (linear.app). Keep every value exact. Map the tokens onto the project's own theme variables instead of hard-coding them. Use the project's own component library for behaviour, or Base UI primitives if it has none.
Fidelity: Concept, not a real feature. Designed from Linear's code blocks in issue descriptions, its card ring and the round ghost icon button. Linear's own syntax colours weren't measured, so the token colours are muted guesses in its hues.
## Tokens
| Token | Light | Dark |
| --- | --- | --- |
| `--border-secondary` | `lch(88.49% 0 282)` | `lch(14.16% 1.48 272)` |
| `--border-tertiary` | `lch(84.44% 0 282)` | `lch(16.32% 1.48 272)` |
| `--text-primary` | `lch(9.794% 0 282)` | `lch(100% 0 272)` |
| `--text-secondary` | `lch(19.588% 1.25 282)` | `lch(90.451% 1.2 272)` |
| `--text-tertiary` | `lch(39.176% 1.25 282)` | `lch(61.803% 1.2 272)` |
| `--text-quaternary` | `lch(64.64% 1.25 282)` | `lch(36.975% 1.2 272)` |
| `--bg-hover` | `lch(94.854% 0.5 282)` | `lch(14.006% 0.593 272)` |
| `--card-bg` | `lch(100% 0 282)` | `lch(9.345% 0.85 272)` |
| `--card-ring` | `lch(0% 0 0 / 0.088)` | `lch(100% 0 0 / 0.083)` |
| `--code-keyword` | `lch(45% 52 300)` | `lch(72% 38 300)` |
| `--code-string` | `lch(46% 38 145)` | `lch(74% 32 145)` |
| `--code-number` | `lch(52% 52 58)` | `lch(76% 38 62)` |
| `--code-function` | `lch(44% 48 270)` | `lch(74% 34 272)` |
| `--code-type` | `lch(47% 34 210)` | `lch(75% 26 210)` |
| `--status-green` | `lch(52% 48 152)` | `lch(70% 45 152)` |
| `--shadow-low` | `lch(0% 0 0 / 0.02) 0 3px 6px -2px, lch(0% 0 0 / 0.04) 0 1px 1px 0` | `lch(0% 0 0 / 0.3) 0 0.5px 1px 1px` |
| `--focus-ring` | `#5e69d1` | `#5e69d1` |
| `--font-monospace` | `"Berkeley Mono", "SFMono Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace` | `"Berkeley Mono", "SFMono Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace` |
| `--font-size-mini` | `0.75rem` | `0.75rem` |
| `--font-size-small` | `0.8125rem` | `0.8125rem` |
| `--font-weight-medium` | `500` | `500` |
| `--radius-rounded` | `9999px` | `9999px` |
| `--speed-highlight-fade-in` | `0s` | `0s` |
| `--speed-highlight-fade-out` | `0.15s` | `0.15s` |
## CSS
Scoped under `[data-style="linear"]`; dark rules sit under `[data-theme="dark"]`. The `ui-*` classes and data attributes match the structure below.
```css
[data-style="linear"] {
.ui-code-block {
box-sizing: border-box;
display: flex;
flex-direction: column;
min-width: 0;
overflow: hidden;
border-radius: 10px;
background-color: var(--card-bg);
box-shadow:
0 0 0 0.5px var(--card-ring),
var(--shadow-low);
color: var(--text-secondary);
font-family: inherit;
font-size: var(--font-size-small);
}
.ui-code-block-header {
box-sizing: border-box;
display: flex;
align-items: center;
gap: 8px;
min-height: 40px;
padding: 0 6px 0 14px;
border-bottom: 0.5px solid var(--border-secondary);
}
.ui-code-block-title {
display: flex;
flex: 1;
align-items: center;
gap: 8px;
min-width: 0;
overflow: hidden;
color: var(--text-primary);
font-weight: var(--font-weight-medium);
text-overflow: ellipsis;
white-space: nowrap;
&::before {
content: "";
width: 14px;
height: 14px;
flex-shrink: 0;
background-color: var(--text-tertiary);
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 2.5h5l3 3v8a.5.5 0 0 1-.5.5h-7.5a.5.5 0 0 1-.5-.5v-10.5a.5.5 0 0 1 .5-.5zM9 2.5v3h3' fill='none' stroke='black' stroke-width='1.25' stroke-linejoin='round'/%3E%3C/svg%3E")
center / contain no-repeat;
}
}
.ui-code-block-actions {
display: flex;
flex-shrink: 0;
align-items: center;
gap: 2px;
margin-left: auto;
}
.ui-code-block-copy {
box-sizing: border-box;
display: inline-flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
margin: 0;
padding: 0;
border: none;
border-radius: var(--radius-rounded);
background-color: transparent;
color: var(--text-tertiary);
cursor: default;
transition:
background-color var(--speed-highlight-fade-out),
color var(--speed-highlight-fade-out);
& svg {
width: 15px;
height: 15px;
}
@media (hover: hover) {
&:hover {
background-color: var(--bg-hover);
color: var(--text-primary);
transition-duration: var(--speed-highlight-fade-in);
}
}
&:focus-visible {
outline: 1px solid var(--focus-ring);
outline-offset: 1px;
}
&[data-copied] {
color: var(--status-green);
}
}
.ui-code-block-content {
box-sizing: border-box;
margin: 0;
padding: 10px 0 12px;
overflow: auto;
color: var(--text-secondary);
font-family: var(--font-monospace);
font-size: var(--font-size-mini);
line-height: 20px;
white-space: pre;
tab-size: 2;
counter-reset: linear-code-line;
outline: none;
scrollbar-width: thin;
scrollbar-color: var(--border-tertiary) transparent;
& code {
display: block;
min-width: max-content;
padding: 0 16px;
font: inherit;
}
&[data-line-numbers] code {
padding-left: 0;
}
&[data-wrap] {
white-space: pre-wrap;
& code {
min-width: 0;
}
}
&:focus-visible {
box-shadow: inset 0 0 0 1px var(--focus-ring);
}
}
.ui-code-block-token {
&[data-token="keyword"] {
color: var(--code-keyword);
}
&[data-token="string"] {
color: var(--code-string);
}
&[data-token="comment"] {
color: var(--text-quaternary);
font-style: italic;
}
&[data-token="number"] {
color: var(--code-number);
}
&[data-token="function"] {
color: var(--code-function);
}
&[data-token="type"] {
color: var(--code-type);
}
}
.ui-code-block-line {
display: block;
&::before {
counter-increment: linear-code-line;
content: counter(linear-code-line);
display: inline-block;
width: 28px;
margin-right: 16px;
color: var(--text-quaternary);
text-align: right;
user-select: none;
}
}
}
```
## Structure
How Replicas assembles it on Base UI.
```tsx
import { Button as BaseButton } from "@base-ui/react/button";
import { type ComponentProps, Fragment, type ReactNode } from "react";
import { join, useCopy } from "./utils";
function Root({ className, language, ...props }: ComponentProps<"div"> & { language?: string }) {
return <div className={join("ui-code-block", className)} data-language={language} {...props} />;
}
function Header({ className, ...props }: ComponentProps<"div">) {
return <div className={join("ui-code-block-header", className)} {...props} />;
}
function Title({ className, ...props }: ComponentProps<"span">) {
return <span className={join("ui-code-block-title", className)} {...props} />;
}
function Actions({ className, ...props }: ComponentProps<"div">) {
return <div className={join("ui-code-block-actions", className)} {...props} />;
}
type CopyProps = Omit<BaseButton.Props, "className" | "value"> & { className?: string; value: string };
function Copy({ className, value, onClick, ...props }: CopyProps) {
const { copied, copy } = useCopy(value);
return (
<BaseButton
className={join("ui-code-block-copy", className)}
aria-label={copied ? "Copied" : "Copy code"}
data-copied={copied || undefined}
onClick={(event) => {
onClick?.(event);
copy();
}}
{...props}
>
{copied ? (
<svg viewBox="0 0 16 16" fill="none" aria-hidden>
<path d="M4 8.5l2.5 2.5L12 5.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
) : (
<svg viewBox="0 0 16 16" fill="none" aria-hidden>
<rect x="5.5" y="5.5" width="7.5" height="7.5" rx="1.5" stroke="currentColor" strokeWidth="1.25" />
<path d="M10.5 3.5V3a1 1 0 0 0-1-1H4a1 1 0 0 0-1 1v5.5a1 1 0 0 0 1 1h.5" stroke="currentColor" strokeWidth="1.25" />
</svg>
)}
</BaseButton>
);
}
const keywords = new Set(
"as async await break case catch class const continue default delete do else export extends false finally for from function if import in instanceof interface let new null of return static super switch this throw true try type typeof undefined var void while with yield".split(
" ",
),
);
const pattern =
/(\/\/.*$|\/\*.*?\*\/)|("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`)|(\b\d[\d_]*(?:\.\d+)?\b)|([A-Za-z_$][\w$]*)(?=\s*\()|([A-Za-z_$][\w$]*)/g;
function highlight(line: string): ReactNode[] {
const nodes: ReactNode[] = [];
let last = 0;
for (const match of line.matchAll(pattern)) {
const [text, comment, string, number, call, word] = match;
const index = match.index ?? 0;
const token = comment
? "comment"
: string
? "string"
: number
? "number"
: call && !keywords.has(call)
? "function"
: keywords.has(call ?? word)
? "keyword"
: /^[A-Z]/.test(word ?? "")
? "type"
: undefined;
if (!token) continue;
if (index > last) nodes.push(line.slice(last, index));
nodes.push(
<span key={index} className="ui-code-block-token" data-token={token}>
{text}
</span>,
);
last = index + text.length;
}
if (last < line.length) nodes.push(line.slice(last));
return nodes;
}
type ContentProps = Omit<ComponentProps<"pre">, "children"> & {
children: string;
lineNumbers?: boolean;
wrap?: boolean;
highlight?: boolean;
};
function Content({ className, children, lineNumbers = false, wrap = false, highlight: tokens = false, ...props }: ContentProps) {
const render = (line: string) => (tokens ? highlight(line) : line);
return (
<pre
className={join("ui-code-block-content", className)}
data-line-numbers={lineNumbers || undefined}
data-wrap={wrap || undefined}
tabIndex={0}
{...props}
>
<code>
{lineNumbers
? children.split("\n").map((line, index) => (
<span key={index} className="ui-code-block-line">
{render(line)}
{"\n"}
</span>
))
: tokens
? children.split("\n").map((line, index) => (
<Fragment key={index}>
{index ? "\n" : null}
{render(line)}
</Fragment>
))
: children}
</code>
</pre>
);
}
export const CodeBlock = {
Root,
Header,
Title,
Actions,
Copy,
Content,
};
```
## Example
```tsx
import { CodeBlock, Tooltip } from "@replicas/ui";
const code = `export async function POST(request: Request) {
const { messages } = await request.json();
const result = streamText({ model, messages });
return result.toUIMessageStreamResponse();
}`;
export default function CodeBlockDemo() {
return (
<CodeBlock.Root language="ts" style={{ width: "100%", maxWidth: 520 }}>
<CodeBlock.Header>
<CodeBlock.Title>app/api/chat/route.ts</CodeBlock.Title>
<CodeBlock.Actions>
<Tooltip content="Copy" side="top">
<CodeBlock.Copy value={code} />
</Tooltip>
</CodeBlock.Actions>
</CodeBlock.Header>
<CodeBlock.Content lineNumbers highlight>
{code}
</CodeBlock.Content>
</CodeBlock.Root>
);
}
```
## Foundations
The rest of the Linear style, as a skill: https://replicas.raphaelsalaja.com/skills/linear/SKILL.md