Skip to content

Commit 4e13a0e

Browse files
authored
Fix incorrect transitionend/transitioncancel events for the Transition component (#1537)
* fix incorrect transitionend/transitioncancel events Due to bubbling, the `Transition` component also "finished" when you had children that uses `transition-colors` for example. This commit ensures that we only care about transition events related to the actual DOM node that we defined the transitions on... * update changelog
1 parent 1d53ac3 commit 4e13a0e

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Add `by` prop for `Listbox`, `Combobox` and `RadioGroup` ([#1482](https://github.com/tailwindlabs/headlessui/pull/1482))
1313
- Add `@headlessui/tailwindcss` plugin ([#1487](https://github.com/tailwindlabs/headlessui/pull/1487))
1414

15+
### Fixed
16+
17+
- Fix incorrect transitionend/transitioncancel events for the Transition component ([#1537](https://github.com/tailwindlabs/headlessui/pull/1537))
18+
1519
## [1.6.4] - 2022-05-29
1620

1721
### Fixed

packages/@headlessui-react/src/components/transitions/utils/transition.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,28 @@ function waitForTransition(node: HTMLElement, done: (reason: Reason) => void) {
5252
)
5353
} else {
5454
listeners.push(
55-
d.addEventListener(
56-
node,
57-
'transitionrun',
58-
() => {
59-
// Cleanup "old" listeners
60-
listeners.splice(0).forEach((dispose) => dispose())
61-
62-
// Register new listeners
63-
listeners.push(
64-
d.addEventListener(
65-
node,
66-
'transitionend',
67-
() => {
68-
done(Reason.Ended)
69-
listeners.splice(0).forEach((dispose) => dispose())
70-
},
71-
{ once: true }
72-
),
73-
d.addEventListener(
74-
node,
75-
'transitioncancel',
76-
() => {
77-
done(Reason.Cancelled)
78-
listeners.splice(0).forEach((dispose) => dispose())
79-
},
80-
{ once: true }
81-
)
82-
)
83-
},
84-
{ once: true }
85-
)
55+
d.addEventListener(node, 'transitionrun', (event) => {
56+
if (event.target !== event.currentTarget) return
57+
58+
// Cleanup "old" listeners
59+
listeners.splice(0).forEach((dispose) => dispose())
60+
61+
// Register new listeners
62+
listeners.push(
63+
d.addEventListener(node, 'transitionend', (event) => {
64+
if (event.target !== event.currentTarget) return
65+
66+
done(Reason.Ended)
67+
listeners.splice(0).forEach((dispose) => dispose())
68+
}),
69+
d.addEventListener(node, 'transitioncancel', (event) => {
70+
if (event.target !== event.currentTarget) return
71+
72+
done(Reason.Cancelled)
73+
listeners.splice(0).forEach((dispose) => dispose())
74+
})
75+
)
76+
})
8677
)
8778
}
8879
} else {

0 commit comments

Comments
 (0)