Repository navigation
fix: keep editor focus when clicking toolbar buttons (#387) - #388
Merged
Merged
Conversation
Prevent the default mousedown behavior in the shared ActionButton so desktop browsers don't move focus from the editor to the clicked toolbar button. Click handlers, popover/dialog/dropdown triggers and keyboard focus are unaffected.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🤖 Contribute at surge-preview |
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cause
Toolbar buttons are rendered by the shared
ActionButton(a RadixToggle<button>). On desktop browsers,mousedownon a button moves focus to it, so the ProseMirror editor blurs before the click handler runs. Actions like Bold calleditor.commands.toggleBold()without.focus(), so focus stays on the button and the caret disappears. Mobile browsers don't focus buttons on tap, which is why it only happens on desktop.Fix
In
src/components/ActionButton.tsx, callpreventDefault()onmousedown(after anyonMouseDownpassed in by the caller). The button no longer takes focus, so the editor keeps its focus and selection for every toolbar button built onActionButton, not just Bold.clickstill fires, so actions and Radix Popover/Dialog/DropdownMenu triggers (which open onclick/pointerdown) work as before. Their content still moves focus where it needs to.ActionMenuButton(Heading/FontSize/FontFamily dropdowns) is unchanged.How I verified it
pnpm lint: 0 errors (only warnings that were already there)pnpm type-check: passesoxfmt --checkon the changed file: passespnpm build:lib: passes.pnpm test:types: passes.esno --testfor word-export, locale-loading, katex-loader, iframe-utils, ai-client and rangi-performance: all passnode --test tests/bundle-isolation.test.mjs: 1 failure (a.default.extend is not a function). It fails the same way onmain, so this change didn't cause it.document.activeElementbecame the toolbar button, and text typed next was not formatted.activeElementstays.ProseMirrorafter clicking Bold/Italic/Underline, and the next typed text is formatted.aria-haspopuptoolbar triggers (color, align, link, image, video, table, katex, excalidraw, drawer, etc.) and the Heading dropdown behaved the same before and after the change.No unit test added: the repo has no DOM/React component test setup (tests are
node:testwithout jsdom).Fixes #387