Skip to content

Conversation

PeikyLiu
Copy link
Contributor

@PeikyLiu PeikyLiu commented Jul 31, 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

Before:
useElementVisibility accepted a reactive parameter rootMargin,and the it typed as MaybeRefOrGetter,but it didn't support dynamic updates.

After:
Support dynamic updates

Solution

use watchEffect for monitoring changes to all reactive options, including rootMargin, threshold and scrollTarget.When these values change, the old IntersectionObserver is cleaned up and a new instance is created, ensuring the configuration is always up to date.

related #4930

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jul 31, 2025
@PeikyLiu PeikyLiu changed the title feat: support reactive options feat: useElementVisibility support reactive options Jul 31, 2025
@dosubot dosubot bot added the enhancement New feature or request label Jul 31, 2025
Copy link

pkg-pr-new bot commented Jul 31, 2025

Open in StackBlitz

@vueuse/components

npm i https://pkg.pr.new/@vueuse/components@4933

@vueuse/core

npm i https://pkg.pr.new/@vueuse/core@4933

@vueuse/electron

npm i https://pkg.pr.new/@vueuse/electron@4933

@vueuse/firebase

npm i https://pkg.pr.new/@vueuse/firebase@4933

@vueuse/integrations

npm i https://pkg.pr.new/@vueuse/integrations@4933

@vueuse/math

npm i https://pkg.pr.new/@vueuse/math@4933

@vueuse/metadata

npm i https://pkg.pr.new/@vueuse/metadata@4933

@vueuse/nuxt

npm i https://pkg.pr.new/@vueuse/nuxt@4933

@vueuse/router

npm i https://pkg.pr.new/@vueuse/router@4933

@vueuse/rxjs

npm i https://pkg.pr.new/@vueuse/rxjs@4933

@vueuse/shared

npm i https://pkg.pr.new/@vueuse/shared@4933

commit: 492fbd4

@doyuli
Copy link
Contributor

doyuli commented Jul 31, 2025

I think we can modify its underlying useIntersectionObserver to support responsive rootMargin, What do you think?

const stopWatch = isSupported.value
    ? watch(
        () => [targets.value, unrefElement(root as MaybeComputedElementRef), isActive.value, toValue(rootMargin)] as const,
        ([targets, root]) => {
          cleanup()
          if (!isActive.value)
            return

          if (!targets.length)
            return

          const observer = new IntersectionObserver(
            callback,
            {
              root: unrefElement(root),
              rootMargin: toValue(rootMargin) ?? '0px',
              threshold,
            },
          )

          targets.forEach(el => el && observer.observe(el))

          cleanup = () => {
            observer.disconnect()
            cleanup = noop
          }
        },
        { immediate, flush: 'post' },
      )
    : noop

In this way, you can just pass the responsive rootMargin directly into useElementVisibility

@PeikyLiu
Copy link
Contributor Author

I think we can modify its underlying useIntersectionObserver to support responsive rootMargin, What do you think?

const stopWatch = isSupported.value
    ? watch(
        () => [targets.value, unrefElement(root as MaybeComputedElementRef), isActive.value, toValue(rootMargin)] as const,
        ([targets, root]) => {
          cleanup()
          if (!isActive.value)
            return

          if (!targets.length)
            return

          const observer = new IntersectionObserver(
            callback,
            {
              root: unrefElement(root),
              rootMargin: toValue(rootMargin) ?? '0px',
              threshold,
            },
          )

          targets.forEach(el => el && observer.observe(el))

          cleanup = () => {
            observer.disconnect()
            cleanup = noop
          }
        },
        { immediate, flush: 'post' },
      )
    : noop

In this way, you can just pass the responsive rootMargin directly into useElementVisibility

First, I'd like to clarify that this bug originates in the useElementVisibility hook rather than in useIntersectionObserver. Looking at the documentation, I noticed that rootMargin in useElementVisibility is defined as a MaybeRefOrGetter, indicating it should support reactive values, while in useIntersectionObserver it's defined as a simple string type. This type definition difference suggests that the original design intention was to keep useIntersectionObserver simpler with static configuration, while enabling reactive options in the higher-level useElementVisibility hook. Therefore, I believe modifying useIntersectionObserver would go against its original design principles and could potentially introduce unexpected behavior in other components that rely on it.

@OrbisK OrbisK changed the title feat: useElementVisibility support reactive options feat(useElementVisibility): support reactive options Aug 7, 2025
element,
(intersectionObserverEntries) => {
let isIntersecting = elementIsVisible.value
const cleanup = shallowRef<() => void>(() => {})
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't have to be a ref I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok!I got it! thanks for your CR

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:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants