@@ -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,56 +17,47 @@ const styles = (theme) => ({
18
17
} ,
19
18
} ) ;
20
19
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 ;
25
22
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 ) ;
33
24
34
- const jssInsertionPointRef = React . useRef ( null ) ;
25
+ const theme = useTheme ( ) ;
26
+ React . useEffect ( ( ) => {
27
+ document . body . dir = theme . direction ;
28
+ } , [ document , theme . direction ] ) ;
35
29
36
- const onContentDidMount = ( ) => {
37
- setState ( {
30
+ const { jss , sheetsManager } = React . useMemo ( ( ) => {
31
+ return {
38
32
jss : create ( {
39
33
plugins : [ ...jssPreset ( ) . plugins , rtl ( ) ] ,
40
- insertionPoint : jssInsertionPointRef . current ,
34
+ insertionPoint : document . head ,
41
35
} ) ,
42
36
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
+ } ;
46
53
47
- const onContentDidUpdate = ( ) => {
48
- frameWindowRef . current . document . body . dir = theme . direction ;
49
- } ;
54
+ function DemoFrame ( props ) {
55
+ const { children, classes, ...other } = props ;
50
56
51
- // NoSsr fixes a strange concurrency issue with iframe and quick React mount/unmount
52
57
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 >
71
61
) ;
72
62
}
73
63
0 commit comments