Popup
A popup that grows with its content and changes its side when it no longer fits.
Demo
Try this: Click Add line: the popup opens above and to the right of the button. Add more lines: when the popup no longer fits above the button it opens below, and when it fits nowhere it is pushed back into the box. Click Remove line: the popup shrinks and goes back above the button. Click the popup or press Escape to close it.
Calculated values
How it works
- The popup is the item, the Add line button is the target, and the dashed box is the boundary. The Remove line button only changes the content of the popup.
-
itemAt: "bottom left"andtargetAt: "top right"put the popup above the button, with its lower left corner at the upper right corner of the button. -
flip: "both"tries the opposite placement when the first one overflows. The popup grows with every line, and from the fourth line the room above the button is too small, so it goes below. Removing a line brings it back above. -
stick: "all"is the last resort. From the sixth line neither side fits completely, and the popup is pulled back inside the box. The example allows at most eight lines. -
Every added or removed line changes the size of the popup. The example calls
resize()beforecalculate()because a calculator keeps the size it has read. -
The example applies
moveByas atransformand removes the transform before the next calculation, becausecalculate()starts from where the popup is now. - A click on the popup or the Escape key closes it. The library does not handle that. It only calculates a position.
Variations
-
In the playground, set
stickto"none"and add eight lines: the popup leaves the box at the bottom. -
Set
flipto"none": the popup stays above the button and leaves the box at the top from the fourth line.