2024-12-17 17:36:02 +00:00
|
|
|
const template = fn => {
|
|
|
|
return (arr, ...params) => {
|
|
|
|
if (arr instanceof Array) {
|
|
|
|
const buffer = []
|
|
|
|
for (let i = 0; i < params.length; i++) {
|
|
|
|
buffer.push(arr[i])
|
|
|
|
buffer.push(params[i])
|
|
|
|
}
|
|
|
|
buffer.push(arr[arr.length - 1])
|
|
|
|
return fn(buffer.join(""))
|
|
|
|
}
|
|
|
|
return fn(arr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const f = template(string => {
|
|
|
|
const template = document.createElement("template")
|
|
|
|
template.innerHTML = string
|
|
|
|
return template.content
|
|
|
|
})
|
|
|
|
|
|
|
|
const css = template(string => {
|
|
|
|
const styleSheet = new CSSStyleSheet
|
|
|
|
styleSheet.replaceSync(string)
|
|
|
|
return styleSheet
|
|
|
|
})
|
|
|
|
|
|
|
|
const childObserver = new MutationObserver(mutations => {
|
|
|
|
for (const mutation of mutations) {
|
|
|
|
mutation.target.mutationCallback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export class BetterSelect extends HTMLElement {
|
2024-12-18 12:41:00 +00:00
|
|
|
/** @type {AbortController} */
|
2024-12-17 17:36:02 +00:00
|
|
|
#abortOpen
|
|
|
|
#value = {}
|
|
|
|
|
|
|
|
#internals = this.attachInternals()
|
|
|
|
static formAssociated = true
|
|
|
|
|
|
|
|
static styleSheet = css`
|
|
|
|
:host {
|
|
|
|
position: relative;
|
2024-12-18 12:41:00 +00:00
|
|
|
display: inline-block;
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
* {
|
|
|
|
box-sizing: border-box;
|
|
|
|
}
|
|
|
|
[part="display"] {
|
2024-12-18 12:41:00 +00:00
|
|
|
min-width: 100%;
|
|
|
|
|
|
|
|
/* Layout */
|
|
|
|
align-items: center;
|
2024-12-17 17:36:02 +00:00
|
|
|
display: inline-flex;
|
|
|
|
flex-flow: row nowrap;
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
/* Styling */
|
2024-12-17 17:36:02 +00:00
|
|
|
cursor: pointer;
|
2024-12-18 12:41:00 +00:00
|
|
|
}
|
|
|
|
[part="display-text"]:empty {
|
|
|
|
display: none;
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
:not(:empty + *)[name="placeholder"] {
|
|
|
|
display: none;
|
|
|
|
}
|
2024-12-18 12:41:00 +00:00
|
|
|
[part="drop-down"], [part="item"] {
|
2024-12-17 17:36:02 +00:00
|
|
|
/* Resets */
|
|
|
|
border: unset;
|
2024-12-18 12:41:00 +00:00
|
|
|
outline: unset;
|
2024-12-17 17:36:02 +00:00
|
|
|
padding: unset;
|
2024-12-18 12:41:00 +00:00
|
|
|
}
|
|
|
|
[part="drop-down"] {
|
2024-12-17 17:36:02 +00:00
|
|
|
background: inherit;
|
|
|
|
color: inherit;
|
|
|
|
|
|
|
|
position: absolute;
|
|
|
|
flex-flow: column;
|
|
|
|
margin: 0;
|
2024-12-19 13:53:01 +00:00
|
|
|
z-index: var(--layer-dropdown, 100);
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
[part="drop-down"]:modal {
|
|
|
|
margin: auto;
|
|
|
|
&::backdrop {
|
|
|
|
background-color: #fff2;
|
|
|
|
backdrop-filter: blur(2px);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[part="drop-down"][open] {
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
[part="list"] {
|
|
|
|
display: contents;
|
|
|
|
}
|
|
|
|
[part="item"] {
|
|
|
|
display: block;
|
|
|
|
cursor: pointer;
|
2024-12-18 12:41:00 +00:00
|
|
|
white-space: nowrap;
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
[part="item"]:focus {
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
[part="item"][hidden] {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
slot[name="loading"] {
|
|
|
|
display: none;
|
|
|
|
}
|
2024-12-18 12:41:00 +00:00
|
|
|
:host(:state(--loading)) {
|
|
|
|
[part="list"] { display: none; }
|
|
|
|
slot[name="loading"] { display: block; }
|
|
|
|
}
|
2024-12-17 17:36:02 +00:00
|
|
|
`
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
/** @type {HTMLElement} */
|
|
|
|
display
|
|
|
|
/** @type {HTMLElement} */
|
|
|
|
text
|
|
|
|
/** @type {HTMLElement} */
|
|
|
|
list
|
|
|
|
/** @type {HTMLElement} */
|
|
|
|
placeholder
|
|
|
|
/** @type {HTMLInputElement} */
|
|
|
|
input
|
|
|
|
/** @type {HTMLDialogElement} */
|
|
|
|
dialog
|
|
|
|
/** @type {HTMLDialogElement} */
|
|
|
|
loading
|
|
|
|
|
2024-12-17 17:36:02 +00:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
childObserver.observe(this, {childList: true})
|
|
|
|
this.attachShadow({mode: "open"}).innerHTML = `
|
|
|
|
<div id="display" part="display">
|
|
|
|
<span part="display-text" id="text"></span>
|
2024-12-18 12:41:00 +00:00
|
|
|
<slot name="placeholder" aria-hidden="true">
|
2024-12-19 14:19:32 +00:00
|
|
|
<span part="placeholder" id="placeholder" aria-hidden="true"></span>
|
2024-12-18 12:41:00 +00:00
|
|
|
</slot>
|
2024-12-17 17:36:02 +00:00
|
|
|
</div>
|
|
|
|
<dialog id="dialog" part="drop-down">
|
2024-12-19 14:26:07 +00:00
|
|
|
<input type="search" id="input" part="search" type="search"></input>
|
2024-12-17 17:36:02 +00:00
|
|
|
<ul id="list" part="list"></ul>
|
2024-12-18 12:41:00 +00:00
|
|
|
<slot id="loading" name="loading"></slot>
|
2024-12-17 17:36:02 +00:00
|
|
|
</dialog>
|
|
|
|
`
|
|
|
|
this.shadowRoot.adoptedStyleSheets = [BetterSelect.styleSheet]
|
2024-12-18 14:38:11 +00:00
|
|
|
this.#internals.setFormValue("", "")
|
2024-12-17 17:36:02 +00:00
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
this.tabIndex = 0
|
|
|
|
|
|
|
|
this.#internals.role = "combobox"
|
2024-12-17 17:36:02 +00:00
|
|
|
|
|
|
|
this.options = this.getElementsByTagName("option")
|
2024-12-18 12:41:00 +00:00
|
|
|
for (const element of this.shadowRoot.querySelectorAll(`[id]`)) {
|
|
|
|
this[element.id] = element
|
|
|
|
}
|
2024-12-17 17:36:02 +00:00
|
|
|
|
|
|
|
this.shadowRoot.addEventListener("click", event => {
|
|
|
|
const item = event.target.closest("#list > li")
|
|
|
|
if (item) {
|
|
|
|
this.setOption(item)
|
|
|
|
this.dispatchEvent(new InputEvent("input", {bubbles: true}))
|
|
|
|
this.close()
|
|
|
|
} else if (!this.#internals.states.has("--open")) {
|
|
|
|
this.open()
|
|
|
|
} else if (this.display.contains(event.target) || this.display.contains(event.target.closest("[slot]")?.assignedSlot)) {
|
|
|
|
this.close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
this.addEventListener("keydown", event => {
|
|
|
|
if (event.key == " " && !this.input.contains(this.shadowRoot.activeElement)) {
|
|
|
|
if (this.#internals.states.has("--open")) {
|
|
|
|
this.close()
|
|
|
|
} else {
|
|
|
|
this.open()
|
|
|
|
}
|
|
|
|
} else if (event.key == "Escape") {
|
|
|
|
this.close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-12-17 17:36:02 +00:00
|
|
|
this.shadowRoot.addEventListener("input", event => {
|
|
|
|
const item = event.target.closest("#input")
|
|
|
|
if (item) {
|
|
|
|
this.search(item.value)
|
|
|
|
event.stopPropagation()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
async open() {
|
2024-12-17 17:36:02 +00:00
|
|
|
if (this.#abortOpen) return
|
|
|
|
|
|
|
|
this.#abortOpen = new AbortController()
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
const signal = this.closeSignal
|
2024-12-17 17:36:02 +00:00
|
|
|
window.addEventListener("click", event => {
|
2024-12-18 12:41:00 +00:00
|
|
|
if (event.target instanceof HTMLElement && !this.contains(event.target)) {
|
2024-12-17 17:36:02 +00:00
|
|
|
this.close()
|
|
|
|
}
|
|
|
|
}, {signal})
|
|
|
|
this.addEventListener("keypress", event => {
|
|
|
|
if (event.key == "Enter") {
|
|
|
|
this.selectDefault()
|
2024-12-18 14:38:58 +00:00
|
|
|
this.dispatchEvent(new InputEvent("input", {bubbles: true}))
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
}, {signal})
|
|
|
|
|
|
|
|
this.dialog.show()
|
|
|
|
this.#internals.states.add("--open")
|
2024-12-18 12:41:00 +00:00
|
|
|
|
|
|
|
if ("populate" in this) {
|
|
|
|
this.#internals.states.add("--loading")
|
|
|
|
await this.populate()
|
|
|
|
this.#internals.states.delete("--loading")
|
|
|
|
}
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.input.value = null
|
|
|
|
for (const hidden of this.list.querySelectorAll("[hidden]"))
|
|
|
|
hidden.removeAttribute("hidden")
|
|
|
|
this.#abortOpen?.abort()
|
|
|
|
this.#abortOpen = null
|
|
|
|
this.#internals.states.delete("--open")
|
|
|
|
this.dialog.close()
|
|
|
|
}
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
get closeSignal() { return this.#abortOpen?.signal }
|
|
|
|
|
|
|
|
/** @param {String} value */
|
2024-12-17 17:36:02 +00:00
|
|
|
search(value) {
|
|
|
|
for (const item of this.list.children) {
|
|
|
|
item.toggleAttribute("hidden", !this.match(value, item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectDefault() {
|
2024-12-18 12:41:00 +00:00
|
|
|
if (this.shadowRoot.activeElement?.matches(`[part="item"]`)) {
|
|
|
|
this.setOption(this.shadowRoot.activeElement)
|
|
|
|
this.close()
|
|
|
|
return
|
|
|
|
}
|
2024-12-17 17:36:02 +00:00
|
|
|
const candidates = [...this.list.children].filter(child => !child.hasAttribute("hidden"))
|
|
|
|
if (candidates.length) {
|
|
|
|
this.setOption(candidates[0])
|
|
|
|
this.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
/**
|
|
|
|
* @param {string} value
|
|
|
|
* @param {HTMLElement} item
|
|
|
|
*/
|
2024-12-17 17:36:02 +00:00
|
|
|
match(value, item) {
|
|
|
|
return item.innerText.toLowerCase().match(value.toLowerCase())
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
this.setOptions()
|
|
|
|
}
|
|
|
|
|
|
|
|
mutationCallback() {
|
|
|
|
this.setOptions()
|
|
|
|
}
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
/** @param {HTMLElement} option */
|
2024-12-17 17:36:02 +00:00
|
|
|
setOption(option) {
|
|
|
|
this.setValue(option.dataset.value, option.innerHTML)
|
|
|
|
}
|
|
|
|
|
2024-12-18 12:41:00 +00:00
|
|
|
/**
|
|
|
|
* @param {string} value
|
|
|
|
* @param {string} state
|
|
|
|
*/
|
2024-12-17 17:36:02 +00:00
|
|
|
setValue(value, state=value) {
|
|
|
|
this.#value = {value, state}
|
2024-12-18 14:38:58 +00:00
|
|
|
this.dispatchEvent(new Event("change", {bubbles: true}));
|
2024-12-17 17:36:02 +00:00
|
|
|
this.#internals.setFormValue(value, state)
|
|
|
|
this.text.innerText = state
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() { return this.#value.value }
|
|
|
|
set value(value) {
|
|
|
|
for (const option of this.options) {
|
|
|
|
if (option.value === value) {
|
2024-12-18 12:41:00 +00:00
|
|
|
this.setOption(option)
|
|
|
|
return
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw `No option with value ${value}`
|
|
|
|
}
|
|
|
|
|
|
|
|
get valueText() { return this.#value.state }
|
|
|
|
|
|
|
|
setOptions() {
|
|
|
|
this.list.replaceChildren()
|
|
|
|
for (const option of this.options) {
|
2024-12-18 12:41:00 +00:00
|
|
|
this.list.append(f`<li tabindex="0" part="item" data-value="${option.value}">${option.innerText}</li>`)
|
2024-12-19 14:44:39 +00:00
|
|
|
if (option.selected) {
|
|
|
|
this.value = option.value
|
|
|
|
}
|
2024-12-17 17:36:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|