Skip to content

Fix a11y for ContentEditable - #4580

Merged
zurfyx merged 1 commit into
meta:mainfrom
maximshipko:contenteditable-aria-readonly
May 31, 2023
Merged

zurfyx merged 1 commit into
meta:mainfrom
maximshipko:contenteditable-aria-readonly

Conversation

@maximshipko

Copy link
Copy Markdown
Contributor

This PR fix accessibility of ContentEditable in the non editable mode.

When Lexical editor is not editable, there is no role attribute, but aria-autocomplete has the value 'none'. That breaks our pipeline during a11y check with the critical warning:
image

As I understand the issue, we can not have aria attributes without a valid role. So I keep the role 'textbox' for non-editable case, but at the same time set aria-readonly="true" as suggested in doc

@vercel

vercel Bot commented May 30, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2023 7:43pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2023 7:43pm

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Hi @maximshipko!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 30, 2023
@maximshipko
maximshipko force-pushed the contenteditable-aria-readonly branch from e059b95 to cbf1212 Compare May 30, 2023 17:07
@maximshipko
maximshipko marked this pull request as ready for review May 30, 2023 17:09
id={id}
ref={ref}
role={!isEditable ? undefined : role}
role={role}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be inclined to say that a non-editable textbox does not require to be marked as a textbox. Lexical can be used in a transparent manner to provide the reading the experience. However, given the other aria and the fact that this is still customizable makes me think that this change is reasonable with what we have now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @zurfyx

aria-labelledby={ariaLabelledBy}
aria-multiline={ariaMultiline}
aria-owns={!isEditable ? undefined : ariaOwns}
{...(!isEditable ? {'aria-readonly': true} : null)}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, I believe the use of undefined should do the same

Suggested change
{...(!isEditable ? {'aria-readonly': true} : null)}
aria-readonly={!isEditable ? true : undefined}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! That is more readable 👍

@moy2010

moy2010 commented May 31, 2023

Copy link
Copy Markdown
Contributor

As I understand the issue, we can not have aria attributes without a valid role. So I keep the role 'textbox' for non-editable case, but at the same time set aria-readonly="true" as suggested in doc

FYI, the documentation actually suggests to use a readonly attribute:

[aria-readonly](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-readonly) attribute

Indicates that the user cannot modify the value of the text field. Instead of using aria-readonly, use the semantic <input type="text"> or <textarea> with a readonly attribute.

@maximshipko
maximshipko requested a review from zurfyx May 31, 2023 19:44
@maximshipko

Copy link
Copy Markdown
Contributor Author

@moy2010,
readonly is for form elements (input, select, textarea etc), but Lexical is using editable DIV, that's why aira-readonly

@zurfyx

zurfyx commented May 31, 2023

Copy link
Copy Markdown
Member

LGTM, thank you!

@zurfyx
zurfyx merged commit 044d94a into meta:main May 31, 2023
@zurfyx zurfyx mentioned this pull request Jul 11, 2023
potatowagon added a commit to potatowagon/astryx that referenced this pull request Sep 29, 2026
## User impact

Anyone using a screen reader on content rendered by `RichTextView` — a stored
comment, a saved note, any persisted rich text.

Lexical's `ContentEditable` applies `role="textbox"` even when the editor is not
editable, and `RichTextView` passes it nothing to name itself with. Two effects:

- axe reports `aria-input-field-name` (serious) on every story that mounts a
  view. Both instances sit in `.github/a11y-baseline.json`, so CI has been green
  on a live defect.
- `textbox` is a widget role, so assistive technology presents the content as
  the field's *value* — one flat string. The heading, list and link structure
  this component exists to render is suppressed, which is the larger harm and
  the one a name would not have fixed.

## Problem and solution fit

Why does the defect occur? `RichTextView` renders Lexical's `ContentEditable`
with no props, and Lexical defaults `role` to `textbox` regardless of
editability.

Why does that harm the task? The task is reading published content. A widget
role misdescribes it and flattens its structure; unnamed, it is also announced
with no indication of what it is.

Why does that matter? This is the read path for stored rich text, so the
component's entire output is affected, and the baseline entry means nothing
surfaces it.

The fix says what the element is rather than patching the symptom. A view is
content, so it takes no widget role — which removes the requirement for a name
instead of satisfying it, and restores the structural roles.

## Expected behavior and authority

The read-only *form field* case is already served: `<RichTextEditor isDisabled />`
is a labelled field by construction. `RichTextView`'s own contract is its
docstring — "a read-only renderer for serialized Lexical content… without any
editing affordances" — i.e. content.

