You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When `setShow` is called, `show` switched to `true` and the `Child` component is rendered. Since `setShow` is called inside <CodeStepstep={1}>startTransition</CodeStep>, and `Child` renders a <CodeStepstep={2}>ViewTransition</CodeStep> before any other DOM nodes, an `enter` animation is triggered.
175
+
176
+
When `show` is switched back to `false`, an `exit` animation is triggered.
177
+
178
+
<Sandpack>
179
+
180
+
```js
181
+
import {
182
+
unstable_ViewTransitionasViewTransition,
183
+
useState,
184
+
startTransition
185
+
} from'react';
186
+
187
+
functionItem() {
167
188
return (
168
-
<>
169
-
<button onClick={() => {
170
-
startTransition(() => {
171
-
setShow(true);
172
-
});
173
-
}}>Show</button>
189
+
<ViewTransition>
190
+
<div className="item">Hello world</div>
191
+
</ViewTransition>
192
+
);
193
+
}
174
194
175
-
{show &&<Component />}
195
+
exportdefaultfunctionComponent() {
196
+
const [showItem, setShowItem] =useState(false);
197
+
return (
198
+
<>
199
+
<button
200
+
onClick={() => {
201
+
startTransition(() => {
202
+
setShowItem((prev) =>!prev);
203
+
});
204
+
}}
205
+
>{showItem ?'➖':'➕'}</button>
206
+
207
+
{showItem ?<Item />:null}
176
208
</>
177
209
);
178
210
}
179
211
```
180
212
181
-
When `setShow` is called, `show` switched to `true` and the `Child` component is rendered. Since `setShow` is called inside <CodeStepstep={1}>startTransition</CodeStep>, and `Child` renders a <CodeStepstep={2}>ViewTransition</CodeStep> before any other DOM nodes, an `enter` animation is triggered.
213
+
```css
214
+
#root {
215
+
display: flex;
216
+
flex-direction: column;
217
+
align-items: center;
218
+
min-height: 150px;
219
+
}
220
+
button {
221
+
border: none;
222
+
border-radius: 50%;
223
+
width: 50px;
224
+
height: 50px;
225
+
display: flex;
226
+
justify-content: center;
227
+
align-items: center;
228
+
background-color: #f0f8ff;
229
+
color: white;
230
+
font-size: 20px;
231
+
cursor: pointer;
232
+
transition: background-color 0.3s, border0.3s;
233
+
}
234
+
button:hover {
235
+
border: 2pxsolid#ccc;
236
+
background-color: #e0e8ff;
237
+
}
238
+
.item {
239
+
width: 100%;
240
+
padding: 10px20px;
241
+
margin: 10px0;
242
+
border-radius: 50px;
243
+
background-color: #f0f8ff;
244
+
color: #333;
245
+
font-size: 16px;
246
+
box-shadow: 02px4pxrgba(0, 0, 0, 0.1);
247
+
height: 40px;
248
+
}
249
+
```
182
250
183
-
When `show` is switched back to `false`, an `exit` animation is triggered.
251
+
```json package.json hidden
252
+
{
253
+
"dependencies": {
254
+
"react": "experimental",
255
+
"react-dom": "experimental",
256
+
"react-scripts": "latest"
257
+
}
258
+
}
259
+
```
260
+
261
+
</Sandpack>
184
262
185
263
<Pitfall>
186
264
@@ -203,16 +281,110 @@ function Component() {
203
281
204
282
Normally, we don't recommend assigning a name to a `<ViewTransition>` and instead let React assign it an automatic name. The reason you might want to assign a name is to animate between completely different components when one tree unmounts and another tree mounts at the same time. To preserve continuity.
205
283
284
+
```js
285
+
<ViewTransition name={UNIQUE_NAME}>
286
+
<Child />
287
+
</ViewTransition>
288
+
```
289
+
206
290
When one tree unmounts and another mounts, if there's a pair where the same name exists in the unmounting tree and the mounting tree, they trigger the "share" animation on both. It animates from the unmounting side to the mounting side.
207
291
208
292
Unlike an exit/enter animation this can be deeply inside the deleted/mounted tree. If a `<ViewTransition>` would also be eligible for exit/enter, then the "share" animation takes precedence.
209
293
210
-
If Transition first unmounts one side an then leads to a `<Suspense>` fallback being shown before eventually the new name being mounted, then no shared element transition happens.
294
+
If Transition first unmounts one side and then leads to a `<Suspense>` fallback being shown before eventually the new name being mounted, then no shared element transition happens.
If either the mounted or unmounted side of a pair is outside the viewport, then no pair is formed. This ensures that it doesn't fly in or out of the viewport when something is scrolled. Instead it's treated as a regular enter/exit by itself.
213
383
214
384
<Note>
215
385
386
+
If either the mounted or unmounted side of a pair is outside the viewport, then no pair is formed. This ensures that it doesn't fly in or out of the viewport when something is scrolled. Instead it's treated as a regular enter/exit by itself.
387
+
216
388
This does not happen if the same Component instance changes position, which triggers an "update". Those animate regardless if one position is outside the viewport.
217
389
218
390
There's currently a quirk where if a deeply nested unmounted `<ViewTransition>` is inside the viewport but the mounted side is not within the viewport, then the unmounted side animates as its own "exit" animation even if it's deeply nested instead of as part of the parent animation.
@@ -261,11 +433,128 @@ function Component() {
261
433
```
262
434
Instead, any parent `<ViewTransition>` would cross-fade. If there is no parent `<ViewTransition>` then there's no animation in that case.
263
435
264
-
This means you might want to avoid a wrapper elements in lists where you want to allow the Component to control its own reorder animation:
The above rule also applies if one of the items updates to resize, which then causes the siblings to resize, it'll also animate its sibling `<ViewTransition>` but only if they're immediate siblings.
270
559
271
560
This means that during an update, which causes a lot of re-layout, it doesn't individually animate every `<ViewTransition>` on the page. That would lead to a lot of noisy animations which distracts from the actual change. Therefore React is more conservative about when an individual animation triggers.
0 commit comments