Skip to content

Commit 58c801b

Browse files
authored
feat: add support for reduced motion (angular#1095)
Makes it so that we disable all animations if the user prefers reduced motion.
1 parent 8a57068 commit 58c801b

File tree

7 files changed

+31
-6
lines changed

7 files changed

+31
-6
lines changed

src/app/app-module.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import {MATERIAL_DOCS_ROUTES} from './routes';
99
import {NavBarModule} from './shared/navbar';
1010
import {CookiePopupModule} from './shared/cookie-popup/cookie-popup-module';
1111

12+
const prefersReducedMotion = typeof matchMedia === 'function' ?
13+
matchMedia('(prefers-reduced-motion)').matches : false;
14+
1215
@NgModule({
1316
imports: [
1417
BrowserModule,
15-
BrowserAnimationsModule,
18+
BrowserAnimationsModule.withConfig({disableAnimations: prefersReducedMotion}),
1619
RouterModule.forRoot(MATERIAL_DOCS_ROUTES, {
1720
scrollPositionRestoration: 'enabled',
1821
anchorScrolling: 'enabled',

src/app/pages/homepage/homepage.scss

+4
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ $margin-promotion-sections-small: 15px;
181181
img {
182182
transition: 0.3s ease-in-out;
183183
width: 100%;
184+
185+
:host(.animations-disabled) & {
186+
transition: none;
187+
}
184188
}
185189

186190
&:hover, &:focus {

src/app/pages/homepage/homepage.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Component, HostBinding, NgModule, OnInit} from '@angular/core';
1+
import {Component, HostBinding, Inject, NgModule, OnInit, Optional} from '@angular/core';
2+
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
23
import {SvgViewerModule} from '../../shared/svg-viewer/svg-viewer';
34
import {MatButtonModule} from '@angular/material/button';
45
import {FooterModule} from '../../shared/footer/footer';
@@ -18,13 +19,19 @@ const TOP_COMPONENTS = ['datepicker', 'input', 'slide-toggle', 'slider', 'button
1819
@Component({
1920
selector: 'app-homepage',
2021
templateUrl: './homepage.html',
21-
styleUrls: ['./homepage.scss']
22+
styleUrls: ['./homepage.scss'],
2223
})
2324
export class Homepage implements OnInit {
2425
@HostBinding('class.main-content') readonly mainContentClass = true;
26+
@HostBinding('class.animations-disabled') readonly animationsDisabled: boolean;
27+
2528
isNextVersion = location.hostname.startsWith('next.material.angular.io');
2629

27-
constructor(public _componentPageTitle: ComponentPageTitle, public guideItems: GuideItems) {
30+
constructor(
31+
public _componentPageTitle: ComponentPageTitle,
32+
public guideItems: GuideItems,
33+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationsModule?: string) {
34+
this.animationsDisabled = animationsModule === 'NoopAnimations';
2835
}
2936

3037
ngOnInit(): void {

src/app/shared/carousel/carousel.scss

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ app-carousel {
88
flex-direction: row;
99
outline: none;
1010
transition: transform 0.5s ease-in-out;
11+
12+
.animations-disabled & {
13+
transition: none;
14+
}
1115
}
1216

1317
.docs-carousel-content-wrapper {

src/app/shared/carousel/carousel.ts

+8
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import {
55
Directive,
66
ElementRef,
77
HostBinding,
8+
Inject,
89
Input,
10+
Optional,
911
QueryList,
1012
ViewChild,
1113
ViewEncapsulation,
1214
} from '@angular/core';
1315
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
1416
import {LEFT_ARROW, RIGHT_ARROW, TAB} from '@angular/cdk/keycodes';
17+
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
1518

1619

1720
@Directive({
@@ -38,6 +41,7 @@ export class Carousel implements AfterContentInit {
3841
@Input('aria-label') ariaLabel: string | undefined;
3942
@ContentChildren(CarouselItem) items!: QueryList<CarouselItem>;
4043
@ViewChild('list') list!: ElementRef<HTMLElement>;
44+
@HostBinding('class.animations-disabled') readonly animationsDisabled: boolean;
4145
position = 0;
4246
showPrevArrow = false;
4347
showNextArrow = true;
@@ -66,6 +70,10 @@ export class Carousel implements AfterContentInit {
6670
}
6771
}
6872

73+
constructor(@Optional() @Inject(ANIMATION_MODULE_TYPE) animationsModule?: string) {
74+
this.animationsDisabled = animationsModule === 'NoopAnimations';
75+
}
76+
6977
ngAfterContentInit(): void {
7078
this._keyManager = new FocusKeyManager<CarouselItem>(this.items);
7179
}

src/app/shared/table-of-contents/_table-of-contents-theme.scss

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
.docs-link {
1616
color: mat.get-color-from-palette($foreground, secondary-text);
17-
transition: color 100ms;
1817

1918
&:hover,
2019
&.docs-active {

src/app/shared/version-picker/version-picker.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{materialVersion}}
44
<mat-icon>arrow_drop_down</mat-icon>
55
</button>
6-
<mat-menu #versionPicker="matMenu">
6+
<mat-menu #versionPicker="matMenu" xPosition="before">
77
<button mat-menu-item *ngFor="let version of docVersions | async"
88
(click)="onVersionChanged(version)">
99
{{version.title}}

0 commit comments

Comments
 (0)