Skip to content

feat(richtext): add a selection toolbar for the editor - #8722

Draft
ernestt wants to merge 7 commits into
richtext-link-cardfrom
richtext-selection-toolbar
Draft

ernestt wants to merge 7 commits into
richtext-link-cardfrom
richtext-selection-toolbar

Conversation

@ernestt

@ernestt ernestt commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #8718 (link popover plugin), which sits on #11189 (shared link popover and format helpers) and #8717 (toolbar link popover). Its diff shows only the selection toolbar; it retargets to main once those land.

User need

A writer formats words where they are working. In a long RichTextEditor document, or in one with no top toolbar, applying bold or a link to selected text means reaching the toolbar at the top of the field, or knowing the shortcut. Document editors put formatting beside the selection. RichText could not, because a caller cannot position UI against the selection inside the editor without reaching into Lexical's DOM.

Problem and solution fit

  • Primary. A new exported RichTextEditorSelectionToolbar, a plugin added through RichTextEditor's plugins slot. The editor gains no prop.
  • When it shows. While the editor is editable, beside a non-collapsed selection, or a selection of table cells. It never takes focus when it appears.
  • Controls. Bold, Italic, Underline, Strikethrough, Inline code, and Link, then endContent after a divider.
  • Keys. It is built on core Toolbar, so it has role="toolbar", a translated label ("Selected text"), and roving focus.
    • Alt+F10 (⌥F10) moves focus into it, and aria-keyshortcuts="Alt+F10" announces this.
    • Arrow keys move along it.
    • Escape returns focus to the editor with the selection kept.
    • Cmd/Ctrl+K opens its link popover only when no RichTextEditorToolbar took the key first.
  • Placement. It is an anchored layer (core useLayer, context mode). A one-pixel anchor sits at the selection's focus inside the element that positions the editable text, as tall as the selected lines, and CSS anchor positioning keeps the toolbar beside it: above a one-line selection or one made upwards, below one made downwards over lines, flipping when there is no room.
    • It overlays the document and never reflows it.
    • It scrolls with the text, a clipping ancestor does not cut it off, and it hides while the selection is scrolled out of view.
    • The anchor moves only when the selection or the text's layout changes; no script follows a scroll.
  • Read-only and disabled editors show no formatting.
  • Supporting changes. One catalog message, with a @astryxdesign/core changeset; doc.mjs guidance, the README, index.ts, publicSurface.test.ts, the story "With selection toolbar", unit tests, and a browser spec.

Current specification

  • AST-106 admits it. The admitted-affordance table lists the selection format bar: it formats selected text from a bar beside the selection, the value keeps nothing, and it adds no Markdown form. FR10: it ships as a plugin through the existing plugins prop, and neither RichTextEditor nor RichTextView gains a prop. FR7: every control is reachable by keyboard from the selection and named for assistive technology. FR6: its chrome overlays the document. FR8: it needs no caller services.
  • Layer runtime. INV21: the toolbar follows its anchor through CSS anchor positioning only. AST-027 FR7–FR8: a floating surface is a layer, so overflow does not clip it.
  • Other constraints. architecture:internationalization INV1a: its label is in the catalog. architecture:public-component-api INV5: its own aria-keyshortcuts cannot be overwritten by caller props.
  • Out of scope. A bar docked above the on-screen keyboard is AST-106's separate "Phone keyboard bar" affordance (FR9), so this toolbar has no docked presentation and no presentation prop.

