|
1 | 1 | import { useUtilsEffect } from '@zardoy/react-util' |
2 | 2 | import { useEffect, useState } from 'react' |
3 | | -import { useMedia } from 'react-use' |
4 | 3 |
|
5 | | -const SMALL_SCREEN_MEDIA = '@media (max-width: 440px)' |
| 4 | +const SMALL_SCREEN_WIDTH = 440 |
6 | 5 | export const useIsSmallWidth = () => { |
7 | | - return useMedia(SMALL_SCREEN_MEDIA.replace('@media ', '')) |
| 6 | + const [isSmall, setIsSmall] = useState(() => document.documentElement.clientWidth <= SMALL_SCREEN_WIDTH) |
| 7 | + |
| 8 | + useEffect(() => { |
| 9 | + const checkWidth = () => { |
| 10 | + setIsSmall(document.documentElement.clientWidth <= SMALL_SCREEN_WIDTH) |
| 11 | + } |
| 12 | + addEventListener('resize', checkWidth) |
| 13 | + return () => { |
| 14 | + removeEventListener('resize', checkWidth) |
| 15 | + } |
| 16 | + }, []) |
| 17 | + |
| 18 | + return isSmall |
8 | 19 | } |
9 | 20 |
|
10 | 21 | export const usePassesWindowDimensions = (minWidth: number | null = null, minHeight: number | null = null) => { |
11 | | - let media = '(' |
12 | | - if (minWidth !== null) { |
13 | | - media += `min-width: ${minWidth}px, ` |
14 | | - } |
15 | | - if (minHeight !== null) { |
16 | | - media += `min-height: ${minHeight}px, ` |
17 | | - } |
18 | | - media += ')' |
19 | | - return useMedia(media) |
| 22 | + const [passes, setPasses] = useState(() => { |
| 23 | + const width = document.documentElement.clientWidth |
| 24 | + const height = document.documentElement.clientHeight |
| 25 | + return (minWidth === null || width >= minWidth) && (minHeight === null || height >= minHeight) |
| 26 | + }) |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const checkDimensions = () => { |
| 30 | + const width = document.documentElement.clientWidth |
| 31 | + const height = document.documentElement.clientHeight |
| 32 | + setPasses((minWidth === null || width >= minWidth) && (minHeight === null || height >= minHeight)) |
| 33 | + } |
| 34 | + addEventListener('resize', checkDimensions) |
| 35 | + return () => { |
| 36 | + removeEventListener('resize', checkDimensions) |
| 37 | + } |
| 38 | + }, [minWidth, minHeight]) |
| 39 | + |
| 40 | + return passes |
20 | 41 | } |
21 | 42 |
|
22 | 43 | export const useCopyKeybinding = (getCopyText: () => string | undefined) => { |
|
0 commit comments