Skip to content
Slotted

Visually Hidden

Removes content from sight and keeps it in the accessibility tree, so a screen reader reads words the page does not show.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes

Naming an icon-only control

VisuallyHidden renders a span that is clipped to one pixel and taken out of flow. It is not display: none or visibility: hidden, which would remove the content from the accessibility tree as well. The button above shows a trash glyph and a screen reader reads “Delete invoice INV-20418”. A control that needs only a name can take aria-label instead, as IconButton does; use VisuallyHidden when the words are content, such as a count or a table heading. To hide content from everyone, use the hidden attribute.

Completing a visible count

A visible figure often needs its unit. The bell and the 3 are all a sighted reader sees, and a screen reader reads “3 unread notifications”. The hidden words sit in the row after the figure, so they are read in document order. The element is out of flow, so the gap of the Inline does not open a second space for it and the row measures the same with or without it.

3unread notifications
Visible: a bell and 3. Read aloud: 3 unread notifications.

Naming a table column

A column that holds only controls has no visible heading, and a table cell without a header has no column name for a screen reader. A hidden Actions heading names the column. Each button in the column carries its own hidden words, so every row reads “Cancel order SO-10421” and not an anonymous button.

Orders placed today
OrderTotalActions
SO-10421$1,240.00
SO-10422$860.50
SO-10423$3,410.25

Revealing on focus

focusable stops the hiding while focus is inside the element. The element is then rendered as it was written: the component paints nothing on the revealed state, so the appearance of a skip link belongs to the link. In React, render hands the props to the anchor; in Angular, the attribute goes on the anchor itself. Tab into the preview to reveal the link, and press Enter to move focus to the heading, which takes tabindex="-1" to receive it.

Accessibility

  • The content stays in the accessibility tree: the stylesheet clips it rather than applying display: none or visibility: hidden.
  • The content costs no space. The element is taken out of flow and held to one pixel, so a control that gains an accessible name this way is the size it was, and a hidden span between two flex items does not become a second gap.
  • A screen reader reads the content in document order, so it belongs where it would be read, not at the end of the document.
  • The whole string stays in the DOM as text. Hidden text is content, not decoration.
  • focusable is for content a sighted keyboard user must be able to reach, such as a skip link. While focus is inside, the hiding rule stops matching and the element renders as it was written — the component paints nothing on the revealed state, so the appearance of a skip link belongs to the link.
  • The component adds no role and no class. Wrap it in the element that carries the semantics, or hand the wiring to that element with render. Select it in the DOM as [data-ui="visually-hidden"].

API

visuallyHidden

Props

focusablebooleanDefault false
Stops hiding the content while focus is inside it.
render(props) => ReactNode
Renders a different element.
childrenReactNode
Content exposed only to assistive technology.
classNamestring
Class names, forwarded untouched.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every visually hidden inside it.