23 lines
778 B
JavaScript
23 lines
778 B
JavaScript
import { Controller } from '@hotwired/stimulus'
|
|
|
|
export default class extends Controller {
|
|
static targets = [ 'today' ]
|
|
|
|
connect() {
|
|
if (false === this.hasTodayTarget) {
|
|
return
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
const containerRect = this.element.getBoundingClientRect()
|
|
const todayRect = this.todayTarget.getBoundingClientRect()
|
|
|
|
const scrollLeft = todayRect.left - containerRect.left + this.element.scrollLeft
|
|
const centered = scrollLeft - (this.element.clientWidth / 2) + (todayRect.width / 2)
|
|
const maxScrollLeft = this.element.scrollWidth - this.element.clientWidth
|
|
|
|
this.element.scrollLeft = Math.min(Math.max(0, centered), maxScrollLeft)
|
|
})
|
|
}
|
|
}
|