Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Transition component completes if nothing is transitioning #2318

Merged
merged 3 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Ensure `Transition` component completes if nothing is transitioning ([#2318](https://github.com/tailwindlabs/headlessui/pull/2318))

## [1.7.12] - 2023-02-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ function waitForTransition(node: HTMLElement, done: () => void) {
dispose()
}, totalDuration)
} else {
d.group((d) => {
// Mark the transition as done when the timeout is reached. This is a fallback in case the
// transitionrun event is not fired.
d.setTimeout(() => {
done()
d.dispose()
}, totalDuration)

// The moment the transitionrun event fires, we should cleanup the timeout fallback, because
// then we know that we can use the native transition events because something is
// transitioning.
d.addEventListener(node, 'transitionrun', (event) => {
if (event.target !== event.currentTarget) return
d.dispose()
})
})

let dispose = d.addEventListener(node, 'transitionend', (event) => {
if (event.target !== event.currentTarget) return
done()
Expand Down
35 changes: 21 additions & 14 deletions packages/@headlessui-react/src/utils/disposables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { microTask } from './micro-task'
export type Disposables = ReturnType<typeof disposables>

export function disposables() {
let disposables: Function[] = []
let _disposables: Function[] = []

let api = {
addEventListener<TEventName extends keyof WindowEventMap>(
Expand Down Expand Up @@ -44,30 +44,37 @@ export function disposables() {
})
},

style(node: HTMLElement, property: string, value: string) {
let previous = node.style.getPropertyValue(property)
Object.assign(node.style, { [property]: value })
return this.add(() => {
Object.assign(node.style, { [property]: previous })
})
},

group(cb: (d: typeof this) => void) {
let d = disposables()
cb(d)
return this.add(() => d.dispose())
},

add(cb: () => void) {
disposables.push(cb)
_disposables.push(cb)
return () => {
let idx = disposables.indexOf(cb)
let idx = _disposables.indexOf(cb)
if (idx >= 0) {
let [dispose] = disposables.splice(idx, 1)
dispose()
for (let dispose of _disposables.splice(idx, 1)) {
dispose()
}
}
}
},

dispose() {
for (let dispose of disposables.splice(0)) {
for (let dispose of _disposables.splice(0)) {
dispose()
}
},

style(node: HTMLElement, property: string, value: string) {
let previous = node.style.getPropertyValue(property)
Object.assign(node.style, { [property]: value })
return this.add(() => {
Object.assign(node.style, { [property]: previous })
})
},
}

return api
Expand Down
26 changes: 20 additions & 6 deletions packages/@headlessui-vue/src/utils/disposables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Disposables = ReturnType<typeof disposables>

export function disposables() {
let disposables: Function[] = []
let _disposables: Function[] = []

let api = {
addEventListener<TEventName extends keyof WindowEventMap>(
Expand Down Expand Up @@ -30,10 +30,6 @@ export function disposables() {
api.add(() => clearTimeout(timer))
},

add(cb: () => void) {
disposables.push(cb)
},

style(node: HTMLElement, property: string, value: string) {
let previous = node.style.getPropertyValue(property)
Object.assign(node.style, { [property]: value })
Expand All @@ -42,8 +38,26 @@ export function disposables() {
})
},

group(cb: (d: typeof this) => void) {
let d = disposables()
cb(d)
return this.add(() => d.dispose())
},

add(cb: () => void) {
_disposables.push(cb)
return () => {
let idx = _disposables.indexOf(cb)
if (idx >= 0) {
for (let dispose of _disposables.splice(idx, 1)) {
dispose()
}
}
}
},

dispose() {
for (let dispose of disposables.splice(0)) {
for (let dispose of _disposables.splice(0)) {
dispose()
}
},
Expand Down