|
| 1 | +import { createAnimation } from '@utils/animation/animation'; |
| 2 | +import { getElementRoot } from '@utils/helpers'; |
| 3 | + |
| 4 | +import type { Animation } from '../../../interface'; |
| 5 | +import { SwipeToCloseDefaults } from '../gestures/swipe-to-close'; |
| 6 | +import type { ModalAnimationOptions } from '../modal-interface'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Transition animation from mobile view to portrait view |
| 10 | + * This handles the case where a card modal is open in mobile view |
| 11 | + * and the user switches to portrait view |
| 12 | + */ |
| 13 | +export const mobileToPortraitTransition = ( |
| 14 | + baseEl: HTMLElement, |
| 15 | + opts: ModalAnimationOptions, |
| 16 | + duration = 300 |
| 17 | +): Animation => { |
| 18 | + const { presentingEl } = opts; |
| 19 | + |
| 20 | + if (!presentingEl) { |
| 21 | + // No transition needed for non-card modals |
| 22 | + return createAnimation('mobile-to-portrait-transition'); |
| 23 | + } |
| 24 | + |
| 25 | + const hasCardModal = |
| 26 | + presentingEl.tagName === 'ION-MODAL' && (presentingEl as HTMLIonModalElement).presentingElement !== undefined; |
| 27 | + const presentingElRoot = getElementRoot(presentingEl); |
| 28 | + const bodyEl = document.body; |
| 29 | + |
| 30 | + const baseAnimation = createAnimation('mobile-to-portrait-transition') |
| 31 | + .addElement(baseEl) |
| 32 | + .easing('cubic-bezier(0.32,0.72,0,1)') |
| 33 | + .duration(duration); |
| 34 | + |
| 35 | + const presentingAnimation = createAnimation(); |
| 36 | + |
| 37 | + if (!hasCardModal) { |
| 38 | + // Non-card modal: transition from mobile state to portrait state |
| 39 | + // Mobile: presentingEl has transform and body has black background |
| 40 | + // Portrait: no transform, no body background, modal wrapper opacity changes |
| 41 | + |
| 42 | + const root = getElementRoot(baseEl); |
| 43 | + const wrapperAnimation = createAnimation() |
| 44 | + .addElement(root.querySelectorAll('.modal-wrapper, .modal-shadow')!) |
| 45 | + .fromTo('opacity', '1', '1'); // Keep wrapper visible in portrait |
| 46 | + |
| 47 | + const backdropAnimation = createAnimation() |
| 48 | + .addElement(root.querySelector('ion-backdrop')!) |
| 49 | + .fromTo('opacity', 'var(--backdrop-opacity)', 'var(--backdrop-opacity)'); // Keep backdrop visible |
| 50 | + |
| 51 | + // Animate presentingEl from mobile state back to normal |
| 52 | + const transformOffset = !CSS.supports('width', 'max(0px, 1px)') ? '30px' : 'max(30px, var(--ion-safe-area-top))'; |
| 53 | + const toPresentingScale = SwipeToCloseDefaults.MIN_PRESENTING_SCALE; |
| 54 | + const fromTransform = `translateY(${transformOffset}) scale(${toPresentingScale})`; |
| 55 | + |
| 56 | + presentingAnimation |
| 57 | + .addElement(presentingEl) |
| 58 | + .beforeAddWrite(() => bodyEl.style.setProperty('background-color', '')) |
| 59 | + .fromTo('transform', fromTransform, 'translateY(0px) scale(1)') |
| 60 | + .fromTo('filter', 'contrast(0.85)', 'contrast(1)') |
| 61 | + .fromTo('border-radius', '10px 10px 0 0', '0px'); |
| 62 | + |
| 63 | + baseAnimation.addAnimation([presentingAnimation, wrapperAnimation, backdropAnimation]); |
| 64 | + } else { |
| 65 | + // Card modal: transition from mobile card state to portrait card state |
| 66 | + const toPresentingScale = SwipeToCloseDefaults.MIN_PRESENTING_SCALE; |
| 67 | + const transformOffset = !CSS.supports('width', 'max(0px, 1px)') ? '30px' : 'max(30px, var(--ion-safe-area-top))'; |
| 68 | + const fromTransform = `translateY(${transformOffset}) scale(${toPresentingScale})`; |
| 69 | + const toTransform = `translateY(-10px) scale(${toPresentingScale})`; |
| 70 | + |
| 71 | + presentingAnimation |
| 72 | + .addElement(presentingElRoot.querySelector('.modal-wrapper')!) |
| 73 | + .fromTo('transform', fromTransform, toTransform) |
| 74 | + .fromTo('filter', 'contrast(0.85)', 'contrast(0.85)'); // Keep same contrast for card |
| 75 | + |
| 76 | + const shadowAnimation = createAnimation() |
| 77 | + .addElement(presentingElRoot.querySelector('.modal-shadow')!) |
| 78 | + .fromTo('opacity', '0', '0') // Shadow stays hidden in portrait for card modals |
| 79 | + .fromTo('transform', fromTransform, toTransform); |
| 80 | + |
| 81 | + baseAnimation.addAnimation([presentingAnimation, shadowAnimation]); |
| 82 | + } |
| 83 | + |
| 84 | + return baseAnimation; |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Transition animation from portrait view to mobile view |
| 89 | + * This handles the case where a card modal is open in portrait view |
| 90 | + * and the user switches to mobile view |
| 91 | + */ |
| 92 | +export const portraitToMobileTransition = ( |
| 93 | + baseEl: HTMLElement, |
| 94 | + opts: ModalAnimationOptions, |
| 95 | + duration = 300 |
| 96 | +): Animation => { |
| 97 | + const { presentingEl } = opts; |
| 98 | + |
| 99 | + if (!presentingEl) { |
| 100 | + // No transition needed for non-card modals |
| 101 | + return createAnimation('portrait-to-mobile-transition'); |
| 102 | + } |
| 103 | + |
| 104 | + const hasCardModal = |
| 105 | + presentingEl.tagName === 'ION-MODAL' && (presentingEl as HTMLIonModalElement).presentingElement !== undefined; |
| 106 | + const presentingElRoot = getElementRoot(presentingEl); |
| 107 | + const bodyEl = document.body; |
| 108 | + |
| 109 | + const baseAnimation = createAnimation('portrait-to-mobile-transition') |
| 110 | + .addElement(baseEl) |
| 111 | + .easing('cubic-bezier(0.32,0.72,0,1)') |
| 112 | + .duration(duration); |
| 113 | + |
| 114 | + const presentingAnimation = createAnimation(); |
| 115 | + |
| 116 | + if (!hasCardModal) { |
| 117 | + // Non-card modal: transition from portrait state to mobile state |
| 118 | + const root = getElementRoot(baseEl); |
| 119 | + const wrapperAnimation = createAnimation() |
| 120 | + .addElement(root.querySelectorAll('.modal-wrapper, .modal-shadow')!) |
| 121 | + .fromTo('opacity', '1', '1'); // Keep wrapper visible |
| 122 | + |
| 123 | + const backdropAnimation = createAnimation() |
| 124 | + .addElement(root.querySelector('ion-backdrop')!) |
| 125 | + .fromTo('opacity', 'var(--backdrop-opacity)', 'var(--backdrop-opacity)'); // Keep backdrop visible |
| 126 | + |
| 127 | + // Animate presentingEl from normal state to mobile state |
| 128 | + const transformOffset = !CSS.supports('width', 'max(0px, 1px)') ? '30px' : 'max(30px, var(--ion-safe-area-top))'; |
| 129 | + const toPresentingScale = SwipeToCloseDefaults.MIN_PRESENTING_SCALE; |
| 130 | + const toTransform = `translateY(${transformOffset}) scale(${toPresentingScale})`; |
| 131 | + |
| 132 | + presentingAnimation |
| 133 | + .addElement(presentingEl) |
| 134 | + .beforeAddWrite(() => bodyEl.style.setProperty('background-color', 'black')) |
| 135 | + .fromTo('transform', 'translateY(0px) scale(1)', toTransform) |
| 136 | + .fromTo('filter', 'contrast(1)', 'contrast(0.85)') |
| 137 | + .fromTo('border-radius', '0px', '10px 10px 0 0'); |
| 138 | + |
| 139 | + baseAnimation.addAnimation([presentingAnimation, wrapperAnimation, backdropAnimation]); |
| 140 | + } else { |
| 141 | + // Card modal: transition from portrait card state to mobile card state |
| 142 | + const toPresentingScale = SwipeToCloseDefaults.MIN_PRESENTING_SCALE; |
| 143 | + const transformOffset = !CSS.supports('width', 'max(0px, 1px)') ? '30px' : 'max(30px, var(--ion-safe-area-top))'; |
| 144 | + const fromTransform = `translateY(-10px) scale(${toPresentingScale})`; |
| 145 | + const toTransform = `translateY(${transformOffset}) scale(${toPresentingScale})`; |
| 146 | + |
| 147 | + presentingAnimation |
| 148 | + .addElement(presentingElRoot.querySelector('.modal-wrapper')!) |
| 149 | + .fromTo('transform', fromTransform, toTransform) |
| 150 | + .fromTo('filter', 'contrast(0.85)', 'contrast(0.85)'); // Keep same contrast for card |
| 151 | + |
| 152 | + const shadowAnimation = createAnimation() |
| 153 | + .addElement(presentingElRoot.querySelector('.modal-shadow')!) |
| 154 | + .fromTo('opacity', '0', '0') // Shadow stays hidden |
| 155 | + .fromTo('transform', fromTransform, toTransform); |
| 156 | + |
| 157 | + baseAnimation.addAnimation([presentingAnimation, shadowAnimation]); |
| 158 | + } |
| 159 | + |
| 160 | + return baseAnimation; |
| 161 | +}; |
0 commit comments