Sha256: b2b04bd5d418e608192150e8f83e499b59f532b9b5e602d0c9dddc9025f611e1

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = [ "button", "tab" ]
  static values  = { index: Number }

  indexValueChanged() {
    this.#showCurrentTab()
  }

  select({ target }) {
    this.indexValue = this.buttonTargets.indexOf(target)
  }

  prev() {
    const hasPrevious = this.indexValue > 0
    hasPrevious && this.indexValue--
    hasPrevious && this.#focusCurrentButton()
  }

  next() {
    const hasNext = this.indexValue < this.#lastIndex
    hasNext && this.indexValue++
    hasNext && this.#focusCurrentButton()
  }

  #showCurrentTab() {
    this.buttonTargets.forEach((button, index) => {
      button.ariaSelected = index == this.indexValue
      button.tabIndex     = index == this.indexValue ? 0 : -1
    })

    this.tabTargets.forEach((tab, index) => {
      tab.hidden = index !== this.indexValue
    })
  }

  #focusCurrentButton() {
    this.buttonTargets[this.indexValue].focus()
  }

  get #lastIndex() {
    return this.tabTargets.length -1
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
css-zero-0.0.47 lib/generators/css_zero/add/templates/app/javascript/controllers/tabs_controller.js