[animations-css] An exit starts from a stale opacity when the element declares no base opacity
Current Behavior
Under @tamagui/animations-css, an element with enterStyle={{ opacity: 0 }} and exitStyle={{ opacity: 0 }} and no base opacity jumps straight to 0 on exit instead of fading. The exit still completes on schedule, and the element unmounts once the duration has passed.
It fades correctly in either of these cases:
- The element declares a base opacity, as a prop (
opacity={1}) or instyled(). The presence demo does this:GalleryItemsetsopacity: 1, which is why it animates. - The element re-renders at least once after its enter finishes and before it exits.
Transform keys in the same exitStyle animate in every case.
Expected Behavior
An exit starts from the opacity the element is actually showing, whether or not the component declares one and whether or not it re-rendered after entering.
Tamagui Version
2.7.7 (latest stable, and the latest @tamagui/animations-css).
Platform (Web, iOS, Android)
Web (react-native-web 0.21.2). The CSS driver is web-only.
Reproduction
import { createAnimations } from '@tamagui/animations-css'
const animations = createAnimations({
quick: 'cubic-bezier(0.215, 0.61, 0.355, 1) 150ms',
})const Item = styled(YStack, {
x: 0,
opacity: 1,
enterStyle: { x: 100, opacity: 0 },
exitStyle: { x: -100, opacity: 0 },
})
const Probe = () => {
const [on, setOn] = useState(false)
const [tick, setTick] = useState(0)
return (
<>
<AnimatePresence>
{on ? (
<View key="a" data-tick={tick} transition="quick" width={100} height={50}
backgroundColor="red" enterStyle={{ opacity: 0 }} exitStyle={{ opacity: 0 }} />
) : null}
</AnimatePresence>
<AnimatePresence>
{on ? (
<View key="b" transition="quick" width={100} height={50} backgroundColor="blue"
opacity={1} enterStyle={{ opacity: 0 }} exitStyle={{ opacity: 0 }} />
) : null}
</AnimatePresence>
<AnimatePresence>
{on ? <Item key="c" transition="quick" width={100} height={50} backgroundColor="green" /> : null}
</AnimatePresence>
<button onClick={() => setTick(t => t + 1)}>rerender</button>
<button onClick={() => setOn(v => !v)}>toggle</button>
</>
)
}Computed opacity on exit, sampled per frame (milliseconds since the toggle, then opacity; X is unmounted). Each case entered, waited 400ms for the enter to finish, then exited:
| Case | Exit samples |
|---|---|
| Red, no base opacity | 0=1.00 32=0.00 66=0.00 99=0.00 133=0.00 169=X |
| Red, no base opacity, one re-render before exit | 0=1.00 12=1.00 43=0.45 77=0.17 110=0.05 143=0.00 177=X |
Blue, opacity={1} prop |
0=1.00 33=0.69 66=0.28 99=0.09 133=0.02 169=X |
Green, styled() base opacity, as in the presence demo |
0=1.00 33=0.69 66=0.28 99=0.09 133=0.02 169=0.00 198=X |
The enter animates identically in all three.
On the failing exit, a MutationObserver shows the reset frame writing the exit value rather than the current one:
11ms style transition: none; transform: translate(0px, 0px); opacity: 0;
12ms style transition: 150ms cubic-bezier(0.215, 0.61, 0.355, 1); transform: translate(0px, -10px); opacity: 0;Cause
The exit layout effect resets each exitStyle key with transition: none, then applies exitStyle on the next frame under the transition. The opacity reset comes from getResetValue:
if (key === "opacity") {
return style?.opacity ?? props.opacity ?? lastNonExitingStyleRef.current.opacity ?? 1;
}With no base opacity, the first two are undefined and the value comes from lastNonExitingStyleRef. That ref is written by a layout effect that runs on each non-exiting render and reads getComputedStyle(host).opacity. An element that mounts with enterStyle={{ opacity: 0 }} and never renders again captures 0 at mount, before the enter transition runs. The exit then resets to 0 and transitions from 0 to 0.
A re-render after the enter updates the ref to 1, which is why that case fades. A base opacity puts 1 in style or props, so the ref is never reached.
Possible fix
Read the opacity when the exit starts rather than at the last render. The exit effect already holds the node:
const node = host;
const currentOpacity = getComputedStyle(node).opacity;
// …
const getResetValue = key => {
if (key === "opacity") {
return style?.opacity ?? props.opacity ?? currentOpacity ?? 1;
}
// …
};Applied locally to the red case above (no base opacity, no re-render):
| Exit | Before | After |
|---|---|---|
| After the enter finished | 0=1.00 16=0.00 49=0.00 83=0.00 116=0.00 148=0.00 182=X |
0=1.00 15=0.69 48=0.28 81=0.10 114=0.02 149=0.00 181=X |
| Interrupting the enter at 70ms | 0=0.55 26=0.00 59=0.00 92=0.00 126=0.00 160=X |
0=0.55 26=0.38 59=0.15 92=0.05 126=0.01 160=X |
In the second row the exit continues from wherever the enter had reached; a value saved at render time only matches that if a render happened at that moment. Unmount timing is unchanged.
System Info
- macOS 26.5, Apple M1
- Node 26.4.0, bun 1.2.23
tamagui/@tamagui/core/@tamagui/animations-css2.7.7react19.2.0,react-native-web0.21.2- Vite (Storybook 10)
Source: tamagui/tamagui