CodeBlock
Code to read and copy, with its file or its language in the bar. It is the same box as the CodeBlock of the docs, so code looks alike everywhere.
import { CodeBlock, CodeBlockSkeleton } from '@sypnia/ui/react'
A file, with a lit line
Line numbers and a lit line. Copy takes the code without the numbers.
import { createClient } from '@sypnia/data';
const db = createClient({ project: 'storefront' });
const orders = await db.from('orders').select('id, total').limit(10);
console.log(orders);Code
import { CodeBlock } from '@sypnia/ui/react';
const code = `import { createClient } from '@sypnia/data';
const db = createClient({ project: 'storefront' });
const orders = await db.from('orders').select('id, total').limit(10);
console.log(orders);`;
// The file it belongs in, in the bar; line 4 is lit because it is the one to look at. Copy takes the whole code.
export default function File() {
return <CodeBlock title="src/orders.ts" lang="ts" code={code} mark={[4]} lineNumbers />;
} Long lines wrap
With wrap nothing scrolls sideways, also on a phone.
SELECT customers.name, count(orders.id) AS orders, sum(orders.total) AS spent FROM customers JOIN orders ON orders.customer_id = customers.id WHERE orders.placed_at > now() - interval '30 days' GROUP BY customers.name ORDER BY spent DESC;Code
import { CodeBlock } from '@sypnia/ui/react';
const sql = `SELECT customers.name, count(orders.id) AS orders, sum(orders.total) AS spent FROM customers JOIN orders ON orders.customer_id = customers.id WHERE orders.placed_at > now() - interval '30 days' GROUP BY customers.name ORDER BY spent DESC;`;
// A long line wraps instead of scrolling sideways: on a phone, the page never scrolls sideways.
export default function Wrap() {
return <CodeBlock lang="sql" code={sql} wrap />;
} Skeleton
The real piece and its skeleton: the same box, so nothing moves when the data arrives.
sypnia login
sypnia deploy --project storefrontCode
import { CodeBlock, CodeBlockSkeleton } from '@sypnia/ui/react';
// The code and its skeleton: the same box, so nothing moves when the code arrives.
export default function Skeleton() {
return (
<div className="sy-stack">
<CodeBlock lang="sh" code={'sypnia login\nsypnia deploy --project storefront'} />
<CodeBlockSkeleton lines={2} />
</div>
);
} When to use it
Use it when
- Code the person will copy or read closely: a setup step, an example, a query.
- Name the file with
title; light the line that matters withmark. - Long lines that must be read whole:
wrap.
Do not use it when
- It is one command to paste: Snippet.
- It is a word inside a sentence: InlineCode.
- It is the output of a program: Terminal or LogViewer.
For everyone
- The code is a region that can take focus, so it can be scrolled with the keyboard, and it is named after its file or language.
- Copy says «Copied» to screen readers. Line numbers are never copied.
Props
| CodeBlock | Type | Default |
|---|---|---|
coderequired | string | |
langThe language, shown in the bar when there is no title: ts, sql, sh, json… | string | text |
lineNumbersNumbers at the start of every line. | boolean | |
markLines to light, counted from 1. | number[] | [] |
titleThe file it belongs in: «src/db.ts». | string | |
wrapLong lines wrap instead of scrolling inside the block. | boolean |
| CodeBlockSkeleton | Type | Default |
|---|---|---|
bar | boolean | true |
lines | number | 4 |