Skip to content

Commit 4a4922d

Browse files
committed
Add addTransitionType
1 parent b23c0c2 commit 4a4922d

File tree

3 files changed

+191
-4
lines changed

3 files changed

+191
-4
lines changed

src/content/reference/react/ViewTransition.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ You can try it by upgrading React packages to the most recent experimental versi
1515

1616
Experimental versions of React may contain bugs. Don't use them in production.
1717

18-
This API is only available inside React Server Components.
19-
2018
</Wip>
2119

2220
<Intro>
@@ -25,6 +23,8 @@ This API is only available inside React Server Components.
2523

2624

2725
```js
26+
import {unstableViewTransition as ViewTransition} from 'react';
27+
2828
<ViewTransition>
2929
<div>...</div>
3030
</ViewTransition>
@@ -99,7 +99,7 @@ These callbacks allow you to adjust the animation imperatively using the [animat
9999

100100
Each callback receives as arguments:
101101
- `element`: The DOM element that was animated.
102-
- `types`: The [transition types)(/todo) included in the animation.
102+
- `types`: The [Transition Types)(/reference/react/addTransitionType) included in the animation.
103103

104104
### View Transition Class {/*view-transition-classes*/}
105105

@@ -363,7 +363,7 @@ TODO
363363
---
364364

365365
### Custom animations with types {/*customizing-animations-with-types*/}
366-
You can use the [`addTransitionType`](/TODO) API to add a class name to the child elements when a specific transition type is activated for a specifc activation trigger. This allows you to customize the animation for each type of transition.
366+
You can use the [`addTransitionType`](/reference/react/addTransitionType) API to add a class name to the child elements when a specific transition type is activated for a specific activation trigger. This allows you to customize the animation for each type of transition.
367367

368368
For example, to customize the animation for all forward and backward navigations:
369369

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
title: unstable_addTransitionType
3+
canary: true
4+
---
5+
6+
<Wip>
7+
8+
**This API is experimental and is not available in a stable version of React yet.**
9+
10+
You can try it by upgrading React packages to the most recent experimental version:
11+
12+
- `react@experimental`
13+
- `react-dom@experimental`
14+
- `eslint-plugin-react-hooks@experimental`
15+
16+
Experimental versions of React may contain bugs. Don't use them in production.
17+
18+
</Wip>
19+
20+
<Intro>
21+
22+
`unstable_addTransitionType` lets you specify the cause of a transition.
23+
24+
25+
```js
26+
startTransition(() => {
27+
unstable_addTransitionType('my-transition-type');
28+
setState(newState);
29+
});
30+
```
31+
32+
</Intro>
33+
34+
<InlineToc />
35+
36+
---
37+
38+
## Reference {/*reference*/}
39+
40+
### `addTransitionType` {/*addtransitiontype*/}
41+
42+
#### Parameters {/*parameters*/}
43+
44+
- `type`: The type of transition to add. This can be any string.
45+
46+
#### Returns {/*returns*/}
47+
48+
`startTransition` does not return anything.
49+
50+
#### Caveats {/*caveats*/}
51+
52+
- If multiple transitions are combined, all Transition Types are collected. You can also add more than one type to a Transition.
53+
- Transition Types are reset after each commit. This means a `<Suspense>` fallback will associate the types after a `startTransition`, but revealing the content does not.
54+
55+
---
56+
57+
## Usage {/*usage*/}
58+
59+
### Adding the cause of a transition {/*adding-the-cause-of-a-transition*/}
60+
61+
Call `addTransitionType` inside of `startTransition` to indicate the cause of a transition:
62+
63+
``` [[1, 6, "unstable_addTransitionType"], [2, 5, "startTransition", [3, 6, "'submit-click'"]]
64+
import { startTransition, unstable_addTransitionType } from 'react';
65+
66+
function Submit({action) {
67+
function handleClick() {
68+
startTransition(() => {
69+
unstable_addTransitionType('submit-click');
70+
action();
71+
});
72+
}
73+
74+
return <button onClick={handleClick}>Click me</button>;
75+
}
76+
77+
```
78+
79+
When you call <CodeStep step={1}>addTransitionType</CodeStep> inside the scope of <CodeStep step={2}>startTransition</CodeStep>, React will associate <CodeStep step={3}>submit-click</CodeStep> as one of the causes for the Transition.
80+
81+
Currently, Transition Types can be used to customize different animations based on what caused the Transition. You have three different ways to choose from for how to use them:
82+
83+
- [Customize animations using browser view transition types](#customize-animations-using-browser-view-transition-types)
84+
- [Customize animations using `View Transition` Class](#customize-animations-using-view-transition-class)
85+
- [Customize animations using `ViewTransiton` events](#customize-animations-using-viewtransition-events)
86+
87+
In the future, we plan to support more use cases for using the cause of a transition.
88+
89+
---
90+
### Customize animations using browser view transition types {/*customize-animations-using-browser-view-transition-types*/}
91+
92+
When a [`ViewTransition`](/reference/react/ViewTransition) activates from a transition, React adds all the Transition Types as browser [view transition types](https://www.w3.org/TR/css-view-transitions-2/#active-view-transition-pseudo-examples) to the element.
93+
94+
This allows you to customize different animations based on CSS scopes:
95+
96+
```js [11]
97+
function Component() {
98+
return (
99+
<ViewTransition>
100+
<div>Hello</div>
101+
</ViewTransition>
102+
);
103+
}
104+
105+
startTransition(() => {
106+
unstable_addTransitionType('my-transition-type');
107+
setShow(true);
108+
});
109+
```
110+
111+
```css
112+
:root:active-view-transition-type(my-transition-type) {
113+
&::view-transition-...(...) {
114+
...
115+
}
116+
}
117+
```
118+
119+
---
120+
121+
### Customize animations using `View Transition` Class {/*customize-animations-using-view-transition-class*/}
122+
123+
You can customize animations for an activated `ViewTransition` based on type by passing an object to the View Transition Class:
124+
125+
```js
126+
function Component() {
127+
return (
128+
<ViewTransition enter={{
129+
'my-transition-type': 'my-transition-class',
130+
}}>
131+
<div>Hello</div>
132+
</ViewTransition>
133+
);
134+
}
135+
136+
// ...
137+
startTransition(() => {
138+
unstable_addTransitionType('my-transition-type');
139+
setState(newState);
140+
});
141+
```
142+
143+
If multiple types match, then they're joined together. If no types match then the special "default" entry is used instead. If any type has the value "none" then that wins and the ViewTransition is disabled (not assigned a name).
144+
145+
These can be combined with enter/exit/update/layout/share props to match based on kind of trigger and Transition Type.
146+
147+
```js
148+
<ViewTransition enter={{
149+
'navigation-back': 'enter-right',
150+
'navigation-forward': 'enter-left',
151+
}}
152+
exit={{
153+
'navigation-back': 'exit-right',
154+
'navigation-forward': 'exit-left',
155+
}}>
156+
```
157+
158+
---
159+
160+
### Customize animations using `ViewTransiton` events {/*customize-animations-using-viewtransition-events*/}
161+
162+
You can imperatively customize animations for an activated `ViewTransition` based on type using View Transition events:
163+
164+
```
165+
<ViewTransition onUpdate={(inst, types) => {
166+
if (types.includes('navigation-back')) {
167+
...
168+
} else if (types.includes('navigation-forward')) {
169+
...
170+
} else {
171+
...
172+
}
173+
}}>
174+
```
175+
176+
This allows you to pick different imperative Animations based on the cause.
177+
178+
---
179+
180+
## Troubleshooting {/*troubleshooting*/}
181+
182+
### TODO {/*todo2*/}

src/sidebarReference.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@
157157
"title": "captureOwnerStack",
158158
"path": "/reference/react/captureOwnerStack",
159159
"version": "canary"
160+
},
161+
{
162+
"title": "unstable_addTransitionType",
163+
"path": "/reference/react/addTransitionType",
164+
"version": "canary"
160165
}
161166
]
162167
},

0 commit comments

Comments
 (0)