From c0360db263e046f7d756bb157fb520167353be91 Mon Sep 17 00:00:00 2001 From: Cameron Dutro Date: Thu, 16 Jan 2025 15:13:20 -0800 Subject: [PATCH] Avoid `TypeError` in useAnnouncements (#5551) * Avoid accessing properties of undefined in useAnnouncements * Add changeset --- .changeset/giant-bees-impress.md | 5 +++++ packages/react/src/FilteredActionList/useAnnouncements.tsx | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .changeset/giant-bees-impress.md diff --git a/.changeset/giant-bees-impress.md b/.changeset/giant-bees-impress.md new file mode 100644 index 00000000000..0b9d17e7e92 --- /dev/null +++ b/.changeset/giant-bees-impress.md @@ -0,0 +1,5 @@ +--- +"@primer/react": patch +--- + +Fix an issue in useAnnouncements.tsx causing a TypeError in production. The activeItem variable may be null. diff --git a/packages/react/src/FilteredActionList/useAnnouncements.tsx b/packages/react/src/FilteredActionList/useAnnouncements.tsx index aa48a8a2b45..43e11c08400 100644 --- a/packages/react/src/FilteredActionList/useAnnouncements.tsx +++ b/packages/react/src/FilteredActionList/useAnnouncements.tsx @@ -4,6 +4,7 @@ import {announce} from '@primer/live-region-element' import {useEffect, useRef} from 'react' import type {FilteredActionListProps} from './FilteredActionListEntry' +import type {ItemInput} from '../deprecated/ActionList/List' // we add a delay so that it does not interrupt default screen reader announcement and queues after it const delayMs = 500 @@ -28,10 +29,10 @@ const getItemWithActiveDescendant = ( const optionElements = listElement.querySelectorAll('[role="option"]') const index = Array.from(optionElements).indexOf(activeItemElement) - const activeItem = items[index] + const activeItem = items[index] as ItemInput | undefined - const text = activeItem.text - const selected = activeItem.selected + const text = activeItem?.text + const selected = activeItem?.selected return {index, text, selected} }