use-breakpoints.ts 661 B

1234567891011121314151617181920212223242526272829
  1. 'use client'
  2. import React from 'react'
  3. export enum MediaType {
  4. mobile = 'mobile',
  5. tablet = 'tablet',
  6. pc = 'pc',
  7. }
  8. const useBreakpoints = () => {
  9. const [width, setWidth] = React.useState(globalThis.innerWidth)
  10. const media = (() => {
  11. if (width <= 640)
  12. return MediaType.mobile
  13. if (width <= 768)
  14. return MediaType.tablet
  15. return MediaType.pc
  16. })()
  17. React.useEffect(() => {
  18. const handleWindowResize = () => setWidth(window.innerWidth)
  19. window.addEventListener('resize', handleWindowResize)
  20. return () => window.removeEventListener('resize', handleWindowResize)
  21. }, [])
  22. return media
  23. }
  24. export default useBreakpoints