|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>Modal | Can Dismiss</title> |
| 7 | + <link rel="stylesheet" href="../../../../common.css" /> |
| 8 | + <script src="../../../../common.js"></script> |
| 9 | + <script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@7/dist/ionic/ionic.esm.js"></script> |
| 10 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@7/css/ionic.bundle.css" /> |
| 11 | + </head> |
| 12 | + |
| 13 | + <body> |
| 14 | + <ion-app> |
| 15 | + <div class="ion-page"> |
| 16 | + <ion-header> |
| 17 | + <ion-toolbar> |
| 18 | + <ion-title>App</ion-title> |
| 19 | + </ion-toolbar> |
| 20 | + </ion-header> |
| 21 | + <ion-content class="ion-padding"> |
| 22 | + <ion-button id="open-modal" expand="block">Open</ion-button> |
| 23 | + |
| 24 | + <ion-modal trigger="open-modal"> |
| 25 | + <ion-header> |
| 26 | + <ion-toolbar> |
| 27 | + <ion-title>Modal</ion-title> |
| 28 | + <ion-buttons slot="end"> |
| 29 | + <ion-button onclick="dismiss()">Close</ion-button> |
| 30 | + </ion-buttons> |
| 31 | + </ion-toolbar> |
| 32 | + </ion-header> |
| 33 | + <ion-content> |
| 34 | + <ion-list> |
| 35 | + <ion-item> |
| 36 | + <ion-checkbox id="override-dismiss"> |
| 37 | + Override Dismiss<br /> |
| 38 | + <ion-note class="ion-text-wrap" |
| 39 | + >Toggle the checkbox to allow immediately dismissing the modal without a prompt.</ion-note |
| 40 | + > |
| 41 | + </ion-checkbox> |
| 42 | + </ion-item> |
| 43 | + </ion-list> |
| 44 | + </ion-content> |
| 45 | + </ion-modal> |
| 46 | + </ion-content> |
| 47 | + </div> |
| 48 | + </ion-app> |
| 49 | + |
| 50 | + <script> |
| 51 | + const modal = document.querySelector('ion-modal'); |
| 52 | + |
| 53 | + modal.canDismiss = promptClose; |
| 54 | + modal.presentingElement = document.querySelector('.ion-page'); |
| 55 | + |
| 56 | + let canDismissCheckbox = false; |
| 57 | + |
| 58 | + function dismiss() { |
| 59 | + modal.dismiss(); |
| 60 | + } |
| 61 | + const checkbox = document.querySelector('#override-dismiss'); |
| 62 | + checkbox.addEventListener('ionChange', (event) => { |
| 63 | + canDismissCheckbox = event.detail.checked; |
| 64 | + }); |
| 65 | + |
| 66 | + async function promptClose() { |
| 67 | + if (canDismissCheckbox) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + const actionSheet = document.createElement('ion-action-sheet'); |
| 71 | + |
| 72 | + actionSheet.header = 'Are you sure?'; |
| 73 | + actionSheet.buttons = [ |
| 74 | + { |
| 75 | + text: 'Yes', |
| 76 | + role: 'confirm', |
| 77 | + }, |
| 78 | + { |
| 79 | + text: 'No', |
| 80 | + role: 'cancel', |
| 81 | + }, |
| 82 | + ]; |
| 83 | + document.body.appendChild(actionSheet); |
| 84 | + await actionSheet.present(); |
| 85 | + |
| 86 | + const { role } = await actionSheet.onWillDismiss(); |
| 87 | + |
| 88 | + return role === 'confirm'; |
| 89 | + } |
| 90 | + </script> |
| 91 | + </body> |
| 92 | +</html> |
0 commit comments