Skip to content

Commit

Permalink
feat: add support for reduced motion (#1095)
Browse files Browse the repository at this point in the history
Makes it so that we disable all animations if the user prefers reduced motion.
  • Loading branch information
crisbeto authored Nov 10, 2021
1 parent 3ad38c6 commit 856a03f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
5 changes: 4 additions & 1 deletion material.angular.io/src/app/app-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {MATERIAL_DOCS_ROUTES} from './routes';
import {NavBarModule} from './shared/navbar';
import {CookiePopupModule} from './shared/cookie-popup/cookie-popup-module';

const prefersReducedMotion = typeof matchMedia === 'function' ?
matchMedia('(prefers-reduced-motion)').matches : false;

@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
BrowserAnimationsModule.withConfig({disableAnimations: prefersReducedMotion}),
RouterModule.forRoot(MATERIAL_DOCS_ROUTES, {
scrollPositionRestoration: 'enabled',
anchorScrolling: 'enabled',
Expand Down
4 changes: 4 additions & 0 deletions material.angular.io/src/app/pages/homepage/homepage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ $margin-promotion-sections-small: 15px;
img {
transition: 0.3s ease-in-out;
width: 100%;

:host(.animations-disabled) & {
transition: none;
}
}

&:hover, &:focus {
Expand Down
13 changes: 10 additions & 3 deletions material.angular.io/src/app/pages/homepage/homepage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Component, HostBinding, NgModule, OnInit} from '@angular/core';
import {Component, HostBinding, Inject, NgModule, OnInit, Optional} from '@angular/core';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {SvgViewerModule} from '../../shared/svg-viewer/svg-viewer';
import {MatButtonModule} from '@angular/material/button';
import {FooterModule} from '../../shared/footer/footer';
Expand All @@ -18,13 +19,19 @@ const TOP_COMPONENTS = ['datepicker', 'input', 'slide-toggle', 'slider', 'button
@Component({
selector: 'app-homepage',
templateUrl: './homepage.html',
styleUrls: ['./homepage.scss']
styleUrls: ['./homepage.scss'],
})
export class Homepage implements OnInit {
@HostBinding('class.main-content') readonly mainContentClass = true;
@HostBinding('class.animations-disabled') readonly animationsDisabled: boolean;

isNextVersion = location.hostname.startsWith('next.material.angular.io');

constructor(public _componentPageTitle: ComponentPageTitle, public guideItems: GuideItems) {
constructor(
public _componentPageTitle: ComponentPageTitle,
public guideItems: GuideItems,
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationsModule?: string) {
this.animationsDisabled = animationsModule === 'NoopAnimations';
}

ngOnInit(): void {
Expand Down
4 changes: 4 additions & 0 deletions material.angular.io/src/app/shared/carousel/carousel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ app-carousel {
flex-direction: row;
outline: none;
transition: transform 0.5s ease-in-out;

.animations-disabled & {
transition: none;
}
}

.docs-carousel-content-wrapper {
Expand Down
8 changes: 8 additions & 0 deletions material.angular.io/src/app/shared/carousel/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import {
Directive,
ElementRef,
HostBinding,
Inject,
Input,
Optional,
QueryList,
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
import {LEFT_ARROW, RIGHT_ARROW, TAB} from '@angular/cdk/keycodes';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';


@Directive({
Expand All @@ -38,6 +41,7 @@ export class Carousel implements AfterContentInit {
@Input('aria-label') ariaLabel: string | undefined;
@ContentChildren(CarouselItem) items!: QueryList<CarouselItem>;
@ViewChild('list') list!: ElementRef<HTMLElement>;
@HostBinding('class.animations-disabled') readonly animationsDisabled: boolean;
position = 0;
showPrevArrow = false;
showNextArrow = true;
Expand Down Expand Up @@ -66,6 +70,10 @@ export class Carousel implements AfterContentInit {
}
}

constructor(@Optional() @Inject(ANIMATION_MODULE_TYPE) animationsModule?: string) {
this.animationsDisabled = animationsModule === 'NoopAnimations';
}

ngAfterContentInit(): void {
this._keyManager = new FocusKeyManager<CarouselItem>(this.items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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

&:hover,
&.docs-active {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{materialVersion}}
<mat-icon>arrow_drop_down</mat-icon>
</button>
<mat-menu #versionPicker="matMenu">
<mat-menu #versionPicker="matMenu" xPosition="before">
<button mat-menu-item *ngFor="let version of docVersions | async"
(click)="onVersionChanged(version)">
{{version.title}}
Expand Down

0 comments on commit 856a03f

Please sign in to comment.