Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/BackToTop/BackToTop.js

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