Skip to content

[lexical-website] Documentation Update: getElementByKey returns a DOM element, not a node - #9192

Merged
etrepum merged 1 commit into
meta:mainfrom
potatowagon:docs-key-management-get-element-by-key
Sep 22, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
potatowagon:docs-key-management-get-element-by-key

Conversation

@potatowagon

Copy link
Copy Markdown
Contributor

Description

The Key-Related APIs snippet in the Key Management guide presents
editor.getElementByKey(key) as a way to "Get node by key", and declares
const node twice in the same block:

// Get node by key
const node = editor.getElementByKey(key);
const node = $getNodeByKey(key);

getElementByKey returns HTMLElement | null — the reconciled DOM element for
that key, not the LexicalNode — and the duplicate const node would not run.
The following lines then call node.getLatest() / node.getWritable(), which
are node methods, so as written they are applied to a DOM element.

This separates the two lookups and gives each its own name and comment.

Test plan

Documentation-only change (markdown is excluded from prettier via
.prettierignore), so the check is that each documented call is described by
what it returns.

Before

$ sed -n '108,112p' packages/lexical-website/docs/concepts/key-management.md
   ```typescript
   // Get node by key
   const node = editor.getElementByKey(key);
   const node = $getNodeByKey(key);

$ grep -n -A2 "  getElementByKey(" packages/lexical/src/LexicalEditor.ts
1727:  getElementByKey(key: NodeKey): HTMLElement | null {
1728-    return this._keyToDOMMap.get(key) || null;
1729-  }

The element lookup is labelled as a node lookup, and const node is redeclared.

After

$ sed -n '108,114p' packages/lexical-website/docs/concepts/key-management.md
   ```typescript
   // Get node by key
   const node = $getNodeByKey(key);

   // Get the DOM element a node key is reconciled to
   const element = editor.getElementByKey(key);

node now comes from $getNodeByKey (packages/lexical/src/LexicalUtils.ts:681),
so the node.getLatest() / node.getWritable() lines that follow apply to a
LexicalNode.

… element, not a node

## Description

The Key-Related APIs snippet in the Key Management guide presents
`editor.getElementByKey(key)` as a way to "Get node by key", and declares
`const node` twice in the same block:

```typescript
// Get node by key
const node = editor.getElementByKey(key);
const node = $getNodeByKey(key);
```

`getElementByKey` returns `HTMLElement | null` — the reconciled DOM element for
that key, not the `LexicalNode` — and the duplicate `const node` would not run.
The following lines then call `node.getLatest()` / `node.getWritable()`, which
are node methods, so as written they are applied to a DOM element.

This separates the two lookups and gives each its own name and comment.

## Test plan

Documentation-only change (markdown is excluded from prettier via
`.prettierignore`), so the check is that each documented call is described by
what it returns.

### Before

```
$ sed -n '108,112p' packages/lexical-website/docs/concepts/key-management.md
   ```typescript
   // Get node by key
   const node = editor.getElementByKey(key);
   const node = $getNodeByKey(key);

$ grep -n -A2 "  getElementByKey(" packages/lexical/src/LexicalEditor.ts
1727:  getElementByKey(key: NodeKey): HTMLElement | null {
1728-    return this._keyToDOMMap.get(key) || null;
1729-  }
```

The element lookup is labelled as a node lookup, and `const node` is redeclared.

### After

```
$ sed -n '108,114p' packages/lexical-website/docs/concepts/key-management.md
   ```typescript
   // Get node by key
   const node = $getNodeByKey(key);

   // Get the DOM element a node key is reconciled to
   const element = editor.getElementByKey(key);

```

`node` now comes from `$getNodeByKey` (`packages/lexical/src/LexicalUtils.ts:681`),
so the `node.getLatest()` / `node.getWritable()` lines that follow apply to a
`LexicalNode`.
@vercel

vercel Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 22, 2026 11:36am UTC
lexical-playground Ready Ready Preview Sep 22, 2026 11:36am UTC

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 Sep 22, 2026
@etrepum
etrepum added this pull request to the merge queue Sep 22, 2026
Merged via the queue into meta:main with commit 6ba3a71 Sep 22, 2026
42 checks passed
@etrepum etrepum mentioned this pull request Sep 28, 2026

This branch was successfully deployed

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

2 participants