Public delta and caller burden

  • New exports. RichTextEditorSelectionToolbar, plus the types RichTextEditorSelectionToolbarProps and RichTextEditorSelectionToolbarFormat.
  • Plugin options. formats (which formats appear, in order), hasLink, hasNewTabLinks (match the toolbar's value), label, endContent, and base props and ref.
  • No new caller decision. The defaults give the whole feature.
  • Nothing changes for existing callers.
<RichTextEditor
  label="Notes"
  plugins={<RichTextEditorSelectionToolbar />}
/>

Design and alternatives

  • Shape. A separate plugin component, not a toolbar prop. Callers who want only floating formatting, as in comment composers, use it without the top toolbar.
    • Rejected: a RichTextEditorToolbar mode. One toolbar would render in two places, and its Selector and history controls do not belong beside a selection.
  • Layer. A context layer anchored with CSS to a one-pixel box at the selection, the pattern core ContextMenu uses for its cursor anchor.
    • Rejected: absolutely positioned DOM placed by script, which an overflow: hidden ancestor clips and which stays pinned at the viewport edge when the selection scrolls away.
  • Escape in the editor is left alone. It keeps its meaning: Escape then Tab leaves the editor, and with the toolbar shown, Tab reaches it.

Evidence

Red arm:

  • The layer test (the toolbar is a popover layer CSS-anchored to a box beside the editable text) fails against the previous absolutely-positioned version and passes now.
  • RichTextEditorSelectionToolbar.test.tsx on the parent branch cannot import the module, so no tests run.

Real-browser screenshots: the browser spec below runs in CI once the stack retargets to main. The stills previously posted here showed the earlier script-placed version and its docked presentation, so they are withdrawn; current desktop and phone stills follow from that run.

Scope

  • One capability is added.
  • Unrelated fixes, cleanup, and policy changes were removed or split.
  • Consumer docs, focused tests, and migration evidence ship with the feature.
  • Public text and artifacts contain no internal Meta context.

Testing

  • pnpm exec vitest run packages/richtext/: 49 files, 803 tests pass. The selection toolbar's suite covers:
    • showing and hiding, and never taking focus;
    • the anchored layer, outside the editable text;
    • the value exporting the same Markdown while it shows;
    • controls, their order, formats, hasLink, and endContent;
    • formatting and pressed state, and its link popover;
    • Alt+F10, arrows, and Escape, including Escape from a control reached another way;
    • Cmd/Ctrl+K alone and beside RichTextEditorToolbar;
    • table cells, read-only, and disabled.
  • Pre-commit hooks (lint-staged ESLint and Prettier, pnpm check:repo) pass.
  • The browser spec (Chromium, against the built Storybook) checks: the toolbar sits clear of the selected words and moves no paragraph; it is an open popover layer with a CSS anchor; axe reports no violations; Alt+F10, ArrowRight, and Escape with the selection kept; a keyboard format; it keeps its place beside the words when the page scrolls; an overflow: hidden ancestor does not clip it; and at a 390 px phone width it fits and moves no paragraph.

Co-authored by @ernestt (original author) and @cixzhang.

Owner and land path

Owner: the Astryx fix loop, lane 3. Carried by lane 3 for the contributor. Waits on the owner's decision for the selection toolbar; after it, Specs, CodeBunny and green CI; the drain merges.

@vercel

vercel Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 14, 2026 11:08pm UTC

Request Review

@astracat-bot

astracat-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
❓ Needs maintainer input (for maintainers only) Nov 14, 2026, 11:19 PM UTC

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 27, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the selection toolbar. Approval is on hold because no current spec governs a new public RichText component like RichTextEditorSelectionToolbar: its props, the Alt+F10 and Escape interaction, placement, hosting and the docked touch presentation. An engineering owner needs to provide or identify that governing spec and records the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so if they are an engineering owner. These need fixing whatever that decision is:

  • The toolbar is positioned DOM with zIndex: 2 instead of a Layer surface, which spec:AST-027 FR7 and FR8 require for floating surfaces, and an overflow: hidden ancestor cuts it off.
  • When the page scrolls the selection out of view, the toolbar stays pinned at the top of the viewport over unrelated content, because selectionToolbarPlacement always clamps it into view.
  • The docked bar positions itself from the visual viewport instead of measuring the keyboard the way architecture:react-component-runtime INV11 requires outside Core.
  • At 2,740 changed lines the pull request is over the 2,000-line scope limit, and landing the link-popover extraction on its own first would bring it under.

[Automated review]

@github-actions github-actions Bot added the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Oct 31, 2026
ernestt and others added 2 commits November 2, 2026 11:21
RichTextEditorSelectionToolbar, rendered in RichTextEditor's plugins
slot, shows formatting beside a non-collapsed selection while the editor
is editable: bold, italic, underline, strikethrough, inline code, and
Link, with the same commands, glyphs, and link popover as
RichTextEditorToolbar, then any endContent. A selection of table cells
gets the formats without Link.

It never takes focus when it appears. Alt+F10 moves focus into it, arrow
keys move along it, and Escape returns focus to the editor with the
selection kept. It renders beside the editable text and overlays the
document without reflowing it, by the selection's focus on the side away
from the selected text. Under a coarse pointer it docks to the bottom of
the visual viewport; presentation keeps either surface on every device.
Read-only and disabled editors show none.

The toolbar's link popover moves into a module both toolbars share, and
its inline formats and glyph resolver are shared with the new toolbar;
neither is exported from the package.

Co-authored-by: Emily Horsman <[email protected]>

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the selection toolbar. Approval is on hold because no current spec governs a new public RichText component like RichTextEditorSelectionToolbar: its props, when it shows, the Alt+F10 and Escape keys, placement, hosting, the docked touch presentation and its look. An engineering owner needs to provide or identify that governing spec and records the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so if they are an engineering owner. These need fixing whatever that decision is:

  • The toolbar is positioned DOM with zIndex: 2 instead of a Layer surface, which spec:AST-027 FR7 and FR8 require for floating surfaces, so an overflow: hidden ancestor cuts it off.
  • When the page scrolls the selection out of view, the toolbar stays pinned at the top of the viewport, where a press formats words the writer cannot see.
  • The focus and drag tracking copies LinkCard's; one shared RichText hook, installed from the editor's root listener, should serve both.
  • The isShownRef Effect synchronizes no external system, and the docked bar measures the keyboard from the visual viewport rather than as architecture:react-component-runtime INV11 asks outside Core.
  • At 2,739 changed lines the pull request is over the 2,000-line scope limit; landing the link-popover extraction on its own first brings it under.

[Automated review]

The toolbar was absolutely positioned DOM placed by script, so an
ancestor that clips its overflow cut it off and it stayed pinned at the
viewport edge when the selection scrolled away. It is now a core
useLayer context layer, CSS-anchored to a one-pixel box that sits at the
selection inside the element that positions the editable text. The box
moves only when the selection or the text's layout changes; the toolbar
follows it with CSS, scrolls with the text, and hides while the
selection is out of view.

The docked touch presentation and its `presentation` prop are out: a bar
above the on-screen keyboard is its own affordance. The toolbar's keys
register with the editor only while it shows, so no Effect mirrors its
shown state into a ref.

Co-authored-by: Ernest Tien <[email protected]>
Takes #8718's shared link popover, URL form, and inline-format helpers (from #11189) in place of this branch's own extraction, and its useEditorFocusAndDrag in place of this toolbar's own focus and drag tracking. Table selections read their formats here, since the shared helper covers range and node selections.

Co-authored-by: Ernest Tien <[email protected]>

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the selection toolbar, which now hosts through Layer and no longer pins to the viewport edge. Approval is on hold because AST-106 admits a selection format bar as a RichText plugin but leaves its exact options, events and visuals to a decision not yet recorded: the props and their defaults, when the bar shows, Alt+F10, Escape and Cmd/Ctrl+K, placement, table selections and the "Selected text" name. An engineering owner needs to provide or identify that governing spec (an AST-106 amendment or a component spec) and record the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so if they are an engineering owner.

These need fixing whatever that decision is:

  • The bar adds its own editor update listener and root ResizeObserver (RichTextEditorSelectionToolbar.tsx:446-484) instead of placing through RichText's shared registerEditorReposition, which the link popover plugin, code block headers and task checkboxes already use.
  • During a mouse drag the bar re-measures the selection and rewrites its anchor on every selection change while it is hidden (14 times in one three-line drag), which architecture:react-component-runtime INV18 rules out; measuring only while the bar shows, through the shared scheduler, fixes it.
  • The description still describes a presentation prop, a RichTextEditorSelectionToolbarPresentation export, a docked touch bar, a non-Layer host and a 21-test suite, none of which the code ships, so it needs rewriting to match the code.
  • The title's richtext scope names no component and ## Scope has no Components: line, so add Components: RichTextEditorSelectionToolbar (or scope the title to it) for the new export and its two types.

Automated review (maintainer link)

Comment on lines +446 to +484
useEffect(
() =>
mergeRegister(
editor.registerEditableListener(editable => setIsEditable(editable)),
editor.registerUpdateListener(({editorState}) => {
const next = editorState.read($readFacts);
if (!sameFacts(factsRef.current, next)) {
factsRef.current = next;
setFacts(next);
}
placeAtSelection(next);
}),
),
[editor, placeAtSelection],
);

// Text that wraps differently (a narrower field, a font that loads) moves
// the selection inside the editor, so the anchor moves with it. Syncs with
// the root element Lexical attaches.
useEffect(() => {
if (typeof ResizeObserver === 'undefined') {
return;
}
const observer = new ResizeObserver(() =>
placeAtSelection(factsRef.current),
);
const unregister = editor.registerRootListener((next, previous) => {
if (previous != null) {
observer.unobserve(previous);
}
if (next != null) {
observer.observe(next);
}
});
return () => {
unregister();
observer.disconnect();
};
}, [editor, placeAtSelection]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This update listener and the ResizeObserver Effect below re-create what registerEditorReposition (editorReposition.ts) already owns: "the one reposition scheduler for chrome placed beside the editable text", measuring once per frame after an editor update, a root resize or a window resize. CodeBlockHeaderPlugin, TaskCheckboxPlugin and RichTextEditorLinkPopoverPlugin call it, so a fix there would miss this bar. Keep this listener for the selection facts only. Place the anchor once as the bar shows, then call registerEditorReposition(editor, () => placeAtSelection(factsRef.current)) while it shows, as RichTextEditorLinkPopoverPlugin.tsx:497-511 does, and delete the ResizeObserver Effect.

factsRef.current = next;
setFacts(next);
}
placeAtSelection(next);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This runs on every editor update, including each selection change while a pointer drags, and the bar is hidden then (!isDragging at line 409). Each call reads the range's client rects and the frame's box, then writes the anchor's top/left/height. In Chromium, one drag over three lines made 60 pointer moves, 15 selection changes and 14 placements, all with the bar hidden. architecture:react-component-runtime INV18 keeps layout reads off such paths. Measuring only while the bar shows, through the shared scheduler, fixes it.

endContent?: ReactNode;
}

