-
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
70e49ae
commit 02e943a
Showing
8 changed files
with
62 additions
and
37 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
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,35 +1,49 @@ | ||
import type { ReactNode } from 'react'; | ||
import React, { createContext, useState, useContext, useEffect } from 'react'; | ||
import React, { createContext, useContext, useEffect, useState } from 'react'; | ||
|
||
import { DeviceBreakpoints } from '@/constants/deviceBreakpoints'; | ||
|
||
interface ResponsiveContextType { | ||
isMobile: boolean; | ||
isTablet: boolean; | ||
} | ||
|
||
const ResponsiveContext = createContext<ResponsiveContextType | undefined>(undefined); | ||
const DefaultResponsiveContext: ResponsiveContextType = { | ||
isMobile: false, | ||
isTablet: false, | ||
}; | ||
|
||
const ResponsiveContext = createContext<ResponsiveContextType>(DefaultResponsiveContext); | ||
|
||
interface ResponsiveProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const ResponsiveProvider = ({ children }: ResponsiveProviderProps) => { | ||
const [isMobile, setIsMobile] = useState<boolean>(false); | ||
const [isTablet, setIsTablet] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const handleResize = () => { | ||
setIsMobile(window.innerWidth <= 768); // Adjust this breakpoint as needed | ||
setIsMobile(window.innerWidth <= DeviceBreakpoints.MOBILE); | ||
setIsTablet(window.innerWidth > DeviceBreakpoints.MOBILE && window.innerWidth <= DeviceBreakpoints.TABLET); | ||
}; | ||
handleResize(); | ||
window.addEventListener('resize', handleResize); | ||
return () => window.removeEventListener('resize', handleResize); | ||
}, []); | ||
|
||
return <ResponsiveContext.Provider value={{ isMobile }}>{children}</ResponsiveContext.Provider>; | ||
return <ResponsiveContext.Provider value={{ isMobile, isTablet }}>{children}</ResponsiveContext.Provider>; | ||
}; | ||
|
||
export const useResponsiveMobileContext = (): boolean => { | ||
return useContext(ResponsiveContext).isMobile; | ||
}; | ||
|
||
export const useResponsiveTabletContext = (): boolean => { | ||
return useContext(ResponsiveContext).isTablet; | ||
}; | ||
|
||
export const useResponsive = (): ResponsiveContextType => { | ||
const context = useContext(ResponsiveContext); | ||
if (context === undefined) { | ||
throw new Error('useResponsive must be used within a ResponsiveProvider'); | ||
} | ||
return context; | ||
export const useSmallScreen = (): boolean => { | ||
return useContext(ResponsiveContext).isMobile || useContext(ResponsiveContext).isTablet; | ||
}; |
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,4 @@ | ||
export const DeviceBreakpoints = { | ||
MOBILE: 576, | ||
TABLET: 768, | ||
}; |