Skip to content

Commit 526c851

Browse files
committed
Expand ViewTransition callback props reference documentation
Document the full callback signatures with ViewTransitionInstance and types arguments, cleanup function return values, instance properties (group, imagePair, old, new), and per-callback behavior.
1 parent a19c98d commit 526c851

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/content/reference/react/ViewTransition.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,38 @@ By default, `<ViewTransition>` animates with a smooth cross-fade. You can custom
8484

8585
#### Callback {/*events*/}
8686

87-
These callbacks allow you to adjust the animation imperatively using the [animate](https://developer.mozilla.org/en-US/docs/Web/API/Element/animate) APIs:
87+
These callbacks allow you to control the animation imperatively using the [Web Animations API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API). React calls them after the View Transition's [`ready`](https://developer.mozilla.org/en-US/docs/Web/API/ViewTransition/ready) Promise resolves, once built-in default animations have already been computed. Only one callback fires per `<ViewTransition>` per Transition.
8888

89-
* **optional** `onEnter`: A function. React calls `onEnter` after an "enter" animation.
90-
* **optional** `onExit`: A function. React calls `onExit` after an "exit" animation.
91-
* **optional** `onShare`: A function. React calls `onShare` after a "share" animation.
92-
* **optional** `onUpdate`: A function. React calls `onUpdate` after an "update" animation.
89+
* **optional** `onEnter`: `(instance, types) => void | (() => void)`. Called when this `<ViewTransition>` is inserted during a Transition without a matching named pair. Use this to animate the entering element imperatively.
90+
* **optional** `onExit`: `(instance, types) => void | (() => void)`. Called when this `<ViewTransition>` is removed during a Transition without a matching named pair. Use this to animate the exiting element imperatively.
91+
* **optional** `onShare`: `(instance, types) => void | (() => void)`. Called when this `<ViewTransition>` is part of a shared element Transition—where a named `<ViewTransition>` is deleted and another with the same name is inserted. Takes precedence over `onEnter` and `onExit`. Use this to animate the shared element Transition imperatively.
92+
* **optional** `onUpdate`: `(instance, types) => void | (() => void)`. Called when this `<ViewTransition>` has DOM mutations inside it, or when the boundary itself changes size or position due to a sibling change. Use this to animate content updates imperatively.
9393

94-
Each callback receives as arguments:
95-
- `element`: The DOM element that was animated.
96-
- `types`: The [Transition Types](/reference/react/addTransitionType) included in the animation.
94+
Each callback receives two arguments:
95+
96+
* `instance`: A View Transition instance object that provides access to the view transition [pseudo-elements](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API/Using#the_view_transition_process). Call `instance.old.animate(keyframes, options)` and `instance.new.animate(keyframes, options)` to imperatively control the animation. The instance has these properties:
97+
* `name`: The `view-transition-name` string for this boundary.
98+
* `group`: The `::view-transition-group` pseudo-element. Supports `.animate()`, `.getAnimations()`, and `.getComputedStyle()`.
99+
* `imagePair`: The `::view-transition-image-pair` pseudo-element. Supports `.animate()`, `.getAnimations()`, and `.getComputedStyle()`.
100+
* `old`: The `::view-transition-old` pseudo-element (the snapshot of the previous state). Supports `.animate()`, `.getAnimations()`, and `.getComputedStyle()`.
101+
* `new`: The `::view-transition-new` pseudo-element (the live representation of the new state). Supports `.animate()`, `.getAnimations()`, and `.getComputedStyle()`.
102+
* `types`: An `Array<string>` of [Transition Types](/reference/react/addTransitionType) included in the animation. Empty array if no types were specified.
103+
104+
Each callback can optionally return a **cleanup function**. The cleanup function is called when the View Transition finishes, allowing you to cancel any manually started animations:
105+
106+
```js
107+
<ViewTransition
108+
onEnter={(instance, types) => {
109+
const anim = instance.new.animate(
110+
[{ opacity: 0 }, { opacity: 1 }],
111+
{ duration: 500 }
112+
);
113+
return () => anim.cancel();
114+
}}
115+
>
116+
<div>...</div>
117+
</ViewTransition>
118+
```
97119

98120
### View Transition Class {/*view-transition-class*/}
99121

0 commit comments

Comments
 (0)