@@ -2,9 +2,8 @@ import React from 'react';
2
2
import PropTypes from 'prop-types' ;
3
3
import { create } from 'jss' ;
4
4
import { withStyles , useTheme , jssPreset , StylesProvider } from '@material-ui/core/styles' ;
5
- import NoSsr from '@material-ui/core/NoSsr' ;
6
5
import rtl from 'jss-rtl' ;
7
- import Frame from 'react-frame-component' ;
6
+ import Frame , { FrameContext } from 'react-frame-component' ;
8
7
import { useSelector } from 'react-redux' ;
9
8
import DemoErrorBoundary from 'docs/src/modules/components/DemoErrorBoundary' ;
10
9
@@ -18,59 +17,47 @@ const styles = (theme) => ({
18
17
} ,
19
18
} ) ;
20
19
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
+
23
25
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 ] ) ;
35
29
36
- const onContentDidMount = ( ) => {
37
- setState ( {
38
- ready : true ,
30
+ const { jss, sheetsManager } = React . useMemo ( ( ) => {
31
+ return {
39
32
jss : create ( {
40
33
plugins : [ ...jssPreset ( ) . plugins , rtl ( ) ] ,
41
- insertionPoint : instanceRef . current . contentWindow [ 'demo-frame-jss' ] ,
34
+ insertionPoint : document . head ,
42
35
} ) ,
43
36
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 ] ) ;
48
41
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 ;
52
56
53
- // NoSsr fixes a strange concurrency issue with iframe and quick React mount/unmount
54
57
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 >
74
61
) ;
75
62
}
76
63
0 commit comments