Skip to content

feat(lab): add maxLength character counter to RichTextEditor - #4502

Merged
potatowagon merged 2 commits into
meta:mainfrom
potatowagon:navi/feat/richtext-maxlength
Jul 30, 2026
Merged

potatowagon merged 2 commits into
meta:mainfrom
potatowagon:navi/feat/richtext-maxlength

Conversation

@potatowagon

@potatowagon potatowagon commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds a maxLength?: number prop to the experimental RichTextEditor (@astryxdesign/lab). When set, a character counter (current/max) renders below the editor.

Why

Parity gap with TextArea (and with EPS RichTextArea, which this component is intended to eventually replace): there was no way to show a character limit / counter. This adds it following the exact TextArea pattern so the two inputs behave consistently.

How

  • New maxLength prop. When set:
    • A counter (current/max) renders below the editor, right-aligned, using the same styles/tokens as TextArea's counter.
    • Turns red (--color-error) when the plain-text length exceeds maxLength.
    • Does not enforce the limit natively — visual only, matching TextArea (maxLength is advisory; submit-time validation is the consumer's job).
    • An aria-live="polite" region (via VisuallyHidden) announces "N characters remaining" / "N over limit" once past 80% of the limit (COUNTER_WARNING_THRESHOLD, same as TextArea).
    • The counter is associated with the editable surface via aria-describedby.
  • The character count is tracked by a small internal CharCountPlugin that reads $getRoot().getTextContent().length on mount (so defaultValue-seeded content counts) and on every registerUpdateListener — the same in-composer pattern as the existing AutoFocusOnMount plugin.

Docs / stories / tests (SYNC contract)

  • RichTextEditor.doc.mjs — added maxLength to the props table.
  • RichTextEditor.stories.tsx — added a WithCharacterLimit story + a maxLength control.
  • RichTextEditor.test.tsx — added 4 tests: no counter when unset, counter reflects seeded length, over-limit styling + aria announcement, and aria-describedby association.

Testing

CI runs the full build/typecheck/lint/test. (Authored on a devserver without a local pnpm toolchain, so relying on CI for the checks — happy to iterate on any red signal.)

Screenshot 2026-07-30 at 12 25 04 AM

Notes

@vercel

vercel Bot commented Jul 29, 2026

Copy link
Copy Markdown

Deployment failed with the following error:

You don't have permission to create a Preview Deployment for this Vercel project: astryx.

View Documentation: https://vercel.com/docs/accounts/team-members-and-roles

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 29, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Jul 29, 2026
@github-actions

github-actions Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 0B

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@potatowagon
potatowagon enabled auto-merge (squash) July 30, 2026 06:07
Adds a `maxLength?: number` prop. When set, a character counter (current/max)
is rendered below the editor, mirroring the TextArea pattern:

- Counter shows plain-text content length / maxLength.
- Turns red (error styling) when the count exceeds the limit.
- Does NOT enforce the limit natively (visual only), matching TextArea.
- aria-live region announces remaining/over-limit chars past 80% of the limit.
- Counter is associated with the editor via aria-describedby.

The count is tracked by a small internal CharCountPlugin that reads
$getRoot().getTextContent().length on mount and via registerUpdateListener —
same in-composer pattern as AutoFocusOnMount.

- Updated RichTextEditor.doc.mjs props table
- Added WithCharacterLimit storybook story + maxLength control
- Added 4 unit tests (no counter when unset, seeded count, over-limit, aria)
… counter

The maxLength CharCountPlugin imported $getRoot from the top-level 'lexical'
package as a runtime value. That runtime import made the sandbox Next build
resolve and transpile lexical's raw src/*.ts (which uses 'declare' class
fields), which Next's Babel loader rejects — breaking build-sandbox:

  SyntaxError: The 'declare' modifier is only allowed when the
  'allowDeclareFields' option ... is enabled.
  (lexical/src/LexicalEditor.ts, LexicalNode.ts, ...)

Replace $getRoot()/getTextContent() with editor-instance methods that need no
top-level 'lexical' value import: registerTextContentListener for updates and
getRootElement().textContent for the initial seeded count. The 'lexical' import
is now type-only again (erased by Babel), matching the pre-existing pattern.

No behavior change to the counter; test/build-storybook were already green.
@potatowagon
potatowagon force-pushed the navi/feat/richtext-maxlength branch from 7aa4db8 to 6356b59 Compare July 30, 2026 06:29
@vercel

vercel Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Jul 30, 2026 6:32am

Request Review

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Jul 30, 2026
@potatowagon
potatowagon merged commit 48f0085 into meta:main Jul 30, 2026
18 checks passed
github-actions Bot added a commit that referenced this pull request Jul 30, 2026

This branch was successfully deployed

1 active deployment
Preview — 6356b590 Deployed Jul 30, 2026 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 Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants