CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-aria--visually-hidden

React components and hooks for creating visually hidden content that remains accessible to screen readers and assistive technologies.

Pending
Overview
Eval results
Files

React Aria Visually Hidden

React Aria Visually Hidden provides React components and hooks for creating visually hidden content that remains accessible to screen readers and assistive technologies. It offers a VisuallyHidden React component that renders content invisible to sighted users but maintains semantic structure for accessibility tools, and a useVisuallyHidden hook that provides the underlying CSS styling and focus management logic.

Package Information

  • Package Name: @react-aria/visually-hidden
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @react-aria/visually-hidden

Core Imports

import { VisuallyHidden, useVisuallyHidden } from "@react-aria/visually-hidden";
import type { VisuallyHiddenProps, VisuallyHiddenAria } from "@react-aria/visually-hidden";

For CommonJS:

const { VisuallyHidden, useVisuallyHidden } = require("@react-aria/visually-hidden");

Basic Usage

import React from "react";
import { VisuallyHidden, useVisuallyHidden } from "@react-aria/visually-hidden";

// Using the component
function SkipLink() {
  return (
    <VisuallyHidden isFocusable>
      <a href="#main">Skip to main content</a>
    </VisuallyHidden>
  );
}

// Using the hook
function CustomHiddenElement() {
  const { visuallyHiddenProps } = useVisuallyHidden({
    isFocusable: true
  });
  
  return (
    <div {...visuallyHiddenProps}>
      Screen reader only content
    </div>
  );
}

Capabilities

VisuallyHidden Component

React component that hides its children visually while keeping content visible to screen readers.

/**
 * VisuallyHidden hides its children visually, while keeping content visible
 * to screen readers.
 */
function VisuallyHidden(props: VisuallyHiddenProps): JSX.Element;

Usage Example:

import { VisuallyHidden } from "@react-aria/visually-hidden";

// Basic hidden content
<VisuallyHidden>
  <span>This text is hidden visually but available to screen readers</span>
</VisuallyHidden>

// Focusable hidden content (like skip links)
<VisuallyHidden isFocusable>
  <a href="#main">Skip to main content</a>
</VisuallyHidden>

// Custom element type
<VisuallyHidden elementType="span">
  <input type="checkbox" />
</VisuallyHidden>

useVisuallyHidden Hook

Provides props for an element that hides its children visually but keeps content visible to assistive technology.

/**
 * Provides props for an element that hides its children visually
 * but keeps content visible to assistive technology.
 */
function useVisuallyHidden(props: VisuallyHiddenProps = {}): VisuallyHiddenAria;

Usage Example:

import { useVisuallyHidden } from "@react-aria/visually-hidden";

function CustomComponent() {
  const { visuallyHiddenProps } = useVisuallyHidden({
    isFocusable: true,
    style: { backgroundColor: 'red' } // Custom styles when focused
  });
  
  return (
    <div {...visuallyHiddenProps}>
      Hidden content with custom styling
    </div>
  );
}

Types

// Base types from React and @react-types/shared
import type { ReactNode, JSXElementConstructor } from 'react';
import type { DOMAttributes } from '@react-types/shared';

interface VisuallyHiddenProps extends DOMAttributes {
  /** The content to visually hide. */
  children?: ReactNode;
  
  /**
   * The element type for the container.
   * @default 'div'
   */
  elementType?: string | JSXElementConstructor<any>;
  
  /** Whether the element should become visible on focus, for example skip links. */
  isFocusable?: boolean;
}

interface VisuallyHiddenAria {
  /** Props to spread onto the element that should be hidden */
  visuallyHiddenProps: DOMAttributes;
}

Important Notes

  • Positioning Requirements: VisuallyHidden uses absolute positioning, so it needs a positioned ancestor to prevent scroll bars. Ensure a parent element has position: relative or another positioned value.
  • Focus Behavior: When isFocusable is true, the element becomes visible when focused, useful for skip links and keyboard navigation aids.
  • CSS Implementation: Uses CSS clipping and positioning techniques (clip: rect(0 0 0 0), clipPath: inset(50%)) to hide content while preserving it in the accessibility tree.
  • Integration: Part of the React Aria ecosystem and integrates with React Aria's focus management system through useFocusWithin.

Dependencies

  • @react-aria/interactions: For focus management via useFocusWithin
  • @react-aria/utils: For utility functions like mergeProps
  • @react-types/shared: For TypeScript interfaces like DOMAttributes
  • React: Peer dependency (^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1)
  • React DOM: Peer dependency (^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1)

Install with Tessl CLI

npx tessl i tessl/npm-react-aria--visually-hidden
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@react-aria/visually-hidden@3.8.x
Publish Source
CLI
Badge
tessl/npm-react-aria--visually-hidden badge