/** Room between the toolbar and the selection, and inside its band. */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking (INV29): "and inside its band" describes the band clamp this head removed. Suggest: /** Room between the toolbar and the selection. */

};
}, [editor, placeAtSelection]);

// Shows the anchored layer while the toolbar is shown.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking (INV29): this restates the code. A note that names the system keeps only the reason, for example // Syncs the native popover with whether the bar shows.

cixzhang and others added 2 commits November 11, 2026 02:37
…TabLinks

A first version keeps only the options RichTextEditorToolbar already has with the same meaning and default. formats, endContent, and label are removed: the bar offers all five formats and names itself from the catalog.

Co-authored-by: Ernest Tien <[email protected]>
Takes #8717's non-scaling wrapper anchor and useToolbarLink's buttonRef. The selection toolbar's Link button gets the same wrapper, so its link popover anchors to a box a press never scales.

Co-authored-by: Ernest Tien <[email protected]>

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the selection toolbar, which now offers only the two options RichTextEditorToolbar already has. Approval is on hold because spec:AST-106 admits a selection format bar as a RichText plugin but leaves its exact options, events and visuals to a decision not yet recorded: the hasLink and hasNewTabLinks props and their defaults, when the bar shows, Alt+F10, Escape and Cmd/Ctrl+K, placement, table selections and the "Selected text" name. An engineering owner needs to provide or identify that governing spec (an AST-106 amendment or a component spec) and record the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so if they are an engineering owner.

