| 1 |
<!-- |
| 2 |
@component |
| 3 |
An element to select element(s) from a dropdown. |
| 4 |
|
| 5 |
It receives its `value` and a list of possible `options`. The `description` prop should |
| 6 |
render a human-readable string for the value (which will be displayed in the button) and |
| 7 |
option (which will be shown in the list popup). Optionally a `multiple_select` function |
| 8 |
can be passed and a selection of multiple options is possible for sets of options for |
| 9 |
which this returns true. |
| 10 |
|
| 11 |
This is an implementation of the Combobox pattern as described by the |
| 12 |
ARIA Authoring Practices Guide (APG) at |
| 13 |
https://www.w3.org/WAI/ARIA/apg/patterns/combobox/ |
| 14 |
In particular it should match the Select-Only Combobox example at |
| 15 |
https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/ |
| 16 |
--> |
| 17 |
<script lang="ts" module> |
| 18 |
/** Incrementing number to use for element ids in the component. */ |
| 19 |
let id = 0; |
| 20 |
</script> |
| 21 |
|
| 22 |
<script lang="ts"> |
| 23 |
interface Props { |
| 24 |
/** The currently entered value. */ |
| 25 |
value: string; |
| 26 |
/** The options for the value. */ |
| 27 |
options: readonly string[]; |
| 28 |
/** Function to get the human readable description of an option and the value. */ |
| 29 |
description: (option: string) => string; |
| 30 |
/** Whether an option can be part of a multi-selection (separated by comma in value). */ |
| 31 |
multiple_select?: (option: string) => boolean; |
| 32 |
} |
| 33 |
|
| 34 |
let { |
| 35 |
value = $bindable(), |
| 36 |
options, |
| 37 |
description, |
| 38 |
multiple_select, |
| 39 |
}: Props = $props(); |
| 40 |
|
| 41 |
/** Whether the list of options in the Combobox popup is hidden. */ |
| 42 |
let hidden = $state(true); |
| 43 |
/** The index of the option that is currently focused */ |
| 44 |
let index = $state(options.indexOf(value)); |
| 45 |
/** The popup list element. */ |
| 46 |
let ul: HTMLUListElement | undefined = $state(); |
| 47 |
|
| 48 |
id += 1; |
| 49 |
const listbox_id = `combobox-listbox-${id.toString()}`; |
| 50 |
|
| 51 |
const SEPARATOR = ","; |
| 52 |
let values = $derived(value.split(SEPARATOR)); |
| 53 |
|
| 54 |
// Scroll focused element into view. |
| 55 |
$effect(() => { |
| 56 |
if (!hidden && index) { |
| 57 |
ul?.children[index]?.scrollIntoView({ |
| 58 |
block: "nearest", |
| 59 |
inline: "nearest", |
| 60 |
}); |
| 61 |
} |
| 62 |
}); |
| 63 |
|
| 64 |
/** The various actions that can be triggered by user mouse or key events. */ |
| 65 |
const actions = { |
| 66 |
/** Close the popup list. */ |
| 67 |
close: () => { |
| 68 |
hidden = true; |
| 69 |
}, |
| 70 |
/** Find the first element matching the typed letter and focus it. */ |
| 71 |
find_letter: (key: string, event: KeyboardEvent) => { |
| 72 |
const match = options.findIndex((o) => o.toLowerCase().startsWith(key)); |
| 73 |
if (match > -1) { |
| 74 |
event.stopPropagation(); |
| 75 |
index = match; |
| 76 |
hidden = false; |
| 77 |
} |
| 78 |
}, |
| 79 |
/** Focus the first element and open if not open yet. */ |
| 80 |
first: () => { |
| 81 |
index = 0; |
| 82 |
hidden = false; |
| 83 |
}, |
| 84 |
/** Focus the last element and open if not open yet. */ |
| 85 |
last: () => { |
| 86 |
index = 0; |
| 87 |
hidden = false; |
| 88 |
}, |
| 89 |
/** Focus the previous element in the popup. */ |
| 90 |
next: () => { |
| 91 |
index = index === 0 ? options.length - 1 : index - 1; |
| 92 |
}, |
| 93 |
/** Open the popup list. */ |
| 94 |
open: () => { |
| 95 |
hidden = false; |
| 96 |
}, |
| 97 |
/** Focus the previous element in the popup. */ |
| 98 |
previous: () => { |
| 99 |
index = index === options.length - 1 ? 0 : index + 1; |
| 100 |
}, |
| 101 |
/** Select the given or the focused element in the options list. */ |
| 102 |
select: (o?: string) => { |
| 103 |
const option = o ?? options[index]; |
| 104 |
if (option != null) { |
| 105 |
if ( |
| 106 |
multiple_select != null && |
| 107 |
multiple_select(option) && |
| 108 |
values.every(multiple_select) |
| 109 |
) { |
| 110 |
value = values.includes(option) |
| 111 |
? values.filter((v) => v !== option).join(SEPARATOR) |
| 112 |
: [...values, option].join(SEPARATOR); |
| 113 |
} else { |
| 114 |
value = option; |
| 115 |
} |
| 116 |
index = options.indexOf(option); |
| 117 |
hidden = true; |
| 118 |
} |
| 119 |
}, |
| 120 |
/** Toggle the popup list. */ |
| 121 |
toggle: () => { |
| 122 |
hidden = !hidden; |
| 123 |
}, |
| 124 |
}; |
| 125 |
|
| 126 |
/** Get the action for a keyboard event. */ |
| 127 |
function key_action(event: KeyboardEvent): (() => void) | null { |
| 128 |
const { key } = event; |
| 129 |
const modifier = event.altKey || event.ctrlKey || event.metaKey; |
| 130 |
|
| 131 |
if (/^[\w]$/.exec(key) && !modifier) { |
| 132 |
return actions.find_letter.bind(null, key, event); |
| 133 |
} |
| 134 |
if (key === "Home") { |
| 135 |
return actions.first; |
| 136 |
} |
| 137 |
if (key === "End") { |
| 138 |
return actions.last; |
| 139 |
} |
| 140 |
if ((key === "Enter" || key === " ") && !hidden) { |
| 141 |
return actions.select; |
| 142 |
} |
| 143 |
if (key === "Escape" && !hidden) { |
| 144 |
return actions.close; |
| 145 |
} |
| 146 |
if (key === "ArrowUp") { |
| 147 |
return hidden ? actions.open : actions.next; |
| 148 |
} |
| 149 |
if (key === "ArrowDown") { |
| 150 |
return hidden ? actions.open : actions.previous; |
| 151 |
} |
| 152 |
return null; |
| 153 |
} |
| 154 |
</script> |
| 155 |
|
| 156 |
<span> |
| 157 |
<button |
| 158 |
type="button" |
| 159 |
role="combobox" |
| 160 |
aria-expanded={!hidden} |
| 161 |
aria-controls={listbox_id} |
| 162 |
class="muted" |
| 163 |
onclick={actions.toggle} |
| 164 |
onblur={actions.close} |
| 165 |
onkeydown={(event) => { |
| 166 |
const action = key_action(event); |
| 167 |
if (action) { |
| 168 |
event.preventDefault(); |
| 169 |
action(); |
| 170 |
} |
| 171 |
}} |
| 172 |
> |
| 173 |
{description(value)} |
| 174 |
</button> |
| 175 |
<ul {hidden} role="listbox" id={listbox_id} bind:this={ul}> |
| 176 |
{#each options as option, i} |
| 177 |
<li |
| 178 |
role="option" |
| 179 |
aria-selected={values.includes(option)} |
| 180 |
class:current={i === index} |
| 181 |
onmousedown={(ev) => { |
| 182 |
if (ev.button === 0) { |
| 183 |
actions.select(option); |
| 184 |
} |
| 185 |
}} |
| 186 |
> |
| 187 |
{description(option)} |
| 188 |
</li> |
| 189 |
{/each} |
| 190 |
</ul> |
| 191 |
</span> |
| 192 |
|
| 193 |
<style> |
| 194 |
span { |
| 195 |
position: relative; |
| 196 |
display: inline-block; |
| 197 |
} |
| 198 |
|
| 199 |
ul { |
| 200 |
position: absolute; |
| 201 |
z-index: var(--z-index-autocomplete); |
| 202 |
overflow: auto; |
| 203 |
background-color: var(--background); |
| 204 |
border: 1px solid var(--border-darker); |
| 205 |
box-shadow: var(--box-shadow-dropdown); |
| 206 |
} |
| 207 |
|
| 208 |
li { |
| 209 |
padding: 2px 5px; |
| 210 |
white-space: nowrap; |
| 211 |
cursor: pointer; |
| 212 |
} |
| 213 |
|
| 214 |
li.current { |
| 215 |
padding: 0 3px; |
| 216 |
border: var(--link-color) dotted 2px; |
| 217 |
} |
| 218 |
|
| 219 |
li[aria-selected="true"], |
| 220 |
li:hover { |
| 221 |
color: var(--background); |
| 222 |
background-color: var(--link-color); |
| 223 |
} |
| 224 |
|
| 225 |
@media print { |
| 226 |
span { |
| 227 |
display: none; |
| 228 |
} |
| 229 |
} |
| 230 |
</style> |