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.
Calculated values
How it works
- The tooltip is the item, the hovered button is the target, and the stage is the boundary. There is one calculator for each button.
-
itemAt: "bottom right"andtargetAt: "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.targetOffsetwithy: -8leaves a gap of 8 pixels. -
flip: "both"lets the library try the opposite placement when the first one overflows the boundary.mirror: trueturns the gap around with it, so it is still 8 pixels, now below the button. -
The tooltip is hidden with
hiddenand has no size then. That is why the example callsresize()after showing it, and only thencalculate(). -
calculate()returns how far to move the tooltip from where it is now. The example addsmoveByto the currentleftandtop.
Variations
-
In the playground, change
flipto"none"and hover the i next to Username: the tooltip stays above and leaves the box.distance.overflowin the result says which edge. - Add
stick: "all"to pull the tooltip back inside the box when no placement fits completely. - Use
targetAt: "middle right"anditemAt: "middle left"for a tooltip at the side.