Skip to content

Commit 6f56583

Browse files
ArthurDarkstoneliliang18Alfred-Skyblue
authored
fix(useEventListener): improve types (#4787)
Co-authored-by: liliang18 <liliang18@tal.com> Co-authored-by: 远方os <yangpanteng@gmail.com>
1 parent 319d821 commit 6f56583

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

packages/core/useEventListener/index.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,51 @@ describe('useEventListener', () => {
360360
expect(addSpy).toHaveBeenLastCalledWith('click', listener, true)
361361
expect(removeSpy).toHaveBeenCalledTimes(1)
362362
})
363+
364+
it('should check document and shadowRoot', async () => {
365+
const element = document.createElement('div')
366+
const shadowRoot = element.attachShadow({ mode: 'open' })
367+
const listener1 = vi.fn()
368+
const listener2 = vi.fn()
369+
370+
useEventListener(shadowRoot, 'click', listener1)
371+
useEventListener(document, 'click', listener2)
372+
373+
shadowRoot.dispatchEvent(new Event('click'))
374+
document.dispatchEvent(new Event('click'))
375+
expect(listener1).toHaveBeenCalledTimes(1)
376+
expect(listener2).toHaveBeenCalledTimes(1)
377+
})
378+
379+
it('should check multiple shadowRoots + multiple events', async () => {
380+
const element1 = document.createElement('div')
381+
const shadowRoot1 = element1.attachShadow({ mode: 'open' })
382+
const element2 = document.createElement('div')
383+
const shadowRoot2 = element2.attachShadow({ mode: 'closed' })
384+
385+
const listener = vi.fn()
386+
387+
useEventListener([element1, element2, shadowRoot1, shadowRoot2], ['click', 'slotchange'], listener)
388+
389+
shadowRoot1.dispatchEvent(new Event('click'))
390+
shadowRoot2.dispatchEvent(new Event('click'))
391+
392+
await nextTick()
393+
394+
expect(listener).toHaveBeenCalledTimes(2)
395+
396+
element1.dispatchEvent(new Event('click'))
397+
element2.dispatchEvent(new Event('click'))
398+
399+
await nextTick()
400+
401+
expect(listener).toHaveBeenCalledTimes(4)
402+
403+
shadowRoot1.dispatchEvent(new Event('slotchange'))
404+
shadowRoot2.dispatchEvent(new Event('slotchange'))
405+
406+
await nextTick()
407+
408+
expect(listener).toHaveBeenCalledTimes(6)
409+
})
363410
})

packages/core/useEventListener/index.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface InferEventTarget<Events> {
1313

1414
export type WindowEventName = keyof WindowEventMap
1515
export type DocumentEventName = keyof DocumentEventMap
16+
export type ShadowRootEventName = keyof ShadowRootEventMap
1617

1718
export interface GeneralEventListener<E = Event> {
1819
(evt: E): void
@@ -65,7 +66,7 @@ export function useEventListener<E extends keyof WindowEventMap>(
6566
* @param options
6667
*/
6768
export function useEventListener<E extends keyof DocumentEventMap>(
68-
target: DocumentOrShadowRoot,
69+
target: Document,
6970
event: MaybeRefOrGetter<Arrayable<E>>,
7071
listener: MaybeRef<Arrayable<(this: Document, ev: DocumentEventMap[E]) => any>>,
7172
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>
@@ -74,7 +75,25 @@ export function useEventListener<E extends keyof DocumentEventMap>(
7475
/**
7576
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
7677
*
77-
* Overload 4: Explicitly HTMLElement target
78+
* Overload 4: Explicitly ShadowRoot target
79+
*
80+
* @see https://vueuse.org/useEventListener
81+
* @param target
82+
* @param event
83+
* @param listener
84+
* @param options
85+
*/
86+
export function useEventListener<E extends keyof ShadowRootEventMap>(
87+
target: MaybeRefOrGetter<Arrayable<ShadowRoot> | null | undefined>,
88+
event: MaybeRefOrGetter<Arrayable<E>>,
89+
listener: MaybeRef<Arrayable<(this: ShadowRoot, ev: ShadowRootEventMap[E]) => any>>,
90+
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>
91+
): Fn
92+
93+
/**
94+
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
95+
*
96+
* Overload 5: Explicitly HTMLElement target
7897
*
7998
* @see https://vueuse.org/useEventListener
8099
* @param target
@@ -87,12 +106,12 @@ export function useEventListener<E extends keyof HTMLElementEventMap>(
87106
event: MaybeRefOrGetter<Arrayable<E>>,
88107
listener: MaybeRef<(this: HTMLElement, ev: HTMLElementEventMap[E]) => any>,
89108
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>
90-
): () => void
109+
): Fn
91110

92111
/**
93112
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
94113
*
95-
* Overload 5: Custom event target with event type infer
114+
* Overload 6: Custom event target with event type infer
96115
*
97116
* @see https://vueuse.org/useEventListener
98117
* @param target
@@ -110,7 +129,7 @@ export function useEventListener<Names extends string, EventType = Event>(
110129
/**
111130
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
112131
*
113-
* Overload 6: Custom event target fallback
132+
* Overload 7: Custom event target fallback
114133
*
115134
* @see https://vueuse.org/useEventListener
116135
* @param target

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy