|
45 | 45 | <ion-button id="create-and-present-toast" onclick="createAndPresentToast()" |
46 | 46 | >Create and Present Toast</ion-button |
47 | 47 | > |
| 48 | + <ion-button id="create-and-present-focus-trap" onclick="createAndPresentFocusTrapModal()" |
| 49 | + >Create and Present Focus Trap Modal</ion-button |
| 50 | + > |
48 | 51 | </ion-content> |
49 | 52 | </div> |
50 | 53 | </ion-app> |
|
128 | 131 | await toast.present(); |
129 | 132 | }; |
130 | 133 |
|
| 134 | + /* |
| 135 | + Presents a modal for checking the focus trap's redirect by hand. An item |
| 136 | + holding a single checkbox is focusable without being clickable, so the |
| 137 | + trap resolves to the item itself and draws the indicator on it. |
| 138 | +
|
| 139 | + Moving focus to an element outside the modal makes the trap redirect |
| 140 | + focus back in. The indicator should only follow that redirect when the |
| 141 | + interaction was keyboard driven. |
| 142 | + */ |
| 143 | + const createAndPresentFocusTrapModal = async () => { |
| 144 | + const div = document.createElement('div'); |
| 145 | + div.innerHTML = ` |
| 146 | + <ion-content class="ion-padding"> |
| 147 | + <ion-list> |
| 148 | + <ion-item> |
| 149 | + <ion-checkbox>Dark Mode</ion-checkbox> |
| 150 | + </ion-item> |
| 151 | + </ion-list> |
| 152 | +
|
| 153 | + <ion-button id="modal-move-focus">Move focus outside</ion-button> |
| 154 | +
|
| 155 | + <p> |
| 156 | + Pointer case: click the button above. Keyboard case: press Tab twice to |
| 157 | + reach it, then Enter. A third Tab escapes the modal, so the count |
| 158 | + matters. |
| 159 | + </p> |
| 160 | +
|
| 161 | + <p> |
| 162 | + Only the keyboard case should leave the indicator on the Dark Mode row. |
| 163 | + </p> |
| 164 | + </ion-content> |
| 165 | + `; |
| 166 | + |
| 167 | + const moveFocusButton = div.querySelector('ion-button#modal-move-focus'); |
| 168 | + moveFocusButton.onclick = () => { |
| 169 | + document.querySelector('#root-input').setFocus(); |
| 170 | + }; |
| 171 | + |
| 172 | + const modal = await modalController.create({ |
| 173 | + component: div, |
| 174 | + }); |
| 175 | + |
| 176 | + await modal.present(); |
| 177 | + |
| 178 | + return modal; |
| 179 | + }; |
| 180 | + |
131 | 181 | const createNestedOverlayModal = async () => { |
132 | 182 | const div = document.createElement('div'); |
133 | 183 | div.innerHTML = ` |
|
0 commit comments