These need fixing whatever that decision is:

  • The bar adds its own editor update listener and root ResizeObserver (RichTextEditorSelectionToolbar.tsx:420-458) instead of placing through RichText's shared registerEditorReposition, which the link popover plugin, code block headers and task checkboxes already use.
  • During a mouse drag the bar re-measures the selection and rewrites its anchor on every selection change while it is hidden (6 times in one three-line drag), which architecture:react-component-runtime INV18 rules out; measuring only while the bar shows, through the shared scheduler, fixes it.
  • The description lists formats, label, endContent and a RichTextEditorSelectionToolbarFormat export that the code no longer ships, so its Controls, Public delta and Testing parts need rewriting to the two props it has.
  • Miss: the "With selection toolbar" story says the bar docks to the bottom of the screen under a coarse pointer (RichTextEditor.stories.tsx:194-195), but it floats beside the selection there too, so that sentence needs removing.
  • The title's richtext scope names no component and ## Scope has no Components: line, so add Components: RichTextEditorSelectionToolbar (or scope the title to it) for the new export and its props type.

Automated review (maintainer link)

Comment on lines +194 to +195
* moves focus into it and Escape returns to the selection. Under a coarse
* pointer it docks to the bottom of the screen.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Miss from the last review: this head has no docked presentation. Under a coarse pointer at 390 px the bar floats beside the selection like any other, so readers of the story are told about a mode that does not exist. Suggest dropping the last sentence:

/**
 * Formatting beside selected text, from the `plugins` slot. Alt+F10 (⌥F10)
 * moves focus into it and Escape returns to the selection.
 */

