Files
myep-team/assets/controllers/timeline_scroll_controller.js
T

27 lines
1007 B
JavaScript

import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = [ 'scroller', 'today' ]
connect() {
requestAnimationFrame(() => this.scrollToToday({ behavior: 'instant' }))
}
// centres today's column; the house column stays in front of the left part
scrollToToday({ behavior = 'smooth' } = {}) {
if (false === this.hasScrollerTarget || false === this.hasTodayTarget) {
return
}
const scroller = this.scrollerTarget
const scrollerRect = scroller.getBoundingClientRect()
const todayRect = this.todayTarget.getBoundingClientRect()
const scrollLeft = todayRect.left - scrollerRect.left + scroller.scrollLeft
const centered = scrollLeft - (scroller.clientWidth / 2) + (todayRect.width / 2)
const maxScrollLeft = scroller.scrollWidth - scroller.clientWidth
scroller.scrollTo({ left: Math.min(Math.max(0, centered), maxScrollLeft), behavior })
}
}