-
Notifications
You must be signed in to change notification settings - Fork 0
π¨ Palette: Add ARIA bindings to WikiCollapsible #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
|
|
||
| ## 2026-04-30 - Accessible Interactive Collapsible using React useId | ||
| **Learning:** For accessible interactive components like collapsibles, custom DOM ID generation can be problematic due to component re-use and SSR mismatches. React's `useId` provides collision-free unique IDs crucial for properly linking interactive elements like buttons via `aria-controls` to the content they control. | ||
| **Action:** When creating accessible widgets (tabs, dialogs, collapsibles), standardize on React's `useId` hook to generate unique IDs and pair them with `aria-controls` mapping to ensure screen readers provide correct structural context, maintaining accessibility without risking DOM ID collisions across instances. |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||
| "use client"; | ||||||||||
|
|
||||||||||
| import { useState } from "react"; | ||||||||||
| import { useState, useId } from "react"; | ||||||||||
|
|
||||||||||
| interface WikiCollapsibleProps { | ||||||||||
| title: string; | ||||||||||
|
|
@@ -14,6 +14,7 @@ export function WikiCollapsible({ | |||||||||
| defaultOpen = true, | ||||||||||
| }: WikiCollapsibleProps) { | ||||||||||
| const [isOpen, setIsOpen] = useState(defaultOpen); | ||||||||||
| const contentId = useId(); | ||||||||||
|
|
||||||||||
| return ( | ||||||||||
| <div className="border border-wiki-border-light bg-wiki-offwhite"> | ||||||||||
|
|
@@ -22,11 +23,13 @@ export function WikiCollapsible({ | |||||||||
| <button | ||||||||||
| onClick={() => setIsOpen(!isOpen)} | ||||||||||
| className="text-wiki-link text-sm hover:underline" | ||||||||||
| aria-expanded={isOpen} | ||||||||||
| aria-controls={contentId} | ||||||||||
|
Comment on lines
23
to
+27
|
||||||||||
| > | ||||||||||
| [{isOpen ? "hide" : "show"}] | ||||||||||
| </button> | ||||||||||
| </div> | ||||||||||
| {isOpen && <div className="px-4 py-3">{children}</div>} | ||||||||||
| {isOpen && <div id={contentId} className="px-4 py-3">{children}</div>} | ||||||||||
|
||||||||||
| {isOpen && <div id={contentId} className="px-4 py-3">{children}</div>} | |
| <div id={contentId} className="px-4 py-3" hidden={!isOpen}> | |
| {children} | |
| </div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The click handler uses
setIsOpen(!isOpen), which can read a stale value under React concurrent updates. Use a functional state update (setIsOpen(v => !v)) to ensure correctness and match existing patterns in wiki components (e.g.,setOpen((v) => !v)insrc/components/wiki/wiki-dropdown.tsx:42).