Tooltip

A tooltip for the info button of a form field. It sits above the button and jumps below it when the top edge is in the way.

Demo

Try this: Hover or tab to the i next to Username: it is close to the top edge, so the tooltip appears below. The i next to Password has room above, so the tooltip appears above. The Calculated values show which placement was used.

How it works

  1. The tooltip is the item, the hovered button is the target, and the stage is the boundary. There is one calculator for each button.
  2. itemAt: "bottom right" and targetAt: "top right" put the bottom right corner of the tooltip on the top right corner of the button. The buttons sit at the right edge of the form, so the tooltip grows to the left and stays inside. targetOffset with y: -8 leaves a gap of 8 pixels.
  3. flip: "both" lets the library try the opposite placement when the first one overflows the boundary. mirror: true turns the gap around with it, so it is still 8 pixels, now below the button.
  4. The tooltip is hidden with hidden and has no size then. That is why the example calls resize() after showing it, and only then calculate().
  5. calculate() returns how far to move the tooltip from where it is now. The example adds moveBy to the current left and top.

Variations

See also