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.

useDismiss + ancestorScroll: dismiss only when reference is scrolled out of ancestor visible viewport

See original GitHub issue

Example: https://codesandbox.io/s/lively-sunset-kyohtw?file=/src/Select.tsx

I modified the Select example from the docs site a bit to try to showcase what I’d like to achieve. I’m trying to set up a reasonable behavior for a Select floating menu in a scrollable area (which can also be a floating element). The Select menu is portaled, so it is essentially detached from the Select control button.

Essentially, the issue is that ancestorScroll: true option in useDismiss hook dismisses the floating element immediately. This however can be quite annoying to users, as they can open the Select, then accidentally trigger a scroll, which will immediately close the menu. I do not wish to use a scroll locking mechanism to be honest, because at times the select menu height can be quite small, so would like to allow users to scroll to top or bottom to increase the visible item count (this small height issue is not very well visible in the linked example).

I would like the floating element to not dismiss immediately, like ancestorScroll currently does, rather when the reference element is scrolled out of the ancestor viewport, i.e. when the reference and floating element get “detached” from each other due to portaling.

Any thoughts, is this reasonable to support in floating-ui directly, or should this be implemented in userland?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
atomikscommented, Nov 2, 2022

hide() is part of the positioning library so it doesn’t have any hooks to the interactions package. It will remain rendered while scrolling out of view, but you use it to visually hide it using a CSS style.

To un-render it when that value becomes true just requires an effect like this:

useEffect(() => {
  if (middlewareData.hide?.referenceHidden) {
    setOpen(false);
  }
}, [middlewareData.hide?.referenceHidden]);
0reactions
mihkeleidastcommented, Nov 2, 2022

Yeah, that makes sense - I guess this is only feasible to use with a virtual focus, then it should not scroll. Thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to check if element is visible after scrolling?
It's easy & efficient to determine if an element is visible in the viewport, or in any scrollable container, by using an observer....
Read more >
A Few Functional Uses for Intersection Observer to Know ...
Intersection Observer observes the “intersection” (i.e. the passing across) of an element through one of its ancestor elements or the area on ...
Read more >
11 Visual effects - W3C
This value indicates that the content is clipped and that no scrolling user interface should be provided to view the content outside the...
Read more >
Know when Element Gets Visible in Screen During Scroll
Many times, during scrolling, it required to know when an element gets visible in the screen or viewport so that the future course...
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