Placement and collisions

How reference points, offsets, flip and stick decide where the item ends up.

Goal

Decide where an item ends up relative to its target: which points meet, how far they are shifted, and what happens when the item leaves the boundary. Each example below was run in a browser.

Before you start

You know how to create an instance, as shown in Getting started. The examples use a button as the target and a tooltip as the item. The code is the same as in the getting-started guide, with other options.

Reference points

itemAt names the point of the item and targetAt the point of the target. The library moves the item so that the two points meet. Each value has the form "vertical horizontal". The default is "top left" for both.

const calculator = new PositionCalculator({
    item: tooltip,
    target: button,
    itemAt: "bottom center",
    targetAt: "top center",
});

With these values the bottom edge of the tooltip touches the top edge of the button, and both are centred on each other.

Offsets

itemOffset and targetOffset shift a reference point after it has been placed. Each offset has y and x, in pixels or as a percentage of the element size, and a mirror flag. A percentage refers to the height for y and to the width for x.

const calculator = new PositionCalculator({
    item: tooltip,
    target: button,
    itemAt: "bottom center",
    targetAt: "top center",
    itemOffset: { y: 8, x: 0, mirror: true },
});

With y: 8 and no flip, the tooltip moves 8 px down, so it overlaps the button by 8 px. A positive value moves the item toward the target in this placement.

mirror decides whether the offset follows the flip. With mirror: true, the sign of the offset is inverted on each flipped axis, so it keeps pointing toward the target after a flip. The default offsets have mirror: true. An offset object you pass without mirror has mirror: false. To keep an 8 px gap on the same side of the target after a flip, use a negative value with mirror: true:

const calculator = new PositionCalculator({
    item: tooltip,
    target: button,
    itemAt: "bottom center",
    targetAt: "top center",
    flip: "both",
    itemOffset: { y: -8, x: 0, mirror: true },
});

With this setup the tooltip keeps 8 px of space to the button, both when it sits above and when the flip puts it below. Without mirror, the same y: 8 keeps its screen direction after a flip, so the tooltip ends 8 px below the button in the flipped case.

Flip

Flip changes the placement when the item overflows an edge of the boundary. It needs a target. The values are "none" (the default), "item", "target", "both", and true, which means "both".

With itemAt: "bottom center" and targetAt: "top center" and a button near the top of the window, the three values give different results:

Flip changes only the axis that overflows. If the item overflows both edges of one axis, for example because it is taller than the boundary, the library does not flip on that axis. The result then keeps the placement and lists both edges in distance.overflow.

If the flipped placement still overflows, the library picks, for each axis, the placement with less overflow. The result object shows which placement was used in itemAt and targetAt.

Stick

Stick moves the item back inside the boundary along the edges you name, after flip has been applied. Values are "none" (the default), "all" or true for every edge, false, or a list of one to four edges separated by spaces, for example "top left". Stick works without a target.

const calculator = new PositionCalculator({
    item: tooltip,
    boundary: "#container",
    stick: "left top",
});

In a check with the item 50 px left of a boundary that starts at x = 0, stick: "left" moved the item 50 px to the right, and no edge overflowed. With stick: "top" the item was not moved, and "left" stayed in distance.overflow. The stick does not change the placement, so itemAt and targetAt stay the same.

Try it

Change the options in the playground and see where the item ends up. The calculate() method gives the final placement.

Next