UNPKG

@trap_stevo/legendarybuilderproreact-ui

Version:

The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton

183 lines 7.93 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _regeneratorRuntime from "@babel/runtime/regenerator"; 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, { useRef, useEffect, useState } from "react"; function HUDHolographicBorder(hudHolographicBorderConfigurationSettings) { var _hudHolographicBorder = hudHolographicBorderConfigurationSettings.holographicBorderContainerConfigurationSettings, holographicBorderContainerConfigurationSettings = _hudHolographicBorder === void 0 ? {} : _hudHolographicBorder, _hudHolographicBorder2 = hudHolographicBorderConfigurationSettings.borderColor, borderColor = _hudHolographicBorder2 === void 0 ? "rgba(26, 188, 156, 0.8)" : _hudHolographicBorder2, _hudHolographicBorder3 = hudHolographicBorderConfigurationSettings.borderRadius, borderRadius = _hudHolographicBorder3 === void 0 ? 10 : _hudHolographicBorder3, _hudHolographicBorder4 = hudHolographicBorderConfigurationSettings.borderWidth, borderWidth = _hudHolographicBorder4 === void 0 ? 5 : _hudHolographicBorder4, _hudHolographicBorder5 = hudHolographicBorderConfigurationSettings.padding, padding = _hudHolographicBorder5 === void 0 ? 2 : _hudHolographicBorder5, _hudHolographicBorder6 = hudHolographicBorderConfigurationSettings.hologramLoadingBorder, hologramLoadingBorder = _hudHolographicBorder6 === void 0 ? true : _hudHolographicBorder6, _hudHolographicBorder7 = hudHolographicBorderConfigurationSettings.spotlightBorder, spotlightBorder = _hudHolographicBorder7 === void 0 ? false : _hudHolographicBorder7, _hudHolographicBorder8 = hudHolographicBorderConfigurationSettings.glowingBorder, glowingBorder = _hudHolographicBorder8 === void 0 ? false : _hudHolographicBorder8, _hudHolographicBorder9 = hudHolographicBorderConfigurationSettings.showBorder, showBorder = _hudHolographicBorder9 === void 0 ? true : _hudHolographicBorder9, children = hudHolographicBorderConfigurationSettings.children; var _useState = useState({ x: 0, y: 0, visible: false }), _useState2 = _slicedToArray(_useState, 2), highlight = _useState2[0], setHighlight = _useState2[1]; var loadingHolographicBorder = useRef(false); var loadingBorder = useRef(true); var containerRef = useRef(null); var canvasRef = useRef(null); var mergedStyles = _objectSpread({ position: "relative", borderRadius: "".concat(borderRadius, "px"), padding: "".concat(padding, "px") }, holographicBorderContainerConfigurationSettings); var innerContainerStyles = { position: "relative", borderRadius: "inherit", background: "inherit", zIndex: 1 }; var drawHighlight = function drawHighlight() { var canvas = canvasRef.current; if (canvas === null) { return; } var ctx = canvas.getContext("2d"); var rect = containerRef.current.getBoundingClientRect(); canvas.height = rect.height; canvas.width = rect.width; ctx.clearRect(0, 0, canvas.width, canvas.height); var gradient = !spotlightBorder ? ctx.createConicGradient(highlight.x, highlight.y, 10, highlight.x, highlight.y, Math.max(rect.width, rect.height)) : ctx.createRadialGradient(highlight.x, highlight.y, 10, highlight.x, highlight.y, Math.max(rect.width, rect.height)); gradient.addColorStop(1, "transparent"); gradient.addColorStop(0, borderColor); ctx.lineWidth = borderWidth; ctx.strokeStyle = gradient; if (glowingBorder) { ctx.shadowBlur = 12.69; ctx.shadowColor = borderColor; } ctx.beginPath(); ctx.moveTo(padding + borderRadius, padding); ctx.arcTo(rect.width - padding, padding, rect.width - padding, rect.height - padding, borderRadius); ctx.arcTo(rect.width - padding, rect.height - padding, padding, rect.height - padding, borderRadius); ctx.arcTo(padding, rect.height - padding, padding, padding, borderRadius); ctx.arcTo(padding, padding, rect.width - padding, padding, borderRadius); ctx.closePath(); ctx.stroke(); }; var clearHighlight = function clearHighlight() { var canvas = canvasRef.current; var ctx = canvas.getContext("2d"); ctx.clearRect(0, 0, canvas.width, canvas.height); }; var handleMouseMove = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(e) { var rect; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: rect = containerRef.current.getBoundingClientRect(); if (!(!spotlightBorder && hologramLoadingBorder && loadingHolographicBorder.current)) { _context.next = 3; break; } return _context.abrupt("return"); case 3: if (!spotlightBorder && hologramLoadingBorder) { loadingHolographicBorder.current = true; setTimeout(function () { loadingHolographicBorder.current = false; }, 69); } setHighlight({ x: e.clientX - rect.left, y: e.clientY - rect.top, visible: true }); case 5: case "end": return _context.stop(); } }, _callee); })); return function handleMouseMove(_x) { return _ref.apply(this, arguments); }; }(); var handleMouseLeave = function handleMouseLeave() { setHighlight({ x: 0, y: 0, visible: false }); }; useEffect(function () { if (!showBorder) { return function () {}; } var observer = new ResizeObserver(function () { if (loadingBorder.current) { return; } drawHighlight(); }); if (containerRef && containerRef.current) { observer.observe(containerRef.current); } return function () { if (containerRef && containerRef.current) { observer.unobserve(containerRef.current); } }; }, []); useEffect(function () { if (!showBorder) { return function () {}; } var loadingBorderTimer = setTimeout(function () { loadingBorder.current = false; }, 1); return function () { clearTimeout(loadingBorderTimer); }; }, [showBorder]); useEffect(function () { if (!showBorder) { return function () {}; } if (highlight.visible) { drawHighlight(); return function () {}; } clearHighlight(); return function () {}; }, [highlight, showBorder, borderColor]); return /*#__PURE__*/React.createElement("div", { ref: containerRef, style: mergedStyles, onMouseLeave: showBorder ? handleMouseLeave : undefined, onMouseMove: showBorder ? handleMouseMove : undefined }, showBorder ? /*#__PURE__*/React.createElement("canvas", { ref: canvasRef, style: { position: "absolute", top: 0, left: 0, pointerEvents: "none" } }) : null, /*#__PURE__*/React.createElement("div", { style: innerContainerStyles }, children)); } export default HUDHolographicBorder;