|
| 1 | +```ts |
| 2 | +import { Component, ElementRef, ViewChild, OnInit } from '@angular/core'; |
| 3 | +import { IonHeader, IonToolbar, IonTitle, IonContent, IonButton, IonModal, IonLabel } from '@ionic/angular/standalone'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + selector: 'app-example', |
| 7 | + templateUrl: 'example.component.html', |
| 8 | + standalone: true, |
| 9 | + imports: [IonHeader, IonToolbar, IonTitle, IonContent, IonButton, IonModal, IonLabel], |
| 10 | +}) |
| 11 | +export class ExampleComponent implements OnInit { |
| 12 | + @ViewChild('modal', { static: true }) modal!: IonModal; |
| 13 | + @ViewChild('appPage', { static: true }) appPage!: ElementRef<HTMLDivElement>; |
| 14 | + |
| 15 | + presentingElement: HTMLElement | undefined; |
| 16 | + |
| 17 | + private readonly DARKEST_PERCENT = 50; |
| 18 | + private readonly BRIGHTNESS_RANGE = 100 - this.DARKEST_PERCENT; |
| 19 | + |
| 20 | + ngOnInit() { |
| 21 | + this.presentingElement = this.appPage.nativeElement; |
| 22 | + } |
| 23 | + |
| 24 | + onModalWillPresent() { |
| 25 | + const appEl = this.appPage.nativeElement; |
| 26 | + |
| 27 | + appEl.style.transition = 'filter 0.4s ease'; |
| 28 | + // Set to max darkness immediately |
| 29 | + appEl.style.setProperty('filter', `brightness(${this.DARKEST_PERCENT}%)`, 'important'); |
| 30 | + } |
| 31 | + |
| 32 | + onDragStart() { |
| 33 | + const appEl = this.appPage.nativeElement; |
| 34 | + |
| 35 | + // Ensure transitions are off during the active drag |
| 36 | + appEl.style.transition = 'none'; |
| 37 | + } |
| 38 | + |
| 39 | + onDragMove(event: any) { |
| 40 | + // `progress` is a value from 1 (top) to 0 (bottom) |
| 41 | + const { progress } = event.detail; |
| 42 | + |
| 43 | + const appEl = this.appPage.nativeElement; |
| 44 | + /** |
| 45 | + * Calculate the current brightness based on how far the user has |
| 46 | + * dragged. |
| 47 | + * |
| 48 | + * When dragging up, the background should become darker, |
| 49 | + * and when dragging down, it should become lighter. |
| 50 | + */ |
| 51 | + const brightnessValue = 100 - progress * this.BRIGHTNESS_RANGE; |
| 52 | + |
| 53 | + // Update the brightness in real-time as the user drags |
| 54 | + appEl.style.setProperty('filter', `brightness(${brightnessValue}%)`, 'important'); |
| 55 | + } |
| 56 | + |
| 57 | + onDragEnd(event: any) { |
| 58 | + // `progress` is a value from 1 (top) to 0 (bottom) |
| 59 | + const { progress } = event.detail; |
| 60 | + |
| 61 | + const appEl = this.appPage.nativeElement; |
| 62 | + /** |
| 63 | + * Snap the background brightness based on the user's drag intent. |
| 64 | + * Progress > 0.4 implies an intent to open (snap dark), |
| 65 | + * while < 0.4 implies a dismissal (snap bright). |
| 66 | + */ |
| 67 | + const brightnessValue = progress > 0.4 ? this.DARKEST_PERCENT : 100; |
| 68 | + |
| 69 | + // Reset to max darkness on snap-back for a nice visual effect |
| 70 | + appEl.style.setProperty('filter', `brightness(${brightnessValue}%)`, 'important'); |
| 71 | + |
| 72 | + // Re-enable transition for a smooth snap-back |
| 73 | + appEl.style.transition = 'filter 0.4s ease'; |
| 74 | + } |
| 75 | + |
| 76 | + onModalWillDismiss() { |
| 77 | + const appEl = this.appPage.nativeElement; |
| 78 | + |
| 79 | + // Clean up styles when the modal is dismissed |
| 80 | + appEl.style.removeProperty('filter'); |
| 81 | + appEl.style.removeProperty('transition'); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
0 commit comments