Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(back-to-top): add <rh-back-to-top> #1517

Merged
merged 18 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/late-cobras-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@rhds/elements": minor
---

✨ Added `<rh-back-to-top>`.
zeroedin marked this conversation as resolved.
Show resolved Hide resolved

Back to top component is a fragment link that allows users to quickly navigate to the top of a lengthy content.

```html
<rh-back-to-top href="#top">Back to top</rh-back-to-top>
```
23 changes: 23 additions & 0 deletions elements/rh-back-to-top/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Back to Top
The back to top component is a shortcut that allows users to quickly navigate to the top of a lengthy content page.


## Installation

If using npm/bundlers:

```bash
npm install @rhds/elements
```

Then once installed, import it to your application:

```js
import '@rhds/elements/rh-button/rh-button.js';
```
## Usage
Back to top button is designed to only be used once per page.

```html
<rh-back-to-top href="#top">Back to top</rh-back-to-top>
```
20 changes: 20 additions & 0 deletions elements/rh-back-to-top/demo/always-visible.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<rh-back-to-top visible="always" href="#top">Back to top</rh-back-to-top>
<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
import '@patternfly/elements/pf-icon/pf-icon.js';
</script>

<style>
main {
display: block;
scroll-behavior: smooth;
max-height: calc(100dvh - var(--pf-demo-header-height)) !important;
}
#bottom {
position: absolute;
bottom: 0;
}
#nav {
height: calc(100dvh - var(--pf-demo-header-height, 4.375rem)); !important;
}
</style>
74 changes: 74 additions & 0 deletions elements/rh-back-to-top/demo/color-context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<rh-context-demo>
<div id="overflow">
<a id="top" href="#bottom">Go to bottom</a>
<p>Scroll down or press tab to see the back to top button</p>
<a id="bottom" href="#top">Go to top</a>
<div>
</rh-context-demo>

<rh-back-to-top href="#top">Back to top</rh-back-to-top>

<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
import '@patternfly/elements/pf-icon/pf-icon.js';
import '@rhds/elements/lib/elements/rh-context-demo/rh-context-demo.js';
</script>

<style>
a {
color: var(--rh-color-interactive-blue-darker, #0066cc);
}

a:is(:hover,:focus) {
color: var(--rh-color-interactive-blue-darkest, #003366);
}

[color-palette^="dark"] a {
color: var(--rh-color-accent-base-on-dark, #92c5f9);
}

[color-palette^="dark"] a:is(:hover,:focus) {
color: var(--rh-color-interactive-blue-lightest, #b9dafc);
}

main {
display: block;
scroll-behavior: smooth;
max-height: calc(100dvh - var(--pf-demo-header-height)) !important;
}

#nav {
height: calc(100dvh - var(--pf-demo-header-height, 4.375rem)) !important;
}

#overflow {
min-height: calc(100dvh + 401px);
position: relative;
}

#bottom {
position: absolute;
bottom: 0;
}

rh-context-demo {
height: auto;
}

@media (prefers-reduced-motion: reduce) {
main {
scroll-behavior: auto;
}
}
</style>


<script type="module">
/* add support for demo/fullscreen and dev server */
const b2t = document.querySelector('rh-back-to-top');
const main = document.querySelector('main');
// if main isn't scrollable use window
if (window.getComputedStyle(main).overflow === 'scroll') {
b2t.scrollableSelector = 'main';
}
</script>
47 changes: 47 additions & 0 deletions elements/rh-back-to-top/demo/no-slotted-text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div id="overflow">
<a id="top" href="#bottom">Go to bottom</a>
<p>Scroll down or press tab to see the back to top button</p>
<a id="bottom" href="#top">Go to top</a>
<rh-back-to-top href="#top" label="Return to top"></rh-back-to-top>
</div>
<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
import '@patternfly/elements/pf-icon/pf-icon.js';
</script>

<style>
main {
display: block;
scroll-behavior: smooth;
max-height: calc(100dvh - var(--pf-demo-header-height)) !important;
}

#nav {
height: calc(100dvh - var(--pf-demo-header-height, 4.375rem)) !important;
}

#overflow {
min-height: calc(100dvh + 401px);
position: relative;
}

#bottom {
position: absolute;
bottom: 0;
}

