1
1
import { 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" ;
3
12
import * as React from "react" ;
4
13
import { Centered } from "./Centered" ;
5
14
import { CenteredVertically } from "./CenteredVertically" ;
6
- import { shortuuid } from "../core-utils" ;
15
+ import { isServer , updateStyleDefinition } from "../core-utils" ;
7
16
8
17
function applyCentering ( children : React . ReactNode , centerType : CenterType | undefined ) {
9
18
switch ( centerType ) {
@@ -22,8 +31,14 @@ export class Space extends React.Component<IReactSpaceInnerProps> {
22
31
}
23
32
24
33
const 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" ] ;
27
42
}
28
43
29
44
const {
@@ -56,11 +71,25 @@ const SpaceInner: React.FC<IReactSpaceInnerProps & { wrapperInstance: Space }> =
56
71
57
72
const { space, domRect, elementRef, resizeHandles } = useSpace ( {
58
73
...props ,
59
- ...{ id : props . id || props . wrapperInstance [ "_react_spaces_uniqueid" ] } ,
74
+ ...{ id : idToUse } ,
60
75
} ) ;
61
76
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
+
62
85
useEffectOnce ( ( ) => {
63
86
space . element = elementRef . current ! ;
87
+
88
+ if ( SSR_SUPPORT_ENABLED ) {
89
+ if ( initialRender ) {
90
+ setInitialRender ( false ) ;
91
+ }
92
+ }
64
93
} ) ;
65
94
66
95
const userClasses = className ? className . split ( " " ) . map ( ( c ) => c . trim ( ) ) : [ ] ;
@@ -89,19 +118,22 @@ const SpaceInner: React.FC<IReactSpaceInnerProps & { wrapperInstance: Space }> =
89
118
90
119
const centeredContent = applyCentering ( children , props . centerContent ) ;
91
120
121
+ const outerProps = {
122
+ ...{
123
+ id : space . id ,
124
+ ref : elementRef ,
125
+ className : outerClasses . join ( " " ) ,
126
+ } ,
127
+ ...events ,
128
+ } as any ;
129
+
92
130
return (
93
131
< >
94
132
{ resizeHandles . mouseHandles . map ( ( handleProps ) => handleRender ?.( handleProps ) || < div { ...handleProps } /> ) }
133
+ { SSR_SUPPORT_ENABLED && space . ssrStyle && initialRender && < style id = { `style_${ space . id } _ssr` } > { space . ssrStyle } </ style > }
95
134
{ React . createElement (
96
135
props . as || "div" ,
97
- {
98
- ...{
99
- id : space . id ,
100
- ref : elementRef ,
101
- className : outerClasses . join ( " " ) ,
102
- } ,
103
- ...events ,
104
- } ,
136
+ outerProps ,
105
137
< div className = { innerClasses . join ( " " ) } style = { innerStyle } >
106
138
< ParentContext . Provider value = { space . id } >
107
139
< LayerContext . Provider value = { undefined } >
0 commit comments