Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you globally disable Angular animations based on media query in a standalone app?

It's previously been possible to disable animations in the whole app based on a media query:

@NgModule({
  bootstrap: [AppComponent],
  declarations: [AppRootComponent],
  imports: [
    BrowserAnimationsModule.withConfig({
      disableAnimations: window.matchMedia("(prefers-reduced-motion)").matches, // <-
    }),
    // ...
  ],
})
export class AppModule {}

With the new provider pattern, the module import is changed to provideAnimations:

bootstrapApplication(AppComponent, {
  providers: [
    provideAnimations(),
    // ...
  ]
});

This, however, doesn't accept any configurations like previously.

There seems to be a provideNoopAnimations alternative, but I don't see how you dynamically change which provider to use, based on a media query (prefers-reduced-motion)

How is this achieved now?

like image 613
Mikkel R. Lund Avatar asked Oct 19 '25 19:10

Mikkel R. Lund


1 Answers

It can actually be done very simply using the two new providers and resolving the media query inline/through a variable:

const disableAnimations: boolean = window.matchMedia(
  "(prefers-reduced-motion: reduce)"
).matches;

bootstrapApplication(AppComponent, {
  providers: [
    !disableAnimations ? provideAnimations() : provideNoopAnimations(),
    // ...
  ]
});
like image 60
Mikkel R. Lund Avatar answered Oct 22 '25 09:10

Mikkel R. Lund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!