@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
130 lines • 7.66 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(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);
};
var __rest = (this && this.__rest) || function (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;
};
import React from "react";
import { utils } from "../../common/index.js";
import { CLASSPREFIX as eccgui } from "../../configuration/constants.js";
/**
* Element wraps the content that need to be displayed sticky.
* The content then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor).
*/
export var StickyTarget = function (_a) {
var className = _a.className, _b = _a.to, to = _b === void 0 ? "top" : _b, _c = _a.local, local = _c === void 0 ? false : _c, offset = _a.offset, _d = _a.background, background = _d === void 0 ? "transparent" : _d, fillMainGap = _a.fillMainGap, fillSecondaryGap = _a.fillSecondaryGap, style = _a.style, getConnectedElement = _a.getConnectedElement, otherDivProps = __rest(_a, ["className", "to", "local", "offset", "background", "fillMainGap", "fillSecondaryGap", "style", "getConnectedElement"]);
var stickyTargetRef = React.useRef(null);
var offsetStyle = {};
if (typeof offset !== "undefined") {
offsetStyle = __assign(__assign({}, style), { "--eccgui-sticky-target-localoffset": offset });
}
React.useEffect(function () {
var removeEventForConnectedOffset = function () {
/* no event need to be removed */
};
var removeEventForStickynessCheck = function () {
/* no event need to be removed */
};
/**
* If the target should be sticky to a defined element then:
* * check for the element and its scroll parent
* * listen to scroll events and use the elements position as offset
*/
if (getConnectedElement && stickyTargetRef) {
var stickyConnection_1 = getConnectedElement(stickyTargetRef);
if (stickyConnection_1) {
var scrollParent_1 = utils.getScrollParent(stickyConnection_1);
var scrollParentFallback_1 = !scrollParent_1 ? document.documentElement : false;
if (scrollParent_1 || scrollParentFallback_1) {
var updateTargetOffset_1 = function () {
var _a;
var connectedOffset = 0;
var scrollParentPosition = (scrollParent_1 || scrollParentFallback_1).getBoundingClientRect();
var stickyConnectionPosition = stickyConnection_1.getBoundingClientRect();
if (to === "top") {
connectedOffset =
stickyConnectionPosition.top -
Math.max(0, scrollParentPosition.top) +
stickyConnectionPosition.height;
}
if (to === "bottom") {
connectedOffset =
Math.max(scrollParentPosition.height, scrollParentPosition.bottom) -
stickyConnectionPosition.bottom +
stickyConnectionPosition.height;
}
(_a = stickyTargetRef.current) === null || _a === void 0 ? void 0 : _a.style.setProperty("--eccgui-sticky-target-applicationoffset", "".concat(connectedOffset, "px"));
};
updateTargetOffset_1();
var eventListeningTarget_1 = scrollParent_1 || window;
var eventListeningMethod_1 = function (_event) {
updateTargetOffset_1();
};
eventListeningTarget_1.addEventListener("scroll", eventListeningMethod_1);
removeEventForConnectedOffset = function () {
eventListeningTarget_1.removeEventListener("scroll", eventListeningMethod_1);
};
}
}
}
/**
* Check if sticky target element is currently in sticky mode.
* sticky mode = current position === defined sticky position
*/
if (stickyTargetRef && (fillMainGap || fillSecondaryGap)) {
var stickyTarget_1 = stickyTargetRef.current;
var scrollParent_2 = utils.getScrollParent(stickyTarget_1);
var checkStickyness_1 = function () {
var definedPosition = parseInt(window.getComputedStyle(stickyTarget_1)[to], 10);
var scrollParentPosition = scrollParent_2 ? scrollParent_2.getBoundingClientRect()[to] : 0;
var currentPosition = (to === "top" ? 1 : -1) * (stickyTarget_1.getBoundingClientRect()[to] - scrollParentPosition);
// check stickyness in a small position range (not exact value) because of float values
var isSticky = currentPosition <= definedPosition + 1 && currentPosition >= definedPosition - 1;
if (isSticky && !stickyTarget_1.classList.contains("".concat(eccgui, "-sticky__target--issticky"))) {
stickyTarget_1.classList.add("".concat(eccgui, "-sticky__target--issticky"));
}
if (!isSticky && stickyTarget_1.classList.contains("".concat(eccgui, "-sticky__target--issticky"))) {
stickyTarget_1.classList.remove("".concat(eccgui, "-sticky__target--issticky"));
}
};
checkStickyness_1();
var eventListeningTarget_2 = scrollParent_2 || window;
var eventListeningMethod_2 = function (_event) {
checkStickyness_1();
};
eventListeningTarget_2.addEventListener("scroll", eventListeningMethod_2);
removeEventForStickynessCheck = function () {
eventListeningTarget_2.removeEventListener("scroll", eventListeningMethod_2);
};
}
return function () {
removeEventForConnectedOffset();
removeEventForStickynessCheck();
};
}, [getConnectedElement, stickyTargetRef, to, fillMainGap, fillSecondaryGap]);
return (React.createElement("div", __assign({ ref: stickyTargetRef, className: "".concat(eccgui, "-sticky__target") +
(to ? " ".concat(eccgui, "-sticky__target--").concat(to) : "") +
(local ? " ".concat(eccgui, "-sticky__target--localscrollarea") : "") +
(background ? " ".concat(eccgui, "-sticky__target--bg-").concat(background) : "") +
(fillMainGap ? " ".concat(eccgui, "-sticky__target--maingapfill-").concat(fillMainGap) : "") +
(fillSecondaryGap ? " ".concat(eccgui, "-sticky__target--secondarygapfill-").concat(fillSecondaryGap) : "") +
(className ? " ".concat(className) : ""), style: offset ? offsetStyle : style }, otherDivProps)));
};
export default StickyTarget;
//# sourceMappingURL=StickyTarget.js.map