-
Notifications
You must be signed in to change notification settings - Fork 2
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
Use event listener composition #293
Use event listener composition #293
Conversation
this PR also adds Vue 3.3 shims and updates |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple questions but overall this is sweet!
if (getCurrentScope()) { | ||
onScopeDispose(() => remove()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use tryOnScopeDispose
for this?
watch(() => toValue(target), () => { | ||
remove() | ||
add() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be a little more sophisticated. If immediate
is false
I think this might add the listener anyway. Similar if remove
is called but then the target changes this will automatically add the listener back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have a better, more elegant solution but it now seems to warrant some unit tests. Let me get some good tests written and I'll update this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple questions about the tests but overall looks really good to me 👍
])('given falsy target never adds event listener', (initialValue) => { | ||
const callback = vi.fn() | ||
const target = ref<HTMLElement | undefined | null>(initialValue) | ||
vi.spyOn(utils, 'toValue').mockReturnValue(initialValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed? Won't toValue already return the initial value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, this is so obvious in retrospect. Check out the updated tests, I think you'll find them significantly more concise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
useEventListener
The
useEventListener
composition can be used to automatically handle setup and teardown of event listeners on the document or HTMElement scope. The options argument extends browser AddEventListenerOptions withimmediate
boolean, which defaults totrue
but when passed in asfalse
, will prevent the composition from adding the listener automatically.Example
Arguments
K (K extends keyof DocumentEventMap)
(this: Document, event: DocumentEventMap[K]) => unknown
AddEventListenerOptions & { immediate: boolean }
Returns