shift
Shifts the floating element to keep it in view.
This prevents the floating element from overflowing along its axis of alignment, thereby preserving the side it’s placed on.
Usage
Options
These are the options you can pass to shift()
.
mainAxis
default: true
This is the main axis in which shifting is applied.
x
-axis for'top'
and'bottom'
placementsy
-axis for'left'
and'right'
placements
crossAxis
default: false
This is the cross axis in which shifting is applied, the opposite
axis of mainAxis
.
Enabling this can lead to the floating element overlapping
the reference element, which may not be desired and is often
replaced by the flip()
middleware.
limiter
default: no-op
This accepts a function that limits the shifting done, in order to prevent detachment or “overly-eager” behavior. The behavior is to stop shifting once the opposite edges of the elements are aligned.
This function itself takes options.
limitShift.mainAxis
default: true
Whether to apply limiting on the main axis.
limitShift.crossAxis
default: true
Whether to apply limiting on the cross axis.
limitShift.offset
default: 0
This will offset when the limiting starts. A positive number will start limiting earlier, while negative later.
This can also take a function, which provides the
Rect
s of each element to read their dimensions:
You may also pass an object to configure both axes:
…detectOverflowOptions
All of detectOverflow
’s options
can be passed. For instance:
If you find the padding does not get applied on the right side, see Handling large content.
Deriving options from state
You can derive the options from the middleware lifecycle state:
Data
The following data is available in middlewareData.shift
:
x
and y
represent how much the floating element
has been shifted along that axis. The values are offsets, and
therefore can be negative.