|
| 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</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@8/dist/ionic/ionic.esm.js"></script> |
| 10 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@8/css/ionic.bundle.css" /> |
| 11 | + </head> |
| 12 | + |
| 13 | + <body> |
| 14 | + <ion-app> |
| 15 | + <ion-header> |
| 16 | + <ion-toolbar> |
| 17 | + <ion-title>App</ion-title> |
| 18 | + </ion-toolbar> |
| 19 | + </ion-header> |
| 20 | + <ion-content class="ion-padding"> |
| 21 | + <ion-button id="open-modal" expand="block">Open Sheet Modal</ion-button> |
| 22 | + |
| 23 | + <ion-modal trigger="open-modal" initial-breakpoint="0.25"> |
| 24 | + <ion-content class="ion-padding"> |
| 25 | + <div class="ion-margin-top"> |
| 26 | + <ion-label>Drag the handle to adjust the header's visibility.</ion-label> |
| 27 | + </div> |
| 28 | + </ion-content> |
| 29 | + </ion-modal> |
| 30 | + </ion-content> |
| 31 | + </ion-app> |
| 32 | + |
| 33 | + <script> |
| 34 | + const modal = document.querySelector('ion-modal'); |
| 35 | + const header = document.querySelector('ion-header'); |
| 36 | + // Assign the current snap breakpoint to the initial breakpoint so |
| 37 | + // that we can track changes during the drag |
| 38 | + let currentSnap = 0.25; |
| 39 | + modal.breakpoints = [0, 0.25, 0.5, 0.75, 1]; |
| 40 | + |
| 41 | + modal.addEventListener('ionDragMove', (event) => { |
| 42 | + // `progress` is a value from 1 (top) to 0 (bottom) |
| 43 | + const { progress, snapBreakpoint } = event.detail; |
| 44 | + |
| 45 | + if (currentSnap !== snapBreakpoint) { |
| 46 | + currentSnap = snapBreakpoint; |
| 47 | + |
| 48 | + console.log('Current snap breakpoint:', snapBreakpoint); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Inverse relationship: |
| 53 | + * 1.0 progress = 0 opacity |
| 54 | + * 0 progress = 1.0 opacity |
| 55 | + */ |
| 56 | + const currentOpacity = 1 - progress; |
| 57 | + |
| 58 | + header.style.opacity = currentOpacity; |
| 59 | + }); |
| 60 | + |
| 61 | + modal.addEventListener('ionDragEnd', (event) => { |
| 62 | + // `progress` is a value from 1 (top) to 0 (bottom) |
| 63 | + // `snapBreakpoint` tells us which snap point the modal will animate to after the drag ends |
| 64 | + const { progress, snapBreakpoint } = event.detail; |
| 65 | + |
| 66 | + /** |
| 67 | + * If the modal is snapping to the closed state (0), reset the |
| 68 | + * styles. |
| 69 | + */ |
| 70 | + if (snapBreakpoint === 0) { |
| 71 | + header.style.removeProperty('opacity'); |
| 72 | + header.style.removeProperty('transition'); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Smooth transition to the final resting opacity |
| 77 | + header.style.transition = 'opacity 0.4s ease'; |
| 78 | + // The final opacity matches the inverse of the resting progress |
| 79 | + header.style.opacity = 1 - progress; |
| 80 | + }); |
| 81 | + |
| 82 | + /** |
| 83 | + * If the user dismisses the modal (e.g. tapping the backdrop), |
| 84 | + * reset the styles. |
| 85 | + */ |
| 86 | + modal.addEventListener('willDismiss', (event) => { |
| 87 | + // Reset styles when the modal is dismissed |
| 88 | + header.style.removeProperty('opacity'); |
| 89 | + header.style.removeProperty('transition'); |
| 90 | + }); |
| 91 | + </script> |
| 92 | + </body> |
| 93 | +</html> |
0 commit comments