Skip to content

feat: add exitBeforeEnter prop to useTransition #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/cool-avocados-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@react-spring/core': minor
'@react-spring/animated': minor
'@react-spring/parallax': minor
'@react-spring/rafz': minor
'react-spring': minor
'@react-spring/shared': minor
'@react-spring/types': minor
'@react-spring/konva': minor
'@react-spring/native': minor
'@react-spring/three': minor
'@react-spring/web': minor
'@react-spring/zdog': minor
---

add experimental exit before enter
29 changes: 15 additions & 14 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
{
"packages": ["packages/*", "targets/*"],
"sandboxes": [
"/demo/src/sandboxes/animating-auto",
"/demo/src/sandboxes/card",
"/demo/src/sandboxes/goo-blobs",
"/demo/src/sandboxes/flip-card",
"/demo/src/sandboxes/slide",
"/demo/src/sandboxes/draggable-list",
"/demo/src/sandboxes/cards-stack",
"/demo/src/sandboxes/viewpager",
"/demo/src/sandboxes/simple-transition",
"/demo/src/sandboxes/chain",
"/demo/src/sandboxes/css-keyframes",
"/demo/src/sandboxes/draggable-list",
"/demo/src/sandboxes/exit-before-enter",
"/demo/src/sandboxes/flip-card",
"/demo/src/sandboxes/goo-blobs",
"/demo/src/sandboxes/image-fade",
"/demo/src/sandboxes/list-reordering",
"/demo/src/sandboxes/masonry",
"/demo/src/sandboxes/animating-auto",
"/demo/src/sandboxes/multistage-transition",
"/demo/src/sandboxes/chain",
"/demo/src/sandboxes/train",
"/demo/src/sandboxes/svg-filter",
"/demo/src/sandboxes/css-keyframes",
"/demo/src/sandboxes/tree",
"/demo/src/sandboxes/notification-hub",
"/demo/src/sandboxes/rocket-decay",
"/demo/src/sandboxes/parallax",
"/demo/src/sandboxes/parallax-vert"
"/demo/src/sandboxes/parallax-vert",
"/demo/src/sandboxes/rocket-decay",
"/demo/src/sandboxes/simple-transition",
"/demo/src/sandboxes/svg-filter",
"/demo/src/sandboxes/slide",
"/demo/src/sandboxes/train",
"/demo/src/sandboxes/tree",
"/demo/src/sandboxes/viewpager"
],
"node": "14"
}
63 changes: 39 additions & 24 deletions demo/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,69 @@ import React from 'react'
import { Link, Route } from 'wouter'
import styles from './styles.module.css'

import GooBlobs from './sandboxes/goo-blobs/src/App'
import AnimatingAuto from './sandboxes/animating-auto/src/App'

import Card from './sandboxes/card/src/App'
import FlipCard from './sandboxes/flip-card/src/App'
import Slide from './sandboxes/slide/src/App'
import DraggableList from './sandboxes/draggable-list/src/App'
import CardsStack from './sandboxes/cards-stack/src/App'
import Viewpager from './sandboxes/viewpager/src/App'
import SimpleTransition from './sandboxes/simple-transition/src/App'
import Chain from './sandboxes/chain/src/App'
import CssKeyframes from './sandboxes/css-keyframes/src/App'

import DecayRocket from './sandboxes/rocket-decay/src/App'
import DraggableList from './sandboxes/draggable-list/src/App'

import ExitBeforeEnter from './sandboxes/exit-before-enter/src/App'

import FlipCard from './sandboxes/flip-card/src/App'

import GooBlobs from './sandboxes/goo-blobs/src/App'

import ImageFade from './sandboxes/image-fade/src/App'

import ListReordering from './sandboxes/list-reordering/src/App'
import Chain from './sandboxes/chain/src/App'

import Masonry from './sandboxes/masonry/src/App'
import AnimatingAuto from './sandboxes/animating-auto/src/App'
import MultiStageTransition from './sandboxes/multistage-transition/src/App'
import Trail from './sandboxes/trail/src/App'
import SvgFilter from './sandboxes/svg-filter/src/App'
import CssKeyframes from './sandboxes/css-keyframes/src/App'

import NotificationHub from './sandboxes/notification-hub/src/App'
import Tree from './sandboxes/tree/src/App'
import DecayRocket from './sandboxes/rocket-decay/src/App'

import Parallax from './sandboxes/parallax/src/App'
import ParallaxVert from './sandboxes/parallax-vert/src/App'
import ParallaxSticky from './sandboxes/parallax-sticky/src/App'

import SimpleTransition from './sandboxes/simple-transition/src/App'
import Slide from './sandboxes/slide/src/App'
import SvgFilter from './sandboxes/svg-filter/src/App'

import Trail from './sandboxes/trail/src/App'
import Tree from './sandboxes/tree/src/App'

import Viewpager from './sandboxes/viewpager/src/App'

