Gathering detailed insights and metrics for react-flip-toolkit
Gathering detailed insights and metrics for react-flip-toolkit
Gathering detailed insights and metrics for react-flip-toolkit
Gathering detailed insights and metrics for react-flip-toolkit
vue-flip-toolkit
A Vue.js port of the inimitable react-flip-toolkit
solid-flip
The most advanced Flip animation library for Solid.js inspired by react-flip-toolkit
react-flip-move
Effortless animation between DOM changes (eg. list reordering) using the FLIP technique.
flip-toolkit
Configurable FLIP animation helpers
npm install react-flip-toolkit
[v7.2.4] Default to ESM export for time being
Published on 06 Jul 2024
[v7.2.0] Add global disableFlip function
Published on 05 Jul 2024
Respect prefers-reduced-motion
Published on 14 May 2023
Add onStart prop to Flipper
Published on 30 Sept 2022
Fix UMD Builds
Published on 04 Sept 2022
v7.0.14
Published on 13 May 2022
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
4,091 Stars
842 Commits
139 Forks
34 Watching
56 Branches
14 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (39.46%)
JavaScript (30.42%)
CSS (16.85%)
HTML (13.27%)
Cumulative downloads
Total Downloads
Last day
-15.5%
14,680
Compared to previous day
Last week
-3.4%
79,207
Compared to previous week
Last month
9.3%
346,258
Compared to previous month
Last year
51.5%
3,219,420
Compared to previous year
2
35
Comparison with other React FLIP libraries
Feature | react-flip-move | react-overdrive | react-flip-toolkit |
---|---|---|---|
Animate position | ✅ | ✅ | ✅ |
Animate scale | ❌ | ✅ | ✅ |
Animate opacity | ❌ | ✅ | ✅ |
Animate parent's size without warping children | ❌ | ❌ | ✅ |
Use real FLIP instead of cloning & crossfading | ✅ | ❌ | ✅ |
Use springs for animations | ❌ | ❌ | ✅ |
Support spring-based stagger effects | ❌ | ❌ | ✅ |
Usable with frameworks other than React | ❌ | ❌ | ✅ |
npm install react-flip-toolkit
or yarn add react-flip-toolkit
Wrap all animated children with a single Flipper
component that has a flipKey
prop that changes every time animations should happen.
Wrap elements that should be animated with Flipped
components that have a flipId
prop matching them across renders.
Fork this example on Code Sandbox
1import React, { useState } from 'react' 2import { Flipper, Flipped } from 'react-flip-toolkit' 3 4const AnimatedSquare = () => { 5 const [fullScreen, setFullScreen] = useState(false) 6 const toggleFullScreen = () => setFullScreen(prevState => !prevState) 7 8 return ( 9 <Flipper flipKey={fullScreen}> 10 <Flipped flipId="square"> 11 <div 12 className={fullScreen ? 'full-screen-square' : 'square'} 13 onClick={toggleFullScreen} 14 /> 15 </Flipped> 16 </Flipper> 17 ) 18}
Fork this example on Code Sandbox
1import React, { useState } from 'react' 2import { Flipper, Flipped } from 'react-flip-toolkit' 3 4const Square = ({ toggleFullScreen }) => ( 5 <Flipped flipId="square"> 6 <div className="square" onClick={toggleFullScreen} /> 7 </Flipped> 8) 9 10const FullScreenSquare = ({ toggleFullScreen }) => ( 11 <Flipped flipId="square"> 12 <div className="full-screen-square" onClick={toggleFullScreen} /> 13 </Flipped> 14) 15 16const AnimatedSquare = () => { 17 const [fullScreen, setFullScreen] = useState(false) 18 const toggleFullScreen = () => setFullScreen(prevState => !prevState) 19 20 return ( 21 <Flipper flipKey={fullScreen}> 22 {fullScreen ? ( 23 <FullScreenSquare toggleFullScreen={toggleFullScreen} /> 24 ) : ( 25 <Square toggleFullScreen={toggleFullScreen} /> 26 )} 27 </Flipper> 28 ) 29}
Fork this example on Code Sandbox
1import React, { useState } from 'react' 2import { Flipper, Flipped } from 'react-flip-toolkit' 3import shuffle from 'lodash.shuffle' 4 5const ListShuffler = () => { 6 const [data, setData] = useState([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 7 const shuffleList = () => setData(shuffle(data)) 8 9 return ( 10 <Flipper flipKey={data.join('')}> 11 <button onClick={shuffleList}> shuffle</button> 12 <ul className="list"> 13 {data.map(d => ( 14 <Flipped key={d} flipId={d}> 15 <li>{d}</li> 16 </Flipped> 17 ))} 18 </ul> 19 </Flipper> 20 ) 21}
Add some interest to a dynamic list of cards by animating changes to cards' sizes and positions.
Fork this example on Code Sandbox
The react-flip-toolkit
library offers spring-driven stagger configurations so that you can achieve complex sequenced effects.
For the most basic stagger effect, you can simply add a stagger
boolean prop to your Flipped
element:
1<Flipped flipId={`element-${i}`} stagger> 2 <AnimatedListItem/> 3</Flipped>
Fork this example on Code Sandbox
react-flip-toolkit
uses springs for animations. To customize the spring, you can pass in a preset name:
1// spring preset can be one of: "stiff", "noWobble", "gentle", "veryGentle", or "wobbly" 2<Flipper flipKey='foo' spring='wobbly'> 3 {/* Flipped components go here...*/} 4</Flipper>
or a custom spring config:
1<Flipper flipKey='foo' spring={{ stiffness: 280, damping: 22 }} > 2 {/* Flipped components go here...*/} 3</Flipper>
View all spring options in the interactive explorer
Interesting animations often involve scale transforms in addition to simple translate transforms. The problem with scale animations has to do with children — if you scale a div up 2x, you will warp any children it has by scaling them up too, creating a weird-looking animation. That's why this library allows you to wrap the child with a Flipped
component that has an inverseFlipId
to counteract the transforms of the parent:
1<Flipped flipId={id}> 2 <div> 3 <Flipped inverseFlipId={id} scale> 4 <div>some text that will not be warped</div> 5 </Flipped> 6 </div> 7</Flipped>
By default, both the scale and the translation transforms of the parent will be counteracted (this allows children components to make their own FLIP animations without being affected by the parent).
But for many use cases, you'll want to additionally specify the scale
prop to limit the adjustment to the scale and allow the positioning to move with the parent.
Note: the DOM element with the inverse transform should lie flush against its parent container for the most seamless animation.
That means any layout styles — padding, flexbox, etc—should be applied to the inverted container (the element wrapped with a Flipped
component with an inverseFlipId
) rather than the parent Flipped
container.
react-flip-toolkit
works great with client-side routers to provide route-driven transitions:
1<Route 2 render={({ location, search }) => { 3 return ( 4 <Flipper 5 flipKey={`${location.pathname}-${location.search}`} 6 > 7 {/* Child routes that contain Flipped components go here...*/} 8 </Flipper> 9 ) 10 }} 11/>
Flipper
The parent wrapper component that contains all the elements to be animated. You'll often need only one of these per page, but sometimes it will be more convenient to have multiple Flipper
regions of your page concerned with different transitions.
1<Flipper flipKey={someKeyThatChanges}>{/* children */}</Flipper>
prop | default | type | details |
---|---|---|---|
flipKey (required) | - | string , number , bool | Changing this tells react-flip-toolkit to transition child elements wrapped in Flipped components. |
children (required) | - | node | One or more element children |
spring | noWobble | string or object | Provide a string referencing one of the spring presets — noWobble (default), veryGentle , gentle , wobbly , or stiff , OR provide an object with stiffness and damping parameters. Explore the spring setting options here. The prop provided here will be the spring default that can be overrided on a per-element basis on the Flipped component. |
applyTransformOrigin | true | bool | Whether or not react-flip-toolkit should apply a transform-origin of "0 0" to animating children (this is generally, but not always, desirable for FLIP animations) |
element | div | string | If you'd like the wrapper element created by the Flipped container to be something other than a div , you can specify that here. |
className | - | string | A class applied to the wrapper element, helpful for styling. |
staggerConfig | - | object | Provide configuration for staggered Flipped children. The config object might look something like the code snippet below: |
1staggerConfig={{ 2 // the "default" config will apply to staggered elements without explicit keys 3 default: { 4 // default direction is forwards 5 reverse: true, 6 // default is .1, 0 < n < 1 7 speed: .5 8 }, 9 // this will apply to Flipped elements with the prop stagger='namedStagger' 10 namedStagger : { speed: .2 } 11 }}
prop | default | type | details |
---|---|---|---|
decisionData | - | any | Sometimes, you'll want the animated children of Flipper to behave differently depending on the state transition — maybe only certain Flipped elements should animate in response to a particular change. By providing the decisionData prop to the Flipper component, you'll make that data available to the shouldFlip and shouldInvert methods of each child Flipped component, so they can decided for themselves whether to animate or not. |
debug | false | boolean | This experimental prop will pause your animation right at the initial application of FLIP-ped styles. That will allow you to inspect the state of the animation at the very beginning, when it should look similar or identical to the UI before the animation began. |
portalKey | - | string | In general, the Flipper component will only apply transitions to its descendents. This allows multiple Flipper elements to coexist on the same page, but it will prevent animations from working if you use portals. You can provide a unique portalKey prop to Flipper to tell it to scope element selections to the entire document, not just to its children, so that elements in portals can be transitioned. |
onStart | - | function | This callback prop will be called before any of the individual FLIP animations have started. It receives as arguments the HTMLElement of the Flipper and the decisionData object described elsewhere. |
onComplete | - | function | This callback prop will be called when all individual FLIP animations have completed. Its single argument is a list of flipId s for the Flipped components that were activated during the animation. If an animation is interrupted, onComplete will be still called right before the in-progress animation is terminated. |
handleEnterUpdateDelete | - | function | By default, react-flip-toolkit finishes animating out exiting elements before animating in new elements, with updating elements transforming immediately. You might want to have more control over the sequence of transitions — say, if you wanted to hide elements, pause, update elements, pause again, and finally animate in new elements. Or you might want transitions to happen simultaneously. If so, provide the function handleEnterUpdateDelete as a prop. The best way to understand how this works is to check out this interactive example. handleEnterUpdateDelete receives the following arguments every time a transition occurs: |
1handleEnterUpdateDelete({
2 // this func applies an opacity of 0 to entering elements so
3 // they can be faded in - it should be called immediately
4 hideEnteringElements,
5 // calls `onAppear` for all entering elements
6 animateEnteringElements,
7 //calls `onExit` for all exiting elements
8 // returns a promise that resolves when all elements have exited
9 animateExitingElements,
10 // the main event: `FLIP` animations for updating elements
11 // this also returns a promise that resolves when
12 // animations have completed
13 animateFlippedElements
14})
Flipped
Wraps an element that should be animated.
E.g. in one component you can have
1<Flipped flipId="coolDiv"> 2 <div className="small" /> 3</Flipped>
and in another component somewhere else you can have
1<Flipped flipId="coolDiv"> 2 <div className="big" /> 3</Flipped>
and they will be tweened by react-flip-toolkit
.
The Flipped
component produces no markup, it simply passes some props down to its wrapped child.
If you want to wrap a React component rather than a JSX element like a div
, you can provide a render prop and then apply the flippedProps
directly to the wrapped element in your component:
1<Flipped> 2 {flippedProps => <MyCoolComponent flippedProps={flippedProps} />} 3</Flipped> 4 5const MyCoolComponent = ({ flippedProps }) => <div {...flippedProps} />
You can also simply provide a regular React component as long as that component spreads unrecognized props directly onto the wrapped element (this technique works well for wrapping styled components):
1<Flipped> 2 <MyCoolComponent /> 3</Flipped> 4 5const MyCoolComponent = ({ knownProp, ...rest }) => <div {...rest} />
prop | default | type | details |
---|---|---|---|
children (required) | - | node or function | Wrap a single element, React component, or render prop child with the Flipped component |
flipId (required unless inverseFlipId is provided) | - | string | Use this to tell react-flip-toolkit how elements should be matched across renders so they can be animated. |
inverseFlipId | - | string | Refer to the id of the parent Flipped container whose transform you want to cancel out. If this prop is provided, the Flipped component will become a limited version of itself that is only responsible for cancelling out its parent transform. It will read from any provided transform props and will ignore all other props (besides inverseFlipId .) Read more about canceling out parent transforms here. |
transformOrigin | "0 0" | string | This is a convenience method to apply the proper CSS transform-origin to the element being FLIP-ped. This will override react-flip-toolkit 's default application of transform-origin: 0 0; if it is provided as a prop. |
spring | noWobble | string or object | Provide a string referencing one of the spring presets — noWobble (default), veryGentle , gentle , wobbly , or stiff , OR provide an object with stiffness and damping parameters. Explore the spring setting options here. |
stagger | false | boolean or string | Provide a natural, spring-based staggering effect in which the spring easing of each item is pinned to the previous one's movement. Provide true to stagger the element with all other staggered elements. If you want to get more granular, you can provide a string key and the element will be staggered with other elements with the same key. |
delayUntil | false | string (flipId) | Delay an animation by providing a reference to another Flipped component that it should wait for before animating (the other Flipped component should have a stagger delay as that is the only use case in which this prop is necessary.) |
The above animation uses onAppear
and onExit
callbacks for fade-in and fade-out animations.
prop | arguments | details |
---|---|---|
onAppear | element , index , {previous: decisionData, current: decisionData } | Called when the element first appears in the DOM. It is provided a reference to the DOM element being transitioned as the first argument, and the index of the element relative to all appearing elements as the second. Note: If you provide an onAppear prop, the default opacity of the element will be set to 0 to allow you to animate it in without any initial flicker. If you don't want any opacity animation, just set the element's opacity to 1 immediately in your onAppear function. |
onStart | element , {previous: decisionData, current: decisionData } | Called when the FLIP animation for the element starts. It is provided a reference to the DOM element being transitioned as the first argument. |
onStartImmediate | element , {previous: decisionData, current: decisionData } | Similar to onStart , but guaranteed to run for all FLIP-ped elements on the initial tick of the FLIP animation, before the next frame has rendered, even if the element in question has a stagger delay. It is provided a reference to the DOM element being transitioned as the first argument. |
onSpringUpdate | springValue | Called with the current spring value (normally between 0 - 1 but might briefly go over or under that range depending on the level of "bounciness" of the spring). Useful if you'd like to tween other, non-FLIP animations in concert with a FLIP transition. |
onComplete | element ,{previous: decisionData, current: decisionData } | Called when the FLIP animation completes. It is provided a reference to the DOM element being transitioned as the first argument. (If transitions are interruped by new ones, onComplete will still be called.) |
onExit | element , index , removeElement , {previous: decisionData, current: decisionData } | Called when the element is removed from the DOM. It must call the removeElement function when the exit transition has completed. |
By default the FLIP-ped elements' translate, scale, and opacity properties are all transformed. However, certain effects require more control so if you specify any of these props, only the specified attribute(s) will be tweened:
prop | type | details |
---|---|---|
translate | bool | Tween translateX and translateY |
scale | bool | Tween scaleX and scaleY |
opacity | bool |
Functions to control when FLIP happens
prop | arguments | details |
---|---|---|
shouldFlip | previousDecisionData , currentDecisionData | A function provided with the current and previous decisionData props passed down by the Flipper component. Returns a boolean to indicate whether a Flipped component should animate at that particular moment or not. |
shouldInvert | previousDecisionData , currentDecisionData | A function provided with the current and previous decisionData props passed down by the Flipper component. Returns a boolean indicating whether to apply inverted transforms to all Flipped children that request it via an inverseFlipId . |
Spring
As a convenience, react-flip-toolkit
exports a tiny function to access the same spring system used to create FLIP transitions.
1import { spring } from 'react-flip-toolkit' 2 3spring({ 4 config: "wobbly", 5 values: { 6 translateY: [-15, 0], 7 opacity: [0, 1] 8 }, 9 onUpdate: ({ translateY, opacity }) => { 10 el.style.opacity = opacity; 11 el.style.transform = `translateY(${translateY}px)`; 12 }, 13 delay: i * 25, 14 onComplete: () => console.log('done') 15});
You can programmatically call the following functions if you need to disable (or re-enable) FLIP animations everywhere.
disableFlip()
Global switch to disable all animations in all Flipper
containers.
enableFlip()
Global switch to (re-)enable all animations in all Flipper
containers. Animations are enabled by default. Calling this function is needed only if animations were previously disabled with disableFlip()
.
isFlipEnabled()
Returns a boolean indicating whether animations are globally enabled or disabled.
flipKey
attribute in the Flipper
component whenever an animation should happen.Flipped
components is wrapping another React component rather than a DOM element, use a render prop to get the Flipped props and pass down to the necessary DOM element.Flipped
visible in the DOM? react-flip-toolkit
attempts to optimize performance by not animating elements that are off-screen or elements that have no width or height.display:inline
elements cannot be animated. If you want an inline
element to animate, set display:inline-block
.flipId
s are unique. At any point, there can only be one element with a specified flipId
on the page. If there are multiple Flipped
elements on the page with the same id, the animation will break.div
, you're actually animating the div, which might have a much wider width that you'd expect at certain points, which will throw off the animation. Check to see if you need to add an inline-block
style to the animated element.debug
prop. If you still can't figure out what's going wrong, you can add the the debug
prop directly on your Flipper
component to pause transitions at the beginning.React-flip-toolkit
does a lot of work under the hood to try to maximize the performance of your animations — for instance, off-screen elements won't be animated, and style updates are batched to prevent layout thrashing.
However, if you are building particularly complex animations—ones that involve dozens of elements or large images— there are some additional strategies you can use to ensure performant animations.
Memoization
When you trigger a complex FLIP animation with react-flip-toolkit
, React
could be spending vital milliseconds doing unnecessary reconciliation work before allowing the animation to start. If you notice a slight delay between when the animation is triggered, and when it begins, this is probably the culprit. To short-circuit this possibly unnecessary work, try memoizing your component by using React.memo
or PureComponent
for your animated elements, and seeing if you can refactor your code to minimize prop updates to animated children when an animation is about to occur.
will-change:transform
1.box { 2 will-change: transform; 3}
This CSS property tells the browser to anticipate changes to an element. It should be used with caution, because it can increase browser resource usage. If you notice rendering issues in your animation, try seeing if it increases the performance of the animation.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
61 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More