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

@dword-design/wait-for-element-transition

v0.0.1

Published

Easily wait for an element to complete it's transition using native vanilla JavaScript

Downloads

5

Readme

Build Status npm version

waitForElementTransition()

Let's say you have the problem (that many of us have) where you need to wait for the completion of your element's CSS transition before your code continues. You can use this library and call a waitForElementTransition method to wait until the element finishes its css transition before doing other things in your javascript code.

Benefits

  • Easily wait for an element's css transition to end using JavaScript
  • Allows you to keep your transition/animation css properties separate from your JS
  • Native javascript with no dependencies
  • Safer and more reliable than transitionstart and transitionend events
  • Plays nicely with the latest specifications

Installation

You can either use the dist file directly in your project:

<script
    type="text/javascript"
    src="/dist/wait-for-element-transition.min.js"
></script>

Or install via npm

npm i wait-for-element-transition

and import

import waitForElementTransition from 'wait-for-element-transition';

Usage

Here's an example where we want to wait for an element's background color to change from black to red.

<style>
    div {
        background-color: black;
        transition-property: background-color;
        transition-duration: 100ms;
        transition-timing-function: ease-out;
    }

    .red {
        background-color: red;
    }
</style>

<div>Transition this element</div>

<script
    type="text/javascript"
    src="/dist/wait-for-element-transition.min.js"
></script>
<script>
    const element = document.querySelector('div');
    element.classList.add('red'); // start transition
    waitForElementTransition(element).then(() => {
        // 100 milliseconds later...
        console.log('element background color changed to red!');
    });
</script>

If the element has already transitioned before the waitForElementTransition() is called, the waitForElementTransition()s promise will resolve immediately. So you can always guarantee that your code will run, just as it would synchronously.

Alternatives

The transitionend event

Using the transitionend or animationend events on an Element will allow you to wait until an Element's transition has ended, but this approach is limited:

  1. The events don't fire in the case where a transition is removed before completion (i.e. display is set to "none" or if the css property is removed) and
  2. The events don't fire when there are no css transition properties specified, which doesn't allow us to run the same animation-completion logic on elements which may or may not be animated.

Web Animations API

If done in a supported browser, the same thing in this package can be achieved using the Web Animations API. But consumers of this package may want a less-complex approach.

Development

Watch source files while developing:

npm start

Run test

npm test