1- import { useContext , useEffect , useRef , useState } from 'react'
1+ import { useContext , useRef , useState } from 'react'
22import ButtonIcon from '../ButtonIcon'
3+ import DesktopSearch from './DesktopSearch'
4+ import MobileSearch from './MobileSearch'
35import { HomeIcon } from './HomeIcon'
46import { UserIcon } from './UserIcon'
5- import MobileSearch from './MobileSearch'
67import {
78 ContextDisableFocusTrap ,
89 ContextFilteredStreamData ,
@@ -14,9 +15,11 @@ import {
1415 ContextSearchText ,
1516 ContextStreamData ,
1617} from '../../../App'
17- import DesktopSearch from './DesktopSearch'
1818import { getSearchFilter } from '../../../helper/getSearchFilter'
1919import { setItemInStorage } from '../../../helper/setItemInStorage'
20+ import { useNavigationScrollY } from '../../../hooks/useNavigationScrollY'
21+ import { useCloseSearchResults } from '../../../hooks/useCloseSearchResults'
22+ import { useHideMobileSearch } from '../../../hooks/useHideMobileSearch'
2023
2124const Navigation = ( ) => {
2225 const contextScreenWidth = useContext ( ContextScreenWidth )
@@ -253,103 +256,24 @@ const Navigation = () => {
253256 } , 0 )
254257 }
255258
256- useEffect ( ( ) => {
257- let lastScrollY = window . scrollY
258- let timer : NodeJS . Timeout
259-
260- const handleScroll = ( ) => {
261- if ( window . scrollY === 0 ) {
262- setNavOpacity ( 'opacity-100' )
263- } else if ( window . scrollY < lastScrollY && ! blockOpacity ) {
264- setNavOpacity ( 'opacity-75' )
265- } else if ( ! blockOpacity ) {
266- setNavOpacity ( 'opacity-95' )
267- }
268- lastScrollY = window . scrollY
269-
270- clearTimeout ( timer )
271- if ( navOpacity !== 'opacity-100' ) {
272- timer = setTimeout ( ( ) => {
273- setNavOpacity ( 'opacity-100' )
274- } , 500 )
275- }
276- }
277-
278- window . addEventListener ( 'scroll' , handleScroll )
279-
280- return ( ) => {
281- window . removeEventListener ( 'scroll' , handleScroll )
282- clearTimeout ( timer )
283- }
284- } , [ blockOpacity , navOpacity ] )
285-
286- useEffect ( ( ) => {
287- const handleClickOutside = ( event : MouseEvent ) => {
288- if (
289- ( ( desktopSearchRef . current &&
290- ! desktopSearchRef . current . contains ( event . target as Node ) ) ||
291- ( mobileSearchRef . current &&
292- ! mobileSearchRef . current . contains (
293- event . target as Node
294- ) ) ) &&
295- searchResultsExpanded
296- ) {
297- event . stopPropagation ( )
298- setSearchResultsExpanded ( false )
299- }
300- }
301-
302- const handleEscape = ( event : KeyboardEvent ) => {
303- if (
304- ( ( desktopSearchRef . current &&
305- desktopSearchRef . current . contains ( event . target as Node ) ) ||
306- ( mobileSearchRef . current &&
307- mobileSearchRef . current . contains (
308- event . target as Node
309- ) ) ) &&
310- searchResultsExpanded &&
311- event . key === 'Escape'
312- ) {
313- event . stopPropagation ( )
314- setSearchResultsExpanded ( false )
315- if ( ! inputRef ?. current ?. onfocus ) {
316- if (
317- contextScreenWidth === 'MOBILE' ||
318- contextScreenWidth === 'TABLET_SMALL'
319- ) {
320- buttonIconRef ?. current ?. focus ( )
321- } else {
322- userIconRef ?. current ?. focus ( )
323- anchorRef ?. current ?. focus ( )
324- }
325- }
326- }
327- }
328-
329- if ( searchResultsExpanded ) {
330- document . addEventListener ( 'keydown' , handleEscape )
331- document . addEventListener ( 'mousedown' , handleClickOutside )
332- } else {
333- document . removeEventListener ( 'keydown' , handleEscape )
334- document . removeEventListener ( 'mousedown' , handleClickOutside )
335- }
336-
337- return ( ) => {
338- document . removeEventListener ( 'keydown' , handleEscape )
339- document . removeEventListener ( 'mousedown' , handleClickOutside )
340- }
341- } , [ contextScreenWidth , searchResultsExpanded ] )
342-
343- useEffect ( ( ) => {
344- if (
345- ( contextScreenWidth === 'MOBILE' ||
346- contextScreenWidth === 'TABLET_SMALL' ) &&
347- inputFocussed
348- ) {
349- setHideSearch ( false )
350- setAriaPressed ( true )
351- }
352- } , [ contextScreenWidth , inputFocussed , setHideSearch ] )
259+ useNavigationScrollY ( blockOpacity , navOpacity , setNavOpacity )
260+ useCloseSearchResults (
261+ anchorRef ,
262+ buttonIconRef ,
263+ contextScreenWidth ,
264+ desktopSearchRef ,
265+ inputRef ,
266+ mobileSearchRef ,
267+ searchResultsExpanded ,
268+ setSearchResultsExpanded ,
269+ userIconRef
270+ )
271+ useHideMobileSearch (
272+ contextScreenWidth ,
273+ inputFocussed ,
274+ setAriaPressed ,
275+ setHideSearch
276+ )
353277
354278 return (
355279 < div className = "sticky top-0 z-10" data-testid = "navigation-container" >
0 commit comments