@@ -3,7 +3,7 @@ import { useSpace, ParentContext, LayerContext, DOMRectContext } from "../core-r
33import * as React from "react" ;
44import { Centered } from "./Centered" ;
55import { CenteredVertically } from "./CenteredVertically" ;
6- import { shortuuid } from "../core-utils" ;
6+ import { shortuuid , updateStyleDefinition } from "../core-utils" ;
77
88function applyCentering ( children : React . ReactNode , centerType : CenterType | undefined ) {
99 switch ( centerType ) {
@@ -60,6 +60,18 @@ const SpaceInner: React.FC<ISpaceProps & { wrapperInstance: Space }> = (props) =
6060
6161 React . useEffect ( ( ) => {
6262 space . element = elementRef . current ! ;
63+
64+ if ( space . element . getAttribute ( "data-ssr" ) === "1" ) {
65+ const preRenderedStyle = space . element . children [ 0 ] ;
66+ if ( preRenderedStyle ) {
67+ const newStyle = document . createElement ( "style" ) ;
68+ newStyle . id = `style_${ space . id } ` ;
69+ newStyle . innerHTML = preRenderedStyle . innerHTML ;
70+ document . head . appendChild ( newStyle ) ;
71+ }
72+ space . element . removeAttribute ( "data-ssr" ) ;
73+ updateStyleDefinition ( space ) ;
74+ }
6375 } , [ ] ) ;
6476
6577 const userClasses = className ? className . split ( " " ) . map ( ( c ) => c . trim ( ) ) : [ ] ;
@@ -86,28 +98,39 @@ const SpaceInner: React.FC<ISpaceProps & { wrapperInstance: Space }> = (props) =
8698
8799 const centeredContent = applyCentering ( children , props . centerContent ) ;
88100
101+ const outerProps = {
102+ ...{
103+ id : space . id ,
104+ ref : elementRef ,
105+ className : outerClasses . join ( " " ) ,
106+ } ,
107+ ...events ,
108+ } as any ;
109+
110+ const isSSR = typeof document === "undefined" ;
111+
112+ if ( isSSR ) {
113+ outerProps [ "data-ssr" ] = "1" ;
114+ }
115+
89116 return (
90117 < >
91118 { resizeHandles . mouseHandles . map ( ( r ) => (
92119 < div { ...r } />
93120 ) ) }
94121 { React . createElement (
95122 props . as || "div" ,
96- {
97- ...{
98- id : space . id ,
99- ref : elementRef ,
100- className : outerClasses . join ( " " ) ,
101- } ,
102- ...events ,
103- } ,
104- < div className = { innerClasses . join ( " " ) } style = { innerStyle } >
105- < ParentContext . Provider value = { space . id } >
106- < LayerContext . Provider value = { undefined } >
107- < DOMRectContext . Provider value = { domRect } > { centeredContent } </ DOMRectContext . Provider >
108- </ LayerContext . Provider >
109- </ ParentContext . Provider >
110- </ div > ,
123+ outerProps ,
124+ < >
125+ { isSSR && space . ssrStyle && < style className = "ssr" > { space . ssrStyle } </ style > }
126+ < div className = { innerClasses . join ( " " ) } style = { innerStyle } >
127+ < ParentContext . Provider value = { space . id } >
128+ < LayerContext . Provider value = { undefined } >
129+ < DOMRectContext . Provider value = { domRect } > { centeredContent } </ DOMRectContext . Provider >
130+ </ LayerContext . Provider >
131+ </ ParentContext . Provider >
132+ </ div >
133+ </ > ,
111134 ) }
112135 </ >
113136 ) ;
0 commit comments