react-hover-animation
A react wrapper component that detects hover and adds animation from the awesome react-spring.
Works both on the web and mobile touch events.
Example:
https://react-hover-animation-example.netlify.com/
Install
npm:
npm install --save react-hover-animation
yarn:
yarn add react-hover-animation
Usage
Basic usage
import React from 'react'import AnimationWrapper from 'react-hover-animation' const App = return <AnimationWrapper> <h1>I animate on hover</h1> </AnimationWrapper>
Default behavior
The wrapper component comes with a build-in behavior on hover,
Animation will play and change the element opacity from 1 to 0.6 and change the element scale from 1 to 0.95.
In order to change the default behavior, you can either pass the reset prop to reset these defaults or pass a config object with styles for initial and onHover state.
Optional props
style
: A style object for the wrapper component - styles will override the hover styles on the config object.config
: A config object to declare more styles to change on hover or overrides the default behavior,
must provide to each CSS property two values of the same type(initial and onHover).initial
: The initial style.onHover
: The style on hover.
reset
: A boolean that will reset the default behavior.animaionConfig
: Config for the animation, can be either a regular react-spring-config-object or a string with the name of one of the react-spring-config-presets (default, gentle, wobbly, stiff, slow, molasses).
Usage with props
import React from 'react'import AnimationWrapper from 'react-hover-animation' const App = return <AnimationWrapper /* "className" */ ='animation-wrapper' /* */ = /* ( ) */ = /* */ = /* */ = /* */ /* ='molasses' */ > <h1>I animate on hover</h1> </AnimationWrapper>
The useHover hook
If you don't want to render a wrapper div you can also import a custom hook and apply the animation directly on the element.
Optional config object can be passed as an argument to the hook with either styles or animation config object.
import React from 'react'import useHover from 'react-hover-animation' const App = /* first call the hook */ const spring animated setHover = return /* add 'animated' to the element */ <animated.h1 /* */ = /* */ = = > I animate on hover </animated.h1>
License
MIT © lulu70