Skip to content

Instantly share code, notes, and snippets.

@piotrwitek
Last active November 23, 2016 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrwitek/8db7532cd9e979c4b534e81caf0e4092 to your computer and use it in GitHub Desktop.
Save piotrwitek/8db7532cd9e979c4b534e81caf0e4092 to your computer and use it in GitHub Desktop.
Element animations library for React
// available animations
export const Animations = {
WIGGLE: 'u-anim-wiggle',
};
const ANIMATION_END = 'animationend';
// HOF for callbacks
export const animateWith = (animation: string) =>
(ev: any) => {
const {currentTarget} = ev;
currentTarget.addEventListener(ANIMATION_END, function handler() {
currentTarget.classList.remove(animation);
currentTarget.removeEventListener(ANIMATION_END, handler);
});
currentTarget.classList.add(animation);
};
/*
CSS
// wiggle animation
.u-anim-wiggle {
animation: u-anim-wiggle 0.1s cubic-bezier(0.4, 1, 0.75, 0.9) 3;
}
@keyframes u-anim-wiggle {
0% {
transform: rotate(0);
}
25% {
transform: rotate(-5deg);
}
75% {
transform: rotate(5deg);
}
100% {
transform: rotate(0);
}
}
*/
@piotrwitek
Copy link
Author

Usage:

import { Animations, animateWith} from 'element-animations';

<button type="button" onClick={animateWith(Animations.WIGGLE)}>
   MAD
</button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment