offset
Translates the floating element along the specified axes.
This lets you add distance (margin or spacing) between the reference and floating element, slightly alter the placement, or even create custom placements.
0px
10px
Usage
The value(s) passed are logical, meaning their effect on the physical result is dependent on the placement, writing direction (e.g. RTL), or alignment.
Order
offset()
should generally be placed at the beginning of
your middleware array.
Options
These are the options you can pass to offset()
.
A number represents the distance (gutter or margin) between the
floating element and the reference element. This is shorthand for
mainAxis
.
An object can also be passed, which enables you to individually configure each axis.
mainAxis
default: 0
The axis that runs along the side of the floating element. Represents the distance (gutter or margin) between the floating element and the reference element.
Here’s how it looks on the four sides:
top
bottom
left
right
crossAxis
default: 0
The axis that runs along the alignment of the floating element. Represents the skidding between the floating element and the reference element.
Here’s how it looks on the four sides:
top
bottom
left
right
alignmentAxis
default: null
The same axis as crossAxis
but applies only to aligned
placements and inverts the end
alignment. When set to
a number, it overrides the crossAxis
value.
A positive number will move the floating element in the direction of the opposite edge to the one that is aligned, while a negative number the reverse.
Here’s how it differentiates from crossAxis
:
top-start
(crossAxis)
top-end
(crossAxis)
top-start
(alignmentAxis)
top-end
(alignmentAxis)
Creating custom placements
While you can only choose 12 different placements as part of the
core library, you can use the offset()
middleware to
create any placement you want.
For example, although the library doesn’t provide a placement for centering on both axes, offset enables this via the function option by allowing you to read the rects:
10px
In this case, the function option starts from the default bottom placement, then using that starting point, returns an offset to center the floating element on both axes.
A diagonal placement is also possible:
This time, 'top-start'
was used as the starting point.
So, it’s straightforward to allow this:
With a wrapper, like this: