• Stars
    star
    1,300
  • Rank 34,671 (Top 0.8 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Painless transitions for CSS Grid

Animate CSS Grid

Performantly animate all CSS grid properties, including:

grid-column and grid-row

grid-column and grid-row

grid-template-columns

grid-template-columns

grid-gap

grid-gap

Fork Photo Grid Example on CodeSandbox

Why use animate-css-grid?

This library uses transforms to transition between different layout states, which means, in comparison to pure CSS animations, it offers:

  • better performance
  • more flexibility in terms of what properties can be animated
  • more configurable animations (easing options, staggers)

Want to have a look for yourself? Feel free to check out this Mondrian animated with CSS keyframes and compare it with the same UI animated with animate-css-grid.

How to use it

Just call the wrapGrid method on your grid container, and optionally provide a config object as a second argument. If the grid is removed from the page, the animations will automatically be cleaned up as well.

yarn add animate-css-grid or npm install animate-css-grid

import { wrapGrid } from 'animate-css-grid'

const grid = document.querySelector(".grid");
wrapGrid(grid);

Or from a script tag:

<script src="https://unpkg.com/animate-css-grid@latest"></script>

<script>
  const grid = document.querySelector(".grid");
  animateCSSGrid.wrapGrid(grid, {duration : 600});
</script>

Optional config object:

{
  // int: default is 0 ms
  stagger: 100,
  // int: default is 250 ms
  duration: 500,
  // string: default is 'easeInOut'
  easing: 'backInOut',
  // function: called with list of elements about to animate
  onStart: (animatingElementList)=> {},
  // function: called with list of elements that just finished animating
  // cancelled animations will not trigger onEnd
  onEnd: (animatingElementList)=> {}
}

Available easing functions:

  • 'linear'
  • 'easeIn' / 'easeOut' / 'easeInOut'
  • 'circIn' / 'circOut' / 'circInOut'
  • 'backIn' / 'backOut' / 'backInOut'
  • 'anticipate'

Learn more about available easing functions here.

Two functions are returned by the wrapGrid call that you probably won't need to use:

import { wrapGrid } from animateCSSGrid

const grid = document.querySelector(".grid");
const { unwrapGrid, forceGridAnimation } = wrapGrid(grid);

// if you want the grid to transition after updating an inline style
// you need to call forceGridAnimation
grid.style.width = '500px'
forceGridAnimation()

// if you want to remove animations but not the grid itself
unwrapGrid()

Requirements

  1. The updates to the grid will have to come from addition or removal of a class or element. Currently, inline style updates will not trigger transitions. (Although you can manually trigger transitions in that case by calling forceGridAnimation())
  2. Important If a grid item has children, they should be surrounded by a single container element. This is so we can apply a counter scale and prevent children elements from getting warped during scale transitions of the parent.

Example:

<!-- grid class -->
<ul class="some-grid-class-that-changes">
  <li class="grid-item">
    <!-- each grid item must have a single direct child -->
    <div>
      <h3>Item title</h3>
      <div>Item body</div>
    </div>
  </li>
<div>

How it works

The script registers a MutationObserver that activates when the grid or one of its children adds or loses a class or element. That means there's no need to remove the animations before removing the grid, everything should be cleaned up automatically. It uses the FLIP animation technique to smoothly update the grid, applying a counter transform to the children of each item so that they do not appear distorted while the transition occurs.

It should work on container elements without CSS grid applied as well, but was developed and tested with CSS grid in mind.

Usage with Frameworks

The animate-css-grid library can easily be used with frameworks like React or Vue.

Check out the React example or the Vue example on Codepen!

More Repositories

1

react-flip-toolkit

A lightweight magic-move library for configurable layout transitions
TypeScript
3,773
star
2

mobile-first-animation

Gesture-driven animation on the mobile web (React Conf 2019)
JavaScript
2,069
star
3

react-animation-comparison

A tour of React animation libraries with a focus on developer experience
JavaScript
685
star
4

bundle-wizard

Magically easy insight into the JavaScript loaded by a web app
JavaScript
684
star
5

react-stripe-menu

A clone of Stripe's animated menu using React, Styled Components and React-Flip-Toolkit
JavaScript
458
star
6

storybook-mobile

[Deprecated] A storybook addon that helps you create truly mobile-friendly components
JavaScript
107
star
7

react-flip-toolkit-router-example

Meaningful transitions with react-router-v4 and react-flip-toolkit
JavaScript
102
star
8

aholachek.github.io

My website
JavaScript
53
star
9

redux-usage-report

A Redux Devtools monitor to audit your app's usage of the store
JavaScript
41
star
10

react-animations-from-scratch

JavaScript
20
star
11

react-2019-interactive-workbook

Learn new features of React interactively
JavaScript
18
star
12

scrollbounce

Add a subtle bounce effect on mobile when the user scrolls (WIP)
JavaScript
17
star
13

react-spring-workshop

Learn by building small animations
JavaScript
9
star
14

gif-search

Take a selfie, get a gif
CSS
8
star
15

github-actions-examples

Tiny test repo for github actions
HTML
7
star
16

review-fetch

Code exercises to help you solidify your knowledge of fetch
JavaScript
6
star
17

react-fancy-tween-circular-progress

A configurable circular progress bar with color and number animation
JavaScript
3
star
18

overdrive-experiment

JavaScript
1
star
19

virgin-airlines

A prototype for a Virgin Airlines mobile ticket site redesign
JavaScript
1
star
20

tap-demo

JavaScript
1
star
21

npm-script-starter

A basic front end development setup using npm scripts
JavaScript
1
star
22

spring-example

JavaScript
1
star
23

gif-search-backend

JavaScript
1
star
24

meaningful-layout-transitions

JavaScript
1
star
25

basic-postcss-setup

A simple npm scripts-powered set up for experimenting with Shoelace.css and postcss in general
HTML
1
star