Skip to content

Commit 0d722f8

Browse files
committed
Add a scroll to top button.
I got tired of scrolling.
1 parent 4e7192d commit 0d722f8

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

app/assets/styles.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,48 @@ section.event {
262262
border: 0;
263263
}
264264

265+
.scroll-to-top {
266+
position: fixed;
267+
bottom: 50px;
268+
right: 30px;
269+
z-index: 10;
270+
display: block;
271+
background: var(--color-gray-500);
272+
border-width: 0;
273+
height: 50px;
274+
width: 50px;
275+
border-radius: 50%;
276+
transition:
277+
all 0.2s linear,
278+
opacity 0.3s;
279+
cursor: pointer;
280+
opacity: 0;
281+
pointer-events: none;
282+
}
283+
284+
.scroll-to-top.is-visible {
285+
opacity: 1;
286+
pointer-events: auto;
287+
}
288+
289+
.scroll-to-top:hover {
290+
transform: scale(1.1);
291+
}
292+
293+
.scroll-to-top::after {
294+
content: "";
295+
position: absolute;
296+
left: 16px;
297+
z-index: 11;
298+
display: block;
299+
width: 15px;
300+
height: 15px;
301+
border-top: 4px solid var(--color-gray-100);
302+
border-left: 4px solid var(--color-gray-100);
303+
transform: rotate(45deg);
304+
top: 20px;
305+
}
306+
265307
/* Ember data uses styled elements in markdown blocks that conflict with the
266308
default styles for blockquotes (because of ::before block that has an
267309
absolutely positioned quote mark).
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { modifier } from 'ember-modifier';
2+
import { on } from '@ember/modifier';
3+
4+
const showOnScroll = modifier(function showOnScroll(element) {
5+
function toggleVisibility() {
6+
if (window.scrollY > window.innerHeight) {
7+
element.classList.add('is-visible');
8+
} else {
9+
element.classList.remove('is-visible');
10+
}
11+
}
12+
13+
toggleVisibility();
14+
window.addEventListener('scroll', toggleVisibility);
15+
16+
return () => {
17+
window.removeEventListener('scroll', toggleVisibility);
18+
};
19+
});
20+
21+
function scrollToTopOfPageOnClick() {
22+
window.scrollTo({ top: 0, behavior: 'smooth' });
23+
}
24+
25+
<template>
26+
<button
27+
type='button'
28+
{{showOnScroll}}
29+
{{on 'click' scrollToTopOfPageOnClick}}
30+
class='scroll-to-top'
31+
alt='Scroll to top'
32+
></button>
33+
</template>

app/controllers/project-version.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export default class ProjectVersionController extends Controller {
1919
@service
2020
project;
2121

22+
@service
23+
fastboot;
24+
2225
@service router;
2326
@service('project') projectService;
2427

app/templates/project-version.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@
3131
</EsSidebar>
3232
<section class="content-wrapper">
3333
{{outlet}}
34+
35+
{{#unless this.fastboot.isFastBoot}}
36+
<ScrollToTopButton />
37+
{{/unless}}
3438
</section>
3539
</div>

0 commit comments

Comments
 (0)