@clayui/shared
Version:
ClayShared component
88 lines (87 loc) • 4.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MouseSafeArea = void 0;
var _react = _interopRequireDefault(require("react"));
var _Portal = require("./Portal");
var _useMousePosition3 = require("./useMousePosition");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) { n[e] = r[e]; } return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) { ; } } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } /**
* SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/
var getLeft = function getLeft(_ref) {
var mouseX = _ref.mouseX,
x = _ref.x;
return mouseX > x ? undefined : "".concat(x - Math.max(x - mouseX, 10), "px");
};
var getRight = function getRight(_ref2) {
var mouseX = _ref2.mouseX,
ownerW = _ref2.ownerW,
w = _ref2.w,
x = _ref2.x;
return mouseX > x ? "".concat(ownerW - (x + w) - Math.max(mouseX - (x + w), 10), "px") : undefined;
};
var getWidth = function getWidth(_ref3) {
var mouseX = _ref3.mouseX,
w = _ref3.w,
x = _ref3.x;
return "".concat(Math.max(mouseX > x ? mouseX - (x + w) : x - mouseX, 10), "px");
};
var getClipPath = function getClipPath(_ref4) {
var h = _ref4.h,
mouseX = _ref4.mouseX,
mouseY = _ref4.mouseY,
x = _ref4.x,
y = _ref4.y;
return mouseX > x ? "polygon(0% 0%, 0% 100%, 100% ".concat(100 * (mouseY - y) / h, "%)") : "polygon(100% 0%, 0% ".concat(100 * (mouseY - y) / h, "%, 100% 100%)");
};
var MouseSafeArea = exports.MouseSafeArea = function MouseSafeArea(_ref5) {
var _parentRef$current, _parentRef$current2;
var parentRef = _ref5.parentRef;
var _useMousePosition = (0, _useMousePosition3.useMousePosition)(),
_useMousePosition2 = _slicedToArray(_useMousePosition, 2),
mouseX = _useMousePosition2[0],
mouseY = _useMousePosition2[1];
var _ref6 = ((_parentRef$current = parentRef.current) === null || _parentRef$current === void 0 ? void 0 : _parentRef$current.getBoundingClientRect()) || {},
_ref6$height = _ref6.height,
h = _ref6$height === void 0 ? 0 : _ref6$height,
_ref6$top = _ref6.top,
top = _ref6$top === void 0 ? 0 : _ref6$top,
_ref6$width = _ref6.width,
w = _ref6$width === void 0 ? 0 : _ref6$width,
_ref6$x = _ref6.x,
x = _ref6$x === void 0 ? 0 : _ref6$x,
_ref6$y = _ref6.y,
y = _ref6$y === void 0 ? 0 : _ref6$y;
var _ref7 = ((_parentRef$current2 = parentRef.current) === null || _parentRef$current2 === void 0 ? void 0 : _parentRef$current2.ownerDocument.body) || {},
_ref7$offsetWidth = _ref7.offsetWidth,
ownerW = _ref7$offsetWidth === void 0 ? 0 : _ref7$offsetWidth;
var positions = {
h: h,
mouseX: mouseX,
mouseY: mouseY,
ownerW: ownerW,
w: w,
x: x,
y: y
};
return /*#__PURE__*/_react.default.createElement(_Portal.ClayPortal, null, /*#__PURE__*/_react.default.createElement("div", {
style: {
clipPath: getClipPath(positions),
height: h,
left: getLeft(positions),
position: 'absolute',
right: getRight(positions),
top: top,
width: getWidth(positions),
zIndex: 1010
}
}));
};