Skip to content

Commit b3a240f

Browse files
committed
fixup! feat(router): Add feature to support the View Transitions API
1 parent 5d70089 commit b3a240f

5 files changed

Lines changed: 22 additions & 45 deletions

File tree

goldens/public-api/router/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
55
```ts
66

7-
/// <reference types="@types/dom-view-transitions" />
8-
97
import { AfterContentInit } from '@angular/core';
108
import { ChangeDetectorRef } from '@angular/core';
119
import { Compiler } from '@angular/core';

packages/router/src/navigation_transition.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ export interface NavigationTransition {
261261
targetRouterState: RouterState|null;
262262
guards: Checks;
263263
guardsResult: boolean|UrlTree|null;
264-
viewTransition?: ViewTransition
265264
}
266265

267266
/**
@@ -306,7 +305,7 @@ export class NavigationTransitions {
306305
private readonly paramsInheritanceStrategy =
307306
this.options.paramsInheritanceStrategy || 'emptyOnly';
308307
private readonly urlHandlingStrategy = inject(UrlHandlingStrategy);
309-
private readonly createTransition = inject(CREATE_VIEW_TRANSITION, {optional: true});
308+
private readonly createViewTransition = inject(CREATE_VIEW_TRANSITION, {optional: true});
310309

311310
navigationId = 0;
312311
get hasRequestedNavigation() {
@@ -614,15 +613,13 @@ export class NavigationTransitions {
614613
switchTap(() => this.afterPreactivation()),
615614

616615
switchMap(() => {
617-
const viewTransitionInfo =
618-
this.createTransition?.(this.environmentInjector);
619-
overallTransitionState.viewTransition = viewTransitionInfo?.transition;
616+
const viewTransitionStarted =
617+
this.createViewTransition?.(this.environmentInjector);
620618

621619
// If view transitions are enabled, block the navigation until the view
622620
// transition callback starts. Otherwise, continue immediately.
623-
return viewTransitionInfo ?
624-
from(viewTransitionInfo.viewTransitionStarted)
625-
.pipe(map(() => overallTransitionState)) :
621+
return viewTransitionStarted ?
622+
from(viewTransitionStarted).pipe(map(() => overallTransitionState)) :
626623
of(overallTransitionState);
627624
}),
628625

@@ -663,10 +660,6 @@ export class NavigationTransitions {
663660
}
664661
}),
665662

666-
switchTap(
667-
() => overallTransitionState.viewTransition?.updateCallbackDone ??
668-
of(void 0)),
669-
670663
// There used to be a lot more logic happening directly within the
671664
// transition Observable. Some of this logic has been refactored out to
672665
// other places but there may still be errors that happen there. This gives

packages/router/src/provide_router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {ROUTER_SCROLLER, RouterScroller} from './router_scroller';
2222
import {ActivatedRoute} from './router_state';
2323
import {UrlSerializer} from './url_tree';
2424
import {afterNextNavigation} from './utils/navigations';
25-
import {CREATE_VIEW_TRANSITION, transitionHelper} from './utils/view_transition';
25+
import {CREATE_VIEW_TRANSITION, createViewTransition} from './utils/view_transition';
2626

2727

2828
/**
@@ -729,7 +729,7 @@ export function withComponentInputBinding(): ComponentInputBindingFeature {
729729
* @experimental
730730
*/
731731
export function withViewTransitions(): ViewTransitionsFeature {
732-
const providers = [{provide: CREATE_VIEW_TRANSITION, useValue: transitionHelper}];
732+
const providers = [{provide: CREATE_VIEW_TRANSITION, useValue: createViewTransition}];
733733
return routerFeature(RouterFeatureKind.ViewTransitionsFeature, providers);
734734
}
735735

packages/router/src/utils/view_transition.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,30 @@
1111
import {afterNextRender, InjectionToken, Injector, NgZone} from '@angular/core';
1212

1313
export const CREATE_VIEW_TRANSITION =
14-
new InjectionToken<typeof transitionHelper>(ngDevMode ? 'view transition helper' : '');
14+
new InjectionToken<typeof createViewTransition>(ngDevMode ? 'view transition helper' : '');
1515

1616
/**
1717
* A helper function for using browser view transitions. This function skips the call to
1818
* `startViewTransition` if the browser does not support it.
1919
*
20-
* @returns An Observable that completes when the `ViewTransition.updateCallbackDone` promise
21-
* resolves.
20+
* @returns A Promise that resolves when the view transition callback begins.
2221
*/
23-
export function transitionHelper(injector: Injector):
24-
{viewTransitionStarted: Promise<void>, transition: ViewTransition} {
22+
export function createViewTransition(injector: Injector): Promise<void> {
2523
// Create promises outside the Angular zone to avoid causing extra change detections
2624
return injector.get(NgZone).runOutsideAngular(() => {
2725
if (!document.startViewTransition) {
28-
// Ensure the timing of the update and finish promises are the same even when view transitions
29-
// aren't supported by the browser. That is, resolve it after the next render in both cases.
30-
const renderPromise = createRenderPromise(injector);
31-
const transition: ViewTransition = {
32-
ready: Promise.resolve(),
33-
updateCallbackDone: renderPromise,
34-
finished: renderPromise,
35-
skipTransition: () => {},
36-
};
37-
return {
38-
transition,
39-
viewTransitionStarted: Promise.resolve(),
40-
};
41-
} else {
42-
let resolveViewTransitionStarted: () => void;
43-
const viewTransitionStarted = new Promise<void>((resolve) => {
44-
resolveViewTransitionStarted = resolve;
45-
});
46-
const transition = document.startViewTransition(() => {
47-
resolveViewTransitionStarted();
48-
return createRenderPromise(injector);
49-
});
50-
return {transition, viewTransitionStarted};
26+
return Promise.resolve();
5127
}
28+
29+
let resolveViewTransitionStarted: () => void;
30+
const viewTransitionStarted = new Promise<void>((resolve) => {
31+
resolveViewTransitionStarted = resolve;
32+
});
33+
document.startViewTransition(() => {
34+
resolveViewTransitionStarted();
35+
return createRenderPromise(injector);
36+
});
37+
return viewTransitionStarted;
5238
});
5339
}
5440

packages/router/test/bootstrap.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ describe('bootstrap', () => {
534534
expect(window.removeEventListener).toHaveBeenCalledWith('hashchange', jasmine.any(Function));
535535
});
536536

537-
fit('should have the correct event order when using view transitions', async () => {
537+
it('should have the correct event order when using view transitions', async () => {
538538
@Component({
539539
selector: 'component-a',
540540
template: `a`,

0 commit comments

Comments
 (0)