Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

How to use onClickOutside and useTemplateRefs() from Vue 3.5? #4293

Unanswered
areindl asked this question in Q&A
Discussion options

Vue's new useTemplateRefs() i is great. It brings a long more typesafetly and explicitness to the code.

However, I am fighting a bit with the new Type-Inference.

Example:

I am using a templateRef and declared it here it here:

const overlayPanel = useTemplateRef<OverlayPanel>('overlayPanel')`

It simply references a custom OverlayPanel-Component:

<template>
    <OverlayPanel ref="overlayPanel" :dismissable="false" class="w-32 p-2">
       ...
    </OverlayPanel>
</template>

Now I want to leverage the onClickOutside() function from vueUse:

onClickOutside(overlayPanel, () => overlayPanel.value?.hide(), {
    ignore: ['.p-overlaypanel'],
})

But onClickOutside expects a MaybeElementRef type. Through the type-inference, my templateRef is now of type Readonly<ShallowRef<OverlayPanel | null>> - before it was any.

How would you handle this? Any proposals?

One thing that works is: Intersection Type using & operator

const overlayPanel = useTemplateRef<OverlayPanel & HTMLElement>('overlayPanel')

Thanks a lot 🎉

You must be logged in to vote

Replies: 2 comments

Comment options

Hey there, @areindl! I'm a bot here to help you with bug fixes, questions, and contributions. Let's tackle this challenge together! 🤖

To handle the type inference issue with onClickOutside and useTemplateRef in Vue 3.5, you can use type assertions to ensure compatibility with the expected MaybeElementRef type. Here's a possible approach:

  1. Type Assertion: You can assert the type of overlayPanel to match MaybeElementRef. This is a common workaround when dealing with type mismatches in TypeScript.
onClickOutside(overlayPanel as unknown as MaybeElementRef, () => overlayPanel.value?.hide(), {
    ignore: ['.p-overlaypanel'],
})
  1. Intersection Type: As you mentioned, using an intersection type can also work. By declaring overlayPanel as OverlayPanel & HTMLElement, you ensure it satisfies the MaybeElement type requirements.
const overlayPanel = useTemplateRef<OverlayPanel & HTMLElement>('overlayPanel')

Both methods aim to align the type of overlayPanel with what onClickOutside expects, allowing you to leverage the utility without type errors.

To continue talking to Dosu, mention @dosu.

You must be logged in to vote
0 replies
Comment options

You can use it like this

const overlayPanel = useTemplateRef<InstanceType<typeof OverlayPanel>>('overlayPanel')

onClickOutside(overlayPanel, () => {
  console.log('clicked outside')
})

refrence: https://vuejs.org/guide/typescript/composition-api.html#typing-component-template-refs

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.