File size: 1,716 Bytes
5010ad7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["tab", "panel"]
static values = { active: { type: String, default: "en" } }
connect() {
this.activate(this.activeValue)
}
switch(event) {
const locale = event.currentTarget.dataset.locale
this.activeValue = locale
this.activate(locale)
}
navigate(event) {
const currentIndex = this.tabTargets.indexOf(event.currentTarget)
const targetIndex = {
ArrowRight: (currentIndex + 1) % this.tabTargets.length,
ArrowLeft: (currentIndex - 1 + this.tabTargets.length) % this.tabTargets.length,
Home: 0,
End: this.tabTargets.length - 1
}[event.key]
if (targetIndex === undefined) return
event.preventDefault()
const tab = this.tabTargets[targetIndex]
this.activeValue = tab.dataset.locale
this.activate(this.activeValue)
tab.focus()
}
activate(locale) {
// Update tabs
this.tabTargets.forEach(tab => {
const isActive = tab.dataset.locale === locale
if (isActive) {
tab.classList.add("border-indigo-500", "text-indigo-600")
tab.classList.remove("border-transparent", "text-gray-500")
} else {
tab.classList.remove("border-indigo-500", "text-indigo-600")
tab.classList.add("border-transparent", "text-gray-500")
}
tab.setAttribute("aria-selected", isActive)
tab.setAttribute("tabindex", isActive ? "0" : "-1")
})
// Update panels
this.panelTargets.forEach(panel => {
if (panel.dataset.locale === locale) {
panel.classList.remove("hidden")
} else {
panel.classList.add("hidden")
}
})
}
}
|