UNPKG

1.11 kBJavaScriptView Raw
1import { getWindow } from 'ssr-window';
2export default function getBreakpoint(breakpoints, base = 'window', containerEl) {
3 if (!breakpoints || base === 'container' && !containerEl) return undefined;
4 let breakpoint = false;
5 const window = getWindow();
6 const currentHeight = base === 'window' ? window.innerHeight : containerEl.clientHeight;
7 const points = Object.keys(breakpoints).map(point => {
8 if (typeof point === 'string' && point.indexOf('@') === 0) {
9 const minRatio = parseFloat(point.substr(1));
10 const value = currentHeight * minRatio;
11 return {
12 value,
13 point
14 };
15 }
16
17 return {
18 value: point,
19 point
20 };
21 });
22 points.sort((a, b) => parseInt(a.value, 10) - parseInt(b.value, 10));
23
24 for (let i = 0; i < points.length; i += 1) {
25 const {
26 point,
27 value
28 } = points[i];
29
30 if (base === 'window') {
31 if (window.matchMedia(`(min-width: ${value}px)`).matches) {
32 breakpoint = point;
33 }
34 } else if (value <= containerEl.clientWidth) {
35 breakpoint = point;
36 }
37 }
38
39 return breakpoint || 'max';
40}
\No newline at end of file