-
-
Notifications
You must be signed in to change notification settings - Fork 536
Add aria-describedby automatically #1257
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
Changes from 2 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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -153,6 +153,7 @@ export interface ITooltip { | |||||||
| afterShow?: () => void | ||||||||
| afterHide?: () => void | ||||||||
| disableTooltip?: (anchorRef: HTMLElement | null) => boolean | ||||||||
| previousActiveAnchor: HTMLElement | null | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don’t make controller-only prop public-required; mark optional and internal. Requiring Apply: - previousActiveAnchor: HTMLElement | null
+ /** @internal Controlled by TooltipController. Do not pass manually. */
+ previousActiveAnchor?: HTMLElement | null📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do agree, @chardin1 can you please check? Also @gabrieljablonski can you please let me know your thoughts about that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems fine to keep, since we already "expose" internals with Maybe just mark it down so we don't forget to move this to an internal interface later? |
||||||||
| activeAnchor: HTMLElement | null | ||||||||
| setActiveAnchor: (anchor: HTMLElement | null) => void | ||||||||
| border?: CSSProperties['border'] | ||||||||
|
|
||||||||
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.
Add cleanup and harden aria-describedby token handling.
aria-describedbyon anchors (especially ifidchanges or Tooltip unmounts while visible)..split(' ')mishandles tabs/newlines and multiple spaces; normalize with/\s+/and filter empties.Apply:
/** * Add aria-describedby to activeAnchor when tooltip is active */ useEffect(() => { if (!id) return - function getAriaDescribedBy(element: HTMLElement | null) { - return element?.getAttribute('aria-describedby')?.split(' ') || [] - } + function getAriaDescribedBy(element: HTMLElement | null) { + const raw = element?.getAttribute('aria-describedby') ?? '' + return raw.trim().split(/\s+/).filter(Boolean) + } - function removeAriaDescribedBy(element: HTMLElement | null) { - const newDescribedBy = getAriaDescribedBy(element).filter((s) => s !== id) - if (newDescribedBy.length) { - element?.setAttribute('aria-describedby', newDescribedBy.join(' ')) - } else { - element?.removeAttribute('aria-describedby') - } - } + function removeAriaDescribedBy(element: HTMLElement | null) { + if (!element) return + const newDescribedBy = getAriaDescribedBy(element).filter((s) => s !== id) + if (newDescribedBy.length) { + const next = newDescribedBy.join(' ') + if (element.getAttribute('aria-describedby') !== next) { + element.setAttribute('aria-describedby', next) + } + } else { + element.removeAttribute('aria-describedby') + } + } if (show) { - removeAriaDescribedBy(previousActiveAnchor) + if (previousActiveAnchor && activeAnchor && !previousActiveAnchor.isSameNode(activeAnchor)) { + removeAriaDescribedBy(previousActiveAnchor) + } const currentDescribedBy = getAriaDescribedBy(activeAnchor) - const describedBy = [...new Set([...currentDescribedBy, id])].filter(Boolean).join(' ') - activeAnchor?.setAttribute('aria-describedby', describedBy) + const describedBy = [...new Set([...currentDescribedBy, id])].filter(Boolean).join(' ') + if (activeAnchor && activeAnchor.getAttribute('aria-describedby') !== describedBy) { + activeAnchor.setAttribute('aria-describedby', describedBy) + } } else { removeAriaDescribedBy(activeAnchor) } - }, [activeAnchor, show, id, previousActiveAnchor]) + return () => { + // Cleanup on id/anchor changes and unmount to avoid stale ARIA refs. + removeAriaDescribedBy(activeAnchor) + removeAriaDescribedBy(previousActiveAnchor) + } + }, [activeAnchor, show, id, previousActiveAnchor])📝 Committable suggestion
🤖 Prompt for AI Agents