VisuallyHidden
A <span> that takes no visible space but stays in the accessibility tree and in find-in-page. Use it for text a sighted user does not need: the name of an icon-only control, a status message for a live region, a heading that only exists for navigation by assistive technology.
It is not display: none, visibility: hidden or the HTML hidden attribute: those remove the content from the accessibility tree as well. For responsive show/hide use Hidden.
Examples
Icon button label
The visible glyph is aria-hidden; the hidden text becomes the accessible name.
import { Button, VisuallyHidden } from '@var-ui/react';
<Button layout="icon" tone="danger">
<span aria-hidden="true">×</span>
<VisuallyHidden>Delete project</VisuallyHidden>
</Button>Live region
Pass any native attribute through: role="status" makes the message announce when its text changes, with nothing shown on screen.
import { useState } from 'react';
import { Button, VisuallyHidden } from '@var-ui/react';
function Save() {
const [saved, setSaved] = useState(false);
return (
<>
<Button onClick={() => setSaved(true)}>Save</Button>
<VisuallyHidden role="status">{saved ? 'Changes saved' : ''}</VisuallyHidden>
</>
);
}When not to use it
- If a control can take an
aria-label, prefer that for a single static name.VisuallyHiddenis for text that is part of the content, can be translated with the rest of it, or changes. - Do not hide focusable content: a keyboard user would land on something they cannot see.
- To show the text on focus (a skip link) use a real link and style it;
VisuallyHiddenhas no focus state.
Astro
VisuallyHidden from @var-ui/astro renders the same <span> and class. Spinner uses it for its label in both packages.
Props
All native <span> attributes are passed through, and ref is the span.
Accessibility
- Rendered with the
visually-hiddenrecipe:position: absolute, a0.0625remclipped box,overflow: hidden,white-space: nowrap. The text is still in the accessibility tree, selectable by assistive technology, and found by browser search. white-space: nowrapkeeps long text from wrapping inside that box, which some screen readers would read word by word.- The text should be written for a listener, in the page language.