-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scroll-top): implement scroll-top component
-------- Co-authored-by: Michel Binder <michel.binder@statistik.ji.zh.ch> Co-authored-by: Dan Büschlen <danbueschlen@gmail.com>
- Loading branch information
1 parent
586509c
commit 07c670c
Showing
6 changed files
with
387 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { html, LitElement } from "lit" | ||
import { classMap } from "lit/directives/class-map.js" | ||
|
||
import styles from "./scroll-top.css" | ||
import "../button/leu-button.js" | ||
import { throttle } from "../../lib/utils.js" | ||
|
||
/** | ||
* @tagname leu-scroll-top | ||
*/ | ||
export class LeuScrollTop extends LitElement { | ||
static styles = styles | ||
|
||
static properties = { | ||
_showButton: { state: true }, | ||
} | ||
|
||
constructor() { | ||
super() | ||
/** @internal */ | ||
this._prevYPos = 0 | ||
/** @internal */ | ||
this._showButton = false | ||
/** @internal */ | ||
this._scrollDown = false | ||
|
||
/** @internal */ | ||
this._scrollListener = undefined | ||
} | ||
|
||
scroll = () => { | ||
const delta = window.scrollY - this._prevYPos | ||
|
||
if (this._scrollDown) { | ||
if (delta < 0) { | ||
this._scrollDown = false | ||
} | ||
} else if (delta > 0) { | ||
this._scrollDown = true | ||
} | ||
|
||
/** | ||
* Only show the button when | ||
* ... the current scroll position is greater than the window height (below-the-fold) and when | ||
* ... scrolling up | ||
*/ | ||
this._showButton = window.scrollY > window.innerHeight && !this._scrollDown | ||
this._prevYPos = window.scrollY | ||
} | ||
|
||
connectedCallback() { | ||
super.connectedCallback() | ||
this._scrollListener = throttle(this.scroll, 100) | ||
document.addEventListener("scroll", this._scrollListener, true) | ||
} | ||
|
||
disconnectedCallback() { | ||
document.removeEventListener("scroll", this._scrollListener, true) | ||
super.disconnectedCallback() | ||
} | ||
|
||
static scrollToTop() { | ||
window.scrollTo({ | ||
top: 0, | ||
left: 0, | ||
behavior: "smooth", | ||
}) | ||
} | ||
|
||
render() { | ||
const cssClasses = { | ||
"scroll-top": true, | ||
hide: !this._showButton, | ||
} | ||
return html` | ||
<div class=${classMap(cssClasses)}> | ||
<leu-button | ||
icon="arrowUp" | ||
label="Zum Seitenanfang" | ||
round | ||
@click="${() => LeuScrollTop.scrollToTop()}" | ||
> | ||
</leu-button> | ||
</div> | ||
` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineElement } from "../../lib/defineElement.js" | ||
import { LeuScrollTop } from "./ScrollTop.js" | ||
|
||
export { LeuScrollTop } | ||
|
||
defineElement("scroll-top", LeuScrollTop) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
.scroll-top { | ||
overflow: hidden; | ||
position: fixed; | ||
right: 1.5rem; | ||
z-index: 1099; | ||
bottom: 8.125rem; | ||
|
||
/* show */ | ||
height: 50px; | ||
pointer-events: auto; | ||
transition: height, bottom 0.9s, 0.6s ease; | ||
} | ||
|
||
.hide { | ||
height: 0; | ||
pointer-events: none; | ||
transition: height, top 0.9s, 0.6s ease; | ||
} | ||
|
||
@keyframes hide-animation { | ||
0% { | ||
transform: rotate(0deg); | ||
} | ||
|
||
100% { | ||
transform: rotate(90deg); | ||
} | ||
} | ||
|
||
.hide leu-button { | ||
animation-name: hide-animation; | ||
animation-duration: 0.45s; | ||
animation-timing-function: ease; | ||
} |
217 changes: 217 additions & 0 deletions
217
src/components/scroll-top/stories/scroll-top.stories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
import { html } from "lit" | ||
import "../leu-scroll-top.js" | ||
|
||
export default { | ||
title: "ScrollTop", | ||
component: "leu-scroll-top", | ||
} | ||
|
||
function Template() { | ||
return html` | ||
<p> | ||
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy | ||
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam | ||
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet | ||
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit | ||
amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam | ||
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed | ||
diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. | ||
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor | ||
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed | ||
diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam | ||
erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea | ||
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum | ||
dolor sit amet. | ||
</p> | ||
<p> | ||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse | ||
molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero | ||
eros et accumsan et iusto odio dignissim qui blandit praesent luptatum | ||
zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum | ||
dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh | ||
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. | ||
</p> | ||
<p> | ||
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper | ||
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel | ||
eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, | ||
vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et | ||
iusto odio dignissim qui blandit praesent luptatum zzril delenit augue | ||
duis dolore te feugait nulla facilisi. | ||
</p> | ||
<p> | ||
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet | ||
doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit | ||
amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod | ||
tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad | ||
minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis | ||
nisl ut aliquip ex ea commodo consequat. | ||
</p> | ||
<p> | ||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse | ||
molestie consequat, vel illum dolore eu feugiat nulla facilisis. | ||
</p> | ||
<p> | ||
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd | ||
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem | ||
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod | ||
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam | ||
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet | ||
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit | ||
amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam | ||
aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed | ||
tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna | ||
no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor | ||
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed | ||
diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam | ||
erat. | ||
</p> | ||
<p> | ||
Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut | ||
labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et | ||
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no | ||
sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit | ||
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt | ||
ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et | ||
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no | ||
sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit | ||
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt | ||
ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et | ||
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no | ||
sea takimata sanctus. | ||
</p> | ||
<p> | ||
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy | ||
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam | ||
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet | ||
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit | ||
amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam | ||
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed | ||
diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. | ||
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor | ||
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed | ||
diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam | ||
erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea | ||
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum | ||
dolor sit amet. | ||
</p> | ||
<p> | ||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse | ||
molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero | ||
eros et accumsan et iusto odio dignissim qui blandit praesent luptatum | ||
zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum | ||
dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh | ||
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. | ||
</p> | ||
<p> | ||
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper | ||
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel | ||
eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, | ||
vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et | ||
iusto odio dignissim qui blandit praesent luptatum zzril delenit augue | ||
duis dolore te feugait nulla facilisi. | ||
</p> | ||
<p> | ||
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet | ||
doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit | ||
amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod | ||
tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad | ||
minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis | ||
nisl ut aliquip ex ea commodo consequat. | ||
</p> | ||
<p> | ||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse | ||
molestie consequat, vel illum dolore eu feugiat nulla facilisis. | ||
</p> | ||
<p> | ||
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd | ||
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem | ||
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod | ||
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam | ||
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet | ||
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit | ||
amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam | ||
aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed | ||
tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna | ||
no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor | ||
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed | ||
diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam | ||
erat. | ||
</p> | ||
<p> | ||
Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut | ||
labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et | ||
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no | ||
sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit | ||
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt | ||
ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et | ||
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no | ||
sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit | ||
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt | ||
ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et | ||
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no | ||
sea takimata sanctus. | ||
</p> | ||
<p> | ||
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy | ||
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam | ||
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet | ||
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit | ||
amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam | ||
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed | ||
diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. | ||
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor | ||
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed | ||
diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam | ||
erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea | ||
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum | ||
dolor sit amet. | ||
</p> | ||
<p> | ||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse | ||
molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero | ||
eros et accumsan et iusto odio dignissim qui blandit praesent luptatum | ||
zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum | ||
dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh | ||
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. | ||
</p> | ||
<p> | ||
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper | ||
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel | ||
eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, | ||
vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et | ||
iusto odio dignissim qui blandit praesent luptatum zzril delenit augue | ||
duis dolore te feugait nulla facilisi. | ||
</p> | ||
<p> | ||
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet | ||
doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit | ||
amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod | ||
tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad | ||
minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis | ||
nisl ut aliquip ex ea commodo consequat. | ||
</p> | ||
<p> | ||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse | ||
molestie consequat, vel illum dolore eu feugiat nulla facilisis. | ||
</p> | ||
<p> | ||
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd | ||
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem | ||
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod | ||
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam | ||
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet | ||
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit | ||
amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam | ||
aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed | ||
tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna | ||
no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor | ||
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed | ||
diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam | ||
erat. | ||
</p> | ||
<leu-scroll-top /> | ||
` | ||
} | ||
|
||
export const Regular = Template.bind({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { html } from "lit" | ||
import { fixture, expect } from "@open-wc/testing" | ||
|
||
import "../leu-scroll-top.js" | ||
|
||
async function defaultFixture() { | ||
return fixture(html` <leu-scroll-top /> `) | ||
} | ||
|
||
describe("LeuScrollTop", () => { | ||
it("is a defined element", async () => { | ||
const el = await customElements.get("leu-scroll-top") | ||
|
||
await expect(el).not.to.be.undefined | ||
}) | ||
|
||
it("passes the a11y audit", async () => { | ||
const el = await defaultFixture() | ||
|
||
await expect(el).shadowDom.to.be.accessible() | ||
}) | ||
}) |
Oops, something went wrong.