Skip to content

Commit

Permalink
[Accessibility][Autocomplete] Announce "no selectable options" (#5662)
Browse files Browse the repository at this point in the history
* Announce no selectable options

* changeset

* change Announce strategy do debounce

* add missing useEffect dep
  • Loading branch information
kendallgassner authored Feb 6, 2025
1 parent 05df3c3 commit 613cf0c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-rules-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Autocomplete: Use aria-live to announce "no selectable options".
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@changesets/changelog-github": "0.5.0",
"@changesets/cli": "2.27.1",
"@github/axe-github": "0.6.1",
"@github/mini-throttle": "2.1.1",
"@github/markdownlint-github": "^0.6.0",
"@github/prettier-config": "0.0.6",
"@mdx-js/react": "1.6.22",
Expand Down
16 changes: 16 additions & 0 deletions packages/react/src/Autocomplete/AutocompleteMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, {useContext, useEffect, useMemo, useRef, useState} from 'react'
import {debounce} from '@github/mini-throttle'
import {announce} from '@primer/live-region-element'
import {scrollIntoView} from '@primer/behaviors'
import type {ScrollIntoViewOptions} from '@primer/behaviors'
import type {ActionListItemProps} from '../ActionList'
Expand Down Expand Up @@ -122,6 +124,14 @@ export type AutocompleteMenuInternalProps<T extends AutocompleteItemProps> = {

const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_staff'

/**
* Announces a message to screen readers at a slowed-down rate. This is useful when you want to announce don't want to
* overwhelm the user with too many announcements in rapid succession.
*/
const debounceAnnouncement = debounce((announcement: string) => {
announce(announcement)
}, 250)

function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMenuInternalProps<T>) {
const autocompleteContext = useContext(AutocompleteContext)
if (autocompleteContext === null) {
Expand Down Expand Up @@ -267,6 +277,12 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe
allItemsToRenderRef.current = allItemsToRender
})

React.useEffect(() => {
if (allItemsToRender.length === 0) {
debounceAnnouncement(emptyStateText as string)
}
}, [allItemsToRender, emptyStateText])

useFocusZone(
{
containerRef: listContainerRef,
Expand Down

0 comments on commit 613cf0c

Please sign in to comment.