(ev: KeyboardEvent)
| 403 | |
| 404 | export const configureKeyboardInteraction = (popoverEl: HTMLIonPopoverElement) => { |
| 405 | const callback = async (ev: KeyboardEvent) => { |
| 406 | const activeElement = document.activeElement as HTMLElement | null; |
| 407 | let items: HTMLIonItemElement[] = []; |
| 408 | |
| 409 | const targetTagName = (ev.target as HTMLElement)?.tagName; |
| 410 | /** |
| 411 | * Only handle custom keyboard interactions for the host popover element |
| 412 | * and children ion-item elements. |
| 413 | */ |
| 414 | if (targetTagName !== 'ION-POPOVER' && targetTagName !== 'ION-ITEM') { |
| 415 | return; |
| 416 | } |
| 417 | /** |
| 418 | * Complex selectors with :not() are :not supported |
| 419 | * in older versions of Chromium so we need to do a |
| 420 | * try/catch here so errors are not thrown. |
| 421 | */ |
| 422 | try { |
| 423 | /** |
| 424 | * Select all ion-items that are not children of child popovers. |
| 425 | * i.e. only select ion-item elements that are part of this popover |
| 426 | */ |
| 427 | items = Array.from( |
| 428 | popoverEl.querySelectorAll( |
| 429 | 'ion-item:not(ion-popover ion-popover *):not([disabled])' |
| 430 | ) as NodeListOf<HTMLIonItemElement> |
| 431 | ); |
| 432 | /* eslint-disable-next-line */ |
| 433 | } catch {} |
| 434 | |
| 435 | switch (ev.key) { |
| 436 | /** |
| 437 | * If we are in a child popover |
| 438 | * then pressing the left arrow key |
| 439 | * should close this popover and move |
| 440 | * focus to the popover that presented |
| 441 | * this one. |
| 442 | */ |
| 443 | case 'ArrowLeft': |
| 444 | const parentPopover = await popoverEl.getParentPopover(); |
| 445 | if (parentPopover) { |
| 446 | popoverEl.dismiss(undefined, undefined, false); |
| 447 | } |
| 448 | break; |
| 449 | /** |
| 450 | * ArrowDown should move focus to the next focusable ion-item. |
| 451 | */ |
| 452 | case 'ArrowDown': |
| 453 | // Disable movement/scroll with keyboard |
| 454 | ev.preventDefault(); |
| 455 | const nextItem = getNextItem(items, activeElement); |
| 456 | if (nextItem !== undefined) { |
| 457 | focusItem(nextItem); |
| 458 | } |
| 459 | break; |
| 460 | /** |
| 461 | * ArrowUp should move focus to the previous focusable ion-item. |
| 462 | */ |
no test coverage detected