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

use-css-transition

v1.2.2

Published

A react hook for applying CSS transitions to multiple elements.

Downloads

117

Readme

use-css-transition

NPM version Build Status Minzipped size

Installation

npm install use-css-transition

Usage

useCssTransition is sort of like a CSS transition group, but with the API Of react-spring's useTransition() hook.

Here's a quick example:

import React, { useState } from 'react';
import { useCSSTransition } from 'use-css-transition';

function AnimatedList() {
  const [list, setList] = useState(['foo', 'bar', 'baz']);

  const transitions = useCSSTransition(list, (item) => item, {
    common: {
      willChange: 'opacity',
      transition: 'opacity 1s',
    },
    from: { opacity: 0 },
    enter: { opacity: 1 },
    leave: { opacity: 0 },
    enterTime: 1000,
    leaveTime: 1000,
  });

  return (
    <div>
      {transitions.map(({ item, style, key }) => {
        <div key={key} style={style}>
          {item}
        </div>;
      })}
    </div>
  );
}

Examples

Masonry Layout: (edit on codesandbox)

Masonry Example

API

useCSSTransition(array, getKey, config)

array is an array of items to render, getKey is a function that takes an item and returns a string, and config is a { common?, initial?, from, enter, update?, leave, enterTime, leaveTime } object. config has the following properties:

  • enterTime, leaveTime are the expected time it takes an object to transition from "enter" to "update" and from "leave" to being gone.
  • common is an optional set of CSS styles to apply to all items.
  • initial, if provided, is a set of CSS styles to apply to items that are in the initial set of items when useCssTransition() is called for the first time. If this is not provided, from will be used instead.
  • from is the set of CSS styles to apply to a new object when it is added to the set of items - this will only be applied for one "tick", as the very next render enter will be used instead.
  • enter is the set of CSS styles to apply to an object while it is entering.
  • update is the set of CSS styles to apply to an object after enterTime has elapsed. If not provided, enter will be used instead.
  • leave is the set of CSS styles to apply to an object after it has been removed from items - in this case the item will be kept in the return results for leaveTime milliseconds, with leave styles applied, before finally being removed. If the item (or another item with the same key) is added back in before this time, the item will transition bask to update.

The return value will be an array of the following fields:

  • item is an item from the array.
  • index is the index of the item in the array. Note that if you move items around in your array, they will not move around in the returned array, but their index will change. We want to render items in a stable order if we want CSS transitions to work.
  • style is the style to apply to the item.
  • key is a unique key for this item.
  • state is one of 'from' | 'enter' | 'update' | 'leave' and represents the current state of this item.
  • nextUpdate is 0 if the item needs to update immediately, or a time in ms since the epoch when this item will next update.

useTransition(array, getKey, config)

This is like useCSSTransition(), except you can pass in any props you want instead of just CSS properties. This is most useful when you want to construct two different styles for related objects (like you want to scale and position and image, and want to position but not scale text over top of the image).

This does not do any kind of interpolation - you have to take care of that part yourself (or let your browser's CSS engine take care of it, ideally).

Copyright 2021 Jason Walton