Layout & text

Slant / Foundations

How Stack, Row, Card, Text and List arrange everything else, with a link to each one's page.

Demo

Usage

@tuttn/slant
import { Badge, Button, Card, Kbd, List, ListItem, Row, Stack, Text, VisuallyHidden } from '@tuttn/slant'

// The surface: a Card, named by a real heading in its title slot.
<Card as="article" aria-labelledby="checkpoint" elevation={1}
  title={<Text as="h3" variant="label" id="checkpoint">Checkpoint · TASK-24</Text>}
  header={<Badge tone="success"><VisuallyHidden>Checkpoint </VisuallyHidden>Saved</Badge>}
  footer={<><Button size="sm" variant="ghost">Discard</Button><Button size="sm" variant="primary">Restore</Button></>}>
  {/* The rhythm, top to bottom: a Stack, loose between groups. */}
  <Stack gap={14}>
    <div>
      <Text variant="title">Draft the handoff</Text>
      <Text muted>Saved 4 minutes ago.</Text>
    </div>
    {/* A tighter Stack inside a group: a Row of two ends over a List of rows. */}
    <Stack gap={6}>
      <Row justify="between"><Text variant="label" id="files">Changed files</Text><Text variant="mono" muted>3 files</Text></Row>
      <List aria-labelledby="files">
        {files.map(file => <ListItem key={file.name} size="sm">
          <Text variant="mono">{file.name}</Text><Text variant="mono" muted>{file.size}</Text>
        </ListItem>)}
      </List>
    </Stack>
    <Text variant="hint">Press <Kbd>Tab</Kbd> to reach the actions.</Text>
  </Stack>
</Card>

Accessibility

  • Pick the element for what the content is, with as: a form, a nav, a section or article named by its heading, an ol for steps, a heading for a title.
  • None of the pieces reorders its children, so the reading order and the Tab order follow the DOM, in either direction.
  • Lists without bullets lose their list role in Safari's VoiceOver. Add role="list" to a List, or a Stack or Row rendered as="ul", where the item count matters.

The pieces

  • Stack puts its children in a column, 10px apart unless you set gap. It's a one-column grid, so they stretch to its width.
  • Row puts them side by side, 8px apart and centred on the cross axis, and wraps them when they run out of room.
  • Card is the surface: a border, the theme's radius, a padded body, and an optional header row and footer.
  • Text sets every run of words: body, title, hint, label and mono.
  • List holds repeated rows with hairlines between them, and each List item spreads its content to both ends.
  • Kbd draws a key, and Visually hidden adds words only screen readers hear.

How they nest

Work from the outside in. A Card is the surface. A Stack inside it sets the rhythm from top to bottom, Rows inside the Stack put things side by side, and Text sets the words. Repeated rows go in a List rather than a Stack of Rows, so they get list semantics, an item count and the hairlines. The demo is built that way: a Card, a Stack, a Row over a List of small rows, and Text throughout.

Spacing

Spacing comes from gaps and paddings, not margins. Stack, Row, List and Text reset their element's margin, so nothing collapses or leaks into a neighbour; the one exception is text straight after a title Text, which sits 2px lower. Use a looser gap between groups and a tighter one inside a group, as the demo's 14px and 6px stacks do. The surfaces bring their own padding, 18px in a Card's body and 10px above and below a List item, so don't add more around them. To run content to a card's edges, pass padding={false}.

Elements

Each piece renders one element: a div for Stack, Row and Card, a ul and an li for List and List item, a p or a span for Text, a kbd for Kbd and a span for Visually hidden. All but Kbd take as to render a better one, and the look stays the same: Stack and Row a form, nav, section, header, footer, ul or li; Card a section, article or li; Text a heading, a label, a time and more.

Refs, attributes and direction

Every piece forwards its ref to its root element and passes className, style, id, aria-* and handlers to it, as Conventions describes. The styles use flex, grid and logical properties, so under <Slant dir="rtl"> rows, list items and card headers and footers mirror with no extra props.