-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 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
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
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,58 @@ | ||
'use client'; | ||
|
||
import React, { | ||
createContext, | ||
useState, | ||
useEffect, | ||
useContext, | ||
useMemo, | ||
} from 'react'; | ||
|
||
interface WindowWidthContextProps { | ||
isWeb: boolean; | ||
} | ||
|
||
const WindowWidthContext = createContext<WindowWidthContextProps | undefined>( | ||
undefined, | ||
); | ||
/** | ||
* | ||
* @param root0 - Provider that checks window width and calculates if this is a web or mobile screen | ||
* @param root0.children - Provider will give context to its children components. | ||
* @returns The WindowWidthProvider component, should be wrapped across the whole project for | ||
* responsive design, per the Progressive Web Application functionality. | ||
* This provider is used in the root layout of the project such that we have global access to the window width | ||
* and can conditionally render accordingly. | ||
*/ | ||
export function WindowWidthProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const [isWeb, setIsWeb] = useState(window.innerWidth >= 1024); | ||
|
||
useEffect(() => { | ||
const handleResize = () => setIsWeb(window.innerWidth >= 1024); | ||
window.addEventListener('resize', handleResize); | ||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, []); | ||
|
||
const windowWidthValue = useMemo(() => ({ isWeb }), [isWeb]); | ||
|
||
return ( | ||
<WindowWidthContext.Provider value={windowWidthValue}> | ||
{children} | ||
</WindowWidthContext.Provider> | ||
); | ||
} | ||
|
||
export const useWebDeviceDetection = () => { | ||
const context = useContext(WindowWidthContext); | ||
if (context === undefined) { | ||
throw new Error('useIsWeb must be used within a WindowWidthProvider'); | ||
} | ||
|
||
return context.isWeb; | ||
}; |