This React Accordion component demonstrates a fully accessible, keyboard-navigable UI component that allows users to expand and collapse content sections dynamically.
- 🌟 Single accordion open at a time
- ♿ Full keyboard support
- 🔍 WCAG accessibility compliance
- 🎨 Responsive and smooth transitions
- 🧩 Modular component design
Many UI components misuse HTML elements, breaking accessibility standards.
- Used semantic
<button>
for interactions instead of<div>
- Implemented proper ARIA attributes to convey component state
- Ensured keyboard operability for all interactions
Dynamically showing/hiding content while maintaining proper screen reader context.
- Implemented
aria-expanded
attribute to indicate current state - Used
aria-hidden
to explicitly control content visibility - Added
aria-controls
to link trigger buttons with content regions
Ensuring full keyboard accessibility without mouse interaction.
- Added
tabIndex={0}
to focusable elements - Implemented focus management with
useRef
anduseEffect
- Created keyboard-friendly toggle mechanism
Automatically focusing newly revealed content for screen reader users.
- Used
useEffect
to focus content when expanded - Added
role="region"
for proper screen reader announcement - Implemented
aria-labelledby
for contextual linking
The accordion is composed of four key components:
-
Parent Wrapper (
App.js
):- Manages global accordion state
- Controls which accordion is currently expanded
- Maps accordion data to individual components
-
Accordion Component (
Accordion.js
):- Renders individual accordion sections
- Handles local interaction and accessibility attributes
- Receives expansion state from parent
-
Heading Button:
- Triggers content expansion
- Provides visual and semantic state indication
- Manages ARIA attributes for accessibility
-
Content Region:
- Dynamically shows/hides based on state
- Ensures proper focus and screen reader behavior
.app-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
- Smooth max-height transitions
- Opacity animations for content
- Rotational icon indicator
aria-expanded
: Indicates current open/closed statearia-hidden
: Controls element visibilityaria-controls
: Links trigger and content elementsaria-labelledby
: Provides contextual labelingrole="region"
: Defines content section for assistive technologies