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

Version Path
css-zero-1.0.4 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-1.0.3 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-1.0.2 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-1.0.1 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-1.0.0 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.98 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.97 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.96 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.95 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.94 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.93 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.92 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.91 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.90 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.89 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.88 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.87 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js
css-zero-0.0.86 lib/generators/css_zero/add/templates/app/javascript/controllers/menu_controller.js