'sharedEditorTheme',
'URL_MATCHER',
'EMAIL_MATCHER',
'selectionToolbarPlacement',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking: nothing in the package defines selectionToolbarPlacement (placement is the module-local placeAnchor), so this name can never be exported and the check can never fail. Dropping it, and "the selection toolbar's placement" from the index.ts header, keeps both honest.

Brings the link popover plugin's retarget onto main. Main moved ToolbarLinkPopover under internal/, so the selection toolbar imports it from there.
@cixzhang
cixzhang marked this pull request as draft November 14, 2026 22:53
@cixzhang cixzhang added the ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) label Nov 14, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the selection toolbar. Approval is on hold because spec:AST-106 admits a selection format bar as a RichText plugin but leaves its exact options, events and visuals to a decision not yet recorded: the hasLink and hasNewTabLinks props and their defaults, when the bar shows, Alt+F10, Escape and Cmd/Ctrl+K, placement, table selections and the "Selected text" name. An engineering owner needs to provide or identify that governing spec (an AST-106 amendment or a component spec) and record the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so if they are an engineering owner.

These need fixing whatever that decision is:

  • The bar adds its own editor update listener and root ResizeObserver (RichTextEditorSelectionToolbar.tsx:420-458) instead of placing through RichText's shared registerEditorReposition, which the link popover plugin, code block headers and task checkboxes already use.
  • During a mouse drag the bar re-measures the selection and rewrites its anchor on every selection change while it is hidden (6 times in one three-line drag), which architecture:react-component-runtime INV18 rules out; measuring only while the bar shows, through the shared scheduler, fixes it.
  • The description lists formats, label, endContent and a RichTextEditorSelectionToolbarFormat export that the code does not ship, so its Controls, Public delta and Testing parts need rewriting to the two props it has.
  • The "With selection toolbar" story says the bar docks to the bottom of the screen under a coarse pointer (RichTextEditor.stories.tsx:225-226), but it floats beside the selection there too, so that sentence needs removing.
  • The title's richtext scope names no component and ## Scope has no Components: line, so add Components: RichTextEditorSelectionToolbar (or scope the title to it) for the new export and its props type.
  • Newly live check: architecture:component-import-boundaries INV10 asks a pull request that adds an optional feature to report the first-load gzip delta of a page that renders the component without it, so the description needs that base-against-head number for a RichTextEditor page without the bar.
Claims and files checked

Review mode: full; reason: author-not-engowner

  • Claim: the richtext changeset's bar (five formats and Link, never takes focus, Alt+F10, arrows, Escape, overlays without reflow, unclipped, read-only and disabled show none, two props plus base props and ref, a translated name): verified in code, unit tests and a Chromium run of the story.
  • Claim: the core changeset's new "Selected text" message: verified in the English catalog.
  • Claim: the description's formats, label, endContent and format-type export: false; the code has hasLink and hasNewTabLinks only.
  • Claim: the description's test count (49 files, 803 tests): stale; the RichText suites now run 52 files and 860 tests, all passing.
  • Claim: Cmd/Ctrl+K opens the bar's link form only when the top toolbar does not take it, and table selections get no Link: verified by unit tests.
  • Claim: no docked presentation: verified in code and at a 390 px touch width; the story still says otherwise.
  • File: RichTextEditorSelectionToolbar.tsx: shared-scheduler and drag-measurement findings; stale header and constant comments (non-blocking).
  • File: RichTextEditor.stories.tsx: docking sentence finding.
  • File: index.ts: header names a placement module that does not exist (non-blocking).
  • File: publicSurface.test.ts: checks a name nothing defines (non-blocking).
  • Files: both changesets, README.md, RichTextEditor.doc.mjs, en.json, RichTextEditorSelectionToolbar.test.tsx, the Chromium spec and RichTextEditorToolbarLink.test.tsx: clear.

Automated review (maintainer link)

Comment on lines +12 to +13
* selection shows while the editor is editable, and its props and format
* type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking (INV29), missed in an earlier review: no format type is exported any more (index.ts:54-55 exports the component and its props type). Suggest:

 *   selection shows while the editor is editable, and its props.

This branch was successfully deployed

1 active deployment
Preview — 69be9721 Deployed Nov 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) CLA Signed This label is managed by the Meta Open Source bot. needs:code-review High-risk change (new package/component/API) — needs human code review before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants