Commit b0d71f10 authored by Jack Short's avatar Jack Short Committed by GitHub

chore: adding useScreenSize hook (#5669)

* feat: adding useScreenSize hook

* updating isTablet

* adding comments
parent 1cfb3d80
// HOW TO USE:
// the hook returns a record of booleans, where the key is the name of the breakpoint
// if the screen size is greater than or equal to the breakpoint, the value will be true
// for example if the screen size is 1024px, the value of the 'md' key will be true and the value of the 'lg' key will be true
// if you were to check if the screen is smaller than a breakpoint you would check if it is false
import { useEffect, useState } from 'react'
import { BREAKPOINTS } from 'theme'
const isClient = typeof window !== 'undefined'
function getScreenSize(): Record<keyof typeof BREAKPOINTS, boolean> {
return Object.keys(BREAKPOINTS).reduce(
(obj, key) =>
Object.assign(obj, {
[key]: isClient ? window.innerWidth >= BREAKPOINTS[key as keyof typeof BREAKPOINTS] : false,
}),
{} as Record<keyof typeof BREAKPOINTS, boolean>
)
}
export function useScreenSize(): Record<keyof typeof BREAKPOINTS, boolean> {
const [screenSize, setScreenSize] = useState(getScreenSize())
useEffect(() => {
function handleResize() {
setScreenSize(getScreenSize())
}
if (isClient) {
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}
return undefined
}, [])
return screenSize
}
import { breakpoints } from 'nft/css/sprinkles.css' // @deprecated in favor of useScreenSize
import { useEffect, useState } from 'react' import { useScreenSize } from 'hooks/useScreenSize'
const isClient = typeof window !== 'undefined'
export function getIsMobile() {
return isClient ? window.innerWidth < breakpoints.sm : false
}
export function useIsMobile(): boolean { export function useIsMobile(): boolean {
const [isMobile, setIsMobile] = useState(getIsMobile) const isScreenSize = useScreenSize()
const isMobile = !isScreenSize['sm']
useEffect(() => {
function handleResize() {
setIsMobile(getIsMobile())
}
if (isClient) {
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}
return undefined
}, [])
return isMobile return isMobile
} }
import { breakpoints } from 'nft/css/sprinkles.css' // @deprecated in favor of useScreenSize
import { useEffect, useState } from 'react' import { useScreenSize } from 'hooks/useScreenSize'
const isClient = typeof window !== 'undefined'
function getIsTablet() {
return isClient ? window.innerWidth < breakpoints.lg && window.innerWidth >= breakpoints.sm : false
}
export function useIsTablet(): boolean { export function useIsTablet(): boolean {
const [isTablet, setIsTablet] = useState(getIsTablet) const isScreenSize = useScreenSize()
const isTablet = !isScreenSize['lg'] && isScreenSize['sm']
useEffect(() => {
function handleResize() {
setIsTablet(getIsTablet())
}
if (isClient) {
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}
return undefined
}, [])
return isTablet return isTablet
} }
import { getIsMobile } from 'nft/hooks' import { breakpoints } from 'nft/css/sprinkles.css'
const isClient = typeof window !== 'undefined'
function getIsMobile() {
return isClient ? window.innerWidth < breakpoints.sm : false
}
export const scrollToTop = () => { export const scrollToTop = () => {
const isMobile = getIsMobile() const isMobile = getIsMobile()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment