Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(richtext): RichTextView renders document content, not an unnamed textbox (#6977)

Cindy Zhangfcd44d1c2377
diff --git a/.github/a11y-baseline.json b/.github/a11y-baseline.json
index 7bcdcf8bc7..6a05bf4076 100644
--- a/.github/a11y-baseline.json
+++ b/.github/a11y-baseline.json
@@ -548,71 +548,11 @@
"impact": "serious",
"helpUrl": "https://dequeuniversity.com/rules/axe/4.12/color-contrast?application=playwright"
},
- {
- "key": "RichTextEditor::Controlled Persistence::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::Custom Transformers::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::Default::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::Error Status::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::Imperative Ref::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::Markdown Serializers::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
{
"key": "RichTextEditor::Markdown Serializers::label",
"impact": "critical",
"helpUrl": "https://dequeuniversity.com/rules/axe/4.12/label?application=playwright"
},
- {
- "key": "RichTextEditor::Read Only::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::Required::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::With Character Limit::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::With Description::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::With Initial Value::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
- {
- "key": "RichTextEditor::With Toolbar::aria-input-field-name",
- "impact": "serious",
- "helpUrl": "https://dequeuniversity.com/rules/axe/4.12/aria-input-field-name?application=playwright"
- },
{
"key": "Schedule::Async Loader::color-contrast",
"impact": "serious",
diff --git a/packages/richtext/README.md b/packages/richtext/README.md
index 51a5c3d472..ccb6798d0d 100644
--- a/packages/richtext/README.md
+++ b/packages/richtext/README.md
@@ -7,7 +7,7 @@ with Astryx design tokens.
import {RichTextEditor, RichTextView} from '@astryxdesign/richtext';
<RichTextEditor label="Notes" onChange={setState} />;
-<RichTextView label="Notes" value={serializedState} />;
+<RichTextView value={serializedState} />;
```
The editor is deliberately minimal and extensible: pass `nodes` and `plugins` to
diff --git a/packages/richtext/src/RichTextEditor.test.tsx b/packages/richtext/src/RichTextEditor.test.tsx
index 0ef8f2a59e..cc06dcc919 100644
--- a/packages/richtext/src/RichTextEditor.test.tsx
+++ b/packages/richtext/src/RichTextEditor.test.tsx
@@ -948,16 +948,33 @@ describe('RichTextEditor Tab keyboard trap escape (WCAG 2.1.2)', () => {
describe('RichTextView', () => {
it('renders serialized content read-only', async () => {
- render(<RichTextView value={HELLO_STATE} />);
+ const {container} = render(<RichTextView value={HELLO_STATE} />);
await waitFor(() =>
expect(screen.getByText('Hello world')).toBeInTheDocument(),
);
- expect(screen.getByRole('textbox')).toHaveAttribute(
+ expect(container.querySelector('[data-lexical-editor]')).toHaveAttribute(
'contenteditable',
'false',
);
});
+ it('renders document content rather than a form field', async () => {
+ const {container} = render(<RichTextView value={HELLO_STATE} />);
+ await waitFor(() =>
+ expect(screen.getByText('Hello world')).toBeInTheDocument(),
+ );
+ // Lexical's ContentEditable applies role="textbox" and widget-only ARIA
+ // even when it is not editable. A view is content: an unnamed widget
+ // fails axe aria-input-field-name, and the widget role exposes the
+ // document as a form field rather than as content.
+ expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
+ const content = container.querySelector('[data-lexical-editor]');
+ expect(content).not.toBeNull();
+ expect(content).not.toHaveAttribute('role');
+ expect(content).not.toHaveAttribute('aria-readonly');
+ expect(content).not.toHaveAttribute('aria-autocomplete');
+ });
+
it('renders custom read-only plugins passed via the plugins prop', () => {
render(
<RichTextView
@@ -999,7 +1016,9 @@ describe('RichTextView', () => {
expect(onError.mock.calls[0][0]).toBeInstanceOf(Error);
expect(screen.getByTestId('view-fallback')).toBeInTheDocument();
// No editor surface is rendered in the error state.
- expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
+ expect(
+ document.querySelector('[data-lexical-editor]'),
+ ).not.toBeInTheDocument();
});
it('renders bullet lists with a disc marker (not bare indentation)', async () => {
@@ -1050,7 +1069,9 @@ describe('RichTextView', () => {
it('renders nothing (no crash) on malformed JSON with no fallback', () => {
expect(() => render(<RichTextView value={'garbage'} />)).not.toThrow();
- expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
+ expect(
+ document.querySelector('[data-lexical-editor]'),
+ ).not.toBeInTheDocument();
});
it('updates the rendered content when the value prop changes', async () => {
diff --git a/packages/richtext/src/RichTextView.tsx b/packages/richtext/src/RichTextView.tsx
index 746ee1a4eb..b484382ca9 100644
--- a/packages/richtext/src/RichTextView.tsx
+++ b/packages/richtext/src/RichTextView.tsx
@@ -7,11 +7,12 @@
* @input Uses React, Lexical (lexical + @lexical/react, composed through
* LexicalExtensionComposer), design tokens
* @output Exports RichTextView component and RichTextViewProps
- * @position Read-only renderer for serialized Lexical editor state; experimental
- * (richtext), exported from @astryxdesign/richtext
+ * @position Read-only renderer for serialized Lexical editor state, exposed to
+ * assistive technology as document content rather than a form field;
+ * experimental (richtext), exported from @astryxdesign/richtext
*
* SYNC: When modified, update these files to stay in sync:
- * - /packages/richtext/src/RichTextView.test.tsx
+ * - /packages/richtext/src/RichTextEditor.test.tsx
* - /packages/richtext/src/index.ts
* - /apps/storybook/stories/RichTextEditor.stories.tsx
*/
@@ -55,6 +56,33 @@ const DEFAULT_NODES: ReadonlyArray<Klass<LexicalNode>> = [
CodeHighlightNode,
];
+/**
+ * Lexical's `ContentEditable` renders `role="textbox"` and widget-only ARIA
+ * (`aria-autocomplete`, `aria-readonly`) even when the editor is not editable.
+ * A view renders document content, not a form field, so all three are cleared:
+ *
+ * - A textbox is a widget, so an unnamed one fails axe
+ * `aria-input-field-name` — and a view has no name to give, because it is
+ * content rather than a labelled input.
+ * - The widget role exposes the rendered document to assistive technology
+ * as a form field rather than as the content it is.
+ * - Widget-only ARIA is invalid without a widget role, so clearing only the
+ * role would trade `aria-input-field-name` for `aria-allowed-attr`.
+ *
+ * `role` must be `null`, not `undefined`: Lexical substitutes its `textbox`
+ * default for `undefined`. The ARIA keys reach the element through Lexical's
+ * trailing prop spread, which is why `undefined` clears them. A proposed
+ * upstream fix (facebook/lexical#9270) would make these overrides no-ops.
+ *
+ * A caller who wants read-only form-field semantics instead wants
+ * `<RichTextEditor isReadOnly />`, which is a labelled field by construction.
+ */
+const VIEW_CONTENT_EDITABLE_PROPS = {
+ 'aria-autocomplete': undefined,
+ 'aria-readonly': undefined,
+ role: null as unknown as undefined,
+} as const;
+
export interface RichTextViewProps extends BaseProps {
/**
* Serialized editor state to render (a JSON string produced by
@@ -133,6 +161,10 @@ function SyncValuePlugin({value}: {value: string}): null {
* A read-only renderer for serialized Lexical content. Renders the same styled
* output as {@link RichTextEditor} without any editing affordances.
*
+ * The output is document content, not a form field: headings, lists and links
+ * keep their own roles for assistive technology, and the view takes no label.
+ * For a labelled, read-only field, use `<RichTextEditor isReadOnly />`.
+ *
* @example
* ```
* import {RichTextView} from '@astryxdesign/richtext';
@@ -231,7 +263,7 @@ export function RichTextView({
contentEditable={null}>
<SyncValuePlugin value={value} />
<RichTextPlugin
- contentEditable={<ContentEditable />}
+ contentEditable={<ContentEditable {...VIEW_CONTENT_EDITABLE_PROPS} />}
placeholder={null}
ErrorBoundary={LexicalErrorBoundary}
/>