@heycar-uikit/core
Version:
The React UI library from HeyCar
111 lines (104 loc) • 4.93 kB
JavaScript
import { useState, useEffect } from 'react';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var BREAKPOINTS = {
'breakpoint-mobile': 767,
'breakpoint-tablet': 768,
'breakpoint-tablet-s': 768,
'breakpoint-tablet-l': 1024,
'breakpoint-desktop': 1280,
'breakpoint-desktop-s': 1280,
'breakpoint-desktop-m': 1366,
'breakpoint-desktop-l': 1440,
};
var RESETTED_BREAKPOINTS = {
isMobile: false,
isTablet: false,
isDesktop: false,
isTabletS: false,
isTabletL: false,
isDesktopS: false,
isDesktopM: false,
isDesktopL: false,
};
function useBreakpoint() {
var _a = useState(0), width = _a[0], setWidth = _a[1];
var _b = useState(__assign(__assign({}, RESETTED_BREAKPOINTS), { isMobile: true })), breakpoints = _b[0], setBreakpoints = _b[1];
useEffect(function () {
var updateBreakpoint = function () {
setWidth(window.innerWidth);
// Desktop
if (window.innerWidth >= BREAKPOINTS['breakpoint-desktop']) {
if (window.innerWidth >= BREAKPOINTS['breakpoint-desktop-s'] &&
window.innerWidth < BREAKPOINTS['breakpoint-desktop-m']) {
setBreakpoints(__assign(__assign({}, RESETTED_BREAKPOINTS), { isDesktop: true, isDesktopS: true }));
}
if (window.innerWidth >= BREAKPOINTS['breakpoint-desktop-m'] &&
window.innerWidth < BREAKPOINTS['breakpoint-desktop-l']) {
setBreakpoints(__assign(__assign({}, RESETTED_BREAKPOINTS), { isDesktop: true, isDesktopM: true }));
}
if (window.innerWidth >= BREAKPOINTS['breakpoint-desktop-l']) {
setBreakpoints(__assign(__assign({}, RESETTED_BREAKPOINTS), { isDesktop: true, isDesktopL: true }));
}
}
// Tablet
if (window.innerWidth >= BREAKPOINTS['breakpoint-tablet'] &&
window.innerWidth < BREAKPOINTS['breakpoint-desktop']) {
if (window.innerWidth >= BREAKPOINTS['breakpoint-tablet-s'] &&
window.innerWidth < BREAKPOINTS['breakpoint-tablet-l']) {
setBreakpoints(__assign(__assign({}, RESETTED_BREAKPOINTS), { isTablet: true, isTabletS: true }));
}
if (window.innerWidth >= BREAKPOINTS['breakpoint-tablet-l']) {
setBreakpoints(__assign(__assign({}, RESETTED_BREAKPOINTS), { isTablet: true, isTabletL: true }));
}
}
// Mobile
if (window.innerWidth < BREAKPOINTS['breakpoint-mobile']) {
setBreakpoints(__assign(__assign({}, RESETTED_BREAKPOINTS), { isMobile: true }));
}
};
updateBreakpoint();
window.addEventListener('resize', updateBreakpoint);
return function () { return window.removeEventListener('resize', updateBreakpoint); };
}, []);
return { breakpoints: breakpoints, width: width };
}
export { RESETTED_BREAKPOINTS as R, __rest as _, __assign as a, useBreakpoint as u };