Skip to content

Commit bb5bb38

Browse files
committed
fix(web): update the wrapper's refs before any layout effect runs
The handler and prop refs were written in a layout effect, which runs in tree order. A component earlier in the tree that focused the editor from its own layout effect reached the onFocus of the previous commit. The refs are now written in an insertion effect, which React runs before every layout effect.
1 parent 2e064a3 commit bb5bb38

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎src/web/CodeEditor.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
useCallback,
66
useEffect,
77
useImperativeHandle,
8-
useLayoutEffect,
8+
useInsertionEffect,
99
useMemo,
1010
useRef,
1111
useState,
@@ -74,8 +74,8 @@ export const CodeEditor = forwardRef<CodeEditorRef, CodeEditorProps>(function Co
7474
onBlur,
7575
onGapLayout,
7676
});
77-
// Written on commit, so a render React drops never calls its handlers.
78-
useLayoutEffect(() => {
77+
// Written on commit before any layout effect runs, so a render React drops never calls its handlers.
78+
useInsertionEffect(() => {
7979
handlers.current = {
8080
onReady,
8181
onScroll,
@@ -158,8 +158,8 @@ export const CodeEditor = forwardRef<CodeEditorRef, CodeEditorProps>(function Co
158158
],
159159
);
160160
const latest = useRef({ options, language, decorations, decorationsRevision });
161-
// Written on commit, so the props of a render React drops never reach the view.
162-
useLayoutEffect(() => {
161+
// Written on commit before any layout effect runs, so the props of a render React drops never reach the view.
162+
useInsertionEffect(() => {
163163
latest.current = { options, language, decorations, decorationsRevision };
164164
});
165165
// The decorations last sent, so the view gets them before its first draw and a repeat is not mapped twice.

‎src/web/__tests__/CodeEditor.test.tsx‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeEach, expect, jest, test } from '@jest/globals';
22
import { act, render } from '@testing-library/react-native';
3-
import { createRef, Suspense, startTransition, use, useState } from 'react';
3+
import { createRef, Suspense, startTransition, use, useLayoutEffect, useState } from 'react';
44
import type { CodeEditorRef } from '../../types';
55
import type { ViewOptions } from '../EditorView';
66

@@ -265,6 +265,32 @@ test('a render that never commits keeps its decorations, options and handlers of
265265
expect(ready).toEqual([['committed', 14]]);
266266
});
267267

268+
test("a layout effect earlier in the tree that focuses the editor reaches the same commit's onFocus", async () => {
269+
const ref = createRef<CodeEditorRef>();
270+
const calls: string[] = [];
271+
function Focuser({ n }: { n: number }) {
272+
useLayoutEffect(() => {
273+
if (n > 0) void ref.current?.focus();
274+
}, [n]);
275+
return null;
276+
}
277+
function App({ n }: { n: number }) {
278+
return (
279+
<>
280+
<Focuser n={n} />
281+
<CodeEditor ref={ref} onFocus={() => calls.push(`focus ${n}`)} />
282+
</>
283+
);
284+
}
285+
const { rerender } = await render(<App n={0} />);
286+
await act(async () => {
287+
mockCore.resolve({});
288+
await tick();
289+
});
290+
await rerender(<App n={1} />);
291+
expect(calls).toEqual(['focus 1']);
292+
});
293+
268294
test('decorations without a revision are for the current text', async () => {
269295
const items = [{ kind: 'lineBackground' as const, line: 1, color: 'red' }];
270296
await render(<CodeEditor decorations={items} />);

0 commit comments

Comments
 (0)