Upstream history agrees this is a defect rather than a design choice.
meta/lexical#2912 dropped the role in read mode; meta/lexical#4580 put
it back only because removing it left an orphaned `aria-autocomplete="none"`
that axe reports as `aria-allowed-attr`. The upstream fix is
meta/lexical#9270; this change is the consumer-side fix, which is needed
regardless because `lexical` is a peer dependency pinned at `^0.46.0`.

## Smallest restoration

Three attributes cleared on the view's `ContentEditable`, with the reasoning
recorded next to them:

- `role` — `null`, not `undefined`, because Lexical applies its `textbox`
  default to `undefined`.
- `aria-autocomplete`, `aria-readonly` — widget-only ARIA, invalid without a
  widget role. Clearing the role alone would trade `aria-input-field-name` for
  `aria-allowed-attr`.

Also in scope because they are the same defect:

- `README.md` used `<RichTextView label="Notes" …>`; that prop has never existed
  and does not type-check. Removed.
- The 12 `RichTextEditor::*::aria-input-field-name` baseline entries are
  deleted: the audit reports 9 of them already resolved before this change, and
  this change resolves the rest.
- The `SYNC:` header in `RichTextView.tsx` pointed at
  `RichTextView.test.tsx`, which does not exist; its tests live in
  `RichTextEditor.test.tsx`. Corrected per the repository's update protocol.

`RichTextEditor` is unaffected — it already names its `ContentEditable`.

## Evidence

**Reproduction before the change** — the new test against unmodified source
prints the three attributes:

```
$ npx vitest run packages/richtext -t "renders content rather than a form field"
 FAIL  packages/richtext/src/RichTextEditor.test.tsx > RichTextView > renders content rather than a form field
expected document not to contain element, found <div
  aria-autocomplete="none"
  aria-readonly="true"
  role="textbox"

      Tests  1 failed | 87 skipped (88)
```

axe over the story set, on `main`:

```
$ node .github/scripts/accessibility-audit.js --baseline .github/a11y-baseline.json \
    --fail-on-new --components RichTextEditor
✓ Audited: RichTextEditor / Controlled Persistence - 1 issues
Audit complete: 1 total violations found
0 new, 1 baselined, 9 resolved, 156 baselined for stories outside this run
  ✓ RichTextEditor::Custom Transformers::aria-input-field-name
  ✓ RichTextEditor::Default::aria-input-field-name
  ... (9 baseline entries reported already resolved)
```

**Result after the change**:

```
$ node .github/scripts/accessibility-audit.js --baseline .github/a11y-baseline.json \
    --fail-on-new --components RichTextEditor
✓ Audited: RichTextEditor / Default - 0 issues
... all 15 stories ...
Audit complete: 0 total violations found
0 new, 0 baselined, 0 resolved, 154 baselined for stories outside this run
No new accessibility violations. Gate passed.

$ node .github/scripts/accessibility-audit.js ... --components RichTextView
Owner route: richtext/RichTextView -> lab-richtexteditor--markdown-serializers
✓ Audited: RichTextEditor / Markdown Serializers - 1 issues
0 new, 1 baselined, 0 resolved
```

The one remaining violation on that story is `label` (critical) — the bare
`<textarea>` in the story's own chrome, not the component. Its baseline entry is
kept deliberately; it is a separate defect.

**Representative unchanged path**: `RichTextEditor`'s own stories are in the
same run above and report 0 issues throughout; the 88 richtext unit tests
include its label wiring, status variants and toolbar behaviour.

**Regression test**: three cases in `RichTextEditor.test.tsx` — the view has no
`textbox` role and none of the widget ARIA, and list structure is reachable by
role. The pre-existing "renders serialized content read-only" test asserted
`getByRole('textbox')`, which encoded the defect; it now queries
`[data-lexical-editor]` and keeps asserting `contenteditable="false"`.

## Scope

- [x] One defect is restored.
- [x] Separable contradictory or unsettled tagalongs were removed or split.
- [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
- [x] Public text and artifacts contain no internal Meta context.

## Testing

```
$ npx vitest run packages/richtext
 Test Files  1 passed (1)
      Tests  88 passed (88)

$ pnpm run lint
✖ 82 problems (0 errors, 82 warnings)     # unchanged; pre-existing no-raw-color

$ pnpm run check:sync
✅ All SYNC comments are clean.
```

Real-browser states: the axe runs above drive the built Storybook in Chromium
via Playwright, which is where the role and ARIA are actually observed.

Full `pnpm run test` was run on this branch and on `main` at the same commit.
The suite is flaky at this commit under parallelism — `main` itself reports 76
failures, this branch 64 — and the differing files are CLI template/perf tests
unrelated to this change. All three that differed pass in isolation on this
branch, and one of them fails in isolation on clean `main`.

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 5cbb23bd Deployed May 31, 2023 by vercel[bot]
Preview – lexical — 5cbb23bd Deployed May 31, 2023 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants