forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackToTop.tsx
More file actions
95 lines (85 loc) · 2.92 KB
/
BackToTop.tsx
File metadata and controls
95 lines (85 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as React from 'react';
import styles from '@patternfly/react-styles/css/components/BackToTop/back-to-top';
import { css } from '@patternfly/react-styles';
import AngleUpIcon from '@patternfly/react-icons/dist/esm/icons/angle-up-icon';
import { canUseDOM } from '../../helpers/util';
import { Button } from '../Button';
interface BackToTopProps extends React.DetailedHTMLProps<React.HTMLProps<HTMLDivElement>, HTMLDivElement> {
/** Additional classes added to the back to top. */
className?: string;
/** Title to appear in back to top button. */
title?: string;
/** @hide Forwarded ref */
innerRef?: React.ForwardedRef<any>;
/** Selector for the scrollable element to spy on. Not passing a selector defaults to spying on window scroll events. */
scrollableSelector?: string;
/** Flag to always show back to top button, defaults to false. */
isAlwaysVisible?: boolean;
}
const BackToTopBase: React.FunctionComponent<BackToTopProps> = ({
className,
title = 'Back to top',
innerRef,
scrollableSelector,
isAlwaysVisible = false,
...props
}: BackToTopProps) => {
const [visible, setVisible] = React.useState(isAlwaysVisible);
React.useEffect(() => {
setVisible(isAlwaysVisible);
}, [isAlwaysVisible]);
const [scrollElement, setScrollElement] = React.useState(null);
const toggleVisible = () => {
const scrolled = scrollElement.scrollY ? scrollElement.scrollY : scrollElement.scrollTop;
if (!isAlwaysVisible) {
if (scrolled > 400) {
setVisible(true);
} else {
setVisible(false);
}
}
};
React.useEffect(() => {
const hasScrollSpy = Boolean(scrollableSelector);
if (hasScrollSpy) {
const scrollEl = document.querySelector(scrollableSelector) as HTMLElement;
if (!canUseDOM || !(scrollEl instanceof HTMLElement)) {
return;
}
setScrollElement(scrollEl);
scrollEl.addEventListener('scroll', toggleVisible);
return () => {
scrollEl.removeEventListener('scroll', toggleVisible);
};
} else {
if (!canUseDOM) {
return;
}
const scrollEl = window;
setScrollElement(scrollEl);
scrollEl.addEventListener('scroll', toggleVisible);
return () => {
scrollEl.removeEventListener('scroll', toggleVisible);
};
}
}, [scrollableSelector, toggleVisible]);
const handleClick = () => {
scrollElement.scrollTo({ top: 0, behavior: 'smooth' });
};
return (
<div
className={css(styles.backToTop, !visible && styles.modifiers.hidden, className)}
ref={innerRef}
onClick={handleClick}
{...props}
>
<Button variant="primary" icon={<AngleUpIcon aria-hidden="true" />} iconPosition="right">
{title}
</Button>
</div>
);
};
export const BackToTop = React.forwardRef((props: BackToTopProps, ref: React.ForwardedRef<any>) => (
<BackToTopBase innerRef={ref} {...props} />
));
BackToTop.displayName = 'BackToTop';