|
13 | 13 | <body> |
14 | 14 | <ion-app> |
15 | 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 Card Modal</ion-button> |
| 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 Card Modal</ion-button> |
23 | 23 |
|
24 | | - <ion-modal trigger="open-modal"> |
25 | | - <ion-header> |
26 | | - <ion-toolbar> |
27 | | - <ion-title>Modal</ion-title> |
28 | | - </ion-toolbar> |
29 | | - </ion-header> |
30 | | - <ion-content class="ion-padding"> |
31 | | - <div class="ion-margin-top"> |
32 | | - <ion-label>Drag the handle to adjust the background brightness based on a custom brightness.</ion-label> |
33 | | - </div> |
34 | | - </ion-content> |
35 | | - </ion-modal> |
36 | | - </ion-content> |
37 | | -</div> |
| 24 | + <ion-modal trigger="open-modal"> |
| 25 | + <ion-header> |
| 26 | + <ion-toolbar> |
| 27 | + <ion-title>Modal</ion-title> |
| 28 | + </ion-toolbar> |
| 29 | + </ion-header> |
| 30 | + <ion-content class="ion-padding"> |
| 31 | + <div class="ion-margin-top"> |
| 32 | + <ion-label>Drag the handle to adjust the background brightness based on a custom brightness.</ion-label> |
| 33 | + </div> |
| 34 | + </ion-content> |
| 35 | + </ion-modal> |
| 36 | + </ion-content> |
| 37 | + </div> |
38 | 38 | </ion-app> |
39 | 39 |
|
40 | 40 | <script> |
41 | | - const modal = document.querySelector('ion-modal'); |
42 | | - const presentingElement = document.querySelector('.ion-page'); |
| 41 | + const modal = document.querySelector('ion-modal'); |
| 42 | + const presentingElement = document.querySelector('.ion-page'); |
43 | 43 |
|
44 | | - modal.presentingElement = presentingElement; |
| 44 | + modal.presentingElement = presentingElement; |
45 | 45 |
|
46 | | - const DARKEST_PERCENT = 50; |
47 | | - const BRIGHTNESS_RANGE = 100 - DARKEST_PERCENT; |
| 46 | + const DARKEST_PERCENT = 50; |
| 47 | + const BRIGHTNESS_RANGE = 100 - DARKEST_PERCENT; |
48 | 48 |
|
49 | | - modal.addEventListener('ionModalWillPresent', () => { |
50 | | - presentingElement.style.transition = 'filter 0.4s ease'; |
51 | | - // Set to max darkness immediately |
52 | | - presentingElement.style.setProperty('filter', `brightness(${DARKEST_PERCENT}%)`, 'important'); |
53 | | - }); |
| 49 | + modal.addEventListener('ionModalWillPresent', () => { |
| 50 | + presentingElement.style.transition = 'filter 0.4s ease'; |
| 51 | + presentingElement.style.setProperty('filter', `brightness(${DARKEST_PERCENT}%)`, 'important'); |
| 52 | + }); |
54 | 53 |
|
55 | | - modal.addEventListener('ionDragStart', () => { |
56 | | - // Ensure transitions are off during the active drag |
57 | | - presentingElement.style.transition = 'none'; |
58 | | - }); |
| 54 | + modal.addEventListener('ionDragStart', () => { |
| 55 | + presentingElement.style.transition = 'none'; |
| 56 | + }); |
59 | 57 |
|
60 | | - modal.addEventListener('ionDragMove', (event) => { |
61 | | - // `progress` is a value from 1 (top) to 0 (bottom) |
62 | | - const { progress } = event.detail; |
| 58 | + modal.addEventListener('ionDragMove', (event) => { |
| 59 | + const { progress } = event.detail; |
| 60 | + const brightnessValue = 100 - progress * BRIGHTNESS_RANGE; |
| 61 | + presentingElement.style.setProperty('filter', `brightness(${brightnessValue}%)`, 'important'); |
| 62 | + }); |
63 | 63 |
|
64 | | - /** |
65 | | - * Calculate the current brightness based on how far the user has |
66 | | - * dragged. |
67 | | - * |
68 | | - * When dragging up, the background should become darker, |
69 | | - * and when dragging down, it should become lighter. |
70 | | - */ |
71 | | - const brightnessValue = 100 - progress * BRIGHTNESS_RANGE; |
| 64 | + modal.addEventListener('ionDragEnd', (event) => { |
| 65 | + const { progress } = event.detail; |
| 66 | + const brightnessValue = progress > 0.4 ? DARKEST_PERCENT : 100; |
72 | 67 |
|
73 | | - // Update the brightness in real-time as the user drags |
74 | | - presentingElement.style.setProperty('filter', `brightness(${brightnessValue}%)`, 'important'); |
75 | | - }); |
| 68 | + presentingElement.style.setProperty('filter', `brightness(${brightnessValue}%)`, 'important'); |
| 69 | + presentingElement.style.transition = 'filter 0.4s ease'; |
| 70 | + }); |
76 | 71 |
|
77 | | - modal.addEventListener('ionDragEnd', (event) => { |
78 | | - // `progress` is a value from 1 (top) to 0 (bottom) |
79 | | - const { progress } = event.detail; |
80 | | - /** |
81 | | - * Snap the background brightness based on the user's drag intent. |
82 | | - * Progress > 0.4 implies an intent to open (snap dark), |
83 | | - * while < 0.4 implies a dismissal (snap bright). |
84 | | - */ |
85 | | - const brightnessValue = progress > 0.4 ? DARKEST_PERCENT : 100; |
86 | | - |
87 | | - // Reset to max darkness on snap-back for a nice visual effect |
88 | | - presentingElement.style.setProperty('filter', `brightness(${brightnessValue}%)`, 'important'); |
89 | | - |
90 | | - // Re-enable transition for a smooth snap-back |
91 | | - presentingElement.style.transition = 'filter 0.4s ease'; |
92 | | - }); |
93 | | - |
94 | | - modal.addEventListener('ionModalWillDismiss', () => { |
95 | | - // Clean up styles when the modal is dismissed |
96 | | - presentingElement.style.removeProperty('filter'); |
97 | | - presentingElement.style.removeProperty('transition'); |
98 | | - }); |
99 | | -</script> |
| 72 | + modal.addEventListener('ionModalWillDismiss', () => { |
| 73 | + presentingElement.style.removeProperty('filter'); |
| 74 | + presentingElement.style.removeProperty('transition'); |
| 75 | + }); |
| 76 | + </script> |
100 | 77 | </body> |
101 | 78 | </html> |
0 commit comments