You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
docuseal/app/javascript/elements/scroll_to.js

25 lines
680 B

export default class extends HTMLElement {
connectedCallback () {
this.selector = document.getElementById(this.dataset.selectorId)
const scrollToTarget = () => {
this.selector.scrollIntoView({ behavior: 'smooth', block: 'start' })
history.replaceState(null, null, `#${this.dataset.selectorId}`)
if (this.selector.tabIndex < 0) {
this.selector.tabIndex = -1
}
this.selector.focus({ preventScroll: true })
}
this.addEventListener('click', scrollToTarget)
this.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
scrollToTarget()
}
})
}
}