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

Co-authored-by: guohao <guohao@huoban.com>
This commit is contained in:
Turner
2024-08-15 10:17:35 +08:00
committed by GitHub
parent cb0b7312a4
commit 510720e727
6 changed files with 27 additions and 16 deletions

View File

@ -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"]

View File

@ -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,

View File

@ -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';
@ -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,

View File

@ -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();
}
}

View File

@ -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"
],

View File

@ -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"
],