-
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.
- Loading branch information
1 parent
312347f
commit df3459e
Showing
5 changed files
with
91 additions
and
30 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { ReactNode } from 'react'; | ||
import React, { createContext, useState, useContext, useEffect } from 'react'; | ||
|
||
interface ResponsiveContextType { | ||
isMobile: boolean; | ||
} | ||
|
||
const ResponsiveContext = createContext<ResponsiveContextType | undefined>(undefined); | ||
|
||
interface ResponsiveProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const ResponsiveProvider = ({ children }: ResponsiveProviderProps) => { | ||
const [isMobile, setIsMobile] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const handleResize = () => { | ||
setIsMobile(window.innerWidth <= 768); // Adjust this breakpoint as needed | ||
}; | ||
handleResize(); | ||
window.addEventListener('resize', handleResize); | ||
return () => window.removeEventListener('resize', handleResize); | ||
}, []); | ||
|
||
return <ResponsiveContext.Provider value={{ isMobile }}>{children}</ResponsiveContext.Provider>; | ||
}; | ||
|
||
export const useResponsive = (): ResponsiveContextType => { | ||
const context = useContext(ResponsiveContext); | ||
if (context === undefined) { | ||
throw new Error('useResponsive must be used within a ResponsiveProvider'); | ||
} | ||
return context; | ||
}; |
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