Files
once-campfire/app/javascript/lib/autocomplete/custom_elements/suggestion_option.js
Kevin McConnell df76a227dc Hello world
First open source release of Campfire 🎉
2025-08-21 09:31:59 +01:00

60 lines
1.1 KiB
JavaScript

import { generateUUID, synchronize } from "lib/autocomplete/helpers"
export default class extends HTMLElement {
constructor() {
super(...arguments)
this.flash = synchronize(this.flash)
}
connectedCallback() {
this.id ||= `option-${generateUUID()}`
}
get selectElement() {
return this.closest("suggestion-select")
}
get index() {
if (this.selectElement) {
return Array.from(this.selectElement.optionElements).indexOf(this)
} else {
return null
}
}
get selected() {
return this.hasAttribute("selected")
}
set selected(value) {
if (value) {
this.setAttribute("selected", "")
} else {
this.removeAttribute("selected")
}
}
get value() {
return this.getAttribute("value")
}
flash(callback) {
const drawFrame = (frame = 0) => {
requestAnimationFrame(() => {
if (frame == 0) {
this.classList.add("flashing-off")
} else if (frame == 4) {
this.classList.remove("flashing-off")
}
if (frame == 7) {
callback()
} else {
drawFrame(frame + 1)
}
})
}
drawFrame(0)
}
}