Skip to content

fix(lab): make RichTextView react to value prop changes - #4671

Merged
potatowagon merged 2 commits into
meta:mainfrom
potatowagon:fix-richtextview-reactive-value
Aug 4, 2026
Merged

potatowagon merged 2 commits into
meta:mainfrom
potatowagon:fix-richtextview-reactive-value

Conversation

@potatowagon

Copy link
Copy Markdown
Contributor

Problem

In the RichTextEditor Markdown Serializers story, section "3. Same JSON rendered read-only via RichTextView" does not update when the Markdown input changes. Editing the Markdown updates the live <RichTextEditor> (section 2) and the round-tripped Markdown (section 4), but the read-only <RichTextView> stays frozen at its initial content.

Root cause

RichTextView passes value to LexicalComposer's initialConfig.editorState, which Lexical reads once on mount. When value changes afterward, the composer is not re-initialized, so the rendered content never updates. (Section 2's editor sidesteps this with key={json}, which forces a full remount — but RichTextView has no such workaround, and consumers of the component hit the same frozen-content bug.)

Fix

Add a small internal SyncValuePlugin inside the composer that re-applies value whenever it changes, via editor.setEditorState(editor.parseEditorState(value)) — skipping the initial mount (already handled by initialConfig.editorState). This:

  • Mirrors the imperative setEditorState/parseEditorState pattern already used in RichTextEditor.tsx (and its documented reason for avoiding a top-level lexical value import that breaks the sandbox Next build).
  • Keeps schema-invalid updates routing through the existing onParseError → errorFallback path (Lexical's onError).
  • Makes the component reactive for all consumers, not just the story. No public API change.

Tests

Added two regression tests to the RichTextView suite:

  • updates the rendered content when the value prop changes — rerenders with a new value and asserts the content swaps (fails on main without this fix; verified).
  • recovers from a malformed value once a valid value is supplied again — malformed value renders the fallback, then a valid value re-mounts and renders.

All 52 tests in RichTextEditor.test.tsx pass. Lint + check:repo pre-commit checks pass.

LexicalComposer's initialConfig.editorState is only read once on mount, so
<RichTextView value={changingValue} /> froze at its first value and never
updated when value changed. This surfaced in the RichTextEditor 'Markdown
Serializers' story: editing the Markdown input updated the live editor (which
remounts via key={json}) and the round-tripped Markdown, but the read-only
RichTextView stayed stale.

Add an internal SyncValuePlugin that re-applies value via
editor.setEditorState(editor.parseEditorState(value)) whenever it changes
(skipping the initial mount, already handled by initialConfig.editorState).
Mirrors the imperative setEditorState pattern already used in RichTextEditor.tsx
and avoids a top-level lexical value import. Schema-invalid updates still route
through the existing onParseError/errorFallback path.

Add regression tests: RichTextView updates on value change, and recovers from a
malformed value once a valid one is supplied.
@vercel

vercel Bot commented Aug 3, 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 Aug 3, 2026 5:56am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 3, 2026
Reference the canonical pattern SyncValuePlugin mirrors: the Playgrounds
ActionsPlugin uses editor.setEditorState(editor.parseEditorState(...)) from
inside a plugin, and LexicalComposer reads initialConfig.editorState once in a
useMemo(..., []). Comment-only; no behavior change.
@github-actions

github-actions Bot commented Aug 3, 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.

Modified Components

RichTextEditor (@astryxdesign/lab) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1309 -
Complexity N/A Very High (76) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/lab N/A 3.4KB 1.4KB

Accessibility Audit

Status: No accessibility violations detected.


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

github-actions Bot added a commit that referenced this pull request Aug 3, 2026
@potatowagon
potatowagon merged commit 0d69c3a into meta:main Aug 4, 2026
20 of 21 checks passed

This branch was successfully deployed

1 active deployment
Preview — ca4cc190 Deployed Aug 3, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants