forked from jenkinsci/pipeline-graph-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll-to-top-bottom.tsx
More file actions
101 lines (88 loc) · 2.67 KB
/
scroll-to-top-bottom.tsx
File metadata and controls
101 lines (88 loc) · 2.67 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
96
97
98
99
100
101
import "./scroll-to-top-bottom.scss";
import { useEffect, useState } from "react";
import { classNames } from "../../../common/utils/classnames.ts";
export default function ScrollToTopBottom() {
const [isAtTop, setIsAtTop] = useState(true);
const [isAtBottom, setIsAtBottom] = useState(false);
const [isScrollable, setIsScrollable] = useState(false);
useEffect(() => {
const updateScrollState = () => {
const scrollTop =
window.pageYOffset || document.documentElement.scrollTop;
const windowHeight = window.innerHeight;
const docHeight = document.documentElement.scrollHeight;
const atTop = scrollTop <= 10;
const atBottom = scrollTop + windowHeight >= docHeight - 10;
const scrollable = docHeight > windowHeight + 10;
setIsAtTop(atTop);
setIsAtBottom(atBottom);
setIsScrollable(scrollable);
};
updateScrollState();
window.addEventListener("scroll", updateScrollState);
window.addEventListener("resize", updateScrollState);
const observer = new MutationObserver(updateScrollState);
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
characterData: true,
});
return () => {
window.removeEventListener("scroll", updateScrollState);
window.removeEventListener("resize", updateScrollState);
observer.disconnect();
};
}, []);
const scrollToTop = () => {
window.scrollTo({
top: 0,
});
};
const scrollToBottom = () => {
window.scrollTo({
top: document.documentElement.scrollHeight,
});
};
return (
<div
className={classNames(`pgv-scroll-to-top-bottom`, {
"pgv-scroll-to-top-bottom--visible": isScrollable,
})}
aria-hidden={!isScrollable}
>
<button
onClick={scrollToTop}
className="jenkins-button"
disabled={isAtTop}
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="48"
d="M112 244l144-144 144 144M256 120v292"
/>
</svg>
</button>
<button
onClick={scrollToBottom}
className="jenkins-button"
disabled={isAtBottom}
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="48"
d="M112 268l144 144 144-144M256 392V100"
/>
</svg>
</button>
</div>
);
}