@@ -19,6 +19,10 @@ export interface StickySidebarProps {
19
19
menu : MenuStore ;
20
20
}
21
21
22
+ export interface StickySidebarState {
23
+ offsetTop ?: string ;
24
+ }
25
+
22
26
const stickyfill = Stickyfill && Stickyfill ( ) ;
23
27
24
28
const StyledStickySidebar = styled . div < { open ?: boolean } > `
@@ -77,13 +81,26 @@ const FloatingButton = styled.div`
77
81
` ;
78
82
79
83
@observer
80
- export class StickyResponsiveSidebar extends React . Component < StickySidebarProps > {
84
+ export class StickyResponsiveSidebar extends React . Component <
85
+ StickySidebarProps ,
86
+ StickySidebarState
87
+ > {
88
+ static contextType = OptionsContext ;
89
+ context ! : React . ContextType < typeof OptionsContext > ;
90
+ state : StickySidebarState = { } ;
91
+
81
92
stickyElement : Element ;
82
93
83
94
componentDidMount ( ) {
84
95
if ( stickyfill ) {
85
96
stickyfill . add ( this . stickyElement ) ;
86
97
}
98
+
99
+ // rerender when hydrating from SSR
100
+ // see https://github.com/facebook/react/issues/8017#issuecomment-256351955
101
+ this . setState ( {
102
+ offsetTop : this . getScrollYOffset ( this . context ) ,
103
+ } ) ;
87
104
}
88
105
89
106
componentWillUnmount ( ) {
@@ -92,7 +109,7 @@ export class StickyResponsiveSidebar extends React.Component<StickySidebarProps>
92
109
}
93
110
}
94
111
95
- getScrollYOffset ( options ) {
112
+ getScrollYOffset ( options : RedocNormalizedOptions ) {
96
113
let top ;
97
114
if ( this . props . scrollYOffset !== undefined ) {
98
115
top = RedocNormalizedOptions . normalizeScrollYOffset ( this . props . scrollYOffset ) ( ) ;
@@ -105,43 +122,32 @@ export class StickyResponsiveSidebar extends React.Component<StickySidebarProps>
105
122
render ( ) {
106
123
const open = this . props . menu . sideBarOpened ;
107
124
108
- const style = options => {
109
- const top = this . getScrollYOffset ( options ) ;
110
- return {
111
- top,
112
- height : `calc(100vh - ${ top } )` ,
113
- } ;
114
- } ;
125
+ const top = this . state . offsetTop || this . getScrollYOffset ( this . context ) ;
115
126
116
127
return (
117
- < OptionsContext . Consumer >
118
- { options => (
119
- < >
120
- < StyledStickySidebar
121
- open = { open }
122
- className = { this . props . className }
123
- style = { style ( options ) }
124
- // tslint:disable-next-line
125
- ref = { el => {
126
- this . stickyElement = el as any ;
127
- } }
128
- >
129
- { this . props . children }
130
- </ StyledStickySidebar >
131
- < FloatingButton onClick = { this . toggleNavMenu } >
132
- < AnimatedChevronButton open = { open } />
133
- </ FloatingButton >
134
- </ >
135
- ) }
136
- </ OptionsContext . Consumer >
128
+ < >
129
+ < StyledStickySidebar
130
+ open = { open }
131
+ className = { this . props . className }
132
+ style = { {
133
+ top,
134
+ height : `calc(100vh - ${ top } )` ,
135
+ } }
136
+ // tslint:disable-next-line
137
+ ref = { el => {
138
+ this . stickyElement = el as any ;
139
+ } }
140
+ >
141
+ { this . props . children }
142
+ </ StyledStickySidebar >
143
+ < FloatingButton onClick = { this . toggleNavMenu } >
144
+ < AnimatedChevronButton open = { open } />
145
+ </ FloatingButton >
146
+ </ >
137
147
) ;
138
148
}
139
149
140
150
private toggleNavMenu = ( ) => {
141
151
this . props . menu . toggleSidebar ( ) ;
142
152
} ;
143
-
144
- // private closeNavMenu = () => {
145
- // this.setState({ open: false });
146
- // };
147
153
}
0 commit comments