Skip to content

Commit b11562d

Browse files
colsondonohueColson Donohue
and
Colson Donohue
authored
fix: Fix flash of content on initialization (#717)
* Fix flash on initialization * Update example to remove state setting on load --------- Co-authored-by: Colson Donohue <colsondonohue@Colsons-MBP.localdomain>
1 parent a5a2ab8 commit b11562d

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

core/src/index.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, forwardRef, useImperativeHandle } from 'react';
1+
import React, { useRef, forwardRef, useImperativeHandle, useCallback } from 'react';
22
import type { EditorState, EditorStateConfig, Extension, StateField } from '@codemirror/state';
33
import type { EditorView, ViewUpdate } from '@codemirror/view';
44
import { type BasicSetupOptions } from '@uiw/codemirror-extensions-basic-setup';
@@ -116,9 +116,8 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
116116
initialState,
117117
...other
118118
} = props;
119-
const editor = useRef<HTMLDivElement>(null);
120-
const { state, view, container } = useCodeMirror({
121-
container: editor.current,
119+
const editor = useRef<HTMLDivElement | null>(null);
120+
const { state, view, container, setContainer } = useCodeMirror({
122121
root,
123122
value,
124123
autoFocus,
@@ -150,13 +149,23 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
150149
view,
151150
]);
152151

152+
const setEditorRef = useCallback(
153+
(el: HTMLDivElement) => {
154+
editor.current = el;
155+
setContainer(el);
156+
},
157+
[setContainer],
158+
);
159+
153160
// check type of value
154161
if (typeof value !== 'string') {
155162
throw new Error(`value must be typeof string but got ${typeof value}`);
156163
}
157164

158165
const defaultClassNames = typeof theme === 'string' ? `cm-theme-${theme}` : 'cm-theme';
159-
return <div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...other}></div>;
166+
return (
167+
<div ref={setEditorRef} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...other}></div>
168+
);
160169
});
161170

162171
ReactCodeMirror.displayName = 'CodeMirror';

core/src/useCodeMirror.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useLayoutEffect, useState } from 'react';
22
import { Annotation, EditorState, StateEffect, type Extension } from '@codemirror/state';
33
import { EditorView, type ViewUpdate } from '@codemirror/view';
44
import { getDefaultExtensions } from './getDefaultExtensions';
@@ -85,7 +85,7 @@ export function useCodeMirror(props: UseCodeMirror) {
8585
}
8686
getExtensions = getExtensions.concat(extensions);
8787

88-
useEffect(() => {
88+
useLayoutEffect(() => {
8989
if (container && !state) {
9090
const config = {
9191
doc: value,
@@ -114,7 +114,11 @@ export function useCodeMirror(props: UseCodeMirror) {
114114
};
115115
}, [container, state]);
116116

117-
useEffect(() => setContainer(props.container), [props.container]);
117+
useEffect(() => {
118+
if (props.container) {
119+
setContainer(props.container);
120+
}
121+
}, [props.container]);
118122

119123
useEffect(
120124
() => () => {

www/src/pages/home/Example.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useState } from 'react';
22
import MarkdownPreview from '@uiw/react-markdown-preview';
33
import { color } from '@uiw/codemirror-extensions-color';
44
import DocumentStr from '@uiw/react-codemirror/README.md';
@@ -10,6 +10,7 @@ import { langs } from '@uiw/codemirror-extensions-langs';
1010
import { Select } from './Select';
1111
import { Options } from '../extensions/basic-setup/example';
1212
import { useTheme } from '../../utils/useTheme';
13+
import javascriptExample from 'code-example/txt/sample.javascript.txt';
1314

1415
const themeOptions = ['dark', 'light']
1516
.concat(Object.keys(alls))
@@ -61,7 +62,7 @@ export default function Example() {
6162
const [autofocus, setAutofocus] = useState(false);
6263
const [editable, setEditable] = useState(true);
6364
const { theme, setTheme } = useTheme();
64-
const [code, setCode] = useState('');
65+
const [code, setCode] = useState(javascriptExample);
6566
const [extensions, setExtensions] = useState<Extension[]>();
6667
const [height, setHeight] = useState('500px');
6768
const [basicSetup, setBasicSetup] = useState<BasicSetupOptions>({
@@ -91,9 +92,7 @@ export default function Example() {
9192
}
9293
} catch (error) {}
9394
}
94-
useEffect(() => {
95-
handleLangChange('javascript');
96-
}, []);
95+
9796
return (
9897
<Warpper className="wmde-markdown-var">
9998
<CodemirrorWarpper>

0 commit comments

Comments
 (0)