Repository navigation
[lexical-react][lexical-playground] Bug Fix: Announce typeahead menu options and result count to screen readers - #8929
Conversation
…options and result count to screen readers A typeahead menu announced itself as a list twice over and numbered its options without naming them, and said nothing as the results narrowed or when nothing matched at all. The listbox role moves onto the element that actually holds the options, options can carry their own spoken name, every menu is named, and the number of matches is announced through the editor's existing aria live region - "10 suggestions available", or "No results" when the query matches nothing.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Ready for a look when you have time — green on the full matrix. There is one open question near the top about the live region that I would value your feedback on. |
|
I don't think that the lack of AriaLiveRegionExtension is a real concern since #8908 is bringing it to every editor using RichTextExtension by default. The tight coupling of the table should be addressed one way or another, would have to take a close look at exactly what the state of everything is during that situation but maybe we could annotate the menu tree with setDOMUnmanaged and make sure that isDOMUnmanaged is checked in the right place, or the event handler could check that the focus is in the right place before handling those keystrokes. |
|
Agreed on the live region — #8908 covers it. On the table, I went looking at both suggestions and may be missing something, so here is what I found.
A focus check ran into something different: when a typeahead menu is open, DOM focus stays in the editor — that is what What the table needs is "is a menu open right now", and the only things that answer that today are attributes the accessibility code sets — Would you rather the menu told the editor directly — a command, or some shared state the table can read — rather than the table inferring it from the DOM? |
|
I'd have to see what the solutions look like to answer that |
|
A command would not be a good solution though, commands are a terrible way to represent state because their initial state can't be observed and propagation isn't guaranteed based on the return value of other listeners of the same command at higher priorities. |
|
That would put an observable The part I am not sure about is where that extension should live. The typeahead is in |
|
It would go in |
|
Either shape would fix the table case. Where they differ is reach: a menu-specific flag only covers menus Lexical itself builds.
My view is that the active descendant is the state worth sharing: a signal in The one place it differs from "a menu is open" is an empty result list, where this PR clears the attribute because there is no option to point at. That is likely the behaviour you want: with nothing to move through, the arrow keys should go back to moving the caret, which is what the table does when the check is false. I am happy to build that as a follow-up. Leaving the |
|
Any thoughts on the shape for this one? Happy to build either the menu-specific state or the wider "something owns the caret keys" version as a follow-up, just let me know. |
|
If you want to prototype both I can take a look and choose the better option, otherwise you may have to wait a bit until someone has the time to review it thoroughly enough to make that kind of decision. |
|
A lint rule changed since this PR was opened so you may need to apply pnpm run lint:fix to this in order to pass integrity checks. There are also conflicts so this can't be considered until all of that is resolved. |
# Conflicts: # packages/lexical-react/src/shared/LexicalMenu.tsx
## Description
Upstream moved the MenuOption class out of shared/LexicalMenu.tsx into its own
entry point. This branch had added an ariaLabel property to that class, so the
property moves with it. Taking the move alone would have compiled and silently
dropped the feature: ariaLabel is what lets an option be announced by a spoken
name rather than by decorative glyphs in its title.
## Test plan
### After
```
$ npx vitest run --project unit packages/lexical-react/src/__tests__/unit/LexicalMenu.test.tsx
Test Files 1 passed (1)
Tests 35 passed (35)
```
There was a problem hiding this comment.
Claude identified a few issues with this approach:
Blocking
Custom menuRenderFn menus lose the listbox role. The PR changes the portal container from role="listbox" to role="presentation" and only puts the listbox on the <ul> inside defaultMenuRenderFn. Any custom renderer now ends up with role="option" items that have no listbox around them. On top of that, the editor root gets aria-owns="typeahead-listbox", which points at an id that doesn't exist. The repo's own examples/website-notion/src/plugins/SlashMenuPlugin.tsx works this way, and so do the menuRenderFn examples in docs/react/plugins.md. None of the playground menus use a custom renderer, so the e2e test can't catch this. Two possible fixes: only set role="presentation" and aria-owns when the default renderer is in use, or give custom renderers a way to get the listbox props.
Should fix
2. "No results" fires while results are still loading. The count announcement is debounced by 500ms and runs as soon as the menu mounts with 0 options. The playground's own mention lookup (dummyLookupService) also resolves after 500ms, so typing @A races it and can say "No results" just before the matches arrive. That will happen for any async source that takes longer than 500ms. Options: skip the announcement on the first mount with 0 options, or add a loading hint.
3. setContainerDivAttributes takes an ariaLabel it never uses. It's also threaded through useMenuAnchorRef and added to that hook's deps, so a label change re-runs positioning for nothing. The container no longer has any label.
Minor
- The unmount cleanup now captures the root element when the effect runs, where it used to read it at cleanup time. If the root changes while the menu is open, the stale element keeps its aria-owns.
- try { announce() } catch {} swallows every error, not just a missing AriaLiveRegionExtension.
- The new props aren't in the .js.flow files. Those files already lag behind, so this matches existing practice.
|
There are also conflicts to resolve. Normally I would assist in getting a PR over the finish line but last I checked you didn't provide access for maintainers to push to your fork so I can't. |
# Conflicts: # packages/lexical-playground/__tests__/e2e/Mentions.spec.mjs # packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx # packages/lexical-react/src/__tests__/unit/LexicalMenu.test.tsx
…again, and a slow lookup says Loading Impact: a menu a project draws itself is a named list again, and the editor points at that list. While a search is still running and nothing is on screen yet, you hear "Loading". When the search finishes with nothing, you hear "No results". A custom menu never draws the built-in list, so the editor was pointing at an id that was not on the page, and the box around the menu was only decoration. The outer box is the list again. The built-in mentions menu is unchanged. The playground mentions menu says when its lookup is still running.
|
Custom menus: the outer box is the list again, and the editor owns that box. It was pointing at a list those menus never draw. The built-in menu stays one list, with the choices directly in it. Giving a custom menu that same direct list, so it can say "3 of 7", can be a follow-up. The render function is unchanged. Empty menu: "Loading" while a lookup is still running, "No results" when it finishes with nothing. This fixes the current gap, so the menu is in better shape. Arrow keys in a table are a standing issue, and there are a few ways to do it. Can we split that out into its own issue? |
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
Thanks for this, and for working through the earlier review. The change looks good. The default menu now exposes a named listbox that holds its options directly, custom menuRenderFn menus keep a listbox on their container, and aria-controls stays typeahead-menu, so table arrow-key handling is unchanged. The new tests in LexicalMenu.test.tsx fail without the fix and pass with it. Everything below is optional and non-blocking.
Suggestions
- Changeset. This changes published
@lexical/reactbehavior (container role,aria-selected,aria-owns, new announcements) and adds props, so AGENTS.md ("Changesets") and CONTRIBUTING.md call for a changeset. Something like.changeset/typeahead-listbox-a11y.mdwith'@lexical/react': patch, the line[fix] Announce typeahead menu options and result count to screen readers, and aVerify:paragraph noting that the listbox role moves from#typeahead-menuto the inner<ul>, that unselected options no longer carryaria-selected="false", and that menus announce their result count. - Configurable, translatable status messages (
packages/lexical-react/src/shared/LexicalMenu.tsx:321-329,:501-511). "N suggestions available", "No results" and "Loading" are fixed English, and they are spoken in every editor that hasAriaLiveRegionExtension, whichRichTextExtensionbrings in. The other announcers (HistoryAnnounceExtension,EditorModeAnnounceExtension,HeadingAnnounceExtension,AutoLinkAnnounceExtension) expose their strings and adisabledflag, which matches AGENTS.md's guidance that toggleable behavior has adisabledconfig. A prop such asgetStatusMessage?: (count: number, loading: boolean) => string | null(returningnullfor silence) would let apps translate or switch these off. - Async menus that don't pass
loading(packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx:154). With the defaultloading = false, any menu whose lookup takes longer than 500 ms says "No results" before its options arrive. Consider announcing "No results" only whenloadingis explicitlyfalse, or only after the list has been non-empty, so menus that haven't adopted the prop stay silent rather than wrong. aria-ownson the editor (LexicalMenu.tsx:548). In Chromium's accessibility tree the editor gains anownsrelation but the listbox stays a child of<body>: Chromium does not reparent into an editable root, and the base already resolvedaria-activedescendantwithout it. Firefox and Safari may behave differently. Either confirm it with VoiceOver on Safari and NVDA on Firefox, or drop it and rely onaria-controls.- Flow types.
ariaLabel,menuAriaLabelandloadingare missing frompackages/lexical-react/flow/LexicalMenuOption.js.flow,LexicalTypeaheadMenuPlugin.js.flow,LexicalNodeMenuPlugin.js.flowandLexicalAutoEmbedPlugin.js.flow. AGENTS.md ("Type System") asks for both type systems to be kept in step. - Smaller items.
- The bare
try/catcharoundannounce(LexicalMenu.tsx:502-508) also hides unrelated errors;getPeerDependencyFromEditor(editor, '@lexical/a11y/AriaLiveRegion')returnsundefinedwhen the extension is absent. - The unmount cleanup (
:572-580) clears the root captured at mount, so a root swapped while the menu is open keepsaria-owns; readingeditor.getRootElement()inside the cleanup avoids that. MenuOption.ariaLabel's JSDoc could say that only the default renderer applies it, and the custom-renderer example inpackages/lexical-website/docs/react/plugins.mdcould showrole="option"and the label.- Importing the live-region hook brings
@lexical/extension's builder into apps that use the typeahead without extensions (about 6 KB gzip forLexicalComposerplusLexicalTypeaheadMenuPlugin). Making the announcement opt-in would avoid that cost too. - The PR description's test counts (17 new, 13 failing before) predate the latest commit: the file now adds 25 cases, 20 of which fail without the fix. AGENTS.md asks the Test plan for pasted Before/After output.
- The bare
Splitting the table arrow-key coupling into its own issue, as you proposed, fits with this PR leaving aria-controls unchanged; maintainers will confirm.
| * An empty menu then says "Loading" instead of "No results". | ||
| * @default false | ||
| */ | ||
| loading?: boolean; |
There was a problem hiding this comment.
Optional: with the default false, a menu whose lookup takes longer than 500 ms and that hasn't adopted this prop announces "No results" before its options arrive. Treating an unset loading as unknown, and announcing "No results" only when it is explicitly false, would keep those menus silent rather than wrong.
## Description Follows the review on this pull request. - Adds the changeset. - Adds a `getStatusMessage` prop to `LexicalTypeaheadMenuPlugin` and `LexicalNodeMenuPlugin`. It replaces the spoken status line, for translation, and returns `null` to stay silent. - Drops `aria-owns` from the editor. `aria-controls` is unchanged. - Checks for a missing live region instead of catching every error, and reads the editor root inside the cleanup so a replaced root is cleaned up. - Adds the missing props to the Flow types, a note on `MenuOption.ariaLabel`, and role and label to the custom renderer example in the docs. ## Test plan `pnpm exec vitest run --project unit packages/lexical-react`: all pass. The menu tests now unmount each menu after the test, so a pending announcement cannot fire in a later test.
|
Pushed the follow-up for the optional items. The changeset is added. There is now a I left |
There was a problem hiding this comment.
Thanks for the thorough work and for addressing the earlier feedback. The change looks good to me. In Chromium, the playground's default menus now expose a single named listbox that owns its options. The editor's aria-activedescendant follows the highlighted option and is cleared when the list is empty, and axe-core finds no violations on the open menu (the base has three). Custom menuRenderFn menus keep a listbox on their container, aria-owns is gone, aria-controls stays as @lexical/table expects, and the live region is now checked for instead of caught.
All of the suggestions below are optional and non-blocking:
packages/lexical-website/docs/react/plugins.md:599: the custom-renderer example nests a second listbox. WhenmenuRenderFnis set,#typeahead-menuis alreadyrole="listbox"(labelled "Typeahead menu"). The example as written therefore produceslistbox "Typeahead menu" > listbox "Mentions" > option, which axe reports asaria-required-children. To keep one listbox that owns the options, passmenuAriaLabel="Mentions"and give the<ul>role="presentation"(or render the options in a<div>). That is the same shape this PR gives the default menu.packages/lexical-react/src/shared/LexicalMenu.tsx:9: bundle size. In a minified, gzipped esbuild bundle withlexicalandreactexternal, theLexicalTypeaheadMenuPluginentry grows from 4.45 KB to 10.68 KB for apps that don't already bundle@lexical/extension. Writingimport type {AriaLiveRegionExtension}and passing the literal name'@lexical/a11y/AriaLiveRegion', aspackages/lexical-code-core/src/CodeNode.tsdoes, removes about 2.2 KB of that with no change in behavior. The rest can wait for the opt-in follow-up you proposed.- Changeset
Verify:. CONTRIBUTING.md ("Changesets") asks a fix that changes DOM output to say what to test. Please add that a custommenuRenderFnmenu now getsaria-activedescendantonly when itstypeahead-item-Nelement is inside the anchor element, and that the attribute is removed while the list is empty. The paragraph about custom menus in plugins.md could say the same. - Types (nits).
LexicalNodeMenuPlugin.js.flowexportsMenuStatusMessageFnbutLexicalNodeMenuPlugin.tsxdoesn't. TheMenuOptiondeclared inLexicalTypeaheadMenuPlugin.js.flowandLexicalNodeMenuPlugin.js.flowalso lacksariaLabel. AGENTS.md ("Type System") asks for TypeScript and Flow types to be kept in step.
The loading default is still open in the earlier thread: with false, a slow lookup that doesn't pass the prop announces "No results" first. Maintainers should decide that one.
| anchorRef.current | ||
| ? createPortal( | ||
| <ul className="typeahead-menu"> | ||
| <ul className="typeahead-menu" role="listbox" aria-label="Mentions"> |
There was a problem hiding this comment.
Optional: with menuRenderFn set, #typeahead-menu is already role="listbox" labelled "Typeahead menu" (setContainerDivAttributes in shared/LexicalMenu.tsx), so this example as written renders listbox "Typeahead menu" > listbox "Mentions" > option, and axe reports aria-required-children on #typeahead-menu. Passing menuAriaLabel="Mentions" to the plugin and giving this <ul> role="presentation" (or using a <div>) keeps one listbox that owns the options, the same shape the default renderer now has.
| * | ||
| */ | ||
|
|
||
| import {AriaLiveRegionExtension} from '@lexical/a11y'; |
There was a problem hiding this comment.
Optional: this value import keeps @lexical/a11y and the signals runtime in every bundle that uses the typeahead. A type-only import plus the literal name, as packages/lexical-code-core/src/CodeNode.ts does, behaves identically and saves about 2.2 KB gzip (the minified, gzipped entry measured 10.68 KB vs 8.52 KB, with lexical and react external):
import type {AriaLiveRegionExtension} from '@lexical/a11y';
// ...
getPeerDependencyFromEditor<typeof AriaLiveRegionExtension>(
editor,
'@lexical/a11y/AriaLiveRegion',
);
Why
Someone using a screen reader types
:smand a list of emoji suggestions opens. They should hear what the list is called, then the highlighted suggestion and where it sits — "Emojis, list, grinning, 1 of 10" — and one new suggestion each time they arrow. Nothing should be named that they could not have typed.They should also hear how many matches there are, and hear that number change as they keep typing, even when the highlighted entry has not moved. And when nothing matches at all, they should be told so.
A sighted user gets all of that for free. They watch the list shrink as they type, and they watch it empty out and disappear. None of it reaches a screen reader on its own.
What was heard instead was "Typeahead menu, list, list, list item, 1 of 10". The menu announced itself as a list twice over, and then numbered an item without naming it — you are told you are on the first of ten, and not told what the first of ten is. The position was there all along. The thing it was counting was not.
None of this is specific to emoji. Every typeahead menu in the editor is built from one shared component, so the same thing happens in all of them. In the playground that is:
:/@All four are fixed and named here.
Open questions
No live region by default. The count and "No results" go through
AriaLiveRegionExtension. An editor built without it gets the rest of the menu but not those two lines. The menu now checks whether the extension is there and stays quiet when it isn't, where before it caught every error.RichTextExtensionbrings the live region in, so a rich text editor has it.The menu name. Naming a menu "Emojis" or "Mentions" needs
menuAriaLabel, which is optional and falls back to "Typeahead menu". Making it required would break existing apps, so not in this PR. Worth considering for a future major version?The table coupling.
@lexical/tablereadsaria-controlsoff the editor and checks it says exactly'typeahead-menu'. This PR leaves that value alone.Bundle size. Announcing needs
@lexical/extension's builder, about 6 KB gzipped for apps that use the typeahead without extensions. Making the announcement opt-in would avoid it. That is a bigger change than this one, so I would do it as a follow-up.What changed, and what it gives you
The listbox now holds the options directly.
This is what the screen reader was handed before:
The positioning container carried
role="listbox", but the options were not inside it. They sat one level down, inside a<ul>, and a<ul>is a list whether or not you ask for one. That middle line is the second list the user heard, and it is why the suggestions came through as list items: an option is only understood as a choice when the listbox itself holds it.The
<ul>is now the listbox, because it is the element that actually holds the options:Outcome: one list instead of two, and the suggestions are announced as choices rather than as anonymous entries in a list.
The editor points at the current option.
aria-activedescendantsays which option is current, and it names an element that now lives in<body>, outside the editor. In Chromium the reference resolves without any help, so the screen reader follows it as you arrow.Outcome: arrowing through the menu announces each option.
I checked this in Chromium only. I have not tested Firefox or Safari.
The current option is restated, not only written when it moves.
The mentions menu asks a server for its results, so it opens empty and fills a moment later. The code that tells the screen reader which option is current only ran when the highlight moved. Opening empty and then filling is not a move — the highlight sits on the first option before and after — so that code never ran, and the editor went on pointing at nothing.
The user heard the menu open, then heard nothing at all about what was in it.
The pointer is now written from whatever is highlighted right now, rather than only when it changes.
Outcome: a menu that fetches its results announces the highlighted option as soon as they land.
Options can carry their own spoken name.
An emoji option shows the glyph followed by its shortcode:
🙂 slightly_smiling_face. A screen reader already pronounces the glyph from its own emoji dictionary — "slightly smiling face" — and then reads the text beside it as well. So the name is announced twice, the second time as a run of underscored words.The obvious repair is to name the option after the emoji's proper description instead. That reads far better, and it is wrong. Only the shortcode and the tags are searched. A user told the option is "face savouring food" has been handed a name that finds nothing when they type it — the thing that actually matches is "yum".
So
MenuOptiongains anariaLabel: what an option should be called, separate from what it shows.Outcome: an emoji is announced once, by the word you would type to find it.
The menus are named.
Without a name, every menu announces as "list". The emoji picker, the block menu and the mentions menu are indistinguishable by ear — the user has to work out which one they opened from what happens to be in it.
One of the two menu components could not be named at all. The embed prompt is built on the other one, which had no such option, so it always said "Typeahead menu" no matter what it was offering. That prop is added here, so both components can be named.
Outcome: the user hears "Emojis", "Blocks", "Mentions" or "Embed" — the name of the thing they are choosing from, rather than the name of the widget.
The number of matches is spoken aloud.
Nothing in ARIA announces a count, and the moment it matters most is the one with nothing to describe: when the query matches nothing, there is no list left to talk about, so correct markup is silence.
The count goes through the editor's existing aria live region. It is keyed on the count alone, so arrowing through options never talks over the option itself, and debounced, so a burst of typing produces one message once the user stops rather than a queue of counts from keystrokes ago.
The spoken line can be replaced with
getStatusMessage(count, loading)on the typeahead and node menu plugins, to translate it or change the wording. Returningnullkeeps the menu quiet.loadingtells an empty menu to say "Loading" while a lookup runs and "No results" when it finishes. It defaults to false, so a menu that does not pass it says "No results" straight away.Outcome: "10 suggestions available", or "No results". The count describes what is on offer, not how many matched — a menu that caps its list at five reports five, because five is all the user can reach.
What was ruled out, and why
Marking the
<ul>presentational.role="presentation"asks the browser to drop an element's role, which on the<ul>should have collapsed the tree to the right shape. It has no effect here. That<ul>is the scrolling element, scrollable elements can take keyboard focus, and the browser will not drop the role of anything focusable. Nothing warns you — the attribute is accepted and ignored. Hence moving the listbox role onto it instead.Stating the position on each option.
aria-posinsetandaria-setsizelook like the safe belt-and-braces choice. Once the listbox holds its options, the browser derives the position from that relationship and passes it on, so the attributes only restate what it already knows. They were tried and removed; the position is announced without them.Writing the current option only when it changes. A menu that loads its options over the network opens with none. While the list is empty the highlighted index is clamped to -1, and when the options arrive it lands back on 0 — so it never "changes", the write never happens, and the editor points at nothing. Hence writing it from the current highlight instead.
Pointing
aria-controlsat the listbox. The listbox is arguably what the editor controls, so repointing the attribute at it looks like a tidy-up. It breaks arrow keys inside a table.@lexical/tabledecides whether a typeahead is open by comparing that attribute to the literal string'typeahead-menu', so the moment it says anything else the table stops recognising the menu and moves between cells instead of down the list.aria-controlsis left exactly as it was.Owning the listbox with
aria-owns. An earlier version of this PR putaria-ownson the editor. In Chromium it does nothing: the browser does not move the list under an editable element, and it already resolvedaria-activedescendantwithout it. It also meant the editor changed two attributes during a session instead of one. Removed after review.aria-controlsis unchanged.Making the editor a combobox. Pointing at an option this way is usually done by declaring the field a combobox. It is not true here: the editor is a rich text field that briefly offers suggestions, and a user told they are in a combobox will expect a control that is not there. It also has to be undone when the menu closes, and changing the role of the focused element makes a screen reader treat it as something new and read it from the beginning, as though focus had jumped. ARIA 1.2 allows
aria-activedescendanton a plain text box, so none of it is needed.Setting
aria-selectedon every option. Marking the restaria-selected="false"is legal, but some screen readers then say "not selected" after every arrow press. Only the highlighted option carries it.Leaving the emoji in the option's name. Screen readers pronounce the glyph from their own dictionary, so an option named "🙂 slightly_smiling_face" is announced twice. Naming it by the emoji's description instead reads better — "face savouring food" — but only the shortcode and tags are searched, so the user would be hearing a name they cannot type.
What this does not announce
typeahead-item-0). When the top match changes the id does not — screen readers tested re-announce on the content change, so this is left alone.Changeset
.changeset/typeahead-listbox-a11y.md,@lexical/reactpatch.Tests
28 new unit tests in
LexicalMenu.test.tsx, which goes from 18 cases to 46. Run against the original code, 19 of the 46 fail. The rest are guard-rails that already pass.The new ones cover the status line (translated, silent, "Loading" and "No results"), the editor with no live region, and the cleanup when the root element is replaced. The tests now unmount each menu afterwards, because a leftover announcement timer from one test was firing during the next.
pnpm exec vitest run --project unit packages/lexical-react: 308 passed.The Flow check (
flow full-check) found 0 errors.One existing e2e test was modified.
Mentions.spec.mjs→ "Sets correct attributes on typeahead menu container" asserted that the positioning container carriesrole="listbox"and the name. That arrangement is the bug, so the assertions move to the listbox, and check that every option is the listbox's own child.The full e2e suite and the browser tests were last run before this follow-up (e2e: 816 passed, 29 skipped, no failures; browser: 112 passed). I have not re-run them since. I re-checked the assertions of the modified e2e test in Edge against the playground, and they hold.