Skip to content

Commit f3af434

Browse files
authored
[docs] Modernize DemoFrame (#20664)
1 parent 8658b0c commit f3af434

File tree

3 files changed

+43
-52
lines changed

3 files changed

+43
-52
lines changed

docs/src/modules/components/DemoSandboxed.js

+33-46
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import { create } from 'jss';
44
import { withStyles, useTheme, jssPreset, StylesProvider } from '@material-ui/core/styles';
5-
import NoSsr from '@material-ui/core/NoSsr';
65
import rtl from 'jss-rtl';
7-
import Frame from 'react-frame-component';
6+
import Frame, { FrameContext } from 'react-frame-component';
87
import { useSelector } from 'react-redux';
98
import DemoErrorBoundary from 'docs/src/modules/components/DemoErrorBoundary';
109

@@ -18,59 +17,47 @@ const styles = (theme) => ({
1817
},
1918
});
2019

21-
function DemoFrame(props) {
22-
const { children, classes, ...other } = props;
20+
function FramedDemo(props) {
21+
const { children } = props;
22+
23+
const { document } = React.useContext(FrameContext);
24+
2325
const theme = useTheme();
24-
const [state, setState] = React.useState({
25-
ready: false,
26-
});
27-
const instanceRef = React.useRef();
28-
29-
const handleRef = React.useCallback((ref) => {
30-
instanceRef.current = {
31-
contentDocument: ref ? ref.node.contentDocument : null,
32-
contentWindow: ref ? ref.node.contentWindow : null,
33-
};
34-
}, []);
26+
React.useEffect(() => {
27+
document.body.dir = theme.direction;
28+
}, [document, theme.direction]);
3529

36-
const onContentDidMount = () => {
37-
setState({
38-
ready: true,
30+
const { jss, sheetsManager } = React.useMemo(() => {
31+
return {
3932
jss: create({
4033
plugins: [...jssPreset().plugins, rtl()],
41-
insertionPoint: instanceRef.current.contentWindow['demo-frame-jss'],
34+
insertionPoint: document.head,
4235
}),
4336
sheetsManager: new Map(),
44-
container: instanceRef.current.contentDocument.body,
45-
window: () => instanceRef.current.contentWindow,
46-
});
47-
};
37+
};
38+
}, [document]);
39+
40+
const getWindow = React.useCallback(() => document.defaultView, [document]);
4841

49-
const onContentDidUpdate = () => {
50-
instanceRef.current.contentDocument.body.dir = theme.direction;
51-
};
42+
return (
43+
<StylesProvider jss={jss} sheetsManager={sheetsManager}>
44+
{React.cloneElement(children, {
45+
window: getWindow,
46+
})}
47+
</StylesProvider>
48+
);
49+
}
50+
FramedDemo.propTypes = {
51+
children: PropTypes.node,
52+
};
53+
54+
function DemoFrame(props) {
55+
const { children, classes, ...other } = props;
5256

53-
// NoSsr fixes a strange concurrency issue with iframe and quick React mount/unmount
5457
return (
55-
<NoSsr defer>
56-
<Frame
57-
ref={handleRef}
58-
className={classes.frame}
59-
contentDidMount={onContentDidMount}
60-
contentDidUpdate={onContentDidUpdate}
61-
{...other}
62-
>
63-
<div id="demo-frame-jss" />
64-
{state.ready ? (
65-
<StylesProvider jss={state.jss} sheetsManager={state.sheetsManager}>
66-
{React.cloneElement(children, {
67-
container: state.container,
68-
window: state.window,
69-
})}
70-
</StylesProvider>
71-
) : null}
72-
</Frame>
73-
</NoSsr>
58+
<Frame className={classes.frame} {...other}>
59+
<FramedDemo>{children}</FramedDemo>
60+
</Frame>
7461
);
7562
}
7663

docs/src/pages/components/drawers/ResponsiveDrawer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const useStyles = makeStyles((theme) => ({
5353
}));
5454

5555
function ResponsiveDrawer(props) {
56-
const { container } = props;
56+
const { window } = props;
5757
const classes = useStyles();
5858
const theme = useTheme();
5959
const [mobileOpen, setMobileOpen] = React.useState(false);
@@ -86,6 +86,8 @@ function ResponsiveDrawer(props) {
8686
</div>
8787
);
8888

89+
const container = window !== undefined ? () => window().document.body : undefined;
90+
8991
return (
9092
<div className={classes.root}>
9193
<CssBaseline />
@@ -171,7 +173,7 @@ ResponsiveDrawer.propTypes = {
171173
* Injected by the documentation to work in an iframe.
172174
* You won't need it on your project.
173175
*/
174-
container: PropTypes.any,
176+
window: PropTypes.func,
175177
};
176178

177179
export default ResponsiveDrawer;

docs/src/pages/components/drawers/ResponsiveDrawer.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ const useStyles = makeStyles((theme: Theme) =>
5353
}),
5454
);
5555

56-
interface ResponsiveDrawerProps {
56+
interface Props {
5757
/**
5858
* Injected by the documentation to work in an iframe.
5959
* You won't need it on your project.
6060
*/
61-
container?: any;
61+
window?: () => Window;
6262
}
6363

64-
export default function ResponsiveDrawer(props: ResponsiveDrawerProps) {
65-
const { container } = props;
64+
export default function ResponsiveDrawer(props: Props) {
65+
const { window } = props;
6666
const classes = useStyles();
6767
const theme = useTheme();
6868
const [mobileOpen, setMobileOpen] = React.useState(false);
@@ -95,6 +95,8 @@ export default function ResponsiveDrawer(props: ResponsiveDrawerProps) {
9595
</div>
9696
);
9797

98+
const container = window !== undefined ? () => window().document.body : undefined;
99+
98100
return (
99101
<div className={classes.root}>
100102
<CssBaseline />

0 commit comments

Comments
 (0)