@trap_stevo/legendarybuilderproreact-ui
Version:
The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton
84 lines • 3.36 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useState, useEffect, useMemo } from "react";
var easeInOut = function easeInOut(t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
};
var sigmoid = function sigmoid(t) {
return 1 / (1 + Math.exp(-12 * (t - 0.5)));
};
var step = function step(t) {
return t < 0.5 ? 0 : 1;
};
var applyCurve = function applyCurve(value, curve) {
if (typeof curve === "function") {
return curve(value);
}
if (curve === "easeInOut") {
return easeInOut(value);
}
if (curve === "sigmoid") {
return sigmoid(value);
}
if (curve === "step") {
return step(value);
}
return value;
};
export var useResponsiveFactors = function useResponsiveFactors() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _config$baseWidth = config.baseWidth,
baseWidth = _config$baseWidth === void 0 ? 1440 : _config$baseWidth,
_config$baseHeight = config.baseHeight,
baseHeight = _config$baseHeight === void 0 ? 900 : _config$baseHeight,
_config$minWidth = config.minWidth,
minWidth = _config$minWidth === void 0 ? 320 : _config$minWidth,
_config$maxWidth = config.maxWidth,
maxWidth = _config$maxWidth === void 0 ? 2560 : _config$maxWidth,
_config$minHeight = config.minHeight,
minHeight = _config$minHeight === void 0 ? 400 : _config$minHeight,
_config$maxHeight = config.maxHeight,
maxHeight = _config$maxHeight === void 0 ? 1440 : _config$maxHeight,
_config$widthCurve = config.widthCurve,
widthCurve = _config$widthCurve === void 0 ? null : _config$widthCurve,
_config$heightCurve = config.heightCurve,
heightCurve = _config$heightCurve === void 0 ? null : _config$heightCurve,
_config$aspectCurve = config.aspectCurve,
aspectCurve = _config$aspectCurve === void 0 ? null : _config$aspectCurve;
var _useState = useState({
height: typeof window !== "undefined" ? window.innerHeight : baseHeight,
width: typeof window !== "undefined" ? window.innerWidth : baseWidth
}),
_useState2 = _slicedToArray(_useState, 2),
windowSize = _useState2[0],
setWindowSize = _useState2[1];
useEffect(function () {
var handleResize = function handleResize() {
setWindowSize({
height: window.innerHeight,
width: window.innerWidth
});
};
window.addEventListener("resize", handleResize);
return function () {
return window.removeEventListener("resize", handleResize);
};
}, []);
var factors = useMemo(function () {
var width = windowSize.width,
height = windowSize.height;
var rawHeightFactor = Math.min(Math.max((height - minHeight) / (maxHeight - minHeight), 0), 1);
var rawWidthFactor = Math.min(Math.max((width - minWidth) / (maxWidth - minWidth), 0), 1);
var rawAspectRatio = width / height;
return {
height: height,
width: width,
heightFactor: rawHeightFactor,
widthFactor: rawWidthFactor,
aspectRatio: rawAspectRatio,
curvedHeightFactor: applyCurve(rawHeightFactor, heightCurve),
curvedWidthFactor: applyCurve(rawWidthFactor, widthCurve),
curvedAspectRatio: applyCurve(rawAspectRatio, aspectCurve)
};
}, [windowSize, minWidth, maxWidth, minHeight, maxHeight, widthCurve, heightCurve, aspectCurve]);
return factors;
};