|
19 | 19 | import type { MenuOption } from '$lib/types/options';
|
20 | 20 | import type { ScrollIntoViewOptions } from '$lib/actions';
|
21 | 21 |
|
| 22 | + type LogReason<T extends Event = any> = { |
| 23 | + reason: string, |
| 24 | + event?: T |
| 25 | + } |
| 26 | +
|
22 | 27 | const dispatch = createEventDispatcher<{
|
23 | 28 | change: { value: any; option: any };
|
24 | 29 | inputChange: string;
|
|
51 | 56 | : undefined;
|
52 | 57 |
|
53 | 58 | let originalIcon = icon;
|
| 59 | + let toggleButtonElement: ComponentProps<Button>['element'] = undefined; |
| 60 | + let toggleButtonIconSpan: ComponentProps<Button>['iconElement'] = undefined; |
54 | 61 |
|
55 | 62 | export let scrollIntoView: Partial<ScrollIntoViewOptions> = {};
|
56 | 63 |
|
57 | 64 | export let classes: {
|
58 | 65 | root?: string;
|
59 | 66 | field?: string | ComponentProps<TextField>['classes'];
|
60 | 67 | options?: string;
|
61 |
| - option?: string; |
| 68 | + option?: string | ComponentProps<MenuItem>['classes']; |
62 | 69 | selected?: string;
|
63 | 70 | group?: string;
|
64 | 71 | empty?: string;
|
|
68 | 75 | let fieldClasses: ComponentProps<TextField>['classes'];
|
69 | 76 | $: fieldClasses = typeof(classes.field) === "string" ? { root: classes.field } : classes.field;
|
70 | 77 |
|
| 78 | + let optionClasses: ComponentProps<MenuItem>['classes']; |
| 79 | + $: optionClasses = typeof(classes.option) === "string" ? { root: classes.option } : classes.option; |
| 80 | +
|
71 | 81 | // Menu props
|
72 | 82 | export let placement: Placement = 'bottom-start';
|
73 | 83 | export let autoPlacement = true;
|
|
194 | 204 | });
|
195 | 205 | }
|
196 | 206 |
|
| 207 | + function isToggleButtonClicked(ev: MouseEvent) { |
| 208 | + return toggleButtonIconSpan && toggleButtonIconSpan === ev.target; |
| 209 | + } |
| 210 | +
|
| 211 | + function isToggleButtonRelated(ev: MouseEvent|FocusEvent) { |
| 212 | + return toggleButtonElement && toggleButtonElement === ev.relatedTarget; |
| 213 | + } |
| 214 | +
|
197 | 215 | function onChange(e: ComponentEvents<TextField>['change']) {
|
198 | 216 | logger.debug('onChange');
|
199 | 217 |
|
200 | 218 | searchText = e.detail.inputValue as string;
|
201 | 219 | dispatch('inputChange', searchText);
|
202 |
| - show(); |
| 220 | + show({ reason: "onChange", event: e }); |
203 | 221 | }
|
204 | 222 |
|
205 |
| - function onFocus() { |
206 |
| - logger.debug('onFocus'); |
207 |
| - show(); |
| 223 | + function onFocus(event: FocusEvent) { |
| 224 | + if (isToggleButtonRelated(event)) { |
| 225 | + return; |
| 226 | + } |
| 227 | + show({ reason: "onFocus", event }); |
208 | 228 | }
|
209 | 229 |
|
210 | 230 | function onBlur(e: FocusEvent|CustomEvent<any>) {
|
|
216 | 236 | fe.relatedTarget instanceof HTMLElement &&
|
217 | 237 | !menuOptionsEl?.contains(fe.relatedTarget) && // TODO: Oddly Safari does not set `relatedTarget` to the clicked on menu option (like Chrome and Firefox) but instead appears to take `tabindex` into consideration. Currently resolves to `.options` after setting `tabindex="-1"
|
218 | 238 | fe.relatedTarget !== menuOptionsEl?.offsetParent && // click on scroll bar
|
219 |
| - !fe.relatedTarget.closest('menu > [slot=actions]') // click on action item |
| 239 | + !fe.relatedTarget.closest('menu > [slot=actions]') && // click on action item |
| 240 | + !isToggleButtonRelated(fe) // click on toggle button |
220 | 241 | ) {
|
221 |
| - hide('blur'); |
| 242 | + hide({ reason: 'blur', event: e }); |
222 | 243 | } else {
|
223 | 244 | logger.debug('ignoring blur');
|
224 | 245 | }
|
|
237 | 258 | break;
|
238 | 259 |
|
239 | 260 | case 'ArrowDown':
|
240 |
| - show(); |
| 261 | + show({ reason: `onKeyDown: '${e.key}'`, event: e }); |
241 | 262 | if (highlightIndex < filteredOptions.length - 1) {
|
242 | 263 | highlightIndex++;
|
243 | 264 | } else {
|
|
247 | 268 | break;
|
248 | 269 |
|
249 | 270 | case 'ArrowUp':
|
250 |
| - show(); |
| 271 | + show({ reason: `onKeyDown: '${e.key}'`, event: e }); |
251 | 272 | if (highlightIndex > 0) {
|
252 | 273 | highlightIndex--;
|
253 | 274 | } else {
|
|
259 | 280 | case 'Escape':
|
260 | 281 | if (open) {
|
261 | 282 | inputEl?.focus();
|
262 |
| - hide('escape'); |
| 283 | + hide({ reason: 'escape', event: e }); |
263 | 284 | }
|
264 | 285 | break;
|
265 | 286 | }
|
|
274 | 295 | }
|
275 | 296 | }
|
276 | 297 |
|
277 |
| - function onClick() { |
278 |
| - logger.debug('onClick'); |
279 |
| - show(); |
| 298 | + function onClick(event: MouseEvent) { |
| 299 | + if (isToggleButtonClicked(event) || isToggleButtonRelated(event)) { |
| 300 | + return; |
| 301 | + } |
| 302 | + show({ reason: 'onClick', event }); |
280 | 303 | }
|
281 | 304 |
|
282 |
| - function show() { |
283 |
| - logger.debug('show'); |
| 305 | + function show<T extends LogReason = any>(reason: string|T = '') { |
| 306 | + const doShow = !disabled && !readonly; |
| 307 | + logger.debug('show', { ...(typeof(reason) === "string" ? { reason } : reason), openBefore: open, openAfter: doShow }); |
284 | 308 |
|
285 |
| - if (!disabled && !readonly) { |
| 309 | + if (doShow) { |
286 | 310 | if (open === false && clearSearchOnOpen) {
|
287 | 311 | searchText = ''; // Show all options on open
|
288 | 312 | }
|
|
291 | 315 | }
|
292 | 316 | }
|
293 | 317 |
|
294 |
| - function hide(reason = '') { |
295 |
| - logger.debug('hide', { reason }); |
| 318 | + function hide<T extends LogReason = any>(reason: string|T = '') { |
| 319 | + logger.debug('hide', { ...(typeof(reason) === "string" ? { reason } : reason), openBefore: open, openAfter: false }); |
296 | 320 | open = false;
|
297 | 321 | highlightIndex = -1;
|
298 | 322 | }
|
|
384 | 408 | on:keydown={onKeyDown}
|
385 | 409 | on:keypress={onKeyPress}
|
386 | 410 | actions={fieldActions}
|
387 |
| - classes={{ container: inlineOptions ? 'border-none shadow-none hover:shadow-none group-focus-within:shadow-none' : undefined }} |
388 |
| - class={cls('h-full', theme.field, fieldClasses)} |
| 411 | + classes={{ ...(fieldClasses ?? {}), container: inlineOptions ? 'border-none shadow-none hover:shadow-none group-focus-within:shadow-none' : undefined }} |
| 412 | + class={cls('h-full', theme.field)} |
389 | 413 | role="combobox"
|
390 | 414 | aria-expanded={open ? "true" : "false"}
|
391 | 415 | aria-autocomplete={!inlineOptions ? "list" : undefined}
|
|
417 | 441 | icon={toggleIcon}
|
418 | 442 | class="text-black/50 p-1 transform {open ? 'rotate-180' : ''}"
|
419 | 443 | tabindex="-1"
|
420 |
| - on:click={() => {logger.debug("toggleIcon clicked")}} |
| 444 | + bind:element={toggleButtonElement} |
| 445 | + bind:iconElement={toggleButtonIconSpan} |
| 446 | + on:click={(e) => { |
| 447 | + logger.debug("toggleIcon clicked", { event: e, open }) |
| 448 | + const func = !open ? show : hide; |
| 449 | + func({ reason: "toggleIcon", event: e }); |
| 450 | + }} |
421 | 451 | />
|
422 | 452 | {/if}
|
423 | 453 | </span>
|
|
434 | 464 | {disableTransition}
|
435 | 465 | moveFocus={false}
|
436 | 466 | bind:open
|
437 |
| - on:close={() => hide('menu on:close')} |
| 467 | + on:close={e => hide({ reason: 'menu on:close', event: e})} |
438 | 468 | {...menuProps}
|
439 | 469 | >
|
440 | 470 | <!-- TODO: Rework into hierarchy of snippets in v2.0 -->
|
|
447 | 477 | <svelte:fragment slot="option" let:option let:index>
|
448 | 478 | <slot name="option" {option} {index} {selected} {value} {highlightIndex}>
|
449 | 479 | <MenuItem
|
| 480 | + classes={optionClasses} |
450 | 481 | class={cls(
|
451 | 482 | index === highlightIndex && '[:not(.group:hover)>&]:bg-black/5',
|
452 | 483 | option === selected && (classes.selected || 'font-semibold'),
|
453 | 484 | option.group ? 'px-4' : 'px-2',
|
454 | 485 | theme.option,
|
455 | 486 | classes.option
|
456 | 487 | )}
|
| 488 | + icon={option.icon} |
457 | 489 | scrollIntoView={{ condition: index === highlightIndex, onlyIfNeeded: inlineOptions, ...scrollIntoView }}
|
458 | 490 | role="option"
|
459 | 491 | aria-selected={option === selected ? "true" : "false"}
|
|
485 | 517 | <svelte:fragment slot="option" let:option let:index>
|
486 | 518 | <slot name="option" {option} {index} {selected} {value} {highlightIndex}>
|
487 | 519 | <MenuItem
|
| 520 | + classes={optionClasses} |
488 | 521 | class={cls(
|
489 | 522 | index === highlightIndex && '[:not(.group:hover)>&]:bg-black/5',
|
490 | 523 | option === selected && (classes.selected || 'font-semibold'),
|
491 | 524 | option.group ? 'px-4' : 'px-2',
|
492 | 525 | theme.option,
|
493 | 526 | classes.option
|
494 | 527 | )}
|
| 528 | + icon={option.icon} |
495 | 529 | scrollIntoView={{ condition: index === highlightIndex, onlyIfNeeded: inlineOptions, ...scrollIntoView }}
|
496 | 530 | role="option"
|
497 | 531 | aria-selected={option === selected ? "true" : "false"}
|
|
0 commit comments