MCPcopy Create free account
hub / github.com/FormidableLabs/react-live / CodeEditor

Function CodeEditor

packages/react-live/src/components/Editor/index.tsx:21–105  ·  view source on GitHub ↗
(props: Props)

Source from the content-addressed store, hash-verified

19};
20
21const CodeEditor = (props: Props) => {
22 const { tabMode = "indentation" } = props;
23 const editorRef = useRef(null);
24 const [code, setCode] = useState(props.code || "");
25 const { theme } = props;
26
27 useEffect(() => {
28 setCode(props.code);
29 }, [props.code]);
30
31 // use-editable keys its editing setup on the element ref, which is still null
32 // during the first render. Standalone Editor does not re-render between
33 // mounting and the first edit, so that setup only reaches the real element
34 // when the edit re-renders: it tears down and rebuilds the contenteditable
35 // surface, resetting `contentEditable` (which blurs the element in Chrome)
36 // and calling focus() while the element is still non-editable. LiveProvider
37 // re-renders when its initial transpile resolves, which is why LiveEditor is
38 // unaffected. Re-render once after mount so the setup settles beforehand.
39 // See #415.
40 const [, forceRender] = useReducer((n: number) => n + 1, 0);
41 useEffect(forceRender, []);
42
43 useEditable(
44 editorRef,
45 (text) => {
46 const t = text.slice(0, -1);
47 setCode(t);
48
49 if (props.onChange) {
50 props.onChange(t);
51 }
52 },
53 {
54 disabled: props.disabled,
55 indentation: tabMode === "indentation" ? 2 : undefined,
56 },
57 );
58
59 return (
60 <div className={props.className} style={props.style}>
61 <Highlight
62 code={code}
63 theme={props.theme || themes.nightOwl}
64 language={props.language}
65 prism={props.prism}
66 >
67 {({
68 className: _className,
69 tokens,
70 getLineProps,
71 getTokenProps,
72 style: _style,
73 }) => (
74 <pre
75 className={_className}
76 style={{
77 margin: 0,
78 outline: "none",

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected