GithubHelp home page GithubHelp logo

mrtnvh / vue-transition-a11y Goto Github PK

View Code? Open in Web Editor NEW
5.0 0.0 0.0 1.94 MB

A extended version of the Vue <transition> component, which takes 'prefers-reduced-motion' in to account.

License: MIT License

JavaScript 6.45% TypeScript 92.63% Shell 0.92%

vue-transition-a11y's Introduction

vue-transition-a11y

https://vanhoofmaarten.github.io/vue-transition-a11y/

Vue transition component with a11y considerations.

Problem

When using the Vue transitions component with CSS transitions or CSS Animations, it's pretty easy to disable these for users who prefer this.

<transition name="slide-fade">
  <!-- ... -->
</transition>

Transitions

.slide-fade-enter-active,
.slide-fade-leave-active {
  transition: all 0.3s ease;
}

@media (prefers-reduced-motion: reduce) {
  .slide-fade-enter-active,
  .slide-fade-leave-active {
    transition: none;
  }
}

Animations

.slide-fade-enter-active,
.slide-fade-leave-active {
  animation: bounce-in 0.5s;
}

@media (prefers-reduced-motion: reduce) {
  .slide-fade-enter-active,
  .slide-fade-leave-active {
    animation: none;
  }
}

@keyframes bounce-in {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.5);
  }
  100% {
    transform: scale(1);
  }
}

Globally

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

But when using JavaScript Hooks, things get a little more complicated.

<transition
  v-on:before-enter="beforeEnter"
  v-on:enter="enter"
  v-on:after-enter="afterEnter"
  v-on:enter-cancelled="enterCancelled"
  v-on:before-leave="beforeLeave"
  v-on:leave="leave"
  v-on:after-leave="afterLeave"
  v-on:leave-cancelled="leaveCancelled"
>
  <!-- ... -->
</transition>
// ...
methods: {
  beforeEnter(el) {
    // ...
  },
  enter(el, done) {
    // ...
    done()
  },
  afterEnter(el) {
    // ...
  },
  enterCancelled(el) {
    // ...
  },
  beforeLeave(el) {
    // ...
  },
  leave(el, done) {
    // ...
    done()
  },
  afterLeave(el) {
    // ...
  },
  leaveCancelled(el) {
    // ...
  }
}

Where in all of these functions are you going to disable your complex animations?

Using the window.matchMedia functionality, you could check if the user's preferences.

const prefersReducedMotion = window.matchMedia(
  'screen and (prefers-reduced-motion: reduce)',
).matches;

// Stop every animation method if prefersReducedMotion === false

A bit over the top, don't you think?

Solution

To tackle this problem and enable Vue developers to make complex, re-usable and accessible transitions and animations with the transition component, I've created this component.

What is does is very simple. If the user prefers reduced motion, don't render the transition component. If not, render it.

This way, JavaScript Hooks aren't called when unnecessary

Easy.

API

The vue-transition-a11y components API extends the standard Vue transition API.

Props

Prop Default value Description
reduceMotion true Take the prefers-reduced-motion: reduce media query in to account.

Usage

<div class="outer">
  <transition-a11y>
    <div class="inner">
      <!-- ... -->
    </div>
  </transition-a11y>
</div>

License

MIT

Copyright (c) 2020 Maarten Van Hoof

vue-transition-a11y's People

Contributors

renovate[bot] avatar renovate-bot avatar mrtnvh avatar dependabot[bot] avatar semantic-release-bot avatar

Stargazers

 avatar Walter Purcaro avatar Tyler Forest-Hauser avatar Jordy avatar Davide Lanfranchi avatar

vue-transition-a11y's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

File: package.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid configuration option: default

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update devdependency @testing-library/vue to v8
  • chore(deps): update devdependency eslint-config-prettier to v9
  • chore(deps): update devdependency gh-pages to v6
  • chore(deps): update devdependency prettier to v3
  • chore(deps): update devdependency typescript to v5
  • chore(deps): update peerdependency vue to v3
  • chore(deps): update peter-evans/create-pull-request action to v5
  • chore(deps): update semantic-release monorepo (major) (@semantic-release/changelog, @semantic-release/exec, @semantic-release/git, semantic-release)

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v4
  • actions/setup-node v3
  • bahmutov/npm-install v1
.github/workflows/prepare.yml
  • actions/checkout v4
  • actions/setup-node v3
  • bahmutov/npm-install v1
  • cycjimmy/semantic-release-action v2
  • actions/checkout v4
  • peter-evans/create-pull-request v3
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-node v3
  • bahmutov/npm-install v1
  • cycjimmy/semantic-release-action v2
  • peaceiris/actions-gh-pages v3
.github/workflows/test.yml
  • actions/checkout v4
  • actions/setup-node v3
  • bahmutov/npm-install v1
npm
package.json
  • @bitjson/npm-scripts-info 1.0.0
  • @bitjson/typedoc 0.15.0-0
  • @pika/pack 0.5.0
  • @pika/plugin-build-node 0.9.2
  • @pika/plugin-build-web 0.9.2
  • @pika/plugin-ts-standard-pkg 0.9.2
  • @semantic-release/changelog 5.0.1
  • @semantic-release/exec 5.0.0
  • @semantic-release/git 9.0.0
  • @testing-library/vue 5.6.2
  • @types/jest 26.0.23
  • @typescript-eslint/eslint-plugin 4.25.0
  • @vue/server-test-utils 1.2.0
  • @vue/test-utils 1.2.0
  • cz-conventional-changelog 3.3.0
  • eslint 7.27.0
  • eslint-config-airbnb-typescript 12.3.1
  • eslint-config-prettier 8.3.0
  • eslint-plugin-import 2.23.4
  • eslint-plugin-jest 24.3.6
  • eslint-plugin-prettier 3.4.0
  • gh-pages 3.2.0
  • jest 26.6.3
  • npm-run-all 4.1.5
  • open-cli 6.0.1
  • prettier 2.3.0
  • semantic-release 17.4.3
  • ts-jest 26.5.6
  • typescript 4.3.2
  • vue 2.6.12
  • vue-server-renderer 2.6.12
  • vue-template-compiler 2.6.12
  • vue 2.x
  • node >=10

  • Check this box to trigger a request for Renovate to run again on this repository

Transition-group support

  • I'm submitting a ...
    [ ] bug report
    [x] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary

Could a similar approach work for transition-groups? It would make for a much more complete upgrade.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.