3
3
* Created by Andrea Blackwell 11/05/2024
4
4
**/
5
5
6
- import React , { useEffect , useState , useCallback } from 'react' ;
6
+ import React , { useEffect , useState } from 'react' ;
7
7
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
8
8
import { throttle } from "lodash" ;
9
9
import PropTypes from "prop-types" ;
@@ -29,10 +29,8 @@ const SidebarWrapper = React.memo(({
29
29
const [ sidebarContentHeight , setSidebarContentHeight ] = useState ( ) ;
30
30
const [ mainContentHeight , setMainContentHeight ] = useState ( ) ;
31
31
const [ isOpened , setIsOpened ] = useState ( sidebarOpen ) ;
32
- const [ isHeaderVisible , setIsHeaderVisible ] = useState ( ) ;
33
32
const [ sidebarIsSticky , setSidebarIsSticky ] = useState ( ) ;
34
33
const [ isFooterVisible , setIsFooterVisible ] = useState ( ) ;
35
- const [ observerSupported , setObserverSupported ] = useState ( false ) ;
36
34
37
35
const mainContentEl = document . querySelector ( "#main-content" ) ;
38
36
const footerEl = document . querySelector ( "footer" ) ;
@@ -41,9 +39,6 @@ const SidebarWrapper = React.memo(({
41
39
const topStickyBarHeight = 60 ;
42
40
const minContentHeight = 124 ;
43
41
44
- const observerOptions = {
45
- threshold : 0.1
46
- } ;
47
42
const toggleOpened = ( e ) => {
48
43
e . preventDefault ( ) ;
49
44
setIsOpened ( ( prevState ) => ! prevState ) ;
@@ -88,17 +83,17 @@ const SidebarWrapper = React.memo(({
88
83
setSidebarContentHeight ( sidebarContentArea ) ;
89
84
} ;
90
85
91
- const updatePosition = ( ) => {
86
+ const updatePosition = ( isHeaderSticky ) => {
92
87
const tmpFooterInView = checkInView ( footerEl ) + footerMargin ;
93
88
94
- if ( ! sidebarIsSticky && isHeaderVisible ) {
89
+ if ( ! isHeaderSticky ) {
95
90
resizeHeightByHeader ( ) ;
96
91
}
97
- else if ( sidebarIsSticky ) {
92
+ else if ( isHeaderSticky ) {
98
93
document . querySelector ( ".search-collapsible-sidebar-container" ) . style . height = '100vh - 60' ;
99
94
}
100
95
101
- if ( isFooterVisible || tmpFooterInView > 0 ) {
96
+ if ( tmpFooterInView > 0 ) {
102
97
resizeHeightByFooter ( ) ;
103
98
}
104
99
} ;
@@ -107,11 +102,14 @@ const SidebarWrapper = React.memo(({
107
102
const tmpFooterInView = checkInView ( footerEl ) + footerMargin ;
108
103
setIsFooterVisible ( tmpFooterInView > 0 ) ;
109
104
const isStickyEl = document . querySelector ( ".usda-page-header--sticky" ) ;
110
- const tmpIsSticky = isStickyEl !== null ;
111
- setSidebarIsSticky ( tmpIsSticky ) ;
105
+ const isHeaderSticky = isStickyEl !== null ;
106
+
107
+ if ( ! isHeaderSticky || sidebarIsSticky !== isHeaderSticky || window . scrollY < 170 || tmpFooterInView > 0 ) {
108
+ updatePosition ( isHeaderSticky ) ;
109
+ }
112
110
113
- if ( ! tmpIsSticky || tmpFooterInView > 45 ) {
114
- updatePosition ( ) ;
111
+ if ( sidebarIsSticky !== isHeaderSticky ) {
112
+ setSidebarIsSticky ( isHeaderSticky ) ;
115
113
}
116
114
} , 20 ) ;
117
115
@@ -192,28 +190,8 @@ const SidebarWrapper = React.memo(({
192
190
document . querySelector ( "#main-content .v2" ) . style . minHeight = `${ window . innerHeight } px` ;
193
191
}
194
192
195
- updatePosition ( ) ;
196
-
197
193
// eslint-disable-next-line react-hooks/exhaustive-deps
198
- } , [ isFooterVisible , isHeaderVisible , sidebarIsSticky , mainContentHeight ] ) ;
199
-
200
- const handleObserver = useCallback ( ( entries ) => {
201
- entries . forEach ( ( entry ) => {
202
- if ( entry . target ?. localName ?. includes ( "footer" ) ) {
203
- setIsFooterVisible ( true ) ;
204
- }
205
-
206
- if ( entry . target ?. className ?. includes ( "usda-page-header" ) && ! entry . target ?. className ?. includes ( "usda-page-header--sticky" ) ) {
207
- setIsHeaderVisible ( true ) ;
208
- setSidebarIsSticky ( false ) ;
209
- }
210
-
211
- if ( entry . target ?. className ?. includes ( "usda-page-header--sticky" ) ) {
212
- setSidebarIsSticky ( true ) ;
213
- setIsHeaderVisible ( false ) ;
214
- }
215
- } ) ;
216
- } ) ;
194
+ } , [ mainContentHeight ] ) ;
217
195
218
196
useEffect ( ( ) => {
219
197
// eslint-disable-next-line no-undef
@@ -240,35 +218,11 @@ const SidebarWrapper = React.memo(({
240
218
// eslint-disable-next-line react-hooks/exhaustive-deps
241
219
} ) ;
242
220
243
- useEffect ( ( ) => {
244
- setObserverSupported ( 'IntersectionObserver' in window ) ;
245
- // eslint-disable-next-line react-hooks/exhaustive-deps
246
- } , [ ] ) ;
247
-
248
- // eslint-disable-next-line consistent-return
249
- useEffect ( ( ) => {
250
- if ( observerSupported ) {
251
- const targets = [ document . querySelector ( ".usda-page-header" ) ] ;
252
- targets . push ( document . querySelector ( ".usda-page-header" ) ) ;
253
-
254
- // eslint-disable-next-line no-undef
255
- const observer = new IntersectionObserver ( handleObserver , observerOptions ) ;
256
- targets . forEach ( ( i ) => {
257
- if ( i . className ) {
258
- observer . observe ( i ) ;
259
- }
260
- } ) ;
261
-
262
- return ( ) => observer . disconnect ( ) ;
263
- }
264
- // eslint-disable-next-line react-hooks/exhaustive-deps
265
- } , [ observerSupported ] ) ;
266
-
267
- const calculateHeight = ( ) => {
221
+ const selectHeight = ( ) => {
268
222
const isStickyEl = document . querySelector ( ".usda-page-header--sticky" ) ;
269
- const tmpIsSticky = isStickyEl !== null ;
223
+ const isHeaderSticky = isStickyEl !== null ;
270
224
271
- if ( tmpIsSticky && ! isFooterVisible ) {
225
+ if ( isHeaderSticky && ! isFooterVisible ) {
272
226
return 'calc(100vh - 60px)' ;
273
227
}
274
228
@@ -280,7 +234,7 @@ const SidebarWrapper = React.memo(({
280
234
className = { `search-collapsible-sidebar-container search-sidebar ${ sidebarIsSticky ? "sticky" : "" } ` }
281
235
style = { isMobile ? { } : { display : "none" } } >
282
236
< div
283
- style = { { height : calculateHeight ( ) , overscrollBehavior : "none" } }
237
+ style = { { height : selectHeight ( ) , overscrollBehavior : "none" } }
284
238
className = { `search-sidebar collapsible-sidebar ${ initialPageLoad ? "is-initial-loaded" : "" } ${ isOpened ? 'opened' : '' } ` } >
285
239
< div
286
240
className = "collapsible-sidebar--toggle"
0 commit comments