{"id":13422279,"url":"https://github.com/joshwcomeau/react-flip-move","last_synced_at":"2025-05-13T16:11:22.920Z","repository":{"id":37412492,"uuid":"50727625","full_name":"joshwcomeau/react-flip-move","owner":"joshwcomeau","description":"Effortless animation between DOM changes (eg. list reordering) using the FLIP technique.","archived":false,"fork":false,"pushed_at":"2022-12-06T19:59:41.000Z","size":6047,"stargazers_count":4121,"open_issues_count":35,"forks_count":257,"subscribers_count":44,"default_branch":"master","last_synced_at":"2025-04-23T20:58:58.625Z","etag":null,"topics":["animation","flip","hardware-acceleration","react","react-flip-move","reactjs","transition-animation"],"latest_commit_sha":null,"homepage":"http://joshwcomeau.github.io/react-flip-move/examples","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joshwcomeau.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-30T14:15:41.000Z","updated_at":"2025-04-18T07:20:54.000Z","dependencies_parsed_at":"2023-01-23T15:00:22.599Z","dependency_job_id":null,"html_url":"https://github.com/joshwcomeau/react-flip-move","commit_stats":null,"previous_names":[],"tags_count":78,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshwcomeau%2Freact-flip-move","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshwcomeau%2Freact-flip-move/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshwcomeau%2Freact-flip-move/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshwcomeau%2Freact-flip-move/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joshwcomeau","download_url":"https://codeload.github.com/joshwcomeau/react-flip-move/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250514767,"owners_count":21443208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["animation","flip","hardware-acceleration","react","react-flip-move","reactjs","transition-animation"],"created_at":"2024-07-30T23:00:40.992Z","updated_at":"2025-04-23T20:59:08.729Z","avatar_url":"https://github.com/joshwcomeau.png","language":"JavaScript","funding_links":[],"categories":["UI Animation","Uncategorized","JavaScript","Ferramentas","目录","Drag and Drop"],"sub_categories":["Form Components","Uncategorized","GraphQL","React相关","Flip"],"readme":"React Flip Move\n=========\n\n[![build status](https://travis-ci.org/joshwcomeau/react-flip-move.svg?branch=master)](https://travis-ci.org/joshwcomeau/react-flip-move)\n[![npm version](https://img.shields.io/npm/v/react-flip-move.svg)](https://www.npmjs.com/package/react-flip-move)\n[![npm monthly downloads](https://img.shields.io/npm/dm/react-flip-move.svg)](https://www.npmjs.com/package/react-flip-move)\n\n\n\nThis module was built to tackle the common but arduous problem of animating a list of items when the list's order changes.\n\nCSS transitions only work for CSS properties. If your list is shuffled, the items have rearranged themselves, but without the use of CSS. The DOM nodes don't know that their on-screen location has changed; from their perspective, they've been removed and inserted elsewhere in the document.\n\nFlip Move uses the [_FLIP technique_](https://aerotwist.com/blog/flip-your-animations/#the-general-approach) to work out what such a transition would look like, and fakes it using 60+ FPS hardware-accelerated CSS transforms.\n\n[**Read more about how it works**](https://medium.com/developers-writing/animating-the-unanimatable-1346a5aab3cd)\n\n[![demo](https://s3.amazonaws.com/githubdocs/fm-main-demo.gif)](http://joshwcomeau.github.io/react-flip-move/examples/#/shuffle)\n\n\n## Current Status\n\nReact Flip Move is [looking for maintainers](https://github.com/joshwcomeau/react-flip-move/issues/233)!\n\nIn the meantime, we'll do our best to make sure React Flip Move continues to work with new versions of React, but otherwise it isn't being actively worked on.\n\nBecause it isn't under active development, you may be interested in checking out projects like [react-flip-toolkit](https://github.com/aholachek/react-flip-toolkit).\n\n\n## Demos\n\n  * \u003ca href=\"http://joshwcomeau.github.io/react-flip-move/examples/#/shuffle\" target=\"_blank\"\u003e__List/Grid Shuffle__\u003c/a\u003e\n  * \u003ca href=\"http://joshwcomeau.github.io/react-flip-move/examples/#/square\" target=\"_blank\"\u003e__Fuscia Square__\u003c/a\u003e\n  * \u003ca href=\"http://joshwcomeau.github.io/react-flip-move/examples/#/scrabble\" target=\"_blank\"\u003e__Scrabble__\u003c/a\u003e\n  * \u003ca href=\"http://joshwcomeau.github.io/react-flip-move/examples/#/laboratory\" target=\"_blank\"\u003e__Laboratory__\u003c/a\u003e\n\n\n\n## Installation\n\nFlip Move can be installed with [NPM](https://www.npmjs.com/) or [Yarn](https://yarnpkg.com/en/).\n\n```bash\nyarn add react-flip-move\n\n# Or, if not using yarn:\nnpm i -S react-flip-move\n```\n\nA UMD build is made available for those not using JS package managers:\n* [react-flip-move.js](https://unpkg.com/react-flip-move/dist/react-flip-move.js)\n* [react-flip-move.min.js](https://unpkg.com/react-flip-move/dist/react-flip-move.min.js)\n\nTo use a UMD build, you can use `\u003cscript\u003e` tags:\n\n```html\n\u003chtml\u003e\n  \u003cbody\u003e\n    \u003cscript src=\"https://unpkg.com/react-flip-move/dist/react-flip-move.js\"\u003e\u003c/script\u003e\n    \u003cscript\u003e\n      // Will be available under the global 'FlipMove'.\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n\n## Features\n\nFlip Move was inspired by Ryan Florence's awesome \u003ca href=\"https://github.com/ryanflorence/react-magic-move\" target=\"_blank\"\u003e_Magic Move_\u003c/a\u003e, and offers:\n\n  * Exclusive use of hardware-accelerated CSS properties (`transform: translate`) instead of positioning properties (`top`, `left`). \u003ca href=\"https://aerotwist.com/blog/pixels-are-expensive/\" target=\"_blank\"\u003e_Read why this matters_\u003c/a\u003e.\n\n  * Full support for enter/exit animations, including some spiffy presets, that all leverage hardware-accelerated CSS properties.\n\n  * Ability to 'humanize' transitions by staggering the delay and/or duration of subsequent elements.\n\n  * Ability to provide `onStart` / `onFinish` callbacks.\n\n  * Compatible with [Preact](https://preactjs.com/) (should work with other React-like libraries as well).\n\n  * Tiny! Gzipped size is \u003c5kb! ⚡\n\n\n## Quickstart\n\nFlip Move aims to be a \"plug and play\" solution, without needing a lot of tinkering. In the ideal case, you can wrap the children you already have with `\u003cFlipMove\u003e`, and get animation for free:\n\n```jsx\n/**\n * BEFORE:\n */\nconst TopArticles = ({ articles }) =\u003e (\n  {articles.map(article =\u003e (\n    \u003cArticle key={article.id} {...article} /\u003e\n  ))}\n);\n\n/**\n * AFTER:\n */\nimport FlipMove from 'react-flip-move';\n\nconst TopArticles = ({ articles }) =\u003e (\n  \u003cFlipMove\u003e\n    {articles.map(article =\u003e (\n      \u003cArticle key={article.id} {...article} /\u003e\n    ))}\n  \u003c/FlipMove\u003e\n);\n```\n\nThere are a number of [options](https://github.com/joshwcomeau/react-flip-move/blob/master/documentation/api_reference.md) you can provide to customize Flip Move. There are also some [gotchas](https://github.com/joshwcomeau/react-flip-move#gotchas) to be aware of.\n\n\n## Usage with Functional Components\n\nFunctional components do not have a `ref`, which is needed by Flip Move to work. To make it work you need to wrap your functional component into [React.forwardRef](https://reactjs.org/docs/forwarding-refs.html) and pass it down to the first element which accepts refs, such as DOM elements or class components:\n\n```jsx\nimport React, { forwardRef } from 'react';\nimport FlipMove from 'react-flip-move';\n\nconst FunctionalArticle = forwardRef((props, ref) =\u003e (\n  \u003cdiv ref={ref}\u003e\n    {props.articleName}\n  \u003c/div\u003e\n));\n\n// you do not have to modify the parent component\n// this will stay as described in the quickstart\nconst TopArticles = ({ articles }) =\u003e (\n  \u003cFlipMove\u003e\n    {articles.map(article =\u003e (\n      \u003cFunctionalArticle key={article.id} {...article} /\u003e\n    ))}\n  \u003c/FlipMove\u003e\n);\n```\n\n\n## API Reference\n\nView the [full API reference documentation](https://github.com/joshwcomeau/react-flip-move/blob/master/documentation/api_reference.md)\n\n\n## Enter/Leave Animations\n\nView the [enter/leave docs](https://github.com/joshwcomeau/react-flip-move/blob/master/documentation/enter_leave_animations.md)\n\n\n## Compatibility\n\n|           | Chrome | Firefox | Safari |   IE  | Edge | iOS Safari/Chrome | Android Chrome |\n|-----------|:------:|:-------:|:------:|:-----:|:----:|:-----------------:|:--------------:|\n| Supported |  ✔ 10+ |   ✔ 4+  | ✔ 6.1+ | ✔ 10+ |   ✔  |       ✔ 6.1+      |        ✔       |\n\n\n## How It Works\n\nCurious how this works, under the hood? [__Read the Medium post__](https://medium.com/@joshuawcomeau/animating-the-unanimatable-1346a5aab3cd).\n\n\n---\n\n### Wrapping Element\n\nBy default, Flip Move wraps the children you pass it in a `\u003cdiv\u003e`:\n\n```jsx\n// JSX\n\u003cFlipMove\u003e\n  \u003cdiv key=\"a\"\u003eHello\u003c/div\u003e\n  \u003cdiv key=\"b\"\u003eWorld\u003c/div\u003e\n\u003c/FlipMove\u003e\n\n// HTML\n\u003cdiv\u003e\n  \u003cdiv\u003eHello\u003c/div\u003e\n  \u003cdiv\u003eWorld\u003c/div\u003e\n\u003c/div\u003e\n```\n\nAny unrecognized props to `\u003cFlipMove\u003e` will be delegated to this wrapper element:\n\n```jsx\n// JSX\n\u003cFlipMove className=\"flip-wrapper\" style={{ color: 'red' }}\u003e\n  \u003cdiv key=\"a\"\u003eHello\u003c/div\u003e\n  \u003cdiv key=\"b\"\u003eWorld\u003c/div\u003e\n\u003c/FlipMove\u003e\n\n// HTML\n\u003cdiv class=\"flip-wrapper\" style=\"color: red;\"\u003e\n  \u003cdiv key=\"a\"\u003eHello\u003c/div\u003e\n  \u003cdiv key=\"b\"\u003eWorld\u003c/div\u003e\n\u003c/div\u003e\n```\n\nYou can supply a different element type with the `typeName` prop:\n\n```jsx\n// JSX\n\u003cFlipMove typeName=\"ul\"\u003e\n  \u003cli key=\"a\"\u003eHello\u003c/li\u003e\n  \u003cli key=\"b\"\u003eWorld\u003c/li\u003e\n\u003c/FlipMove\u003e\n\n// HTML\n\u003cul\u003e\n  \u003cli key=\"a\"\u003eHello\u003c/li\u003e\n  \u003cli key=\"b\"\u003eWorld\u003c/li\u003e\n\u003c/ul\u003e\n```\n\nFinally, if you're using React 16 or higher, and Flip Move 2.10 or higher, you can use the new \"wrapperless\" mode. This takes advantage of a React Fiber feature, which allows us to omit this wrapping element:\n\n```jsx\n// JSX\n\u003cdiv className=\"your-own-element\"\u003e\n  \u003cFlipMove typeName={null}\u003e\n    \u003cdiv key=\"a\"\u003eHello\u003c/div\u003e\n    \u003cdiv key=\"b\"\u003eWorld\u003c/div\u003e\n  \u003c/FlipMove\u003e\n\u003c/div\u003e\n\n// HTML\n\u003cdiv class=\"your-own-element\"\u003e\n  \u003cdiv key=\"a\"\u003eHello\u003c/div\u003e\n  \u003cdiv key=\"b\"\u003eWorld\u003c/div\u003e\n\u003c/div\u003e\n```\n\nWrapperless mode is nice, because it makes Flip Move more \"invisible\", and makes it easier to integrate with parent-child CSS properties like flexbox. However, there are some things to note:\n\n- This is a new feature in FlipMove, and isn't as battle-tested as the traditional method. Please test thoroughly before using in production, and report any bugs!\n- Flip Move does some positioning magic for enter/exit animations - specifically, it temporarily applies `position: absolute` to its children. For this to work correctly, you'll need to make sure that `\u003cFlipMove\u003e` is within a container that has a non-static position (eg. `position: relative`), and no padding:\n\n```jsx\n// BAD - this will cause children to jump to a new position before exiting:\n\u003cdiv style={{ padding: 20 }}\u003e\n  \u003cFlipMove typeName={null}\u003e\n    \u003cdiv key=\"a\"\u003eHello world\u003c/div\u003e\n  \u003c/FlipMove\u003e\n\u003c/div\u003e\n\n// GOOD - a non-static position and a tight-fitting wrapper means children will\n// stay in place while exiting:\n\u003cdiv style={{ position: 'relative' }}\u003e\n  \u003cFlipMove typeName={null}\u003e\n    \u003cdiv key=\"a\"\u003eHello world\u003c/div\u003e\n  \u003c/FlipMove\u003e\n\u003c/div\u003e\n```\n\n---\n\n\n## Gotchas\n\n  * Does not work with stateless functional components without a [React.forwardRef](https://reactjs.org/docs/forwarding-refs.html), read more about [here](#usage-with-functional-components). This is because Flip Move uses refs to identify and apply styles to children, and stateless functional components cannot be given refs. Make sure the children you pass to `\u003cFlipMove\u003e` are either native DOM elements (like `\u003cdiv\u003e`), or class components.\n\n  * All children **need a unique `key` property**. Even if Flip Move is only given a single child, it needs to have a unique `key` prop for Flip Move to track it.\n\n  * Flip Move clones the direct children passed to it and overwrites the `ref` prop. As a result, you won't be able to set a `ref` on the top-most elements passed to FlipMove. To work around this limitation, you can wrap each child you pass to `\u003cFlipMove\u003e` in a `\u003cdiv\u003e`.\n\n  * Elements whose positions have not changed between states will not be animated. This means that no `onStart` or `onFinish` callbacks will be executed for those elements.\n\n  * Sometimes you'll want to update or change an item _without_ triggering a Flip Move animation. For example, with optimistic updating, you may render a temporary version before replacing it with the server-validated one. In this case, use the same `key` for both versions, and Flip Move will treat them as the same item.\n\n  * If you have a vertical list with numerous elements, exceeding viewport, and you are experiencing automatic scrolling issues when reordering an item (i.e. the browser scrolls to the moved item's position), you can add `style={{ overflowAnchor: 'none' }}` to the container element (e.g. `\u003cul\u003e`) to prevent this issue.\n\n## Known Issues\n\n  * **Interrupted enter/leave animations can be funky**. This has gotten better recently thanks to our great contributors, but extremely fast adding/removing of items can cause weird visual glitches, or cause state to become inconsistent. Experiment with your usecase!\n\n  * **Existing transition/transform properties will be overridden.** I am hoping to change this in a future version, but at present, Flip Move does not take into account existing `transition` or `transform` CSS properties on its direct children.\n\n\n## Note on `will-change`\n\nTo fully benefit from hardware acceleration, each item being translated should have its own compositing layer. This can be accomplished with the [CSS will-change property](https://dev.opera.com/articles/css-will-change-property/).\n\nApplying `will-change` too willy-nilly, though, can have an adverse effect on mobile browsers, so I have opted to not use it at all.\n\nIn my personal experimentations on modern versions of Chrome, Safari, Firefox and IE, this property offers little to no gain (in Chrome's timeline I saw a savings of ~0.5ms on a 24-item shuffle).\n\nYMMV: Feel free to experiment with the property in your CSS. Flip Move will respect the wishes of your stylesheet :)\n\nFurther reading: [CSS will-change Property](https://dev.opera.com/articles/css-will-change-property/)\n\n\n\n## Contributions\n\nContributors welcome! Please discuss new features with me ahead of time, and submit PRs for bug fixes with tests (Testing stack is Mocha/Chai/Sinon, tested in-browser by Karma).\n\nThere is a shared prepush hook which launches eslint, flow checks, and tests. It sets itself up automatically during `npm install`.\n\n\n## Development\n\nThis project uses [React Storybook](https://github.com/kadirahq/react-storybook) in development. The developer experience is absolutely lovely, and it makes testing new features like enter/leave presets super straightforward.\n\nAfter installing dependencies, launch the Storybook dev server with `npm run storybook`.\n\nThis project adheres to the formatting established by [airbnb's style guide](https://github.com/airbnb/javascript/tree/master/react). When contributing, you can make use of the autoformatter [prettier](https://github.com/prettier/prettier) to apply these rules by running the eslint script `npm run lint:fix`. If there are conflicts, the linter triggered by the prepush hook will inform you of those as well. To check your code by hand, run `npm run lint`.\n\n## Flow support\n\nFlip Move's sources are type-checked with [Flow](https://flow.org/). If your project uses it too, you may want to install typings for our public API from [flow-typed](https://github.com/flowtype/flow-typed) repo.\n\n```bash\nnpm install --global flow-typed # if not already\nflow-typed install react-flip-move@\u003cversion\u003e\n```\n\nIf you're getting some flow errors coming from `node_modules/react-flip-move/src` path, you should add this to your `.flowconfig` file:\n\n```\n[ignore]\n.*/node_modules/react-flip-move/.*\n```\n\n\n## License\n\n[MIT](https://github.com/joshwcomeau/flip-move/blob/master/LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshwcomeau%2Freact-flip-move","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoshwcomeau%2Freact-flip-move","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshwcomeau%2Freact-flip-move/lists"}