|
| 1 | +import { createAnimation, isPlatform } from '@ionic/core'; |
| 2 | +import type { Animation } from '@ionic/core/dist/types/utils/animation/animation-interface'; |
| 3 | +import { getElementRoot } from '../../utils'; |
| 4 | +import { calculateWindowAdjustment, getArrowDimensions, getPopoverDimensions, getPopoverPosition, shouldShowArrow } from '../utils'; |
| 5 | + |
| 6 | +const POPOVER_IOS_BODY_PADDING = 5; |
| 7 | +export const POPOVER_IOS_BODY_MARGIN = 8; |
| 8 | + |
| 9 | +/** |
| 10 | + * iOS Popover Enter Animation |
| 11 | + */ |
| 12 | +// TODO(FW-2832): types |
| 13 | +export const iosEnterAnimation = (baseEl: HTMLElement, opts?: any): Animation => { |
| 14 | + const { event: ev, size, trigger, reference, side, align } = opts; |
| 15 | + const doc = baseEl.ownerDocument as any; |
| 16 | + const isRTL = doc.dir === 'rtl'; |
| 17 | + const bodyWidth = doc.defaultView.innerWidth; |
| 18 | + const bodyHeight = doc.defaultView.innerHeight; |
| 19 | + |
| 20 | + const root = getElementRoot(baseEl); |
| 21 | + const contentEl = root.querySelector('.popover-content') as HTMLElement; |
| 22 | + const arrowEl = root.querySelector('.popover-arrow') as HTMLElement | null; |
| 23 | + |
| 24 | + const referenceSizeEl = trigger || ev?.detail?.ionShadowTarget || ev?.target; |
| 25 | + const { contentWidth, contentHeight } = getPopoverDimensions(size, contentEl, referenceSizeEl); |
| 26 | + const { arrowWidth, arrowHeight } = getArrowDimensions(arrowEl); |
| 27 | + |
| 28 | + const isReplace = ((): boolean => { |
| 29 | + if (!['ion-button', 'ion-buttons'].includes(referenceSizeEl.localName)) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + if (referenceSizeEl.classList.contains('ios26-disabled')) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + return true; |
| 36 | + })(); |
| 37 | + |
| 38 | + const defaultPosition = { |
| 39 | + top: bodyHeight / 2 - contentHeight / 2, |
| 40 | + left: bodyWidth / 2 - contentWidth / 2, |
| 41 | + originX: isRTL ? 'right' : 'left', |
| 42 | + originY: 'top', |
| 43 | + }; |
| 44 | + |
| 45 | + const results = getPopoverPosition( |
| 46 | + isRTL, |
| 47 | + contentWidth, |
| 48 | + contentHeight, |
| 49 | + arrowWidth, |
| 50 | + arrowHeight, |
| 51 | + reference, |
| 52 | + side, |
| 53 | + align, |
| 54 | + defaultPosition, |
| 55 | + trigger, |
| 56 | + ev, |
| 57 | + ); |
| 58 | + |
| 59 | + const padding = size === 'cover' ? 0 : POPOVER_IOS_BODY_PADDING; |
| 60 | + const margin = size === 'cover' ? 0 : 25; |
| 61 | + |
| 62 | + const { originX, originY, top, left, bottom, checkSafeAreaLeft, checkSafeAreaRight, arrowTop, arrowLeft, addPopoverBottomClass } = |
| 63 | + calculateWindowAdjustment( |
| 64 | + side, |
| 65 | + results.top, |
| 66 | + results.left, |
| 67 | + padding, |
| 68 | + bodyWidth, |
| 69 | + bodyHeight, |
| 70 | + contentWidth, |
| 71 | + contentHeight, |
| 72 | + margin, |
| 73 | + results.originX, |
| 74 | + results.originY, |
| 75 | + results.referenceCoordinates, |
| 76 | + results.arrowTop, |
| 77 | + results.arrowLeft, |
| 78 | + arrowHeight, |
| 79 | + referenceSizeEl.getBoundingClientRect(), |
| 80 | + isReplace, |
| 81 | + ); |
| 82 | + |
| 83 | + const baseAnimation = createAnimation(); |
| 84 | + const backdropAnimation = createAnimation(); |
| 85 | + const contentAnimation = createAnimation(); |
| 86 | + const targetAnimation = createAnimation(); |
| 87 | + |
| 88 | + backdropAnimation |
| 89 | + .delay(100) |
| 90 | + .duration(300) |
| 91 | + .addElement(root.querySelector('ion-backdrop')!) |
| 92 | + .fromTo('opacity', 0.01, 'var(--backdrop-opacity)') |
| 93 | + .beforeStyles({ |
| 94 | + 'pointer-events': 'none', |
| 95 | + }) |
| 96 | + .afterClearStyles(['pointer-events']); |
| 97 | + |
| 98 | + // In Chromium, if the wrapper animates, the backdrop filter doesn't work. |
| 99 | + // The Chromium team stated that this behavior is expected and not a bug. The element animating opacity creates a backdrop root for the backdrop-filter. |
| 100 | + // To get around this, instead of animating the wrapper, animate both the arrow and content. |
| 101 | + // https://bugs.chromium.org/p/chromium/issues/detail?id=1148826 |
| 102 | + contentAnimation |
| 103 | + .easing('cubic-bezier(0, 1, 0.22, 1)') |
| 104 | + .delay(100) |
| 105 | + .duration(400) |
| 106 | + .addElement(root.querySelector('.popover-arrow')!) |
| 107 | + .addElement(root.querySelector('.popover-content')!) |
| 108 | + .beforeStyles({ 'transform-origin': `${originY} ${originX}` }) |
| 109 | + .beforeAddWrite(() => { |
| 110 | + /** |
| 111 | + * 'transformOrigin' use for leave animation. |
| 112 | + */ |
| 113 | + root.querySelector<HTMLElement>('.popover-content')!.dataset['transformOrigin'] = `${originY} ${originX}`; |
| 114 | + }) |
| 115 | + .fromTo('transform', 'scale(0)', 'scale(1)') |
| 116 | + .fromTo('opacity', 0.01, 1); |
| 117 | + // TODO(FW-4376) Ensure that arrow also blurs when translucent |
| 118 | + |
| 119 | + if (isReplace) { |
| 120 | + targetAnimation |
| 121 | + .delay(0) |
| 122 | + .duration(200) |
| 123 | + .addElement(referenceSizeEl) |
| 124 | + .beforeStyles({ 'transform-origin': `${originY} ${originX}` }) |
| 125 | + .beforeAddClass('ios26-replace-element') |
| 126 | + .fromTo('transform', 'scale(1)', 'scale(1.05)') |
| 127 | + .fromTo('opacity', 1, 0); |
| 128 | + } |
| 129 | + |
| 130 | + return baseAnimation |
| 131 | + .easing('ease') |
| 132 | + .delay(100) |
| 133 | + .duration(100) |
| 134 | + .beforeAddWrite(() => { |
| 135 | + if (size === 'cover') { |
| 136 | + baseEl.style.setProperty('--width', `${contentWidth}px`); |
| 137 | + } |
| 138 | + |
| 139 | + if (addPopoverBottomClass) { |
| 140 | + baseEl.classList.add('popover-bottom'); |
| 141 | + } |
| 142 | + |
| 143 | + if (bottom !== undefined) { |
| 144 | + contentEl.style.setProperty('bottom', `${bottom}px`); |
| 145 | + } |
| 146 | + |
| 147 | + const safeAreaLeft = ' + var(--ion-safe-area-left, 0)'; |
| 148 | + const safeAreaRight = ' - var(--ion-safe-area-right, 0)'; |
| 149 | + |
| 150 | + let leftValue = `${left}px`; |
| 151 | + |
| 152 | + if (checkSafeAreaLeft) { |
| 153 | + leftValue = `${left}px${safeAreaLeft}`; |
| 154 | + } |
| 155 | + if (checkSafeAreaRight) { |
| 156 | + leftValue = `${left}px${safeAreaRight}`; |
| 157 | + } |
| 158 | + |
| 159 | + contentEl.style.setProperty('top', `calc(${top}px + var(--offset-y, 0))`); |
| 160 | + contentEl.style.setProperty('left', `calc(${leftValue} + var(--offset-x, 0))`); |
| 161 | + contentEl.style.setProperty('transform-origin', `${originY} ${originX}`); |
| 162 | + |
| 163 | + if (arrowEl !== null) { |
| 164 | + const didAdjustBounds = results.top !== top || results.left !== left; |
| 165 | + const showArrow = shouldShowArrow(side, didAdjustBounds, ev, trigger); |
| 166 | + |
| 167 | + if (showArrow) { |
| 168 | + arrowEl.style.setProperty('top', `calc(${arrowTop}px + var(--offset-y, 0))`); |
| 169 | + arrowEl.style.setProperty('left', `calc(${arrowLeft}px + var(--offset-x, 0))`); |
| 170 | + } else { |
| 171 | + arrowEl.style.setProperty('display', 'none'); |
| 172 | + } |
| 173 | + } |
| 174 | + }) |
| 175 | + .addAnimation([backdropAnimation, contentAnimation, targetAnimation]); |
| 176 | +}; |
0 commit comments