|
| 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*/} |
0 commit comments