-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathutils.ts
More file actions
152 lines (131 loc) · 4.34 KB
/
utils.ts
File metadata and controls
152 lines (131 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useState, useEffect, useRef } from 'react';
import { Dimensions } from './types';
// There are polyfills for this, but they add hundreds of lines of code
class FakeResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
function throttle(f: Function, delay: number) {
let timer = 0;
return function (this: Function, ...args: any) {
clearTimeout(timer);
timer = window.setTimeout(() => f.apply(this, args), delay);
};
}
const MyResizeObserver = globalThis.ResizeObserver || FakeResizeObserver;
const hasResizeObserver = globalThis.ResizeObserver !== undefined;
const useResizeObserver = hasResizeObserver;
const useWindowListener = !useResizeObserver;
export function useSize(
containerRef: React.MutableRefObject<HTMLElement | null>
) {
const [size, setSize] = useState<Dimensions>({
width: 0,
height: 0,
});
// internet explorer does not support ResizeObservers.
useEffect(() => {
if (typeof window !== 'undefined') {
const handleResize = () => {
setSize({
width: window.innerWidth,
height: window.innerHeight,
});
};
if (useWindowListener) {
// only pay attention to window size changes when we do not have the resizeObserver (IE only)
handleResize();
window.addEventListener('resize', handleResize);
}
return () => window.removeEventListener('resize', handleResize);
}
}, []);
const observer = useRef(
new MyResizeObserver(
throttle((entries: any) => {
if (useResizeObserver) {
setSize({
width: entries[entries.length - 1].contentRect.width,
height: entries[entries.length - 1].contentRect.height,
});
}
}, 0)
)
);
useEffect(() => {
const current = observer.current;
if (containerRef.current && useResizeObserver) {
current.observe(containerRef.current);
}
return () => {
current.disconnect();
if (containerRef.current && useResizeObserver) {
current.unobserve(containerRef.current);
}
};
}, [containerRef, observer]);
return size;
}
/**
* Listen for devicePixelRatio changes and set the new value accordingly. This could
* happen for reasons such as:
* - User moves window from retina screen display to a separate monitor
* - User controls zoom settings on the browser
*
* Source: https://github.com/rexxars/use-device-pixel-ratio/blob/main/index.ts
*
* @returns dpr: Number - Device pixel ratio; ratio of physical px to resolution in CSS pixels for current device
*/
export function useDevicePixelRatio() {
const dpr = getDevicePixelRatio();
const [currentDpr, setCurrentDpr] = useState(dpr);
useEffect(() => {
const canListen = typeof window !== 'undefined' && 'matchMedia' in window;
if (!canListen) {
return;
}
const updateDpr = () => {
const newDpr = getDevicePixelRatio();
setCurrentDpr(newDpr);
};
const mediaMatcher = window.matchMedia(
`screen and (resolution: ${currentDpr}dppx)`
);
mediaMatcher.hasOwnProperty('addEventListener')
? mediaMatcher.addEventListener('change', updateDpr)
: mediaMatcher.addListener(updateDpr);
return () => {
mediaMatcher.hasOwnProperty('removeEventListener')
? mediaMatcher.removeEventListener('change', updateDpr)
: mediaMatcher.removeListener(updateDpr);
};
}, [currentDpr]);
return currentDpr;
}
export function getDevicePixelRatio(): number {
const hasDprProp =
typeof window !== 'undefined' &&
typeof window.devicePixelRatio === 'number';
const dpr = hasDprProp ? window.devicePixelRatio : 1;
return Math.min(Math.max(1, dpr), 3);
}
export function usePrefersReducedMotion(): boolean {
const [prefersReducedMotion, setPrefersReducedMotion] =
useState<boolean>(false);
useEffect(() => {
const canListen = typeof window !== 'undefined' && 'matchMedia' in window;
if (!canListen) {
return;
}
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
function updatePrefersReducedMotion() {
setPrefersReducedMotion(() => mediaQuery.matches);
}
mediaQuery.addEventListener('change', updatePrefersReducedMotion);
updatePrefersReducedMotion();
return () =>
mediaQuery.removeEventListener('change', updatePrefersReducedMotion);
}, []);
return prefersReducedMotion;
}