size
Provides data to change the size of a floating element.
This is useful to ensure the floating element isn’t too big to fit in the viewport (or more specifically, its clipping context), especially when a maximum size isn’t specified. It also allows matching the width/height of the reference element.
Usage
If your floating element’s content cannot be resized such as in
the example, you can make the floating element scrollable with
overflow: scroll
(or auto
). Ensure your CSS
is using box-sizing: border-box
!
Options
These are the options you can pass to size()
.
apply
default: undefined
Unlike other middleware, in which you assign styles after
computePosition()
has done its work, size()
has its
own apply
function to do the work during the
lifecycle:
availableWidth
Represents how wide the floating element can be before it will
overflow its clipping context. You’ll generally set this as the
maxWidth
CSS property.
availableHeight
Represents how tall the floating element can be before it will
overflow its clipping context. You’ll generally set this as the
maxHeight
CSS property.
…middlewareState
See MiddlewareState.
Many useful properties are also accessible via this callback,
such as rects
and elements
.
…detectOverflowOptions
All of detectOverflow
’s options
can be passed. For instance:
Deriving options from state
You can derive the options from the middleware lifecycle state:
Using with flip()
Using size()
together with flip()
enables some
useful behavior. The floating element can be resized, thus
allowing it to prefer its initial placement as much as possible,
until it reaches a minimum size, at which point it will flip.
If you’re using the padding
option in either middleware,
ensure they share the same value.
bestFit
The 'bestFit'
fallback strategy in the flip()
middleware is the default, which ensures the best fitting
placement is used. In this scenario, place size()
after flip()
:
This strategy ensures the floating element stays in view at all times at the most optimal size.
initialPlacement
If instead, you want the initial placement to take precedence,
and are setting a minimum acceptable size, place size()
before flip()
:
Match reference width
A common feature of select dropdowns is that the dropdown matches
the width of the reference regardless of its contents. You can
also use size()
for this, as the Rect
s get
passed in:
Troubleshooting
maxHeight
style left on floating element
Leaving the maxHeight
style on the floating element can cause
issues in certain situations where it can and should expand more.
By removing the style inside the apply
function
when the scrollHeight
is less than the availableHeight
, you
can prevent this: