Rails-8-API-Image-Processing / app /javascript /controllers /language_tabs_controller.js
denise-oneill-inbox-ru's picture
D8 staging from 92665ef4298ecff6735b080ff7901f2c19f719a7
5010ad7 verified
Raw History Blame Contribute Delete
1.72 kB
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")
}
})
}
}