-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(useElementBySelector): add simple querySelector utility #4764
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
base: main
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,12 @@ | |||
// eslint-disable-next-line no-restricted-imports |
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.
you should remove this and use tryOnMounted
from vueuse instead
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.
Done ✅
// eslint-disable-next-line no-restricted-imports | ||
import { ref as deepRef, onMounted } from 'vue' | ||
|
||
export function useElementBySelector(selector: string) { |
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.
If we want to merge this we should have reactivity
export function useElementBySelector(selector: string) { | |
export function useElementBySelector(selector: MaybeRefOrGetter<string>) { |
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.
Have now added reactive support for the selector argument. Great idea thanks 🙏🏻
|
||
export function useElementBySelector(selector: MaybeRefOrGetter<string>) { | ||
const el = deepRef<Element | null>() | ||
const select = (): Element | null => el.value = document.querySelector(toValue(selector)) |
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.
lets do this:
const select = (): Element | null => el.value = document.querySelector(toValue(selector)) | |
const select = (newSelector): Element | null => el.value = document.querySelector(newSelector) |
this ensures the right value for the current watch cycle
for tryOnMounted
:
tryOnMounted(()=>select(toValue(selector)))
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.
Please checkout the guidelines for globals like document: https://vueuse.org/guidelines.html#configurable-globals
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.
Updated 🙏🏻
watch( | ||
() => toValue(selector), | ||
select, | ||
{ immediate: true }, |
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.
Please check out the Guidelines for watchers: https://vueuse.org/guidelines.html#watch-options
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.
Added
immediate = true, | ||
} = options | ||
|
||
const el = deepRef<TElement | null>() |
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.
const el = deepRef<TElement | null>() | |
const el = shallowRef<TElement | null>() |
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.
Updated
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.
Looks good to me. We should just change the el to a readonly shallow ref
{ immediate }, | ||
) | ||
|
||
return el |
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.
return el | |
return readonly(el) |
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.
updated
Before submitting the PR, please make sure you do the following
fixes #123
).Description
Adds small simple utility for selecting elements using querySelector. I am opening this PR as it appears to be a common pattern used.
Pairs well with other vueuse utils. For example
useElementSize
.Additional context