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] Modernize DemoFrame #20664

Merged
merged 5 commits into from
Apr 21, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 33 additions & 46 deletions docs/src/modules/components/DemoSandboxed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React from 'react';
import PropTypes from 'prop-types';
import { create } from 'jss';
import { withStyles, useTheme, jssPreset, StylesProvider } from '@material-ui/core/styles';
import NoSsr from '@material-ui/core/NoSsr';
import rtl from 'jss-rtl';
import Frame from 'react-frame-component';
import Frame, { FrameContext } from 'react-frame-component';
import { useSelector } from 'react-redux';
import DemoErrorBoundary from 'docs/src/modules/components/DemoErrorBoundary';

Expand All @@ -18,59 +17,47 @@ const styles = (theme) => ({
},
});

function DemoFrame(props) {
const { children, classes, ...other } = props;
function FramedDemo(props) {
const { children } = props;

const { document } = React.useContext(FrameContext);

const theme = useTheme();
const [state, setState] = React.useState({
ready: false,
});
const instanceRef = React.useRef();

const handleRef = React.useCallback((ref) => {
instanceRef.current = {
contentDocument: ref ? ref.node.contentDocument : null,
contentWindow: ref ? ref.node.contentWindow : null,
};
}, []);
React.useEffect(() => {
document.body.dir = theme.direction;
}, [document, theme.direction]);

const onContentDidMount = () => {
setState({
ready: true,
const { jss, sheetsManager } = React.useMemo(() => {
return {
jss: create({
plugins: [...jssPreset().plugins, rtl()],
insertionPoint: instanceRef.current.contentWindow['demo-frame-jss'],
insertionPoint: document.head,
}),
sheetsManager: new Map(),
container: instanceRef.current.contentDocument.body,
window: () => instanceRef.current.contentWindow,
});
};
};
}, [document]);

const getWindow = React.useCallback(() => document.defaultView, [document]);

const onContentDidUpdate = () => {
instanceRef.current.contentDocument.body.dir = theme.direction;
};
return (
<StylesProvider jss={jss} sheetsManager={sheetsManager}>
{React.cloneElement(children, {
window: getWindow,
})}
</StylesProvider>
);
}
FramedDemo.propTypes = {
children: PropTypes.node,
};

function DemoFrame(props) {
const { children, classes, ...other } = props;

// NoSsr fixes a strange concurrency issue with iframe and quick React mount/unmount
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliviertassinari Do you recall the symptoms of that issue?

Copy link
Member

@oliviertassinari oliviertassinari Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could find #13705 as the origin.

Copy link
Member

@oliviertassinari oliviertassinari Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to make sure https://deploy-preview-20664--material-ui.netlify.app/zh/components/drawers/ displays without a crash. It was before hooks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check locally if /zh/components/drawers/ crashes. That was the issue in #13705, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not crash locally in small and large viewporet.

I got warnings in concurrent mode for "state transition during render" which is indicative of some issue with react-frame-component. Might just be an issue with class components and concurrent mode.

Hopefully this fixes concurrent mode issues as well. If not then at least let it not be a bug in react :)

return (
<NoSsr defer>
<Frame
ref={handleRef}
className={classes.frame}
contentDidMount={onContentDidMount}
contentDidUpdate={onContentDidUpdate}
{...other}
>
<div id="demo-frame-jss" />
{state.ready ? (
<StylesProvider jss={state.jss} sheetsManager={state.sheetsManager}>
{React.cloneElement(children, {
container: state.container,
window: state.window,
})}
</StylesProvider>
) : null}
</Frame>
</NoSsr>
<Frame className={classes.frame} {...other}>
<FramedDemo>{children}</FramedDemo>
</Frame>
);
}

Expand Down
6 changes: 4 additions & 2 deletions docs/src/pages/components/drawers/ResponsiveDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const useStyles = makeStyles((theme) => ({
}));

function ResponsiveDrawer(props) {
const { container } = props;
const { window } = props;
const classes = useStyles();
const theme = useTheme();
const [mobileOpen, setMobileOpen] = React.useState(false);
Expand Down Expand Up @@ -86,6 +86,8 @@ function ResponsiveDrawer(props) {
</div>
);

const container = window !== undefined ? () => window().document.body : undefined;

return (
<div className={classes.root}>
<CssBaseline />
Expand Down Expand Up @@ -171,7 +173,7 @@ ResponsiveDrawer.propTypes = {
* Injected by the documentation to work in an iframe.
* You won't need it on your project.
*/
container: PropTypes.any,
window: PropTypes.func,
};

export default ResponsiveDrawer;
10 changes: 6 additions & 4 deletions docs/src/pages/components/drawers/ResponsiveDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ const useStyles = makeStyles((theme: Theme) =>
}),
);

interface ResponsiveDrawerProps {
interface Props {
/**
* Injected by the documentation to work in an iframe.
* You won't need it on your project.
*/
container?: any;
window?: () => Window;
}

export default function ResponsiveDrawer(props: ResponsiveDrawerProps) {
const { container } = props;
export default function ResponsiveDrawer(props: Props) {
const { window } = props;
const classes = useStyles();
const theme = useTheme();
const [mobileOpen, setMobileOpen] = React.useState(false);
Expand Down Expand Up @@ -95,6 +95,8 @@ export default function ResponsiveDrawer(props: ResponsiveDrawerProps) {
</div>
);

const container = window !== undefined ? () => window().document.body : undefined;

return (
<div className={classes.root}>
<CssBaseline />
Expand Down