Visually hidden

Slant / Primitives

Text for screen readers only: hidden from sight, not from assistive technology.

Demo

Usage

@tuttn/slant
import { Button, TrashIcon, VisuallyHidden } from '@tuttn/slant'

// The hidden text names the button; the icon is aria-hidden.
<Button icon><TrashIcon /><VisuallyHidden>Delete checkpoint</VisuallyHidden></Button>

// Extra words where the visible ones lean on what's around them.
<Button size="sm">Retry<VisuallyHidden> handoff.md</VisuallyHidden></Button>

// as picks the element: a legend for a fieldset, a caption for a table.
<fieldset>
  <VisuallyHidden as="legend">Choose a plan</VisuallyHidden>
  …
</fieldset>

Props

PropTypeDefaultDescription
as'span' | 'p' | 'legend' | 'label' | 'caption' | 'div''span'The element to render: a legend for a fieldset, a caption for a table, a label for a control.
…restHTMLAttributes—className, style, id and every other attribute go on the element, and a ref reaches it. With an id, a control can point at it through aria-labelledby or aria-describedby.

Accessibility

  • Hidden from sight only. The sl-sr class clips it to a 1px box, so screen readers still read it; display: none or hidden would hide it from everyone.
  • It stays hidden when focused, so keep links and buttons out of it: a keyboard user would land on something they can't see.
  • Start an addition with a space or a comma (<VisuallyHidden>, done</VisuallyHidden>) so the words don't run together. Stepper does exactly this.
  • Prefer visible text when there is room. Hidden text suits icon-only buttons and repeated actions, such as Retry or Remove, that need the item's name.