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

[lexical-react] Fix: Fix React.startTransition on Webpack + React 17 #6517

Merged
merged 6 commits into from
Aug 15, 2024
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
1 change: 1 addition & 0 deletions packages/lexical-devtools/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"shared/invariant": ["../shared/src/invariant.ts"],
"shared/normalizeClassNames": ["../shared/src/normalizeClassNames.ts"],
"shared/react-test-utils": ["../shared/src/react-test-utils.ts"],
"shared/reactPatches": ["../shared/src/reactPatches.ts"],
"shared/simpleDiffWithCursor": ["../shared/src/simpleDiffWithCursor.ts"],
"shared/useLayoutEffect": ["../shared/src/useLayoutEffect.ts"],
"shared/warnOnlyOnce": ["../shared/src/warnOnlyOnce.ts"]
Expand Down
9 changes: 1 addition & 8 deletions packages/lexical-react/src/LexicalNodeMenuPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@ import {
} from 'lexical';
import {useCallback, useEffect, useState} from 'react';
import * as React from 'react';
import {startTransition} from 'shared/reactPatches';

import {LexicalMenu, MenuOption, useMenuAnchorRef} from './shared/LexicalMenu';

function startTransition(callback: () => void) {
if (React.startTransition) {
React.startTransition(callback);
} else {
callback();
}
}

export type NodeMenuPluginProps<TOption extends MenuOption> = {
onSelectOption: (
option: TOption,
Expand Down
9 changes: 1 addition & 8 deletions packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from 'lexical';
import {useCallback, useEffect, useState} from 'react';
import * as React from 'react';
import {startTransition} from 'shared/reactPatches';

import {LexicalMenu, MenuOption, useMenuAnchorRef} from './shared/LexicalMenu';

Expand Down Expand Up @@ -105,14 +106,6 @@ function isSelectionOnEntityBoundary(
});
}

function startTransition(callback: () => void) {
if (React.startTransition) {
React.startTransition(callback);
} else {
callback();
}
}

// Got from https://stackoverflow.com/a/42543908/2013580
export function getScrollParent(
element: HTMLElement,
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/src/reactPatches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import React from 'react';

// Webpack + React 17 fails to compile on the usage of `React.startTransition` or
// `React["startTransition"]` even if it's behind a feature detection of
// `"startTransition" in React`. Moving this to a constant avoids the issue :/
const START_TRANSITION = 'startTransition';

export function startTransition(callback: () => void) {
if (START_TRANSITION in React) {
React[START_TRANSITION](callback);
} else {
callback();
}
}
1 change: 1 addition & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"./packages/shared/src/normalizeClassNames.ts"
],
"shared/react-test-utils": ["./packages/shared/src/react-test-utils.ts"],
"shared/reactPatches": ["./packages/shared/src/reactPatches.ts"],
"shared/simpleDiffWithCursor": [
"./packages/shared/src/simpleDiffWithCursor.ts"
],
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
"./packages/shared/src/normalizeClassNames.ts"
],
"shared/react-test-utils": ["./packages/shared/src/react-test-utils.ts"],
"shared/reactPatches": ["./packages/shared/src/reactPatches.ts"],
"shared/simpleDiffWithCursor": [
"./packages/shared/src/simpleDiffWithCursor.ts"
],
Expand Down
Loading