Skip to content

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'

On this page
  1. A file, with a lit line
  2. Long lines wrap
  3. Skeleton
  4. When to use it
  5. For everyone
  6. Props

A file, with a lit line

Line numbers and a lit line. Copy takes the code without the numbers.

src/orders.ts
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.

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;
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.

Shell
sypnia login
sypnia deploy --project storefront
Code
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 with mark.
  • 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

CodeBlockTypeDefault
coderequired string
lang

The language, shown in the bar when there is no title: ts, sql, sh, json…

string text
lineNumbers

Numbers at the start of every line.

boolean
mark

Lines to light, counted from 1.

number[] []
title

The file it belongs in: «src/db.ts».

string
wrap

Long lines wrap instead of scrolling inside the block.

boolean
CodeBlockSkeletonTypeDefault
bar boolean true
lines number 4
  1. A file, with a lit line
  2. Long lines wrap
  3. Skeleton
  4. When to use it
  5. For everyone
  6. Props

    Search pages, sections and terms