@media (prefers-reduced-motion: reduce) {
main {
scroll-behavior: auto;
}
}
</style>
<script type="module">
/* add support for demo/fullscreen and dev server */
const b2t = document.querySelector('rh-back-to-top');
const main = document.querySelector('main');
// if main isn't scrollable use window
if (window.getComputedStyle(main).overflow === 'scroll') {
b2t.scrollableSelector = 'main';
}
</script>
100 changes: 100 additions & 0 deletions elements/rh-back-to-top/demo/position-sticky.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<div id="mock" class="viewport">
<div class="nav" id="top"></div>
<div class="page-body" >
<div class="hero">
<div>
<h2>Back to Top Demo</h2>
<p><pf-icon icon="arrow-down"></pf-icon> Scroll down or press tab to show back to top link.</p>
<p>Back to top link will stay above the footer</p>
</div>
</div>
<div class="main">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis tellus nec erat sodales ultrices. Morbi fringilla, purus ut cursus volutpat, dui ipsum varius massa, a lobortis mauris mi et justo. Integer lobortis sed magna in aliquet. Pellentesque suscipit elit enim, in consectetur metus tincidunt eu. In quam dolor, bibendum ac lorem et, blandit lacinia lorem. Praesent bibendum magna vel finibus facilisis. Cras eleifend est sed nisi malesuada, vitae varius sem venenatis. Nam maximus ligula bibendum quam porttitor tempor. Cras in lacus in lacus consectetur placerat lobortis ac velit. Pellentesque venenatis leo sit amet neque commodo pulvinar. Integer quis nulla ac eros luctus scelerisque. Pellentesque interdum sapien eros. Integer id velit sed eros fringilla porttitor.</p>
zeroedin marked this conversation as resolved.
Show resolved Hide resolved
<p>Integer sit amet ultricies felis. Pellentesque consectetur metus lacus, vitae ultricies augue imperdiet non. Phasellus consectetur sapien elit, at blandit sapien scelerisque at. Sed congue venenatis sem nec ultrices. Aenean vel nunc semper, auctor lacus id, imperdiet ipsum. Aliquam gravida ultricies auctor. Suspendisse potenti.</p>
<p>Maecenas volutpat nisi lacus, sed facilisis sapien porta eget. Fusce interdum euismod faucibus. Curabitur sit amet lorem eu massa rutrum tempor vitae vitae urna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nullam ultricies lacinia nisl, sed pharetra tortor porttitor vitae. Donec tincidunt lectus vitae quam volutpat malesuada. Nunc felis orci, commodo vitae euismod non, vestibulum vel mauris. Quisque vel ipsum non dolor faucibus porttitor. Aenean porttitor sollicitudin elit, sit amet dictum ante dapibus vitae.</p>
<p>Nam imperdiet id ipsum sit amet congue. Sed nisl felis, pharetra in convallis varius, pretium a sem. Duis tincidunt luctus ipsum, a vestibulum nulla pharetra eget. In varius pellentesque lorem sed viverra. Phasellus ut leo pellentesque, finibus urna quis, elementum sapien. Nunc lacinia risus tortor, sit amet hendrerit dui vehicula ornare. Phasellus porttitor hendrerit mauris, vel euismod elit posuere ut.</p>
<p>Nullam tristique egestas ligula vitae interdum. Aliquam erat volutpat. Morbi gravida, enim eget convallis efficitur, risus ante sagittis magna, a viverra dolor felis a velit. Sed blandit semper nulla eu congue. Etiam gravida iaculis diam, ut vehicula tortor consectetur et. Mauris vitae tincidunt est. Phasellus sit amet nulla leo. Phasellus luctus et libero non mollis. Pellentesque efficitur, massa non vehicula auctor, risus dui vehicula sem, at aliquet elit justo et nibh. Ut sodales lectus vitae lacus aliquam, quis pharetra ante viverra. Aliquam tristique, mi non egestas viverra, nunc turpis malesuada lectus, vitae malesuada enim ante et arcu. Nullam interdum nulla et purus molestie interdum. Nulla et eros porttitor, dignissim sapien nec, ornare augue.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis tellus nec erat sodales ultrices. Morbi fringilla, purus ut cursus volutpat, dui ipsum varius massa, a lobortis mauris mi et justo. Integer lobortis sed magna in aliquet. Pellentesque suscipit elit enim, in consectetur metus tincidunt eu. In quam dolor, bibendum ac lorem et, blandit lacinia lorem. Praesent bibendum magna vel finibus facilisis. Cras eleifend est sed nisi malesuada, vitae varius sem venenatis. Nam maximus ligula bibendum quam porttitor tempor. Cras in lacus in lacus consectetur placerat lobortis ac velit. Pellentesque venenatis leo sit amet neque commodo pulvinar. Integer quis nulla ac eros luctus scelerisque. Pellentesque interdum sapien eros. Integer id velit sed eros fringilla porttitor.</p>
<a id="bottom" href="#focusable-element-bottom">Focusable element</a>
</div>
<rh-back-to-top scrollable-selector="#mock" href="#top">Back to top</rh-back-to-top>
<div class="footer"></div>
</div>

</div>

<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
import '@patternfly/elements/pf-icon/pf-icon.js';
</script>

<style>
/* override to place rh-back-to-top in context of page elements */
rh-back-to-top {
position: sticky;
float: right;
}

/* Addtional styles for demo page layout */
#mock {
--_nav-height: 70px;
--_footer-height: 400px;
--_hero-height: 300px;
--_content-width: 60rem;

scroll-behavior: smooth;
height: calc(100vh - calc(24px * 2) - var(--pf-demo-header-height));
overflow-y: scroll;
border: var(--rh-border-width-md, 2px) solid var(--rh-color-border-subtle-on-light, #c7c7c7);
}
#mock .page-body {
position: relative;
}
#mock .nav {
background-color: #151515;
height: var(--_nav-height);
color: #ffffff;
}
#mock .main {
position: relative;
margin: 0 auto;
max-width: var(--_content-width);
margin-block: var(--rh-spacer-2xl, 32px);
padding: var(--rh-spacer-2xl, 32px);
min-height: calc(100dvh - var(--_nav-height) - var(--_hero-height));

}
#mock .hero {
display: flex;
justify-content: space-evenly;
height: var(--_hero-height);
background-color: var(--rh-color-surface-lighter, #f2f2f2);
padding-inline: var(--rh-spacer-2xl, 32px);
flex-direction: column;
}
#mock .hero div {
display: flex;
align-items: center;
flex-direction: column;
padding-inline: var(--rh-spacer-2xl, 32px);
margin: 0 auto;
max-width: var(--_content-width);
}
#mock .footer {
position: static;
background-color: #151515;
height: var(--_footer-height);
margin-block-start: var(--rh-space-6xl, 96px);
color: #ffffff;
}
#mock #bottom {
position: absolute;
bottom: 0;
}

@media (prefers-reduced-motion: reduce) {
#mock .viewport {
scroll-behavior: auto;
}
}
</style>
48 changes: 48 additions & 0 deletions elements/rh-back-to-top/demo/rh-back-to-top.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<div id="overflow">
<a id="top" href="#bottom">Go to bottom</a>
<p>Scroll down or press tab to see the back to top button</p>
<a id="bottom" href="#top">Go to top</a>
<rh-back-to-top href="#top">Back to top</rh-back-to-top>
</div>
<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
import '@patternfly/elements/pf-icon/pf-icon.js';
</script>

<style>
main {
display: block;
scroll-behavior: smooth;
max-height: calc(100dvh - var(--pf-demo-header-height)) !important;
}

#nav {
height: calc(100dvh - var(--pf-demo-header-height, 4.375rem)) !important;
}

#overflow {
min-height: calc(100dvh + 401px);
position: relative;
}

#bottom {
position: absolute;
bottom: 0;
}

@media (prefers-reduced-motion: reduce) {
main {
scroll-behavior: auto;
}
}
</style>

<script type="module">
/* add support for demo/fullscreen and dev server */
const b2t = document.querySelector('rh-back-to-top');
const main = document.querySelector('main');
// if main isn't scrollable use window
if (window.getComputedStyle(main).overflow === 'scroll') {
b2t.scrollableSelector = 'main';
}
</script>
50 changes: 50 additions & 0 deletions elements/rh-back-to-top/demo/scroll-distance.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<div id="overflow">
<a id="top" href="#bottom">Go to bottom</a>
<p>Scroll down (50px) or press tab to see the back to top button</p>
<a id="bottom" href="#top">Go to top</a>
<rh-back-to-top scroll-distance="50" href="#top">Back to top</rh-back-to-top>
</div>
<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
import '@patternfly/elements/pf-icon/pf-icon.js';
</script>

<style>
main {
display: block;
scroll-behavior: smooth;
max-height: calc(100dvh - var(--pf-demo-header-height)) !important;
}

#nav {
height: calc(100dvh - var(--pf-demo-header-height, 4.375rem)) !important;
}

#overflow {
/* scroll distance set to 50px */
min-height: calc(100dvh + 51px);
position: relative;
}

#bottom {
position: absolute;
bottom: 0;
}

@media (prefers-reduced-motion: reduce) {
main {
scroll-behavior: auto;
}
}
</style>


<script type="module">
/* add support for demo/fullscreen and dev server */
const b2t = document.querySelector('rh-back-to-top');
const main = document.querySelector('main');
// if main isn't scrollable use window
if (window.getComputedStyle(main).overflow === 'scroll') {
b2t.scrollableSelector = 'main';
}
</script>
Loading
Loading