Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gkweb
Copy link

@gkweb gkweb commented May 21, 2025

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.
⚠️ Slowing down new functions

Warning: Slowing down new functions

As the VueUse audience continues to grow, we have been inundated with an overwhelming number of feature requests and pull requests. As a result, maintaining the project has become increasingly challenging and has stretched our capacity to its limits. As such, in the near future, we may need to slow down our acceptance of new features and prioritize the stability and quality of existing functions. Please note that new features for VueUse may not be accepted at this time. If you have any new ideas, we suggest that you first incorporate them into your own codebase, iterate on them to suit your needs, and assess their generalizability. If you strongly believe that your ideas are beneficial to the community, you may submit a pull request along with your use cases, and we would be happy to review and discuss them. Thank you for your understanding.


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.

<script setup lang="ts">
import { useElementBySelector } from '@vueuse/core'

const el = useElementBySelector('body')
</script>

<template>
  <div>
    <p>Body width: {{ el.offsetWidth }}</p>
    <p>Body height: {{ el.offsetHeight }}</p>
  </div>
</template>

Additional context

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. enhancement New feature or request labels May 21, 2025
@@ -0,0 +1,12 @@
// eslint-disable-next-line no-restricted-imports
Copy link
Collaborator

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

Copy link
Author

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) {
Copy link
Collaborator

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

Suggested change
export function useElementBySelector(selector: string) {
export function useElementBySelector(selector: MaybeRefOrGetter<string>) {

Copy link
Author

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))
Copy link
Collaborator

@OrbisK OrbisK May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets do this:

Suggested change
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)))

Copy link
Collaborator

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

Copy link
Author

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 },
Copy link
Collaborator

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels May 23, 2025
@gkweb gkweb requested a review from OrbisK May 28, 2025 05:09
immediate = true,
} = options

const el = deepRef<TElement | null>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const el = deepRef<TElement | null>()
const el = shallowRef<TElement | null>()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Collaborator

@OrbisK OrbisK left a 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return el
return readonly(el)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@OrbisK OrbisK requested a review from ilyaliao May 28, 2025 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants