Skip to content

Commit 6423280

Browse files
committed
feat: less refs
1 parent e059edc commit 6423280

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

docs/src/modules/components/DemoSandboxed.js

+32-42
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,56 +17,47 @@ const styles = (theme) => ({
1817
},
1918
});
2019

21-
function DemoFrame(props) {
22-
const { children, classes, ...other } = props;
23-
const theme = useTheme();
24-
const [state, setState] = React.useState(null);
20+
function FramedDemo(props) {
21+
const { children } = props;
2522

26-
/**
27-
* @type {import('react').Ref<Window | null>}
28-
*/
29-
const frameWindowRef = React.useRef(null);
30-
const handleRef = React.useCallback((instance) => {
31-
frameWindowRef.current = instance !== null ? instance.node.contentWindow : null;
32-
}, []);
23+
const { document } = React.useContext(FrameContext);
3324

34-
const jssInsertionPointRef = React.useRef(null);
25+
const theme = useTheme();
26+
React.useEffect(() => {
27+
document.body.dir = theme.direction;
28+
}, [document, theme.direction]);
3529

36-
const onContentDidMount = () => {
37-
setState({
30+
const { jss, sheetsManager } = React.useMemo(() => {
31+
return {
3832
jss: create({
3933
plugins: [...jssPreset().plugins, rtl()],
40-
insertionPoint: jssInsertionPointRef.current,
34+
insertionPoint: document.head,
4135
}),
4236
sheetsManager: new Map(),
43-
window: () => frameWindowRef.current,
44-
});
45-
};
37+
};
38+
}, [document]);
39+
40+
const getWindow = React.useCallback(() => document.defaultView, [document]);
41+
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+
};
4653

47-
const onContentDidUpdate = () => {
48-
frameWindowRef.current.document.body.dir = theme.direction;
49-
};
54+
function DemoFrame(props) {
55+
const { children, classes, ...other } = props;
5056

51-
// NoSsr fixes a strange concurrency issue with iframe and quick React mount/unmount
5257
return (
53-
<NoSsr defer>
54-
<Frame
55-
ref={handleRef}
56-
className={classes.frame}
57-
contentDidMount={onContentDidMount}
58-
contentDidUpdate={onContentDidUpdate}
59-
{...other}
60-
>
61-
<div ref={jssInsertionPointRef} />
62-
{state !== null ? (
63-
<StylesProvider jss={state.jss} sheetsManager={state.sheetsManager}>
64-
{React.cloneElement(children, {
65-
window: state.window,
66-
})}
67-
</StylesProvider>
68-
) : null}
69-
</Frame>
70-
</NoSsr>
58+
<Frame className={classes.frame} {...other}>
59+
<FramedDemo>{children}</FramedDemo>
60+
</Frame>
7161
);
7262
}
7363

0 commit comments

Comments
 (0)