-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseResponsiveBreakpoints.js
41 lines (32 loc) · 1.12 KB
/
useResponsiveBreakpoints.js
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
import React from "react";
export default function useResponsiveBreakpoints(elRef, breakpoints) {
const firstQuery = Object.keys(breakpoints[0])[0];
const [breakSize, setBreakSize] = React.useState(firstQuery);
const observer = React.useRef(
new ResizeObserver(entries => {
// Only care about the first element, we expect one element ot be watched
const { width } = entries[0].contentRect;
setBreakSize(findBreakPoint(breakpoints, width));
})
);
React.useEffect(() => {
if (elRef.current) {
observer.current.observe(elRef.current);
}
return () => {
observer.current.unobserve();
};
}, [elRef, observer]);
return breakSize;
}
// Find the largest breakpoint the element is less than
function findBreakPoint(breakpoints, width) {
const breakpointIndex = breakpoints
.map(x => Object.values(x)[0])
.findIndex(x => width < x);
// element is larger than every breakpoint so it must be the last breakpoint
if (breakpointIndex === -1) {
return Object.keys(breakpoints[breakpoints.length - 1])[0];
}
return Object.keys(breakpoints[breakpointIndex])[0];
}