Dropdown
A small dropdown that opens upwards when there is no room below.
Demo
Try this: The two buttons sit in the lower part of the dashed box. Open Default: the menu runs out of the box and is cut off. Open Auto position: the menu opens upwards and stays complete.
Calculated values
How it works
- The menu is the item, the button of the same dropdown is the target, and the dashed box is the boundary. The buttons sit in the lower part of the box, like a button near the bottom edge of a window.
- The first dropdown does not use the library. CSS puts its menu below the button, and the box cuts off the part that does not fit. It is only there for comparison.
-
The second dropdown asks the library.
itemAt: "top left"andtargetAt: "bottom left"put the menu below the button. That leaves the boundary at the bottom, soflip: "both"tries the opposite placement: the menu sits above the button. -
stick: "all"is the last resort. If neither placement fits completely, the menu is pulled back inside the box. -
The menu is hidden with
hiddenand has no size then, so the example callsresize()after showing it, and only thencalculate(). It appliesmoveByas atransform, and removes the transform before the next calculation, becausecalculate()starts from where the menu is now.
Variations
-
In the playground, set
flipandstickto"none": the second menu is cut off like the first one. -
Use
itemAt: "top right"andtargetAt: "bottom right"for a menu that is aligned to the right edge of its button.