Skip to content

Commit

Permalink
Add threshold and scrollto props to back-to-top
Browse files Browse the repository at this point in the history
  • Loading branch information
albinazs committed Jul 28, 2023
1 parent a783a74 commit 7746586
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/js/explorer-1.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ <h2 class="text-h3">Heading</h2>
Maintained by DesignLab
</p>
</div>
<div id="BackToTop" class="BlockBackToTop">
<div id="BackToTop" class="BlockBackToTop" data-threshold="600">
<button
class="BaseButton text-contrast-none inline-block -primary -compact fixed bottom-10 right-10 z-60"
>
Expand Down
12 changes: 6 additions & 6 deletions src/js/components/_BlockBackToTop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
*/

document.addEventListener('DOMContentLoaded', () => {
const backToTopBtn = document
.getElementById('BackToTop')
.querySelector('button')
const backToTop = document.getElementById('BackToTop')
const backToTopBtn = backToTop.querySelector('button')
const threshold = parseInt(backToTop.dataset.threshold) || 300
const scrollTo = parseInt(backToTop.dataset.scrollto) || 0
const alwaysVisible = backToTopBtn.classList.contains('always-visible')
const threshold = 300

const onScroll = () => {
if (!alwaysVisible) {
showBackToTop = window.scrollY > threshold
let showBackToTop = window.scrollY > threshold
showBackToTop
? (backToTopBtn.style.display = 'block')
: (backToTopBtn.style.display = 'none')
}
}

const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: 'smooth' })
window.scrollTo({ top: scrollTo, behavior: 'smooth' })
}

window.addEventListener('scroll', onScroll)
Expand Down
11 changes: 9 additions & 2 deletions storybook/stories/components/BlockBackToTop/BlockBackToTop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { BaseButtonTemplate } from '../BaseButton/BaseButton'

export const BlockBackToTopTemplate = ({ alwaysVisible }) => {
return `<div id="BackToTop" class="BlockBackToTop">
export const BlockBackToTopTemplate = ({
alwaysVisible,
threshold,
scrollTo,
}) => {
if (!threshold) threshold = ''
if (!scrollTo) scrollTo = ''

return `<div id="BackToTop" class="BlockBackToTop" data-threshold="${threshold}" data-scrollto="${scrollTo}">
${BaseButtonTemplate({
variant: 'primary',
cssClass: `z-60 ${alwaysVisible ? 'always-visible' : ''}`,
Expand Down

0 comments on commit 7746586

Please sign in to comment.