-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathindex.ts
More file actions
381 lines (324 loc) · 11.5 KB
/
index.ts
File metadata and controls
381 lines (324 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { config } from '@global/config';
import { Build, writeTask } from '@stencil/core';
import {
LIFECYCLE_DID_ENTER,
LIFECYCLE_DID_LEAVE,
LIFECYCLE_WILL_ENTER,
LIFECYCLE_WILL_LEAVE,
} from '../../components/nav/constants';
import type { NavOptions, NavDirection } from '../../components/nav/nav-interface';
import type { Animation, AnimationBuilder } from '../animation/animation-interface';
import { createFocusController } from '../focus-controller';
import { raf } from '../helpers';
const iosTransitionAnimation = () => import('./ios.transition');
const mdTransitionAnimation = () => import('./md.transition');
const focusController = createFocusController();
// TODO(FW-2832): types
/**
* Executes the main page transition.
* It also manages the lifecycle of header visibility (if any)
* to prevent visual flickering in iOS. The flickering only
* occurs for a condensed header that is placed above the content.
*
* @param opts Options for the transition.
* @returns A promise that resolves when the transition is complete.
*/
export const transition = (opts: TransitionOptions): Promise<TransitionResult> => {
return new Promise((resolve, reject) => {
writeTask(() => {
const transitioningInactiveHeader = getIosIonHeader(opts);
beforeTransition(opts, transitioningInactiveHeader);
runTransition(opts)
.then(
(result) => {
if (result.animation) {
result.animation.destroy();
}
afterTransition(opts);
resolve(result);
},
(error) => {
afterTransition(opts);
reject(error);
}
)
.finally(() => {
// Ensure that the header is restored to its original state.
setHeaderTransitionClass(transitioningInactiveHeader, false);
});
});
});
};
const beforeTransition = (opts: TransitionOptions, transitioningInactiveHeader: HTMLElement | null) => {
const enteringEl = opts.enteringEl;
const leavingEl = opts.leavingEl;
focusController.saveViewFocus(leavingEl);
setZIndex(enteringEl, leavingEl, opts.direction);
// Prevent flickering of the header by adding a class.
setHeaderTransitionClass(transitioningInactiveHeader, true);
if (opts.showGoBack) {
enteringEl.classList.add('can-go-back');
} else {
enteringEl.classList.remove('can-go-back');
}
setPageHidden(enteringEl, false);
/**
* When transitioning, the page should not
* respond to click events. This resolves small
* issues like users double tapping the ion-back-button.
* These pointer events are removed in `afterTransition`.
*/
enteringEl.style.setProperty('pointer-events', 'none');
if (leavingEl) {
setPageHidden(leavingEl, false);
leavingEl.style.setProperty('pointer-events', 'none');
}
};
const runTransition = async (opts: TransitionOptions): Promise<TransitionResult> => {
const animationBuilder = await getAnimationBuilder(opts);
const ani = animationBuilder && Build.isBrowser ? animation(animationBuilder, opts) : noAnimation(opts); // fast path for no animation
return ani;
};
const afterTransition = (opts: TransitionOptions) => {
const enteringEl = opts.enteringEl;
const leavingEl = opts.leavingEl;
enteringEl.classList.remove('ion-page-invisible');
enteringEl.style.removeProperty('pointer-events');
if (leavingEl !== undefined) {
leavingEl.classList.remove('ion-page-invisible');
leavingEl.style.removeProperty('pointer-events');
}
focusController.setViewFocus(enteringEl);
};
const getAnimationBuilder = async (opts: TransitionOptions): Promise<AnimationBuilder | undefined> => {
if (!opts.leavingEl || !opts.animated || opts.duration === 0) {
return undefined;
}
if (opts.animationBuilder) {
return opts.animationBuilder;
}
const getAnimation =
opts.mode === 'ios'
? (await iosTransitionAnimation()).iosTransitionAnimation
: (await mdTransitionAnimation()).mdTransitionAnimation;
return getAnimation;
};
const animation = async (animationBuilder: AnimationBuilder, opts: TransitionOptions): Promise<TransitionResult> => {
await waitForReady(opts, true);
const trans = animationBuilder(opts.baseEl, opts);
fireWillEvents(opts.enteringEl, opts.leavingEl);
const didComplete = await playTransition(trans, opts);
if (opts.progressCallback) {
opts.progressCallback(undefined);
}
if (didComplete) {
fireDidEvents(opts.enteringEl, opts.leavingEl);
}
return {
hasCompleted: didComplete,
animation: trans,
};
};
const noAnimation = async (opts: TransitionOptions): Promise<TransitionResult> => {
const enteringEl = opts.enteringEl;
const leavingEl = opts.leavingEl;
const focusManagerEnabled = config.get('focusManagerPriority', false);
/**
* If the focus manager is enabled then we need to wait for Ionic components to be
* rendered otherwise the component to focus may not be focused because it is hidden.
*/
await waitForReady(opts, focusManagerEnabled);
fireWillEvents(enteringEl, leavingEl);
fireDidEvents(enteringEl, leavingEl);
return {
hasCompleted: true,
};
};
const waitForReady = async (opts: TransitionOptions, defaultDeep: boolean) => {
const deep = opts.deepWait !== undefined ? opts.deepWait : defaultDeep;
if (deep) {
await Promise.all([deepReady(opts.enteringEl), deepReady(opts.leavingEl)]);
}
await notifyViewReady(opts.viewIsReady, opts.enteringEl);
};
const notifyViewReady = async (
viewIsReady: undefined | ((enteringEl: HTMLElement) => Promise<any>),
enteringEl: HTMLElement
) => {
if (viewIsReady) {
await viewIsReady(enteringEl);
}
};
const playTransition = (trans: Animation, opts: TransitionOptions): Promise<boolean> => {
const progressCallback = opts.progressCallback;
const promise = new Promise<boolean>((resolve) => {
trans.onFinish((currentStep: any) => resolve(currentStep === 1));
});
// cool, let's do this, start the transition
if (progressCallback) {
// this is a swipe to go back, just get the transition progress ready
// kick off the swipe animation start
trans.progressStart(true);
progressCallback(trans);
} else {
// only the top level transition should actually start "play"
// kick it off and let it play through
// ******** DOM WRITE ****************
trans.play();
}
// create a callback for when the animation is done
return promise;
};
const fireWillEvents = (enteringEl: HTMLElement | undefined, leavingEl: HTMLElement | undefined) => {
lifecycle(leavingEl, LIFECYCLE_WILL_LEAVE);
lifecycle(enteringEl, LIFECYCLE_WILL_ENTER);
};
const fireDidEvents = (enteringEl: HTMLElement | undefined, leavingEl: HTMLElement | undefined) => {
lifecycle(enteringEl, LIFECYCLE_DID_ENTER);
lifecycle(leavingEl, LIFECYCLE_DID_LEAVE);
};
export const lifecycle = (el: HTMLElement | undefined, eventName: string) => {
if (el) {
const ev = new CustomEvent(eventName, {
bubbles: false,
cancelable: false,
});
el.dispatchEvent(ev);
}
};
/**
* Wait two request animation frame loops.
* This allows the framework implementations enough time to mount
* the user-defined contents. This is often needed when using inline
* modals and popovers that accept user components. For popover,
* the contents must be mounted for the popover to be sized correctly.
* For modals, the contents must be mounted for iOS to run the
* transition correctly.
*
* On Angular and React, a single raf is enough time, but for Vue
* we need to wait two rafs. As a result we are using two rafs for
* all frameworks to ensure contents are mounted.
*/
export const waitForMount = (): Promise<void> => {
return new Promise((resolve) => raf(() => raf(() => resolve())));
};
export const deepReady = async (el: any | undefined): Promise<void> => {
const element = el as any;
if (element) {
if (element.componentOnReady != null) {
// eslint-disable-next-line custom-rules/no-component-on-ready-method
const stencilEl = await element.componentOnReady();
if (stencilEl != null) {
return;
}
/**
* Custom elements in Stencil will have __registerHost.
*/
} else if (element.__registerHost != null) {
/**
* Non-lazy loaded custom elements need to wait
* one frame for component to be loaded.
*/
const waitForCustomElement = new Promise((resolve) => raf(resolve));
await waitForCustomElement;
return;
}
await Promise.all(Array.from(element.children).map(deepReady));
}
};
export const setPageHidden = (el: HTMLElement, hidden: boolean) => {
if (hidden) {
el.setAttribute('aria-hidden', 'true');
el.classList.add('ion-page-hidden');
} else {
el.hidden = false;
el.removeAttribute('aria-hidden');
el.classList.remove('ion-page-hidden');
}
};
const setZIndex = (
enteringEl: HTMLElement | undefined,
leavingEl: HTMLElement | undefined,
direction: NavDirection | undefined
) => {
if (enteringEl !== undefined) {
enteringEl.style.zIndex = direction === 'back' ? '99' : '101';
}
if (leavingEl !== undefined) {
leavingEl.style.zIndex = '100';
}
};
/**
* Add a class to ensure that the header (if any)
* does not flicker during the transition. By adding the
* transitioning class, we ensure that the header has
* the necessary styles to prevent the following flickers:
* 1. When entering a page with a condensed header, the
* header should never be visible. However,
* it briefly renders the background color while
* the transition is occurring.
* 2. When leaving a page with a condensed header, the
* header has an opacity of 0 and the pages
* have a z-index which causes the entering page to
* briefly show it's content underneath the leaving page.
* 3. When entering a page or leaving a page with a fade
* header, the header should not have a background color.
* However, it briefly shows the background color while
* the transition is occurring.
*
* @param header The header element to modify.
* @param isTransitioning Whether the transition is occurring.
*/
const setHeaderTransitionClass = (header: HTMLElement | null, isTransitioning: boolean) => {
if (!header) {
return;
}
const transitionClass = 'header-transitioning';
if (isTransitioning) {
header.classList.add(transitionClass);
} else {
header.classList.remove(transitionClass);
}
};
export const getIonPageElement = (element: HTMLElement) => {
if (element.classList.contains('ion-page')) {
return element;
}
const ionPage = element.querySelector(':scope > .ion-page, :scope > ion-nav, :scope > ion-tabs');
if (ionPage) {
return ionPage;
}
// idk, return the original element so at least something animates and we don't have a null pointer
return element;
};
/**
* Retrieves the ion-header element from a page based on the
* direction of the transition.
*
* @param opts Options for the transition.
* @returns The ion-header element or null if not found or not in 'ios' mode.
*/
const getIosIonHeader = (opts: TransitionOptions): HTMLElement | null => {
const enteringEl = opts.enteringEl;
const leavingEl = opts.leavingEl;
const direction = opts.direction;
const mode = opts.mode;
if (mode !== 'ios') {
return null;
}
const element = direction === 'back' ? leavingEl : enteringEl;
if (!element) {
return null;
}
return element.querySelector('ion-header');
};
export interface TransitionOptions extends NavOptions {
progressCallback?: (ani: Animation | undefined) => void;
baseEl: any;
enteringEl: HTMLElement;
leavingEl: HTMLElement | undefined;
}
export interface TransitionResult {
hasCompleted: boolean;
animation?: Animation;
}