Skip to content

Commit 1c7c263

Browse files
authored
Use useId() for stable unique ID in omnibar aria-controls (#1791)
- Replace hardcoded 'suggestions-list' ID with useId() generated value - Update SearchForm to generate stable unique ID for aria-controls - Update SuggestionsList to accept and use id prop - Ensures proper ARIA relationship and prevents ID conflicts Addresses PR review comment: #1786 (comment)
1 parent 951b1ac commit 1c7c263

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

special-pages/pages/new-tab/app/omnibar/components/SearchForm.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cn from 'classnames';
22
import { Fragment, h } from 'preact';
3-
import { useContext } from 'preact/hooks';
3+
import { useContext, useId } from 'preact/hooks';
44
import { eventToTarget } from '../../../../../shared/handlers';
55
import { AiChatIcon, SearchIcon } from '../../components/Icons.js';
66
import { usePlatformName } from '../../settings.provider';
@@ -26,6 +26,7 @@ export function SearchForm({ enableAi, term, setTerm }) {
2626

2727
const { t } = useTypedTranslationWith(/** @type {Strings} */ ({}));
2828
const platformName = usePlatformName();
29+
const suggestionsListId = useId();
2930

3031
const {
3132
suggestions,
@@ -67,7 +68,7 @@ export function SearchForm({ enableAi, term, setTerm }) {
6768
aria-label={t('searchForm_placeholder')}
6869
aria-expanded={suggestions.length > 0}
6970
aria-haspopup="listbox"
70-
aria-controls="suggestions-list"
71+
aria-controls={suggestionsListId}
7172
aria-activedescendant={selectedSuggestion?.id}
7273
spellcheck={false}
7374
autoComplete="off"
@@ -103,6 +104,7 @@ export function SearchForm({ enableAi, term, setTerm }) {
103104
</div>
104105
</div>
105106
<SuggestionsList
107+
id={suggestionsListId}
106108
suggestions={suggestions}
107109
selectedSuggestion={selectedSuggestion}
108110
setSelectedSuggestion={setSelectedSuggestion}

special-pages/pages/new-tab/app/omnibar/components/SuggestionList.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ import styles from './SuggestionList.module.css';
1212

1313
/**
1414
* @param {object} props
15+
* @param {string} props.id
1516
* @param {SuggestionModel[]} props.suggestions
1617
* @param {SuggestionModel | null} props.selectedSuggestion
1718
* @param {(suggestion: SuggestionModel) => void} props.setSelectedSuggestion
1819
* @param {() => void} props.clearSelectedSuggestion
1920
*/
20-
export function SuggestionsList({ suggestions, selectedSuggestion, setSelectedSuggestion, clearSelectedSuggestion }) {
21+
export function SuggestionsList({ id, suggestions, selectedSuggestion, setSelectedSuggestion, clearSelectedSuggestion }) {
2122
const { openSuggestion } = useContext(OmnibarContext);
2223
const platformName = usePlatformName();
2324
return (
24-
<div role="listbox" id="suggestions-list" class={styles.list}>
25+
<div role="listbox" id={id} class={styles.list}>
2526
{suggestions.map((suggestion) => {
2627
return (
2728
<button

0 commit comments

Comments
 (0)