Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Chrome dev mode scrolls into center aligned text editor #4975

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions @types/navigator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Navigator {
userAgentData?: { brands: Array<{ brand: string; version: string }> };
}
15 changes: 15 additions & 0 deletions apps/builder/app/canvas/features/text-editor/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ const CaretColorPlugin = () => {
return null;
};

const isChrome = () =>
navigator.userAgentData?.brands.some(
(brand) => brand.brand === "Google Chrome"
);

const OnChangeOnBlurPlugin = ({
onChange,
}: {
Expand All @@ -187,6 +192,16 @@ const OnChangeOnBlurPlugin = ({

useEffect(
() => () => {
// Ensures editable content is saved if no blur event occurs before unmount.
// This can happen in Firefox and Safari.
// To reproduce: create a Content Block, edit a paragraph, then type `/` and select Heading or Paragraph from the menu.
// Without this, changes may be lost on unmount in FF and Safari.

if (isChrome()) {
// Fixes an issue in DEV MODE where, if text is center-aligned inside Flex/Grid,
// the code below causes Chrome to scroll the editable text block to the center of the view.
return;
}
// The issue is related to React’s development mode.
// When we set the initial selection in the Editor, we disable Lexical’s internal
// scrolling using the update operation tag tag: "skip-scroll-into-view".
Expand Down