Sha256: 684262369d86c5c3915b6866ab44b620f1fe3f375b94e9c609eea3611a112a5a
Contents?: true
Size: 1.04 KB
Versions: 18
Compression:
Stored size: 1.04 KB
Contents
import { Controller } from "@hotwired/stimulus" export default class extends Controller { static targets = [ "item" ] static values = { index: Number } #observer initialize() { this.#observer = new IntersectionObserver(this.#reset.bind(this)) } connect() { this.#observer.observe(this.element) } disconnect() { this.#observer.disconnect() } prev() { if (this.indexValue > 0) { this.indexValue-- this.#update() } } next() { if (this.indexValue < this.#lastIndex) { this.indexValue++ this.#update() } } #reset([ entry ]) { if (entry.isIntersecting) { this.indexValue = 0 this.#update() } } #update() { this.#updateTabstops() this.#focusCurrentItem() } #updateTabstops() { this.itemTargets.forEach((element, index) => { element.tabIndex = index === this.indexValue ? 0 : -1 }) } #focusCurrentItem() { this.itemTargets[this.indexValue].focus() } get #lastIndex() { return this.itemTargets.length -1 } }
Version data entries
18 entries across 18 versions & 1 rubygems