Skip to content

Commit b2cb602

Browse files
MeAkibcrisbeto
authored andcommitted
docs: cross-fade between pages with view transitions (#33888)
Navigating between pages in the docs app swapped the content instantly. This enables the router's `withViewTransitions()`, so browsers that support the View Transitions API cross-fade from the old page to the new one. Other browsers keep the current behavior. Changes: - Add `withViewTransitions({skipInitialTransition: true})` to the router config. The first page load isn't animated, since there's no previous page to transition from. - Add a global `prefers-reduced-motion: reduce` rule that disables the view transition animations, so those users get an instant page swap. Checked in Chrome against the dev server, comparing against a run with `document.startViewTransition` removed to get the behavior without transitions: - Clicking a category card runs a 250ms cross-fade of the root. - The first page load doesn't start a transition. - Focus still moves to the new page's `focusOnNavigation` target. - Navigating to another page with a `#fragment` still scrolls the target heading into view, at the same position as without transitions. - With reduced motion, the transition starts but runs no animations. (cherry picked from commit 6064a60)
1 parent 8793a4c commit b2cb602

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎docs/src/main.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {ErrorHandler, provideZoneChangeDetection} from '@angular/core';
1010

1111
import {LocationStrategy, PathLocationStrategy} from '@angular/common';
1212
import {bootstrapApplication} from '@angular/platform-browser';
13-
import {provideRouter, withComponentInputBinding, withInMemoryScrolling} from '@angular/router';
13+
import {
14+
provideRouter,
15+
withComponentInputBinding,
16+
withInMemoryScrolling,
17+
withViewTransitions,
18+
} from '@angular/router';
1419
import {MaterialDocsApp} from './app/material-docs-app';
1520
import {MATERIAL_DOCS_ROUTES} from './app/routes';
1621
import {AnalyticsErrorReportHandler} from './app/shared/analytics/error-report-handler';
@@ -33,6 +38,9 @@ bootstrapApplication(MaterialDocsApp, {
3338
// Binds route params (e.g. `:section` and `:id`) to inputs of the routed components. Nested
3439
// routes also receive the params of their parent routes, since the router inherits them.
3540
withComponentInputBinding(),
41+
// Cross-fades between pages in browsers that support the View Transitions API. The first
42+
// page load isn't animated, since there's no previous page to transition from.
43+
withViewTransitions({skipInitialTransition: true}),
3644
),
3745
provideZoneChangeDetection(),
3846
],

‎docs/src/styles.scss‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ html {
2929
@include api.api-styles;
3030
@include markdown.markdown-styles;
3131
@include tables.table-styles;
32+
33+
// Skip the cross-fade between pages (see `withViewTransitions` in `main.ts`) for users who
34+
// prefer reduced motion. The new page then replaces the old one instantly.
35+
@media (prefers-reduced-motion: reduce) {
36+
// stylelint-disable selector-pseudo-element-no-unknown -- View Transitions pseudo-elements.
37+
::view-transition-group(*),
38+
::view-transition-old(*),
39+
::view-transition-new(*) {
40+
animation: none !important;
41+
}
42+
// stylelint-enable selector-pseudo-element-no-unknown
43+
}

0 commit comments

Comments
 (0)