Todae

Todae

Code and Kbd

Code.Root is a native <code>, CodeBlock.Root a native <pre> and Kbd.Root a native <kbd>, styled from the code and kbd contract tokens. Server Components can import them from @tounsoo/todae/code, @tounsoo/todae/code-block and @tounsoo/todae/kbd.

On this page

Code

Code.Root keeps the text color around it and tints its box from that color, so it reads the same in a link or a primary button. It is a step smaller than the text around it. CodeBlock.Root is a block that scrolls sideways when a line is too long, and code inside it, a Code.Root or a plain <code>, drops its own box.

import { Code } from '@tounsoo/todae/code';
import { CodeBlock } from '@tounsoo/todae/code-block';
import { Stack } from '@tounsoo/todae/stack';
import { Text } from '@tounsoo/todae/text';

export default function CodeInText() {
  return (
    <Stack.Root gap="condensed">
      <Text.Root>
        Call <Code.Root>getPlan()</Code.Root> before you render the billing page.
      </Text.Root>
      <CodeBlock.Root tabIndex={0} role="region" aria-label="Example code">
        <code>{`const plan = await getPlan();\nrender(<Billing plan={plan} />);`}</code>
      </CodeBlock.Root>
    </Stack.Root>
  );
}

Call getPlan() before you render the billing page.

const plan = await getPlan();
render(<Billing plan={plan} />);

A CodeBlock.Root that scrolls needs tabIndex={0} so keyboard users can reach and scroll it, and role="region" with an aria-label so that stop has a name. Set wrap when a long line should wrap instead. The block then keeps its width and does not scroll sideways.

import { CodeBlock } from '@tounsoo/todae/code-block';

const line = `const label = '${'token-'.repeat(12)}end';`;

export default function CodeBlockWrap() {
  return (
    <CodeBlock.Root wrap>
      <code>{line}</code>
    </CodeBlock.Root>
  );
}
const label = 'token-token-token-token-token-token-token-token-token-token-token-token-end';

Cap the height by wrapping the block in ScrollArea.Root. This site already puts each fence in one (DocsPre inside ScrollRegion) and moves the scrolling off the block, with overflow: visible on the pre. Do the same, and give the area a max-block-size, focusable and a name. A block that still scrolls sideways uses axis="both", so the one area scrolls both ways. The site's fences use axis="inline" because the page owns their height and DocsPre folds a long fence.

import { CodeBlock, ScrollArea } from '@tounsoo/todae';

const source = [
  `const label = '${'token-'.repeat(16)}end';`,
  ...Array.from({ length: 24 }, (_, index) => `const line${index + 1} = ${index + 1};`),
].join('\n');

export default function CodeBlockHeight() {
  return (
    <ScrollArea.Root focusable axis="both" aria-label="Long source" style={{ maxBlockSize: '8rem' }}>
      <CodeBlock.Root style={{ overflow: 'visible' }}>
        <code>{source}</code>
      </CodeBlock.Root>
    </ScrollArea.Root>
  );
}
const label = 'token-token-token-token-token-token-token-token-token-token-token-token-token-token-token-token-end';
const line1 = 1;
const line2 = 2;
const line3 = 3;
const line4 = 4;
const line5 = 5;
const line6 = 6;
const line7 = 7;
const line8 = 8;
const line9 = 9;
const line10 = 10;
const line11 = 11;
const line12 = 12;
const line13 = 13;
const line14 = 14;
const line15 = 15;
const line16 = 16;
const line17 = 17;
const line18 = 18;
const line19 = 19;
const line20 = 20;
const line21 = 21;
const line22 = 22;
const line23 = 23;
const line24 = 24;

code.font.family and code.font.scale (the size as a share of the text around it) set the font of inline code and code blocks alike. Restyle the inline box through code.tint (how much of the text color it takes), code.radius, code.padding.x and code.padding.y, and the block through the code.block tokens.

Kbd

Kbd.Root is drawn as a key cap. For a shortcut, put one Kbd.Root per key inside an outer one, as HTML defines it: the outer one stays plain text in the color around it, so the joining + is not boxed.

import { Kbd } from '@tounsoo/todae/kbd';
import { Text } from '@tounsoo/todae/text';

export default function KbdShortcut() {
  return (
    <Text.Root>
      Press{' '}
      <Kbd.Root>
        <Kbd.Root>Ctrl</Kbd.Root>+<Kbd.Root>K</Kbd.Root>
      </Kbd.Root>{' '}
      to search, or <Kbd.Root>Esc</Kbd.Root> to close.
    </Text.Root>
  );
}

Press Ctrl+K to search, or Esc to close.

Restyle keys through kbd.fg, kbd.bg, kbd.border, kbd.border-width, kbd.radius, kbd.font.family, kbd.font.scale, kbd.padding.x and kbd.padding.y.

Accessible composition

Todae handles: Only the look. The parts stay native <code>, <pre> and <kbd> elements, and Todae adds no role, tabindex or name. Code.Root keeps the text color around it. CodeBlock.Root scrolls sideways when a line is too long, or wraps that line when wrap is set. Todae does not make the block focusable and does not cap its height.

You supply: For a CodeBlock.Root that scrolls on its own, the tabIndex={0}, role="region" and aria-label described above. To cap the height, wrap the block in ScrollArea.Root, set the block's overflow to visible, and give the area focusable, a max-block-size and an aria-label. Use axis="both" when the lines are also too long to fit. A company code block component should require the name in its props instead of filling in one default. If it adds a copy button, give the button a name and report the result, for example with useAnnouncer.

// The server entry, so a Server Component can render it.
import { CodeBlock } from '@tounsoo/todae/code-block';

type SourceBlockProps = { label: string; children: string };

export function SourceBlock({ label, children }: SourceBlockProps) {
  return (
    <CodeBlock.Root tabIndex={0} role="region" aria-label={label}>
      <code>{children}</code>
    </CodeBlock.Root>
  );
}

Watch for: A default name such as "Code" on every block, which gives a page many regions with the same name; name each block after what it shows. tabIndex={0} without role and aria-label, which adds a Tab stop that has no name.

When to use

When: Code → a name, value or snippet the reader types or reads in code. Kbd → a key or shortcut the reader presses.

When not: A label for a state or count → Badge. Text that only needs a mono font → your own class, so screen readers and search do not treat it as code.

Props

Code.Root props
NameTypeDescription
childrenReact.ReactNode

The code.

CodeBlock.Root props
NameTypeDescription
wrapboolean

Wraps long lines instead of scrolling sideways.

Default: false

tabIndexnumber

Set 0 when the block can scroll, so keyboard users can reach and scroll it.

childrenReact.ReactNode

The code, usually in a code element, which drops its own box here.

Kbd.Root props
NameTypeDescription
childrenReact.ReactNode

A key name, or for a shortcut one Kbd.Root per key.