dify/web/hooks/use-breakpoints.ts

30 lines
661 B
TypeScript
Raw Normal View History

2023-05-15 08:51:32 +08:00
'use client'
import React from 'react'
export enum MediaType {
mobile = 'mobile',
tablet = 'tablet',
pc = 'pc',
}
const useBreakpoints = () => {
const [width, setWidth] = React.useState(globalThis.innerWidth)
2023-05-15 08:51:32 +08:00
const media = (() => {
if (width <= 640)
return MediaType.mobile
if (width <= 768)
return MediaType.tablet
return MediaType.pc
})()
2023-05-15 08:51:32 +08:00
React.useEffect(() => {
const handleWindowResize = () => setWidth(window.innerWidth)
window.addEventListener('resize', handleWindowResize)
return () => window.removeEventListener('resize', handleWindowResize)
}, [])
2023-05-15 08:51:32 +08:00
return media
2023-05-15 08:51:32 +08:00
}
export default useBreakpoints