| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.BackToTop = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 6 |
const react_1 = require("react"); |
| 7 |
const back_to_top_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/BackToTop/back-to-top")); |
| 8 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 9 |
const rh_microns_caret_up_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-up-icon')); |
| 10 |
const util_1 = require("../../helpers/util"); |
| 11 |
const Button_1 = require("../Button"); |
| 12 |
const BackToTopBase = (_a) => { |
| 13 |
var { className, title = 'Back to top', innerRef, scrollableSelector, isAlwaysVisible = false } = _a, props = tslib_1.__rest(_a, ["className", "title", "innerRef", "scrollableSelector", "isAlwaysVisible"]); |
| 14 |
const [visible, setVisible] = (0, react_1.useState)(isAlwaysVisible); |
| 15 |
(0, react_1.useEffect)(() => { |
| 16 |
setVisible(isAlwaysVisible); |
| 17 |
}, [isAlwaysVisible]); |
| 18 |
const [scrollElement, setScrollElement] = (0, react_1.useState)(null); |
| 19 |
const toggleVisible = () => { |
| 20 |
if (scrollElement) { |
| 21 |
const scrolled = scrollElement.scrollY ? scrollElement.scrollY : scrollElement.scrollTop; |
| 22 |
if (!isAlwaysVisible) { |
| 23 |
if (scrolled > 400) { |
| 24 |
setVisible(true); |
| 25 |
} |
| 26 |
else { |
| 27 |
setVisible(false); |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
}; |
| 32 |
(0, react_1.useEffect)(() => { |
| 33 |
const hasScrollSpy = Boolean(scrollableSelector); |
| 34 |
if (hasScrollSpy) { |
| 35 |
const scrollEl = document.querySelector(scrollableSelector); |
| 36 |
if (!util_1.canUseDOM || !(scrollEl instanceof HTMLElement)) { |
| 37 |
return; |
| 38 |
} |
| 39 |
setScrollElement(scrollEl); |
| 40 |
scrollEl.addEventListener('scroll', toggleVisible); |
| 41 |
return () => { |
| 42 |
scrollEl.removeEventListener('scroll', toggleVisible); |
| 43 |
}; |
| 44 |
} |
| 45 |
else { |
| 46 |
if (!util_1.canUseDOM) { |
| 47 |
return; |
| 48 |
} |
| 49 |
const scrollEl = window; |
| 50 |
setScrollElement(scrollEl); |
| 51 |
scrollEl.addEventListener('scroll', toggleVisible); |
| 52 |
return () => { |
| 53 |
scrollEl.removeEventListener('scroll', toggleVisible); |
| 54 |
}; |
| 55 |
} |
| 56 |
}, [scrollableSelector, toggleVisible]); |
| 57 |
const handleClick = () => { |
| 58 |
scrollElement.scrollTo({ top: 0, behavior: 'smooth' }); |
| 59 |
}; |
| 60 |
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ className: (0, react_styles_1.css)(back_to_top_1.default.backToTop, !visible && back_to_top_1.default.modifiers.hidden, className), ref: innerRef, onClick: handleClick }, props, { children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "primary", icon: (0, jsx_runtime_1.jsx)(rh_microns_caret_up_icon_1.default, {}), iconPosition: "end", children: title }) }))); |
| 61 |
}; |
| 62 |
exports.BackToTop = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(BackToTopBase, Object.assign({ innerRef: ref }, props)))); |
| 63 |
exports.BackToTop.displayName = 'BackToTop'; |
| 64 |
//# sourceMappingURL=BackToTop.js.map |