@trap_stevo/legendarybuilderproreact-ui
Version:
The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton
340 lines • 17.8 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["default"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { useState, useEffect, useMemo, useRef } from "react";
import { motion, useAnimation } from "framer-motion";
import { ConvertMillisecondsToETATime, ConvertSecondsToETATime } from "../HUDManagers/HUDUniversalHUDUtilityManager.js";
var animationPresets = {
slideFade: {
initial: {
opacity: 0,
y: 6
},
animate: {
opacity: 1,
y: 0
},
transition: {
duration: 0.3
}
},
zoomIn: {
initial: {
scale: 0.85,
opacity: 0
},
animate: {
scale: 1,
opacity: 1
},
transition: {
duration: 0.25
}
},
fade: {
initial: {
opacity: 0
},
animate: {
opacity: 1
},
transition: {
duration: 0.2
}
},
glitch: {
initial: {
opacity: 0.8,
x: -2,
skewX: 5
},
animate: {
opacity: [0.8, 1, 0.8, 1],
x: [0, 2, -2, 0],
skewX: [5, -5, 3, 0]
},
transition: {
duration: 0.3,
ease: "easeInOut"
}
}
};
function getTimeRemaining(targetDate) {
var now = Date.now();
var total = Math.max(targetDate.getTime() - now, 0);
return {
total: total,
rawMs: total
};
}
;
function normalizeAppearanceThresholds(input) {
if (typeof input === "function") {
return null;
}
if (!input) {
return {
"default": {},
thresholds: []
};
}
if ("thresholds" in input) {
return input;
}
var _input$default = input["default"],
defaultStyle = _input$default === void 0 ? {} : _input$default,
rest = _objectWithoutProperties(input, _excluded);
var thresholds = Object.entries(rest).filter(function (_ref) {
var _ref2 = _slicedToArray(_ref, 1),
k = _ref2[0];
return !isNaN(parseFloat(k));
}).map(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
threshold = _ref4[0],
style = _ref4[1];
return {
threshold: parseFloat(threshold),
style: style
};
}).sort(function (a, b) {
return a.threshold - b.threshold;
});
return {
"default": defaultStyle,
thresholds: thresholds
};
}
;
function resolveThresholdStyle(source, normalized, percent) {
if (typeof source === "function") {
return source(percent) || {};
}
var style = _objectSpread({}, (normalized === null || normalized === void 0 ? void 0 : normalized["default"]) || {});
var _iterator = _createForOfIteratorHelper((normalized === null || normalized === void 0 ? void 0 : normalized.thresholds) || []),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var entry = _step.value;
if (percent <= entry.threshold) {
style = _objectSpread(_objectSpread({}, style), entry.style);
break;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return style;
}
;
function HUDCountdown(_ref5) {
var _ref5$countdownSubTit = _ref5.countdownSubTitleContainerAnimationTransitionConfigurationSettings,
countdownSubTitleContainerAnimationTransitionConfigurationSettings = _ref5$countdownSubTit === void 0 ? {} : _ref5$countdownSubTit,
_ref5$countdownSubTit2 = _ref5.countdownSubTitleContainerInitialAnimationConfigurationSettings,
countdownSubTitleContainerInitialAnimationConfigurationSettings = _ref5$countdownSubTit2 === void 0 ? {} : _ref5$countdownSubTit2,
_ref5$countdownSubTit3 = _ref5.countdownSubTitleContainerAnimationConfigurationSettings,
countdownSubTitleContainerAnimationConfigurationSettings = _ref5$countdownSubTit3 === void 0 ? {} : _ref5$countdownSubTit3,
_ref5$countdownSubTit4 = _ref5.countdownSubTitleAnimationConfigurationSettings,
countdownSubTitleAnimationConfigurationSettings = _ref5$countdownSubTit4 === void 0 ? {} : _ref5$countdownSubTit4,
_ref5$countdownSubTit5 = _ref5.countdownSubTitleContainerConfigurationSettings,
countdownSubTitleContainerConfigurationSettings = _ref5$countdownSubTit5 === void 0 ? {} : _ref5$countdownSubTit5,
_ref5$countdownSubTit6 = _ref5.countdownSubTitleConfigurationSettings,
countdownSubTitleConfigurationSettings = _ref5$countdownSubTit6 === void 0 ? {} : _ref5$countdownSubTit6,
_ref5$invalidCountdow = _ref5.invalidCountdownConfigurationSettings,
invalidCountdownConfigurationSettings = _ref5$invalidCountdow === void 0 ? {} : _ref5$invalidCountdow,
_ref5$countdownTitleC = _ref5.countdownTitleConfigurationSettings,
countdownTitleConfigurationSettings = _ref5$countdownTitleC === void 0 ? {} : _ref5$countdownTitleC,
_ref5$style = _ref5.style,
style = _ref5$style === void 0 ? {} : _ref5$style,
_ref5$countdownSubTit7 = _ref5.countdownSubTitleContainerConfigurations,
countdownSubTitleContainerConfigurations = _ref5$countdownSubTit7 === void 0 ? {} : _ref5$countdownSubTit7,
_ref5$countdownContai = _ref5.countdownContainerConfigurations,
countdownContainerConfigurations = _ref5$countdownContai === void 0 ? {} : _ref5$countdownContai,
_ref5$countdownSubTit8 = _ref5.countdownSubTitleConfigurations,
countdownSubTitleConfigurations = _ref5$countdownSubTit8 === void 0 ? {} : _ref5$countdownSubTit8,
_ref5$countdownTitleC2 = _ref5.countdownTitleConfigurations,
countdownTitleConfigurations = _ref5$countdownTitleC2 === void 0 ? {} : _ref5$countdownTitleC2,
onComplete = _ref5.onComplete,
onTick = _ref5.onTick,
_ref5$invalidCountdow2 = _ref5.invalidCountdownUpperContent,
invalidCountdownUpperContent = _ref5$invalidCountdow2 === void 0 ? null : _ref5$invalidCountdow2,
_ref5$invalidCountdow3 = _ref5.invalidCountdownLowerContent,
invalidCountdownLowerContent = _ref5$invalidCountdow3 === void 0 ? null : _ref5$invalidCountdow3,
_ref5$appearanceSubti = _ref5.appearanceSubtitleThresholds,
appearanceSubtitleThresholds = _ref5$appearanceSubti === void 0 ? {} : _ref5$appearanceSubti,
_ref5$appearanceTitle = _ref5.appearanceTitleThresholds,
appearanceTitleThresholds = _ref5$appearanceTitle === void 0 ? {} : _ref5$appearanceTitle,
_ref5$subtitleAnimati = _ref5.subtitleAnimation,
subtitleAnimation = _ref5$subtitleAnimati === void 0 ? "slideFade" : _ref5$subtitleAnimati,
_ref5$useMilliseconds = _ref5.useMilliseconds,
useMilliseconds = _ref5$useMilliseconds === void 0 ? false : _ref5$useMilliseconds,
_ref5$useETAFormat = _ref5.useETAFormat,
useETAFormat = _ref5$useETAFormat === void 0 ? true : _ref5$useETAFormat,
targetDate = _ref5.targetDate,
_ref5$timeZone = _ref5.timeZone,
timeZone = _ref5$timeZone === void 0 ? "UTC" : _ref5$timeZone,
_ref5$title = _ref5.title,
title = _ref5$title === void 0 ? "" : _ref5$title,
format = _ref5.format;
var parsedTarget = useMemo(function () {
return targetDate instanceof Date ? targetDate : new Date(targetDate);
}, [targetDate]);
var _useState = useState(function () {
return getTimeRemaining(parsedTarget);
}),
_useState2 = _slicedToArray(_useState, 2),
remaining = _useState2[0],
setRemaining = _useState2[1];
var containerControls = useAnimation();
var subtitleControls = useAnimation();
var lastSubtitleText = useRef("");
var animating = useRef(false);
var origin = useMemo(function () {
var _performance$timing;
return ((_performance$timing = performance.timing) === null || _performance$timing === void 0 ? void 0 : _performance$timing.navigationStart) || Date.now();
}, []);
var totalDuration = useMemo(function () {
return parsedTarget.getTime() - origin;
}, [parsedTarget, origin]);
var invalidCountdown = useMemo(function () {
return isNaN(parsedTarget.getTime());
}, [parsedTarget]);
var subtitleText = useMemo(function () {
if (useETAFormat) {
return useMilliseconds ? ConvertMillisecondsToETATime(remaining.rawMs) : ConvertSecondsToETATime(Math.floor(remaining.rawMs / 1000));
}
return "".concat(parsedTarget.getHours().toString().padStart(2, "0"), ":").concat(parsedTarget.getMinutes().toString().padStart(2, "0"), ":").concat(parsedTarget.getSeconds().toString().padStart(2, "0"));
}, [parsedTarget, remaining.rawMs, useETAFormat, useMilliseconds]);
var resolvedAnimation = useMemo(function () {
return typeof subtitleAnimation === "string" ? animationPresets[subtitleAnimation] || animationPresets.slideFade : subtitleAnimation;
}, [subtitleAnimation]);
var percentageRemaining = useMemo(function () {
return totalDuration > 0 ? remaining.rawMs / totalDuration : 0;
}, [remaining.rawMs, totalDuration]);
var normalizedSubtitleAppearance = useMemo(function () {
return normalizeAppearanceThresholds(appearanceSubtitleThresholds);
}, [appearanceSubtitleThresholds]);
var normalizedTitleAppearance = useMemo(function () {
return normalizeAppearanceThresholds(appearanceTitleThresholds);
}, [appearanceTitleThresholds]);
var resolvedTitleStyle = useMemo(function () {
return resolveThresholdStyle(appearanceTitleThresholds, normalizedTitleAppearance, percentageRemaining);
}, [appearanceTitleThresholds, normalizedTitleAppearance, percentageRemaining]);
var resolvedSubtitleStyle = useMemo(function () {
return resolveThresholdStyle(appearanceSubtitleThresholds, normalizedSubtitleAppearance, percentageRemaining);
}, [appearanceSubtitleThresholds, normalizedSubtitleAppearance, percentageRemaining]);
var formattedDate = function formattedDate() {
if (format === "time-only") {
return null;
}
var options = format === "mmdd" ? {
month: "2-digit",
day: "2-digit",
year: "numeric"
} : {
month: "long",
day: "numeric",
year: "numeric"
};
return new Intl.DateTimeFormat("en-US", _objectSpread(_objectSpread({}, options), timeZone ? {
timeZone: timeZone
} : {})).format(parsedTarget);
};
useEffect(function () {
if (invalidCountdown) {
return;
}
var timer = setInterval( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var timeLeft;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
timeLeft = getTimeRemaining(parsedTarget);
setRemaining(timeLeft);
if (typeof onTick === "function") {
onTick(timeLeft);
}
if (animating.current) {
_context.next = 8;
break;
}
animating.current = true;
_context.next = 7;
return containerControls.start(resolvedAnimation.animate);
case 7:
animating.current = false;
case 8:
if (subtitleText !== lastSubtitleText.current) {
subtitleControls.set({
opacity: 0
});
subtitleControls.start({
opacity: 1,
transition: {
duration: 0.2
}
});
lastSubtitleText.current = subtitleText;
}
if (timeLeft.total <= 0) {
clearInterval(timer);
if (onComplete) {
onComplete();
}
}
case 10:
case "end":
return _context.stop();
}
}, _callee);
})), 1000);
return function () {
return clearInterval(timer);
};
}, [parsedTarget, onTick, onComplete, invalidCountdown, resolvedAnimation.animate, subtitleText]);
return /*#__PURE__*/React.createElement("div", _extends({
style: _objectSpread({
position: "relative",
display: "flex",
flexDirection: "column",
alignItems: "center",
padding: "10px 0"
}, style !== null && style !== void 0 ? style : {})
}, countdownContainerConfigurations !== null && countdownContainerConfigurations !== void 0 ? countdownContainerConfigurations : {}), invalidCountdown ? /*#__PURE__*/React.createElement("div", {
style: _objectSpread({
fontWeight: 600,
color: "red"
}, invalidCountdownConfigurationSettings !== null && invalidCountdownConfigurationSettings !== void 0 ? invalidCountdownConfigurationSettings : {})
}, invalidCountdownUpperContent, "Invalid targetDate", invalidCountdownLowerContent) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", _extends({
style: _objectSpread(_objectSpread({
fontWeight: 600,
fontSize: "1.2rem"
}, resolvedTitleStyle !== null && resolvedTitleStyle !== void 0 ? resolvedTitleStyle : {}), countdownTitleConfigurationSettings !== null && countdownTitleConfigurationSettings !== void 0 ? countdownTitleConfigurationSettings : {})
}, countdownTitleConfigurations !== null && countdownTitleConfigurations !== void 0 ? countdownTitleConfigurations : {}), formattedDate() || subtitleText), formattedDate() && /*#__PURE__*/React.createElement(motion.div, _extends({
animate: _objectSpread(_objectSpread({}, containerControls), countdownSubTitleContainerAnimationConfigurationSettings !== null && countdownSubTitleContainerAnimationConfigurationSettings !== void 0 ? countdownSubTitleContainerAnimationConfigurationSettings : {}),
initial: _objectSpread(_objectSpread({}, resolvedAnimation.initial), countdownSubTitleContainerInitialAnimationConfigurationSettings !== null && countdownSubTitleContainerInitialAnimationConfigurationSettings !== void 0 ? countdownSubTitleContainerInitialAnimationConfigurationSettings : {}),
transition: _objectSpread(_objectSpread({}, resolvedAnimation.transition), countdownSubTitleContainerAnimationTransitionConfigurationSettings !== null && countdownSubTitleContainerAnimationTransitionConfigurationSettings !== void 0 ? countdownSubTitleContainerAnimationTransitionConfigurationSettings : {}),
style: _objectSpread(_objectSpread({
fontWeight: 700,
fontSize: "1.5rem",
marginTop: "0.25rem"
}, resolvedSubtitleStyle !== null && resolvedSubtitleStyle !== void 0 ? resolvedSubtitleStyle : {}), countdownSubTitleContainerConfigurationSettings !== null && countdownSubTitleContainerConfigurationSettings !== void 0 ? countdownSubTitleContainerConfigurationSettings : {})
}, countdownSubTitleContainerConfigurations !== null && countdownSubTitleContainerConfigurations !== void 0 ? countdownSubTitleContainerConfigurations : {}), /*#__PURE__*/React.createElement(motion.span, _extends({
animate: _objectSpread(_objectSpread({}, subtitleControls), countdownSubTitleAnimationConfigurationSettings !== null && countdownSubTitleAnimationConfigurationSettings !== void 0 ? countdownSubTitleAnimationConfigurationSettings : {}),
style: _objectSpread({
display: "inline-block"
}, countdownSubTitleConfigurationSettings !== null && countdownSubTitleConfigurationSettings !== void 0 ? countdownSubTitleConfigurationSettings : {})
}, countdownSubTitleConfigurations !== null && countdownSubTitleConfigurations !== void 0 ? countdownSubTitleConfigurations : {}), subtitleText))));
}
;
export default HUDCountdown;