Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

calculate-position

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

calculate-position - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

3

build/main/lib/direction.d.ts

@@ -20,5 +20,6 @@ import { Dimensions, Direction, DirectionPoint, Rectangle } from './types';

*/
export declare const getDirections: ({ anchor, dimensions, }: {
export declare const getDirections: ({ anchor, dimensions, viewport, }: {
anchor: Rectangle;
dimensions: Dimensions;
viewport?: Dimensions | undefined;
}) => DirectionPoint;

@@ -25,0 +26,0 @@ /**

@@ -24,3 +24,4 @@ "use strict";

*/
const getDirections = ({ anchor, dimensions, }) => {
const getDirections = ({ anchor, dimensions, viewport, }) => {
var _a, _b;
return {

@@ -35,2 +36,6 @@ [types_1.Direction.BottomRight]: {

},
[types_1.Direction.BottomCenter]: {
left: Math.round((((_a = viewport === null || viewport === void 0 ? void 0 : viewport.width) !== null && _a !== void 0 ? _a : 0) - dimensions.width) / 2),
top: anchor.top + anchor.height,
},
[types_1.Direction.TopRight]: {

@@ -44,2 +49,6 @@ left: anchor.left + anchor.width,

},
[types_1.Direction.TopCenter]: {
left: Math.round((((_b = viewport === null || viewport === void 0 ? void 0 : viewport.width) !== null && _b !== void 0 ? _b : 0) - dimensions.width) / 2),
top: anchor.top - anchor.height,
},
};

@@ -94,2 +103,3 @@ };

dimensions,
viewport,
}), }) => {

@@ -115,2 +125,2 @@ const viewportRectangle = {

exports.calculateBestDirection = calculateBestDirection;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGlyZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xpYi9kaXJlY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsMkNBQWtE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NLElBQUksR0FBRywrQkFBbUIsQ0FBQyxrQkFBa0IsRUFBRSxpQkFBaUIsQ0FBQyxDQUFDO1FBRXhFLElBQUksSUFBSSxHQUFHLFFBQVEsRUFBRTtZQUNuQixRQUFRLEdBQUcsSUFBSSxDQUFDO1lBQ2hCLGFBQWEsR0FBRyxTQUFzQixDQUFDO1NBQ3hDO0tBQ0Y7SUFFRCxPQUFPLGFBQWEsQ0FBQztBQUN2QixDQUFDLENBQUM7QUF4Q1csUUFBQSxzQkFBc0IsMEJBd0NqQyJ9

@@ -25,3 +25,3 @@ "use strict";

const calculateBestPosition = ({ anchor, dimensions, viewport = direction_1.getWindowDimensions(), }) => {
const directions = direction_1.getDirections({ anchor, dimensions });
const directions = direction_1.getDirections({ anchor, dimensions, viewport });
const direction = direction_1.calculateBestDirection({

@@ -36,2 +36,2 @@ anchor,

exports.calculateBestPosition = calculateBestPosition;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zaXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3Bvc2l0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLDJDQUlxQjtBQUdyQjs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBa0JHO0FBQ0ksTUFBTSxxQkFBcUIsR0FBRyxDQUFDLEVBQ3BDLE1BQU0sRUFDTixVQUFVLEVBQ1YsUUFBUSxHQUFHLCtCQUFtQixFQUFFLEdBS2pDLEVBQVMsRUFBRTtJQUNWLE1BQU0sVUFBVSxHQUFHLHlCQUFhLENBQUMsRUFBRSxNQUFNLEVBQUUsVUFBVSxFQUFFLENBQUMsQ0FBQztJQUV6RCxNQUFNLFNBQVMsR0FBRyxrQ0FBc0IsQ0FBQztRQUN2QyxNQUFNO1FBQ04sVUFBVTtRQUNWLFFBQVE7UUFDUixVQUFVO0tBQ1gsQ0FBQyxDQUFDO0lBRUgsT0FBTyxVQUFVLENBQUMsU0FBUyxDQUFDLENBQUM7QUFDL0IsQ0FBQyxDQUFDO0FBbkJXLFFBQUEscUJBQXFCLHlCQW1CaEMifQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zaXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3Bvc2l0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLDJDQUlxQjtBQUdyQjs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBa0JHO0FBQ0ksTUFBTSxxQkFBcUIsR0FBRyxDQUFDLEVBQ3BDLE1BQU0sRUFDTixVQUFVLEVBQ1YsUUFBUSxHQUFHLCtCQUFtQixFQUFFLEdBS2pDLEVBQVMsRUFBRTtJQUNWLE1BQU0sVUFBVSxHQUFHLHlCQUFhLENBQUMsRUFBRSxNQUFNLEVBQUUsVUFBVSxFQUFFLFFBQVEsRUFBRSxDQUFDLENBQUM7SUFFbkUsTUFBTSxTQUFTLEdBQUcsa0NBQXNCLENBQUM7UUFDdkMsTUFBTTtRQUNOLFVBQVU7UUFDVixRQUFRO1FBQ1IsVUFBVTtLQUNYLENBQUMsQ0FBQztJQUVILE9BQU8sVUFBVSxDQUFDLFNBQVMsQ0FBQyxDQUFDO0FBQy9CLENBQUMsQ0FBQztBQW5CVyxRQUFBLHFCQUFxQix5QkFtQmhDIn0=

@@ -12,4 +12,6 @@ export interface Dimensions {

BottomLeft = "BottomLeft",
BottomCenter = "BottomCenter",
TopRight = "TopRight",
TopLeft = "TopLeft"
TopLeft = "TopLeft",
TopCenter = "TopCenter"
}

@@ -16,0 +18,0 @@ export interface Rectangle {

@@ -8,5 +8,7 @@ "use strict";

Direction["BottomLeft"] = "BottomLeft";
Direction["BottomCenter"] = "BottomCenter";
Direction["TopRight"] = "TopRight";
Direction["TopLeft"] = "TopLeft";
Direction["TopCenter"] = "TopCenter";
})(Direction = exports.Direction || (exports.Direction = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3R5cGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQVVBLElBQVksU0FLWDtBQUxELFdBQVksU0FBUztJQUNuQix3Q0FBMkIsQ0FBQTtJQUMzQixzQ0FBeUIsQ0FBQTtJQUN6QixrQ0FBcUIsQ0FBQTtJQUNyQixnQ0FBbUIsQ0FBQTtBQUNyQixDQUFDLEVBTFcsU0FBUyxHQUFULGlCQUFTLEtBQVQsaUJBQVMsUUFLcEIifQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3R5cGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQVVBLElBQVksU0FPWDtBQVBELFdBQVksU0FBUztJQUNuQix3Q0FBMkIsQ0FBQTtJQUMzQixzQ0FBeUIsQ0FBQTtJQUN6QiwwQ0FBNkIsQ0FBQTtJQUM3QixrQ0FBcUIsQ0FBQTtJQUNyQixnQ0FBbUIsQ0FBQTtJQUNuQixvQ0FBdUIsQ0FBQTtBQUN6QixDQUFDLEVBUFcsU0FBUyxHQUFULGlCQUFTLEtBQVQsaUJBQVMsUUFPcEIifQ==

@@ -20,5 +20,6 @@ import { Dimensions, Direction, DirectionPoint, Rectangle } from './types';

*/
export declare const getDirections: ({ anchor, dimensions, }: {
export declare const getDirections: ({ anchor, dimensions, viewport, }: {
anchor: Rectangle;
dimensions: Dimensions;
viewport?: Dimensions | undefined;
}) => DirectionPoint;

@@ -25,0 +26,0 @@ /**

@@ -21,3 +21,4 @@ import { getRectangleOverlap } from './rectangle';

*/
export const getDirections = ({ anchor, dimensions, }) => {
export const getDirections = ({ anchor, dimensions, viewport, }) => {
var _a, _b;
return {

@@ -32,2 +33,6 @@ [Direction.BottomRight]: {

},
[Direction.BottomCenter]: {
left: Math.round((((_a = viewport === null || viewport === void 0 ? void 0 : viewport.width) !== null && _a !== void 0 ? _a : 0) - dimensions.width) / 2),
top: anchor.top + anchor.height,
},
[Direction.TopRight]: {

@@ -41,2 +46,6 @@ left: anchor.left + anchor.width,

},
[Direction.TopCenter]: {
left: Math.round((((_b = viewport === null || viewport === void 0 ? void 0 : viewport.width) !== null && _b !== void 0 ? _b : 0) - dimensions.width) / 2),
top: anchor.top - anchor.height,
},
};

@@ -89,2 +98,3 @@ };

dimensions,
viewport,
}), }) => {

@@ -100,6 +110,3 @@ const viewportRectangle = {

for (const direction in directions) {
const directionRectangle = {
...directions[direction],
...dimensions,
};
const directionRectangle = Object.assign(Object.assign({}, directions[direction]), dimensions);
const area = getRectangleOverlap(directionRectangle, viewportRectangle);

@@ -113,2 +120,2 @@ if (area > bestArea) {

};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGlyZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xpYi9kaXJlY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sYUFBYSxDQUFDO0FBQ2xELE9BQU8sRUFBYyxTQUFTLEVBQTZCLE1BQU0sU0FBUyxDQUFDO0FBRTNFOzs7Ozs7Ozs7Ozs7Ozs7OztHQWlCRztBQUNILE1BQU0sQ0FBQyxNQUFNLGFBQWEsR0FBRyxDQUFDLEVBQzVCLE1BQU0sRUFDTixVQUFVLEdBSVgsRUFBa0IsRUFBRTtJQUNuQixPQUFPO1FBQ0wsQ0FBQyxTQUFTLENBQUMsV0FBVyxDQUFDLEVB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WEsQ0FBQztBQUN2QixDQUFDLENBQUMifQ==

@@ -22,3 +22,3 @@ import { calculateBestDirection, getDirections, getWindowDimensions, } from './direction';

export const calculateBestPosition = ({ anchor, dimensions, viewport = getWindowDimensions(), }) => {
const directions = getDirections({ anchor, dimensions });
const directions = getDirections({ anchor, dimensions, viewport });
const direction = calculateBestDirection({

@@ -32,2 +32,2 @@ anchor,

};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zaXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3Bvc2l0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFDTCxzQkFBc0IsRUFDdEIsYUFBYSxFQUNiLG1CQUFtQixHQUNwQixNQUFNLGFBQWEsQ0FBQztBQUdyQjs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBa0JHO0FBQ0gsTUFBTSxDQUFDLE1BQU0scUJBQXFCLEdBQUcsQ0FBQyxFQUNwQyxNQUFNLEVBQ04sVUFBVSxFQUNWLFFBQVEsR0FBRyxtQkFBbUIsRUFBRSxHQUtqQyxFQUFTLEVBQUU7SUFDVixNQUFNLFVBQVUsR0FBRyxhQUFhLENBQUMsRUFBRSxNQUFNLEVBQUUsVUFBVSxFQUFFLENBQUMsQ0FBQztJQUV6RCxNQUFNLFNBQVMsR0FBRyxzQkFBc0IsQ0FBQztRQUN2QyxNQUFNO1FBQ04sVUFBVTtRQUNWLFFBQVE7UUFDUixVQUFVO0tBQ1gsQ0FBQyxDQUFDO0lBRUgsT0FBTyxVQUFVLENBQUMsU0FBUyxDQUFDLENBQUM7QUFDL0IsQ0FBQyxDQUFDIn0=
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zaXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3Bvc2l0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFDTCxzQkFBc0IsRUFDdEIsYUFBYSxFQUNiLG1CQUFtQixHQUNwQixNQUFNLGFBQWEsQ0FBQztBQUdyQjs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBa0JHO0FBQ0gsTUFBTSxDQUFDLE1BQU0scUJBQXFCLEdBQUcsQ0FBQyxFQUNwQyxNQUFNLEVBQ04sVUFBVSxFQUNWLFFBQVEsR0FBRyxtQkFBbUIsRUFBRSxHQUtqQyxFQUFTLEVBQUU7SUFDVixNQUFNLFVBQVUsR0FBRyxhQUFhLENBQUMsRUFBRSxNQUFNLEVBQUUsVUFBVSxFQUFFLFFBQVEsRUFBRSxDQUFDLENBQUM7SUFFbkUsTUFBTSxTQUFTLEdBQUcsc0JBQXNCLENBQUM7UUFDdkMsTUFBTTtRQUNOLFVBQVU7UUFDVixRQUFRO1FBQ1IsVUFBVTtLQUNYLENBQUMsQ0FBQztJQUVILE9BQU8sVUFBVSxDQUFDLFNBQVMsQ0FBQyxDQUFDO0FBQy9CLENBQUMsQ0FBQyJ9

@@ -12,4 +12,6 @@ export interface Dimensions {

BottomLeft = "BottomLeft",
BottomCenter = "BottomCenter",
TopRight = "TopRight",
TopLeft = "TopLeft"
TopLeft = "TopLeft",
TopCenter = "TopCenter"
}

@@ -16,0 +18,0 @@ export interface Rectangle {

@@ -5,5 +5,7 @@ export var Direction;

Direction["BottomLeft"] = "BottomLeft";
Direction["BottomCenter"] = "BottomCenter";
Direction["TopRight"] = "TopRight";
Direction["TopLeft"] = "TopLeft";
Direction["TopCenter"] = "TopCenter";
})(Direction || (Direction = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3R5cGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVVBLE1BQU0sQ0FBTixJQUFZLFNBS1g7QUFMRCxXQUFZLFNBQVM7SUFDbkIsd0NBQTJCLENBQUE7SUFDM0Isc0NBQXlCLENBQUE7SUFDekIsa0NBQXFCLENBQUE7SUFDckIsZ0NBQW1CLENBQUE7QUFDckIsQ0FBQyxFQUxXLFNBQVMsS0FBVCxTQUFTLFFBS3BCIn0=
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3R5cGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVVBLE1BQU0sQ0FBTixJQUFZLFNBT1g7QUFQRCxXQUFZLFNBQVM7SUFDbkIsd0NBQTJCLENBQUE7SUFDM0Isc0NBQXlCLENBQUE7SUFDekIsMENBQTZCLENBQUE7SUFDN0Isa0NBQXFCLENBQUE7SUFDckIsZ0NBQW1CLENBQUE7SUFDbkIsb0NBQXVCLENBQUE7QUFDekIsQ0FBQyxFQVBXLFNBQVMsS0FBVCxTQUFTLFFBT3BCIn0=
{
"name": "calculate-position",
"version": "1.0.1",
"version": "1.0.2",
"description": "Calculate the optimum position for an element relative to another.",

@@ -13,4 +13,4 @@ "main": "build/main/index.js",

"build": "run-p build:*",
"build:main": "tsc -p tsconfig.json",
"build:module": "tsc -p tsconfig.module.json",
"build:main": "tsc --build tsconfig.json",
"build:module": "tsc --build tsconfig.module.json",
"fix": "run-s fix:*",

@@ -17,0 +17,0 @@ "fix:prettier": "prettier \"src/**/*.ts\" --write",

# calculate-position
Calculates the best position (left/top coords) for a given anchor and dimensions.
### Example (es module)
Mostly useful for placing popups and tooltips correctly in the viewports.
Behind the scenes we calculate all the position positions for the popup element (TopLeft, TopRight etc) and then choose the one that has the largest overlap with the viewport (normal the window).
### calculateBestDirection
Calculates the best direction (e.g. BottomLeft) for a given anchor and dimensions.
Valid directions are:
- `TopLeft`
- `TopRight`
- `TopCenter`
- `BottomLeft`
- `BottomRight`
- `BottomCenter`
`BottomRight` is the default direction.
Valid arguments:
```
@param anchor - anchor position that calculations will be based of
@param dimensions - width/height dimensions of popup element
@param viewport - (optional) viewport rectangle (defaults to window)
```
Example:
```js
import calculateBestPosition from 'calculate-position'
import {calculateBestDirection} from 'calculate-position'
const anchor = popupAnchorElement.getBoundingClientRect()
const dimensions = {width: popupWidth, height: popupHeight}
console.log(calculateBestPosition({anchor, dimensions}))
// => {left: 200, top: 100}
console.log(calculateBestDirection({anchor, dimensions}))
// => Direction.TopLeft
```
### calculateBestPosition
Returns coordinates (i.e. `top` and `left`) for a given anchor and dimensions.
Valid arguments:
```
@param anchor - anchor position that calculations will be based of. Typically an element being hovered over.
@param dimensions - width/height dimensions of popup element
@param viewport - optional viewport rectangle (defaults to window)
```
Example:
```js
import {calculateBestPosition} from 'calculate-position'
const anchor = anchorElement.getBoundingClientRect()
const dimensions = {width: popupWidth, height: popupHeight}
console.log(calculateBestPosition({anchor, dimensions}))
// => {left: 200, top: 100}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc