|
1 | | -# Hover Position (An NPM package) |
| 1 | +# Hover Position (A JavaScript package) |
2 | 2 |
|
3 | | -Some text |
| 3 | +A small package to help position a floating element. This can be positioned relative to another element or to a mouse event. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +If you want to position an element to another fixed element then you can use the sample below |
| 8 | + |
| 9 | +```TS |
| 10 | +return new HoverPosition( |
| 11 | + { |
| 12 | + anchor: document.getElementById("Anchor"), |
| 13 | + target: document.getElementById("Target"), |
| 14 | + my: "top center", |
| 15 | + at: "bottom center", |
| 16 | + } |
| 17 | +); |
| 18 | +``` |
| 19 | + |
| 20 | +If you'd rather position the element to the mouse's movement then you can use the sample below |
| 21 | + |
| 22 | +> ⚠ It is recommended to debounce the below sample, just to prevent performance issues |
| 23 | +
|
| 24 | +```TS |
| 25 | +document.addEventListener("mousemove", function(e){ |
| 26 | + const target = document.getElementById("Target"), |
| 27 | + position = new HoverPosition( |
| 28 | + { |
| 29 | + anchor: document.getElementById("Anchor"), |
| 30 | + target: target, |
| 31 | + my: "top center", |
| 32 | + at: "bottom center", |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + target.style.top = pos.top; |
| 37 | + target.style.left = pos.left; |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +## Options |
| 42 | + |
| 43 | +### `my` |
| 44 | + |
| 45 | +The location of the `target` to position from |
| 46 | + |
| 47 | +**Type:** `PositionAlignment` |
| 48 | + |
| 49 | +### `at` |
| 50 | + |
| 51 | +The location of the `anchor` to position to |
| 52 | + |
| 53 | +**Type:** `PositionAlignment` | `PositionAlignment` | |
| 54 | + |
| 55 | +### `anchor` |
| 56 | + |
| 57 | +**Type:** `HTMLElement` OR `MouseEvent` | The element or mouse event to calculate against |
| 58 | + |
| 59 | +### `target` |
| 60 | + |
| 61 | +**Type:** `HTMLElement` | The target that we're going to be positioning |
| 62 | + |
| 63 | +### `collision` - Optional |
| 64 | + |
| 65 | +How to handle any collisions. |
| 66 | + |
| 67 | +**Type:** `"bestFit"` OR `"flipfit"` OR `"ignore"` | |
| 68 | + |
| 69 | +- bestFit |
| 70 | + - This will find the best fit before trying to flip the element |
| 71 | +- flipFit |
| 72 | + - This will flip the element completely vertically and horizontally |
| 73 | +- ignore |
| 74 | + - This will just ignore any collisions and place the element exactly where you wanted it |
| 75 | + |
| 76 | +### `bestFitPreference` - Optional |
| 77 | + |
| 78 | +When `collision` is set to `bestFit`, this is the preferred "best" direction |
| 79 | + |
| 80 | +**Type:** `"horizontal"` OR `"vertical"` |
| 81 | + |
| 82 | +### `defaults` - Optional |
| 83 | + |
| 84 | +This has two uses. |
| 85 | + |
| 86 | +1. If only `"top"` is suppled to `my`, `my`'s horizontal position is fetched from here |
| 87 | +2. If you are allowing `at` or `my` to be parsed from an untrusted source then this allows you to fall back to a default |
| 88 | + |
| 89 | +**Type:** `{ my: CombinedAlignment; at: CombinedAlignment }` |
| 90 | + |
| 91 | +#### The `CombinedAlignment` property |
| 92 | + |
| 93 | +The `CombinedAlignment` is a combination of vertical and horizontal alignments. |
| 94 | + |
| 95 | +Strings that match this are: |
| 96 | + |
| 97 | +- "top center" |
| 98 | +- "top left" |
| 99 | +- "top right" |
| 100 | +- "center center" |
| 101 | +- "center left" |
| 102 | +- "center right" |
| 103 | +- "bottom center" |
| 104 | +- "bottom left" |
| 105 | +- "bottom right" |
| 106 | + |
| 107 | +#### The `PositionAlignment` property |
| 108 | + |
| 109 | +The `PositionAlignment` is an extension of the `CombinedAlignment` property but it also allows just a vertical or horizontal alignment to be passed to it. |
| 110 | + |
| 111 | +So, as well as those above, it will also allow: |
| 112 | + |
| 113 | +- "top" |
| 114 | +- "bottom" |
| 115 | +- "center" |
| 116 | +- "left" |
| 117 | +- "right" |
0 commit comments