npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@magica11y/prefers-reduced-motion

v1.0.11

Published

Detects user’s preferences for reduced motion using the 'prefers-reduce-motion' CSS3 level 5 media query.

Downloads

61

Readme

prefersReducedMotion()

Detects user’s preferences for reduced motion using the prefers-reduce-motion CSS3 level 5 media query.

Travis NPM Bundlephobia Bundlephobia Coveralls David DM David DM NodeJS License Snyk

Magica11y cover

:sparkles: Introduction

Quoting from the CSS3 level 5 media queries specfication…

The prefers-reduced-motion media feature is used to detect if the user has requested the system to minimize the amount of animation or motion it uses.

:roller_coaster: prefersReducedMotion() is part of :crystal_ball: Magica11y, which provides a suite of functions to detect “user-preference” and “environment” media features.

Magica11y functions are awesome because…

  • They have zero dependencies
  • They’re lightweight; e.g. prefersReducedMotion() is just Bundlephobia minified, or Bundlephobia minified & gzipp’d
  • They use the window.matchMedia API underneath
  • They’re optimized for performance; all the module functions are designed in such a way that they exit early
  • They provide a clean, well-documented and semantic API to work with

In addition to prefersReducedMotion(), Magica11y also provides…

:rocket: Getting started

:building_construction: Installation

You can install prefersReducedMotion() using a package manager such as yarn or npm

$ yarn add "@magica11y/prefers-reduced-motion"
# OR
$ npm install --save "@magica11y/prefers-reduced-motion"

You can also include prefersReducedMotion() from a CDN on your page, such as jsDelivr or unpkg

<script src="https://cdn.jsdelivr.net/npm/@magica11y/prefers-reduced-motion@latest/dist/magica11y.prefersReducedMotion.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/@magica11y/prefers-reduced-motion@latest/dist/magica11y.prefersReducedMotion.js"></script>

:game_die: Usage

prefersReducedMotion() is distributed as a UMD module, so you can use it as a browser global…

var motionPreference = window.magica11y.prefersReducedMotion.default();
var disableAnimations = (motionPreference === window.magica11y.prefersReducedMotion.motionPreferences.REDUCE);

… or as a CommonJS module…

const prefersReducedMotion = require('@magica11y/prefers-reduced-motion');
const motionPreference = prefersReducedMotion.default();
const disableAnimations = (motionPreference === prefersReducedMotion.motionPreferences.REDUCE);

… or as an ES module…

import prefersReducedMotion, { motionPreferences } from '@magica11y/prefers-reduced-motion';

const motionPreference = prefersReducedMotion();
const disableAnimations = (motionPreference === motionPreferences.REDUCE);

The motionPreferences object contains all the possible values supported by the 'prefers-reduce-motion' media query…

  • motionPreferences.NO_PREFERENCE (spec: 'no-preference')

    Indicates that the user has made no preference known to the system.

  • motionPreferences.REDUCE (spec: 'reduce')

    Indicates that user has notified the system that they prefer an interface that minimizes the amount of movement or animation, preferably to the point where all non-essential movement is removed.

  • null

    The user’s preference could not be determined.

:checkered_flag: Typechecking

You can import the Flow types from the provided libdefs in node_modules/@magica11y/prefers-reduced-motion/lib by configuring them in your .flowconfig

[libs]
node_modules/@magica11y/prefers-reduced-motion/lib

Now, you can use the Flow types as follows…

// @flow
import prefersReducedMotion, { type MotionPreference } from '@magica11y/prefers-reduced-motion';

const motionPreference: ?MotionPreference = prefersReducedMotion();

:tophat: Note: prefersReducedMotion() returns a nullable type (i.e. MotionPreference). So using the ? prefix to indicate nullable types is recommended (i.e. ?MotionPreference).

:books: Further reading

:scroll: License

License

See LICENSE.md for more details.

Handcrafted with :heart: by Rishabh.

Twitter