Skip to content

Fix floating toolbar positioning - #59

Merged
janglad merged 4 commits into
mainfrom
fix/web-3984-editor-floating-formatting-bar-jumps-when-text-width-changes
Sep 22, 2026
Merged

janglad merged 4 commits into
mainfrom
fix/web-3984-editor-floating-formatting-bar-jumps-when-text-width-changes

Conversation

@janglad

@janglad janglad commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fixes floating selection toolbar jumps and incorrect initial placement.

Before

(notice: micro jumps of bar on bold, wrong positioning on alignment)

Screen.Recording.2026-09-22.at.14.35.05.mov
  • Width-changing formats moved toolbar with selection center.
  • Alignment changes could require second click before toolbar reached correct position.
  • Fresh selections could place toolbar at text-end caret.

Cause

  • Cached selection geometry could race React layout.
  • Transient collapsed DOM ranges were accepted as valid selection geometry.

After

Screen.Recording.2026-09-22.at.14.34.40.mov
  • Add host-controlled horizontal anchoring so toolbars remain stable when selection width changes.
  • Re-read selection geometry during pre-paint placement.
  • Fall back to editor geometry when native range is collapsed.
  • Use SSR-safe layout effects.

New behavior

  • Toolbar anchor stays stable when formatting changes text width.
  • Alignment changes reposition correctly on first click.
  • Fresh selections position against full selected range.

Note

Low Risk
UI-only positioning changes in the selection toolbar with added tests; no auth, data, or document mutation behavior changes.

Overview
Fixes floating selection toolbar jumps and wrong first paint by remeasuring selection geometry after layout and re-reading it when positioning the toolbar.

useSelectionToolbar now bumps a revision on selection, commit, scroll, and resize, applies rect updates in useIsomorphicLayoutEffect, exports resolveSelectionToolbarRect, and ignores collapsed native DOM ranges so placement can fall back to editor geometry when the browser selection is transient.

SelectionToolbar.Content adds horizontalAlign (left | center | right) so hosts can pin the bar to a stable selection edge when width changes (e.g. bold), and computes position from live geometry during the pre-paint layout pass instead of a stale commit rect—so RTL/alignment updates land on the first interaction.

Tests cover collapsed-native fallback, stale measurements, left-anchor stability across width changes, and scroll repositioning; the React package spec and changeset document the new API.

Reviewed by Cursor Bugbot for commit 4c475f6. Bugbot is set up for automated code reviews on this repo. Configure here.

This update introduces a new feature that keeps selection toolbars fixed while formatting operations alter the geometry of selected content. Additionally, it includes a test to ensure the selection toolbar remains stationary across document commits. The `useSelectionToolbar` hook has been modified to maintain the captured rect during formatting, preventing toolbar movement during the same selection.

Key changes:
- New test case for fixed selection toolbar behavior.
- Adjustments to `useSelectionToolbar` to preserve toolbar position during formatting.
- Added documentation for the new feature in the changeset.
This update introduces a feature that allows selection toolbars to be anchored to stable selection edges with horizontal alignment options (left, center, right). The `useSelectionToolbar` hook has been modified to remeasure the toolbar's position after document commits, ensuring accurate placement during formatting changes. Additionally, tests have been added to verify the toolbar's behavior with live geometry adjustments.

Key changes:
- New horizontal alignment options for selection toolbars.
- Updates to `useSelectionToolbar` for live geometry measurement.
- Enhanced tests for toolbar positioning and alignment behavior.
This commit introduces a new test case that verifies the behavior of the selection toolbar when the native selection is collapsed. The test ensures that the toolbar correctly uses the editor's selection geometry to position itself accurately. Additionally, the `useSelectionToolbar` hook has been updated to handle cases where the selection range is collapsed, returning null for the bounding rectangle in such scenarios.

Key changes:
- New test for handling collapsed native selection in the selection toolbar.
- Updates to `useSelectionToolbar` to return null for collapsed selections.
@janglad

janglad commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

bugbot run

1 similar comment
@janglad

janglad commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 4c475f6. Configure here.

@krijnrijshouwer krijnrijshouwer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lovely

@janglad
janglad merged commit 170d69c into main Sep 22, 2026
36 checks passed
@janglad
janglad deleted the fix/web-3984-editor-floating-formatting-bar-jumps-when-text-width-changes branch September 22, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants