Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Fix layout jump on first mistake #35215

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions docs/src/modules/components/Demo.js
Expand Up @@ -94,7 +94,7 @@ function useDemoData(codeVariant, demo, githubLocation) {
}, [canonicalAs, codeVariant, demo, githubLocation, userLanguage]);
}

function useDemoElement({ demoOptions, demoData, editorCode, setDebouncedError }) {
function useDemoElement({ demoData, editorCode, setDebouncedError, liveDemoActive }) {
const debouncedSetError = React.useMemo(
() => debounce(setDebouncedError, 300),
[setDebouncedError],
Expand Down Expand Up @@ -127,13 +127,10 @@ function useDemoElement({ demoOptions, demoData, editorCode, setDebouncedError }
[demoData, debouncedSetError, editorCode.isPreview, editorCode.value],
);

const renderBundledComponent =
// No need for a live environment if the code matches with the component rendered server-side.
editorCode.value === editorCode.initialEditorCode ||
// A limitation from https://github.com/nihgwu/react-runner, we can inject the `window` of the iframe
demoOptions.disableLiveEdit;

return renderBundledComponent ? BundledComponent : LiveComponent;
// No need for a live environment if the code matches with the component rendered server-side.
return editorCode.value === editorCode.initialEditorCode && liveDemoActive === false
? BundledComponent
: LiveComponent;
}

const Root = styled('div')(({ theme }) => ({
Expand Down Expand Up @@ -396,11 +393,13 @@ export default function Demo(props) {

const [debouncedError, setDebouncedError] = React.useState(null);

const [liveDemoActive, setLiveDemoActive] = React.useState(false);

const demoElement = useDemoElement({
demoOptions,
demoData,
editorCode,
setDebouncedError,
liveDemoActive,
});

return (
Expand Down Expand Up @@ -459,6 +458,8 @@ export default function Demo(props) {
</NoSsr>
)}
<Collapse in={openDemoSource} unmountOnExit>
{/* A limitation from https://github.com/nihgwu/react-runner,
we can't inject the `window` of the iframe so we need a disableLiveEdit option. */}
{demoOptions.disableLiveEdit ? (
<DemoCodeViewer
code={editorCode.value}
Expand All @@ -481,6 +482,9 @@ export default function Demo(props) {
value,
});
}}
onFocus={() => {
setLiveDemoActive(true);
}}
id={demoSourceId}
language={demoData.sourceLanguage}
copyButtonProps={{
Expand Down
5 changes: 3 additions & 2 deletions docs/src/modules/components/DemoEditor.tsx
Expand Up @@ -57,7 +57,7 @@ const StyledSimpleCodeEditor = styled(SimpleCodeEditor)(({ theme }) => ({
},
}));

interface DemoEditorProps {
interface DemoEditorProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
copyButtonProps: {};
id: string;
Expand All @@ -67,7 +67,7 @@ interface DemoEditorProps {
}

export default function DemoEditor(props: DemoEditorProps) {
const { language, value, onChange, copyButtonProps, children, id } = props;
const { language, value, onChange, copyButtonProps, children, id, ...other } = props;
const t = useTranslate();
const contextTheme = useTheme();
const wrapperRef = React.useRef<HTMLElement | null>(null);
Expand Down Expand Up @@ -100,6 +100,7 @@ export default function DemoEditor(props: DemoEditorProps) {
}
}
}}
{...other}
>
<div className="MuiCode-root" {...handlers}>
<div className="scrollContainer">
Expand Down