Skip to content

Commit c0360db

Browse files
authored
Avoid TypeError in useAnnouncements (#5551)
* Avoid accessing properties of undefined in useAnnouncements * Add changeset
1 parent b8284ce commit c0360db

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

.changeset/giant-bees-impress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
Fix an issue in useAnnouncements.tsx causing a TypeError in production. The activeItem variable may be null.

packages/react/src/FilteredActionList/useAnnouncements.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import {announce} from '@primer/live-region-element'
55
import {useEffect, useRef} from 'react'
66
import type {FilteredActionListProps} from './FilteredActionListEntry'
7+
import type {ItemInput} from '../deprecated/ActionList/List'
78

89
// we add a delay so that it does not interrupt default screen reader announcement and queues after it
910
const delayMs = 500
@@ -28,10 +29,10 @@ const getItemWithActiveDescendant = (
2829
const optionElements = listElement.querySelectorAll('[role="option"]')
2930

3031
const index = Array.from(optionElements).indexOf(activeItemElement)
31-
const activeItem = items[index]
32+
const activeItem = items[index] as ItemInput | undefined
3233

33-
const text = activeItem.text
34-
const selected = activeItem.selected
34+
const text = activeItem?.text
35+
const selected = activeItem?.selected
3536

3637
return {index, text, selected}
3738
}

0 commit comments

Comments
 (0)