| 47 | * Borrowed from this great post! https://bitsofco.de/accessible-modal-dialog/ |
| 48 | */ |
| 49 | const handleKeyDown = (e: KeyboardEvent) => { |
| 50 | const { tour } = step; |
| 51 | switch (e.keyCode) { |
| 52 | case KEY_TAB: |
| 53 | if ( |
| 54 | (!focusableAttachToElements || |
| 55 | focusableAttachToElements.length === 0) && |
| 56 | focusableDialogElements && |
| 57 | focusableDialogElements.length === 0 |
| 58 | ) { |
| 59 | e.preventDefault(); |
| 60 | break; |
| 61 | } |
| 62 | // Backward tab |
| 63 | if (e.shiftKey) { |
| 64 | if ( |
| 65 | document.activeElement === firstFocusableDialogElement || |
| 66 | document.activeElement?.classList.contains('shepherd-element') |
| 67 | ) { |
| 68 | e.preventDefault(); |
| 69 | ( |
| 70 | (lastFocusableAttachToElement ?? |
| 71 | lastFocusableDialogElement) as HTMLElement |
| 72 | )?.focus(); |
| 73 | } else if (document.activeElement === firstFocusableAttachToElement) { |
| 74 | e.preventDefault(); |
| 75 | (lastFocusableDialogElement as HTMLElement)?.focus(); |
| 76 | } |
| 77 | } else { |
| 78 | if (document.activeElement === lastFocusableDialogElement) { |
| 79 | e.preventDefault(); |
| 80 | ( |
| 81 | (firstFocusableAttachToElement ?? |
| 82 | firstFocusableDialogElement) as HTMLElement |
| 83 | )?.focus(); |
| 84 | } else if (document.activeElement === lastFocusableAttachToElement) { |
| 85 | e.preventDefault(); |
| 86 | (firstFocusableDialogElement as HTMLElement)?.focus(); |
| 87 | } |
| 88 | } |
| 89 | break; |
| 90 | case KEY_ESC: |
| 91 | if (tour.options.exitOnEsc) { |
| 92 | e.preventDefault(); |
| 93 | e.stopPropagation(); |
| 94 | step.cancel(); |
| 95 | } |
| 96 | break; |
| 97 | case LEFT_ARROW: |
| 98 | if (tour.options.keyboardNavigation) { |
| 99 | e.preventDefault(); |
| 100 | e.stopPropagation(); |
| 101 | tour.back(); |
| 102 | } |
| 103 | break; |
| 104 | case RIGHT_ARROW: |
| 105 | if (tour.options.keyboardNavigation) { |
| 106 | e.preventDefault(); |