Skip to content

Commit aa1a48c

Browse files
authored
Use watchEffect instead of immediately using an event listener on window (#817)
* use `watchEffect` instead of immediately using an event listener on `window` * update changelog
1 parent 91b436a commit aa1a48c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
- Stop the event from propagating in the `Popover` component ([#798](https://github.com/tailwindlabs/headlessui/pull/798))
2020
- Allow to click on elements inside a `DialogOverlay` ([#816](https://github.com/tailwindlabs/headlessui/pull/816))
21+
- Fix SSR crash because of `useWindowEvent` ([#817](https://github.com/tailwindlabs/headlessui/pull/817))
2122

2223
## [@headlessui/react@v1.4.1] - 2021-08-30
2324

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { onUnmounted } from 'vue'
1+
import { watchEffect } from 'vue'
22

33
export function useWindowEvent<TType extends keyof WindowEventMap>(
44
type: TType,
55
listener: (this: Window, ev: WindowEventMap[TType]) => any,
66
options?: boolean | AddEventListenerOptions
77
) {
8-
window.addEventListener(type, listener, options)
9-
onUnmounted(() => window.removeEventListener(type, listener, options))
8+
watchEffect(onInvalidate => {
9+
window.addEventListener(type, listener, options)
10+
11+
onInvalidate(() => {
12+
window.removeEventListener(type, listener, options)
13+
})
14+
})
1015
}

0 commit comments

Comments
 (0)