question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to observe only when element is in view, and not when it goes out of view?

See original GitHub issue

I would like just to track when the element comes into view, and keep the boolean true for the rest of the session duration. At the moment the library toggles back and forth. Is it possible to do it?

I dug into your options and the options in observe-element-in-viewport and I couldn’t find anything 👀

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
bittttttencommented, Mar 15, 2020

Ah sorry, I did something very similar I just extended your example. Something like this:

import { useEffect, useState } from 'react'
import useIsInViewport, { CallbackRef, HookOptions } from 'use-is-in-viewport'

export const useHasBeenInViewportOnce = (
    options?: HookOptions,
): [boolean, CallbackRef, CallbackRef] => {
    const [isInViewport, ref, viewportRef] = useIsInViewport(options)
    const [hasBeenInViewportOnce, setHasBeen] = useState(false)

    useEffect(() => {
        if (isInViewport && !hasBeenInViewportOnce) {
            setHasBeen(true)
        }
    }, [isInViewport, options, hasBeenInViewportOnce])

    return [hasBeenInViewportOnce, ref, viewportRef]
}

But yes, my problem was solved! So we can close it.

1reaction
fcabanasmcommented, Oct 6, 2020

You can (somewhat) approximate this via by using the await isInViewport(target) format. I wouldn’t recommend it though has that would meet your usecase only if the element was already outside the viewport to begin with.

Instead, while this hook doesn’t support it natively, you can build a custom hook based on this hook to do so as shown below —

import { useState } from 'react'
import useIsInViewport from 'use-is-in-viewport'

function useClampedIsInViewport(options) {
  const [isInViewport, ...etc] = useIsInViewport(options)
  const [wasInViewportAtleastOnce, setWasInViewportAtleastOnce] = useState(isInViewport)
  
  setWasInViewportAtleastOnce(prev => {
    // this will clamp it to the first true
    // received from useIsInViewport
    if (!prev) {
      return isInViewport
    }
    return prev
  })

  return [wasInViewportAtleastOnce, ...etc]
}

its actually does not working 😦. But @bitttttten example did it

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check if any part of an element is out of the viewport ...
I have a helper function that you can use to check if an element is in the viewport. It returns true if any...
Read more >
How can I tell if a DOM element is visible in the current viewport?
This solution works only if element is hidden under the viewport. When scrolled on the upper side out of viewport, it still returns...
Read more >
Check If an Element is Visible in the Viewport in JavaScript
In this tutorial, you'll learn how to check if an element is visible in the viewport using JavaScript.
Read more >
A Few Functional Uses for Intersection Observer to Know ...
Knowing when an element is about to come into view, if it has gone out of view, or how long it's been since...
Read more >
Check if an element is still inside the viewport after a given time
My first thought was to do it without IntersectionObserver , meaning I'd have to check the "is inside viewport" state after a timeout....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found