From a58da5570bdb4900b3db38047d808e125218139e Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Sat, 12 Sep 2020 21:16:44 +0800 Subject: [PATCH] refactor(use): adjust useEventListener params --- .../vant-use/src/useEventListener/index.ts | 26 ++++++++++--------- src/dropdown-menu/index.js | 16 +++++------- src/index-bar/index.js | 6 +---- src/list/index.js | 6 +---- src/sticky/index.js | 6 +---- 5 files changed, 24 insertions(+), 36 deletions(-) diff --git a/packages/vant-use/src/useEventListener/index.ts b/packages/vant-use/src/useEventListener/index.ts index 9a380589b..2cad8dbe2 100644 --- a/packages/vant-use/src/useEventListener/index.ts +++ b/packages/vant-use/src/useEventListener/index.ts @@ -24,26 +24,28 @@ if (inBrowser) { } export type UseEventListenerOptions = { - type: string; - target: EventTarget | Ref; + target?: EventTarget | Ref; capture?: boolean; passive?: boolean; - listener: EventListener; }; -export function useEventListener({ - type, - target, - passive = false, - capture = false, - listener, -}: UseEventListenerOptions) { +export function useEventListener( + type: string, + listener: EventListener, + options: UseEventListenerOptions +) { + if (!inBrowser) { + return; + } + + const { target = window, passive = false, capture = false } = options; + let attached: boolean; const add = () => { const element = unref(target); - if (element && inBrowser && !attached) { + if (element && !attached) { element.addEventListener( type, listener, @@ -56,7 +58,7 @@ export function useEventListener({ const remove = () => { const element = unref(target); - if (element && inBrowser && attached) { + if (element && attached) { element.removeEventListener(type, listener, capture); attached = false; } diff --git a/src/dropdown-menu/index.js b/src/dropdown-menu/index.js index ca2109d3a..385c46452 100644 --- a/src/dropdown-menu/index.js +++ b/src/dropdown-menu/index.js @@ -72,6 +72,12 @@ export default createComponent({ } }; + const onScroll = () => { + if (opened.value) { + updateOffset(); + } + }; + const toggleItem = (active) => { children.forEach((item, index) => { if (index === active) { @@ -121,15 +127,7 @@ export default createComponent({ callback: onClickOutside, }); - useEventListener({ - type: 'scroll', - target: scroller, - listener() { - if (opened.value) { - updateOffset(); - } - }, - }); + useEventListener('scroll', onScroll, { target: scroller }); return () => (
diff --git a/src/index-bar/index.js b/src/index-bar/index.js index c75ba013e..8583bbdbc 100644 --- a/src/index-bar/index.js +++ b/src/index-bar/index.js @@ -156,11 +156,7 @@ export default createComponent({ } }; - useEventListener({ - type: 'scroll', - target: scroller, - listener: onScroll, - }); + useEventListener('scroll', onScroll, { target: scroller }); watch( () => props.indexList, diff --git a/src/list/index.js b/src/list/index.js index 208b10c32..c3ae9ac20 100644 --- a/src/list/index.js +++ b/src/list/index.js @@ -143,11 +143,7 @@ export default createComponent({ usePublicApi({ check }); - useEventListener({ - type: 'scroll', - target: scroller, - listener: check, - }); + useEventListener('scroll', check, { target: scroller }); return () => { const Content = slots.default?.(); diff --git a/src/sticky/index.js b/src/sticky/index.js index 1e41c11e9..b2f149562 100644 --- a/src/sticky/index.js +++ b/src/sticky/index.js @@ -101,11 +101,7 @@ export default createComponent({ emitScrollEvent(scrollTop); }; - useEventListener({ - type: 'scroll', - target: scroller, - listener: onScroll, - }); + useEventListener('scroll', onScroll, { target: scroller }); useVisibilityChange(rootRef, onScroll);