Skip to content

Commit

Permalink
add liveRegion.clear
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthkp committed Sep 17, 2024
1 parent 0f9778e commit 0fa24b2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/react/src/FilteredActionList/useAnnouncements.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Announcements for FilteredActionList (and SelectPanel) based
// on https://github.com/github/multi-select-user-testing

import {announce} from '@primer/live-region-element'
import {LiveRegionElement, announce} from '@primer/live-region-element'

Check failure on line 4 in packages/react/src/FilteredActionList/useAnnouncements.tsx

View workflow job for this annotation

GitHub Actions / lint

Imports "LiveRegionElement" are only used as type
import {useEffect, useRef} from 'react'
import type {FilteredActionListProps} from './FilteredActionListEntry'

Expand Down Expand Up @@ -41,6 +41,8 @@ export const useAnnouncements = (
listContainerRef: React.RefObject<HTMLUListElement>,
inputRef: React.RefObject<HTMLInputElement>,
) => {
const liveRegion = document.querySelector('live-region') as LiveRegionElement

useEffect(
function announceInitialFocus() {
const focusHandler = () => {
Expand All @@ -56,22 +58,24 @@ export const useAnnouncements = (
`${selected ? 'selected' : 'not selected'}`,
`${index + 1} of ${items.length}`,
].join(', ')
announce(announcementText, {delayMs})
announce(announcementText, {delayMs, from: liveRegion})
})
}

const inputElement = inputRef.current
inputElement?.addEventListener('focus', focusHandler)
return () => inputElement?.removeEventListener('focus', focusHandler)
},
[listContainerRef, inputRef, items],
[listContainerRef, inputRef, items, liveRegion],
)

const isFirstRender = useFirstRender()
useEffect(
function announceListUpdates() {
if (isFirstRender) return // ignore on first render as announceInitialFocus will also announce

liveRegion.clear() // clear previous announcements

if (items.length === 0) {
announce('No matching items.', {delayMs})
return
Expand All @@ -89,9 +93,9 @@ export const useAnnouncements = (
`${selected ? 'selected' : 'not selected'}`,
`${index + 1} of ${items.length}`,
].join(', ')
announce(announcementText, {delayMs})
announce(announcementText, {delayMs, from: liveRegion})
})
},
[listContainerRef, inputRef, items, isFirstRender],
[listContainerRef, inputRef, items, isFirstRender, liveRegion],
)
}

0 comments on commit 0fa24b2

Please sign in to comment.