11import { CenterType , ResizeHandlePlacement , AnchorType , Type } from "../core-types" ;
2- import { useSpace , ParentContext , LayerContext , DOMRectContext , IReactSpaceInnerProps , useEffectOnce } from "../core-react" ;
2+ import {
3+ useSpace ,
4+ ParentContext ,
5+ LayerContext ,
6+ DOMRectContext ,
7+ IReactSpaceInnerProps ,
8+ useEffectOnce ,
9+ SSR_SUPPORT_ENABLED ,
10+ useUniqueId ,
11+ } from "../core-react" ;
312import * as React from "react" ;
413import { Centered } from "./Centered" ;
514import { CenteredVertically } from "./CenteredVertically" ;
6- import { shortuuid } from "../core-utils" ;
15+ import { isServer , updateStyleDefinition } from "../core-utils" ;
716
817function applyCentering ( children : React . ReactNode , centerType : CenterType | undefined ) {
918 switch ( centerType ) {
@@ -22,8 +31,14 @@ export class Space extends React.Component<IReactSpaceInnerProps> {
2231}
2332
2433const SpaceInner : React . FC < IReactSpaceInnerProps & { wrapperInstance : Space } > = ( props ) => {
25- if ( ! props . id && ! props . wrapperInstance [ "_react_spaces_uniqueid" ] ) {
26- props . wrapperInstance [ "_react_spaces_uniqueid" ] = `s${ shortuuid ( ) } ` ;
34+ let idToUse = props . id ?? props . wrapperInstance [ "_react_spaces_uniqueid" ] ;
35+ const [ initialRender , setInitialRender ] = React . useState ( SSR_SUPPORT_ENABLED ? true : false ) ;
36+
37+ const uniqueId = useUniqueId ( ) ;
38+
39+ if ( ! idToUse ) {
40+ props . wrapperInstance [ "_react_spaces_uniqueid" ] = uniqueId ;
41+ idToUse = props . wrapperInstance [ "_react_spaces_uniqueid" ] ;
2742 }
2843
2944 const {
@@ -56,11 +71,25 @@ const SpaceInner: React.FC<IReactSpaceInnerProps & { wrapperInstance: Space }> =
5671
5772 const { space, domRect, elementRef, resizeHandles } = useSpace ( {
5873 ...props ,
59- ...{ id : props . id || props . wrapperInstance [ "_react_spaces_uniqueid" ] } ,
74+ ...{ id : idToUse } ,
6075 } ) ;
6176
77+ if ( SSR_SUPPORT_ENABLED && ! isServer ( ) ) {
78+ const preRenderedStyle = document . getElementById ( `style_${ idToUse } _ssr` ) ;
79+ if ( preRenderedStyle ) {
80+ space . ssrStyle = preRenderedStyle . innerHTML ;
81+ }
82+ updateStyleDefinition ( space ) ;
83+ }
84+
6285 useEffectOnce ( ( ) => {
6386 space . element = elementRef . current ! ;
87+
88+ if ( SSR_SUPPORT_ENABLED ) {
89+ if ( initialRender ) {
90+ setInitialRender ( false ) ;
91+ }
92+ }
6493 } ) ;
6594
6695 const userClasses = className ? className . split ( " " ) . map ( ( c ) => c . trim ( ) ) : [ ] ;
@@ -89,19 +118,22 @@ const SpaceInner: React.FC<IReactSpaceInnerProps & { wrapperInstance: Space }> =
89118
90119 const centeredContent = applyCentering ( children , props . centerContent ) ;
91120
121+ const outerProps = {
122+ ...{
123+ id : space . id ,
124+ ref : elementRef ,
125+ className : outerClasses . join ( " " ) ,
126+ } ,
127+ ...events ,
128+ } as any ;
129+
92130 return (
93131 < >
94132 { resizeHandles . mouseHandles . map ( ( handleProps ) => handleRender ?.( handleProps ) || < div { ...handleProps } /> ) }
133+ { SSR_SUPPORT_ENABLED && space . ssrStyle && initialRender && < style id = { `style_${ space . id } _ssr` } > { space . ssrStyle } </ style > }
95134 { React . createElement (
96135 props . as || "div" ,
97- {
98- ...{
99- id : space . id ,
100- ref : elementRef ,
101- className : outerClasses . join ( " " ) ,
102- } ,
103- ...events ,
104- } ,
136+ outerProps ,
105137 < div className = { innerClasses . join ( " " ) } style = { innerStyle } >
106138 < ParentContext . Provider value = { space . id } >
107139 < LayerContext . Provider value = { undefined } >
0 commit comments