-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: persist drawer state between route changes
- Loading branch information
1 parent
ae58a91
commit 776ed31
Showing
4 changed files
with
53 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { AppRouter } from '@app/lib/routes' | ||
import { DrawerContextProvider } from '@features/drawer/lib/drawerContext' | ||
|
||
function App() { | ||
return <AppRouter /> | ||
return ( | ||
<DrawerContextProvider> | ||
<AppRouter /> | ||
</DrawerContextProvider> | ||
) | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useMediaQuery } from '@shared/lib/useMediaQuery' | ||
import { createContext, useContext, useEffect, useState } from 'react' | ||
|
||
type DrawerContextValue = { | ||
isOpen: boolean | ||
setIsOpen: React.Dispatch<React.SetStateAction<boolean>> | ||
} | ||
|
||
export const DrawerContext = createContext<DrawerContextValue>({ | ||
isOpen: false, | ||
setIsOpen: () => {} | ||
}) | ||
|
||
export const DrawerContextProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [isOpen, setIsOpen] = useState<boolean>(true) | ||
|
||
const isLargeScreen = useMediaQuery('(min-width: 1280px)') | ||
|
||
useEffect(() => { | ||
setIsOpen(isLargeScreen) | ||
}, [isLargeScreen]) | ||
|
||
return <DrawerContext.Provider value={{ isOpen, setIsOpen }}>{children}</DrawerContext.Provider> | ||
} | ||
|
||
export const useDrawer = () => useContext(DrawerContext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
export const useMediaQuery = (query: string) => { | ||
const [matches, setMatches] = useState(true) | ||
useEffect(() => { | ||
const media = window.matchMedia(query) | ||
if (media.matches !== matches) { | ||
setMatches(media.matches) | ||
} | ||
const listener = () => { | ||
setMatches(media.matches) | ||
} | ||
media.addEventListener('change', listener) | ||
return () => media.removeEventListener('change', listener) | ||
}, [matches, query]) | ||
return matches | ||
} |