Skip to content

[lexical-website] Documentation Update: Document NodeState in $config JSON serialization - #8467

Merged
etrepum merged 5 commits into
meta:mainfrom
markselby9:docs/7781-nodestate-config-json
May 6, 2026
Merged

etrepum merged 5 commits into
meta:mainfrom
markselby9:docs/7781-nodestate-config-json

Conversation

@markselby9

Copy link
Copy Markdown
Contributor

Summary

Addresses #7781:

  • New ### Flat serialization with $config subsection in node-state.md covering how flat keys appear at the top level of a node's JSON and when each key is emitted vs. omitted (default-equality check).
  • New ### Upgrading a legacy JSON property to NodeState recipe with a Before/After ColoredNode example, copied/adapted from the existing Extending TextNode with $config example as suggested in the issue.
  • Parallel pointer in serialization.md JSON section linking readers to the NodeState content before they roll their own exportJSON/importJSON.

The In the future ... see #7260 paragraph is replaced — that issue shipped (it's what this PR documents). A trailing comma in an existing JSON code fence ("question": ...,) was removed in passing.

Test plan

  • pnpm -C packages/lexical-website run build succeeds with onBrokenAnchors: 'throw' and onBrokenMarkdownLinks: 'throw'
  • prettier clean
  • All cross-link anchors verified to resolve

Closes #7781

markselby9 added 3 commits May 6, 2026 11:23
… JSON serialization

Since meta#7258 nodes can declare a `StateConfig` in `$config` with
`flat: true` to lift NodeState values to the top level of the serialized
node, but the NodeState and Serialization concept docs only described
the `'$'`-nested layout. Users had to read the `nodes.mdx` `$config`
example to learn that flat state preserves the legacy JSON shape, which
is also the migration path away from a hand-written
`__property` + `exportJSON`/`importJSON` node.

This PR:

- Adds a "Flat serialization with `$config`" subsection to
  `node-state.md` explaining when state appears at the top level vs.
  under `'$'`, with concrete JSON shapes for both, and notes the
  superclass-key-collision constraint.
- Adds an "Upgrading a legacy JSON property to NodeState" recipe with
  before/after `ColoredNode` snippets, calling out that the on-the-wire
  JSON is unchanged when the state key name and default value match the
  old property.
- Adds a parallel pointer at the top of the JSON section in
  `serialization.md` linking to both new subsections so users landing on
  the generic serialization page can find the `$config`-specific
  behavior.
- Drops the now-stale "in the future ... lift those values to the
  top-level" sentence in the Serialization intro, which referenced
  meta#7260 (the very feature this PR documents as shipped).
- Fixes an invalid trailing comma in the existing `'$'` JSON example.

`pnpm -C packages/lexical-website run build` succeeds; the new anchors
(`#flat-serialization-with-config`,
`#upgrading-a-legacy-json-property-to-nodestate`) resolve cleanly under
the existing `onBrokenAnchors: 'throw'` config.

Closes meta#7781
- Replace inaccurate "throws at editor construction" claim for flat-key
  collisions with the actual silent-shadowing behavior (no runtime check;
  superclass field wins on serialize, value lifted into NodeState on parse).
- Fix "second argument" -> "first argument" when describing the serialized
  key passed to createState (key is the first arg of createState(key, valueConfig)).
…n note

The previous note asserted that the superclass field wins on serialize
when a flat NodeState key collides with a superclass-serialized property.
That's not generally true — TextNode's exportJSON spreads super last, so
NodeState would actually overwrite the superclass field there. Reframe
the note to describe the ambiguity without making a directional claim
either way, since the precedence depends on the superclass's spread order.
@vercel

vercel Bot commented May 6, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment May 6, 2026 5:53am
lexical-playground Ready Ready Preview, Comment May 6, 2026 5:53am

Request Review

@meta-cla meta-cla 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 6, 2026
Comment on lines +279 to +287
:::tip

To stage the migration without breaking existing payloads, keep the
serialized key name the same as the old property name (the first
argument to `createState`) and keep the default value identical to the
one your old `parse`/`updateFromJSON` produced for missing or invalid
input. Existing JSON will then deserialize unchanged.

:::

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The wording here is confusing, all that really matters is that the name used in the JSON representation is the same.

It's probably also worth mentioning that migrating from a not-flat NodeState to a flat NodeState also works. State that appears in the $ (NODE_STATE_KEY) will still be parsed even if the state is configured to be flat (but the flattened value takes precedence if they are both present).

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.

Tightened the wording in 250b903 and added the non-flat → flat migration note (with the flat-takes-precedence detail). Thanks for the pointer.

Comment on lines +193 to +199
on `TextNode`). There's no runtime check, but the resulting JSON shape is
ambiguous and not round-trip stable: which value wins on serialize depends
on the order the superclass's `exportJSON` spreads its own fields and
`super.exportJSON()`, and on parse the top-level value is lifted into
NodeState while the superclass's `updateFromJSON` may also reassign it to
the original property — leaving both representations populated and prone
to drift.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think the documentation needs to go into this much detail about what happens when you break the rules, will be easier for people to understand "don't do this" rather than "don't do this [with lots of extra text to gloss over]"

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.

Trimmed the note in 250b903 — kept just the rule, dropped the failure-mode explanation.

…g docs

Two changes per inline review on meta#8467:

- Trim the flat-key collision note to the actionable rule ("don't do
  this") and drop the multi-clause explanation of the failure mode.
- Tighten the migration tip to focus on what actually matters (keeping
  the JSON key name the same). Add the non-flat → flat migration note:
  state under `$` (NODE_STATE_KEY) still parses when configured flat,
  and the flat value takes precedence when both are present.
The previous claim that exportJSON/importJSON/updateFromJSON/clone/afterCloneFrom
are "all generated from $config" was loose. Per LexicalUtils.ts the $config
machinery only installs clone and importJSON; the others are inherited from
LexicalNode/TextNode and already understand NodeState. Reframe as
"no override needed" so the doc doesn't make a generation claim it can't back.
@etrepum
etrepum added this pull request to the merge queue May 6, 2026
Merged via the queue into meta:main with commit 33d3a4e May 6, 2026
9 checks passed
@etrepum etrepum mentioned this pull request May 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — d62cacc8 Deployed May 6, 2026 by vercel[bot]
Preview – lexical-playground — d62cacc8 Deployed May 6, 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 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.

Feature: Update NodeState and JSON documentation for $config

2 participants