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.

How it works

  1. 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.
  2. 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.
  3. The second dropdown asks the library. itemAt: "top left" and targetAt: "bottom left" put the menu below the button. That leaves the boundary at the bottom, so flip: "both" tries the opposite placement: the menu sits above the button.
  4. stick: "all" is the last resort. If neither placement fits completely, the menu is pulled back inside the box.
  5. The menu is hidden with hidden and has no size then, so the example calls resize() after showing it, and only then calculate(). It applies moveBy as a transform, and removes the transform before the next calculation, because calculate() starts from where the menu is now.

Variations

See also