detect Overflow
Detects when the floating or reference element is overflowing a clipping container or custom boundary.
A clipping container (or boundary) is one that causes child elements inside it to be clipped if they overflow it. Visibility optimizer middleware use this function for collision detection, making it useful for your own custom middleware that do the same.
Usage
Inside your custom middleware, make your fn
async
and await
it, passing in the middleware
state
:
The returned value, overflow
, is a
SideObject
containing side properties with numbers
representing offsets.
- A positive number means the element is overflowing the clipping boundary by that number of pixels.
- A negative number means the element has that number of pixels left before it will overflow the clipping boundary.
0
means the side lies flush with the clipping boundary.
Options
detectOverflow()
takes options as a second argument.
boundary
This describes the clipping element(s) or area that overflow will
be checked relative to. The default is
'clippingAncestors'
, which are the overflow ancestors
which will cause the element to be clipped.
rootBoundary
This describes the root boundary that the element will be checked
for overflow relative to. The default is 'viewport'
, which
is the area of the page the user can see on the screen. This is
the
Visual Viewport
which correctly handles pinch-zooming and mobile viewports when
the keyboard is open.
The other string option is 'document'
, which is the entire
page outside the viewport.
You may also pass a Rect
object to define a custom
boundary area (relative to the viewport).
padding
This describes the virtual padding around the boundary to check for overflow.
elementContext
By default, the floating element is the one being checked for overflow.
But you can also change the context to 'reference'
to
instead check its overflow relative to its clipping boundary.
altBoundary
This is a boolean value which determines whether to check the
alternate elementContext
’s boundary.
For instance, if the elementContext
is
'floating'
, and you enable this option, then the boundary
in which overflow is checked for is the 'reference'
’s
boundary. This only applies if you are using the default
'clippingAncestors'
string as the boundary
.