Browse Source

[Beta] Optimize editor re-renders (#5113)

main
dan 3 years ago
committed by GitHub
parent
commit
4f04184f97
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 72
      beta/src/components/MDX/Sandpack/CustomPreset.tsx

72
beta/src/components/MDX/Sandpack/CustomPreset.tsx

@ -1,7 +1,7 @@
/* /*
* Copyright (c) Facebook, Inc. and its affiliates. * Copyright (c) Facebook, Inc. and its affiliates.
*/ */
import {useRef, useState} from 'react'; import {memo, useRef, useState} from 'react';
import {flushSync} from 'react-dom'; import {flushSync} from 'react-dom';
import { import {
useSandpack, useSandpack,
@ -18,7 +18,7 @@ import {Preview} from './Preview';
import {useSandpackLint} from './useSandpackLint'; import {useSandpackLint} from './useSandpackLint';
export function CustomPreset({ export const CustomPreset = memo(function CustomPreset({
showDevTools, showDevTools,
onDevToolsLoad, onDevToolsLoad,
devToolsLoaded, devToolsLoaded,
@ -30,19 +30,47 @@ export function CustomPreset({
providedFiles: Array<string>; providedFiles: Array<string>;
}) { }) {
const {lintErrors, lintExtensions} = useSandpackLint(); const {lintErrors, lintExtensions} = useSandpackLint();
const lineCountRef = useRef<{[key: string]: number}>({});
const containerRef = useRef<HTMLDivElement>(null);
const {sandpack} = useSandpack(); const {sandpack} = useSandpack();
const {code} = useActiveCode(); const {code} = useActiveCode();
const [isExpanded, setIsExpanded] = useState(false);
const {activeFile} = sandpack; const {activeFile} = sandpack;
const lineCountRef = useRef<{[key: string]: number}>({});
if (!lineCountRef.current[activeFile]) { if (!lineCountRef.current[activeFile]) {
lineCountRef.current[activeFile] = code.split('\n').length; lineCountRef.current[activeFile] = code.split('\n').length;
} }
const lineCount = lineCountRef.current[activeFile]; const lineCount = lineCountRef.current[activeFile];
const isExpandable = lineCount > 16 || isExpanded; const isExpandable = lineCount > 16;
return (
<SandboxShell
showDevTools={showDevTools}
onDevToolsLoad={onDevToolsLoad}
devToolsLoaded={devToolsLoaded}
providedFiles={providedFiles}
lintErrors={lintErrors}
lintExtensions={lintExtensions}
isExpandable={isExpandable}
/>
);
});
const SandboxShell = memo(function SandboxShell({
showDevTools,
onDevToolsLoad,
devToolsLoaded,
providedFiles,
lintErrors,
lintExtensions,
isExpandable,
}: {
showDevTools: boolean;
devToolsLoaded: boolean;
onDevToolsLoad: () => void;
providedFiles: Array<string>;
lintErrors: Array<any>;
lintExtensions: Array<any>;
isExpandable: boolean;
}) {
const containerRef = useRef<HTMLDivElement>(null);
const [isExpanded, setIsExpanded] = useState(false);
return ( return (
<> <>
<div <div
@ -52,22 +80,16 @@ export function CustomPreset({
<SandpackLayout <SandpackLayout
className={cn( className={cn(
showDevTools && devToolsLoaded && 'sp-layout-devtools', showDevTools && devToolsLoaded && 'sp-layout-devtools',
!isExpandable && 'rounded-b-lg overflow-hidden', !(isExpandable || isExpanded) && 'rounded-b-lg overflow-hidden',
isExpanded && 'sp-layout-expanded' isExpanded && 'sp-layout-expanded'
)}> )}>
<SandpackCodeEditor <Editor lintExtensions={lintExtensions} />
showLineNumbers
showInlineErrors
showTabs={false}
showRunButton={false}
extensions={lintExtensions}
/>
<Preview <Preview
className="order-last xl:order-2" className="order-last xl:order-2"
isExpanded={isExpanded} isExpanded={isExpanded}
lintErrors={lintErrors} lintErrors={lintErrors}
/> />
{isExpandable && ( {(isExpandable || isExpanded) && (
<button <button
translate="yes" translate="yes"
className="sandpack-expand flex text-base justify-between dark:border-card-dark bg-wash dark:bg-card-dark items-center z-10 p-1 w-full order-2 xl:order-last border-b-1 relative top-0" className="sandpack-expand flex text-base justify-between dark:border-card-dark bg-wash dark:bg-card-dark items-center z-10 p-1 w-full order-2 xl:order-last border-b-1 relative top-0"
@ -107,4 +129,20 @@ export function CustomPreset({
</div> </div>
</> </>
); );
} });
const Editor = memo(function Editor({
lintExtensions,
}: {
lintExtensions: Array<any>;
}) {
return (
<SandpackCodeEditor
showLineNumbers
showInlineErrors
showTabs={false}
showRunButton={false}
extensions={lintExtensions}
/>
);
});

Loading…
Cancel
Save