Skip to content

Commit

Permalink
feat: indicates current position of page via filling the icon (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Jan 3, 2024
1 parent f8e5456 commit 7a1c51e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 20 additions & 1 deletion assets/hb/modules/back-to-top/js/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Button {
const btn = document.createElement('button')
btn.className = 'hb-back-to-top'
btn.ariaLabel = 'Back to top'
btn.innerHTML = icon
btn.innerHTML = this.transformIcon(icon)
document.body.appendChild(btn)
this.btn = btn

Expand All @@ -23,6 +23,7 @@ export default class Button {
btn.classList.remove('scrolling')
}
y = top
this.updatePos()
});

this.btn.addEventListener('click', () => {
Expand All @@ -35,6 +36,24 @@ export default class Button {
})
}

transformIcon(icon) {
return icon.replace(/<svg(.*)>((.|\n)*)<\/svg>/, `<svg$1>
<defs><clipPath id="icon">$2</clipPath></defs>
<rect x="0" y="0" fill="currentColor" width="100%" height="100%" clip-path="url(#icon)" />
<rect x="0" y="100%" width="100%" height="100%" clip-path="url(#icon)" />
</svg>`)
}

private posIcon: HTMLElement

updatePos() {
if (!this.posIcon) {
this.posIcon = this.btn.querySelectorAll<HTMLElement>('rect')[1]
}
const pos = document.documentElement.scrollTop / (document.documentElement.offsetHeight - document.documentElement.clientHeight)
this.posIcon.setAttribute('y', (1 - pos) * 100 + '%')
}

show() {
this.btn.classList.add('show')
}
Expand Down
6 changes: 6 additions & 0 deletions assets/hb/modules/back-to-top/scss/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@

.hb-back-to-top-icon {
display: flex;

rect {
&:nth-of-type(2) {
fill: var(--#{$prefix}primary);
}
}
}

/*! purgecss end ignore */

0 comments on commit 7a1c51e

Please sign in to comment.