const links = {
'animating-auto': AnimatingAuto,
card: Card,
'cards-stack': CardsStack,
chain: Chain,
'css-keyframes': CssKeyframes,
'decay-rocket': DecayRocket,
'draggable-list': DraggableList,
'exit-before-enter': ExitBeforeEnter,
'flip-card': FlipCard,
'goo-blobs': GooBlobs,
slide: Slide,
'draggable-list': DraggableList,
'cards-stack': CardsStack,
viewpager: Viewpager,
'simple-transition': SimpleTransition,
'image-fade': ImageFade,
'list-reordering': ListReordering,
masonry: Masonry,
'animating-auto': AnimatingAuto,
'multistage-transition': MultiStageTransition,
trail: Trail,
'svg-filter': SvgFilter,
'css-keyframes': CssKeyframes,
'notification-hub': NotificationHub,
tree: Tree,
'decay-rocket': DecayRocket,
parallax: Parallax,
'parallax-vert': ParallaxVert,
'parallax-sticky': ParallaxSticky,
'simple-transition': SimpleTransition,
slide: Slide,
'svg-filter': SvgFilter,
trail: Trail,
tree: Tree,
viewpager: Viewpager,
}

const Example = ({ link }) => {
Expand Down
29 changes: 29 additions & 0 deletions demo/src/sandboxes/exit-before-enter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "spring-exit-before-enter",
"version": "1.0.0",
"main": "src/index.tsx",
"dependencies": {
"@react-spring/web": "*",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"bic": false,
"devDependencies": {
"@types/react": "^17.0.2",
"@types/react-dom": "^17.0.1",
"typescript": "^4.2.3"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions demo/src/sandboxes/exit-before-enter/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React Spring Sandbox</title>
</head>

<body>
<noscript> You need to enable JavaScript to run this app. </noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
81 changes: 81 additions & 0 deletions demo/src/sandboxes/exit-before-enter/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useEffect, useLayoutEffect, useState } from 'react'
import {
useSpringRef,
animated,
useSpring,
useTransition,
config,
} from '@react-spring/web'

import styles from './styles.module.css'

import IMG_01 from '../public/img_01.jpg'
import IMG_02 from '../public/img_02.png'
import IMG_03 from '../public/img_03.jpeg'

const IMAGES = [IMG_01, IMG_02, IMG_03]

export default function App() {
const [activeIndex, setActiveIndex] = useState(0)
const springApi = useSpringRef()

const transitions = useTransition(activeIndex, {
from: {
clipPath: 'polygon(0% 0%, 0% 100%, 0% 100%, 0% 0%)',
},
enter: {
clipPath: 'polygon(0% 0%, 0% 100%, 100% 100%, 100% 0%)',
},
leave: {
clipPath: 'polygon(100% 0%, 100% 100%, 100% 100%, 100% 0%)',
},
onRest: (_springs, _ctrl, item) => {
if (activeIndex === item) {
setActiveIndex(activeIndex === IMAGES.length - 1 ? 0 : activeIndex + 1)
}
},
exitBeforeEnter: true,
config: {
duration: 4000,
},
ref: springApi,
})

const springs = useSpring({
from: {
strokeDashoffset: 120,
},
to: {
strokeDashoffset: 0,
},
config: {
duration: 8000,
},
loop: true,
ref: springApi,
})

useLayoutEffect(() => {
springApi.start()
}, [activeIndex])

return (
<div className={styles.container}>
{transitions((springs, item) => (
<animated.div className={styles.img__container} style={springs}>
<img src={IMAGES[item]} />
</animated.div>
))}
<div className={styles.ticker}>
<div />
<animated.svg
width="40"
height="40"
viewBox="0 0 40 40"
style={springs}>
<path d="M19.9999 38.5001C17.5704 38.5001 15.1648 38.0216 12.9203 37.0919C10.6758 36.1622 8.63633 34.7995 6.91845 33.0816C5.20058 31.3638 3.83788 29.3243 2.90817 27.0798C1.97846 24.8353 1.49995 22.4296 1.49995 20.0002C1.49995 17.5707 1.97846 15.1651 2.90817 12.9206C3.83788 10.6761 5.20058 8.63663 6.91846 6.91875C8.63634 5.20087 10.6758 3.83818 12.9203 2.90847C15.1648 1.97876 17.5705 1.50024 19.9999 1.50024C22.4293 1.50024 24.835 1.97876 27.0795 2.90847C29.324 3.83818 31.3635 5.20088 33.0813 6.91876C34.7992 8.63663 36.1619 10.6761 37.0916 12.9206C38.0213 15.1651 38.4998 17.5707 38.4998 20.0002C38.4998 22.4296 38.0213 24.8353 37.0916 27.0798C36.1619 29.3243 34.7992 31.3638 33.0813 33.0816C31.3635 34.7995 29.324 36.1622 27.0795 37.0919C24.835 38.0216 22.4293 38.5001 19.9999 38.5001L19.9999 38.5001Z" />
</animated.svg>
</div>
</div>
)
}
30 changes: 30 additions & 0 deletions demo/src/sandboxes/exit-before-enter/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
html,
body,
#root {
height: 100%;
width: 100%;
}

body {
font-family: system-ui;
margin: 0;
}

*,
*:after,
*:before {
box-sizing: border-box;
}

.flex {
display: flex;
align-items: center;
}

.flex.fill {
height: 100%;
}

.flex.center {
justify-content: center;
}
12 changes: 12 additions & 0 deletions demo/src/sandboxes/exit-before-enter/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import './index.css'

const rootElement = document.getElementById('root')
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
rootElement
)
Loading