center-center 0.0.44 → 0.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/common/index.d.mts +53 -46
- package/src/common/index.mjs +18 -12
- package/src/dom/index.d.mts +8 -6
- package/src/dom/index.mjs +1 -1
- package/src/index.d.mts +12 -10
- package/src/svg/index.d.mts +8 -6
- package/src/svg/index.mjs +1 -1
- package/src/svg/view-box/index.d.mts +26 -0
- package/src/svg/view-box/index.mjs +14 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "center-center",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.45",
|
|
4
4
|
"description": "Position an element at the center center of a container",
|
|
5
5
|
"main": "./src/index.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -24,7 +24,6 @@
|
|
|
24
24
|
"lint:mts:fix": "npm run lint:mts -- --fix",
|
|
25
25
|
"test": "cross-env NODE_ENV=test mocha test --recursive",
|
|
26
26
|
"prepare": "husky",
|
|
27
|
-
"types": "tsc",
|
|
28
27
|
"start": "DEBUG=center-center* nodemon docs/index.mjs --watch ."
|
|
29
28
|
},
|
|
30
29
|
"dependencies": {
|
|
@@ -36,6 +35,7 @@
|
|
|
36
35
|
"@babel/eslint-parser": "^7.23.10",
|
|
37
36
|
"@babel/preset-env": "^7.24.0",
|
|
38
37
|
"@sequencemedia/hooks": "^1.0.483",
|
|
38
|
+
"@types/debug": "^4.1.12",
|
|
39
39
|
"@typescript-eslint/eslint-plugin": "^7.1.1",
|
|
40
40
|
"@typescript-eslint/parser": "^7.1.1",
|
|
41
41
|
"chai": "^5.1.0",
|
package/src/common/index.d.mts
CHANGED
|
@@ -1,57 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
declare namespace CenterCenterTypes {
|
|
2
|
+
/**
|
|
3
|
+
* The document `scrollingElement`
|
|
4
|
+
*/
|
|
5
|
+
export interface ScrollingElement {
|
|
6
|
+
scrollLeft: number
|
|
7
|
+
scrollTop: number
|
|
8
|
+
}
|
|
8
9
|
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export interface CenterCenterRect {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
10
|
+
/**
|
|
11
|
+
* A Center Center `Rect`
|
|
12
|
+
*/
|
|
13
|
+
export interface CenterCenterRect {
|
|
14
|
+
left: number
|
|
15
|
+
top: number
|
|
16
|
+
width: number
|
|
17
|
+
height: number
|
|
18
|
+
right: number
|
|
19
|
+
bottom: number
|
|
20
|
+
}
|
|
20
21
|
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export interface CenterCenterRects {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Center Center `Rects`
|
|
24
|
+
*/
|
|
25
|
+
export interface CenterCenterRects {
|
|
26
|
+
viewport: CenterCenterRect
|
|
27
|
+
container: CenterCenterRect
|
|
28
|
+
target: CenterCenterRect
|
|
29
|
+
}
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
declare module 'center-center/common' {
|
|
33
|
+
export type ScrollingElement = CenterCenterTypes.ScrollingElement
|
|
34
|
+
export type CenterCenterRect = CenterCenterTypes.CenterCenterRect
|
|
35
|
+
export type CenterCenterRects = CenterCenterTypes.CenterCenterRects
|
|
31
36
|
|
|
32
|
-
export function
|
|
33
|
-
export function
|
|
34
|
-
export function
|
|
35
|
-
export function
|
|
36
|
-
export function
|
|
37
|
+
export function getScrollingElement (): ScrollingElement
|
|
38
|
+
export function createViewportRect (): CenterCenterRect
|
|
39
|
+
export function createDOMElementRect (element: Element): CenterCenterRect
|
|
40
|
+
export function createSVGElementRect (element: SVGElement): CenterCenterRect
|
|
41
|
+
export function createDOMRects (container: Element, target: Element): CenterCenterRect
|
|
42
|
+
export function createSVGRects (container: Element, target: SVGElement): CenterCenterRect
|
|
37
43
|
|
|
38
|
-
export function getRectLeft (rect: CenterCenterRect): number
|
|
39
|
-
export function getRectTop (rect: CenterCenterRect): number
|
|
40
|
-
export function getRectWidth (rect: CenterCenterRect): number
|
|
41
|
-
export function getRectHeight (rect: CenterCenterRect): number
|
|
44
|
+
export function getRectLeft (rect: CenterCenterRect): number
|
|
45
|
+
export function getRectTop (rect: CenterCenterRect): number
|
|
46
|
+
export function getRectWidth (rect: CenterCenterRect): number
|
|
47
|
+
export function getRectHeight (rect: CenterCenterRect): number
|
|
42
48
|
|
|
43
|
-
export function getViewportRect (rects: CenterCenterRects): CenterCenterRect
|
|
44
|
-
export function getContainerRect (rects: CenterCenterRects): CenterCenterRect
|
|
45
|
-
export function getTargetRect (rects: CenterCenterRects): CenterCenterRect
|
|
49
|
+
export function getViewportRect (rects: CenterCenterRects): CenterCenterRect
|
|
50
|
+
export function getContainerRect (rects: CenterCenterRects): CenterCenterRect
|
|
51
|
+
export function getTargetRect (rects: CenterCenterRects): CenterCenterRect
|
|
46
52
|
|
|
47
|
-
export function calculateRectX (rect: CenterCenterRect): number
|
|
48
|
-
export function calculateRectY (rect: CenterCenterRect): number
|
|
53
|
+
export function calculateRectX (rect: CenterCenterRect): number
|
|
54
|
+
export function calculateRectY (rect: CenterCenterRect): number
|
|
49
55
|
|
|
50
|
-
export function calculateBoundaryX (rects: CenterCenterRects): number
|
|
51
|
-
export function calculateBoundaryY (rects: CenterCenterRects): number
|
|
56
|
+
export function calculateBoundaryX (rects: CenterCenterRects): number
|
|
57
|
+
export function calculateBoundaryY (rects: CenterCenterRects): number
|
|
52
58
|
|
|
53
|
-
export function calculateContainerX (rects: CenterCenterRects): number
|
|
54
|
-
export function calculateContainerY (rects: CenterCenterRects): number
|
|
59
|
+
export function calculateContainerX (rects: CenterCenterRects): number
|
|
60
|
+
export function calculateContainerY (rects: CenterCenterRects): number
|
|
55
61
|
|
|
56
|
-
export function calculateTargetX (rects: CenterCenterRects): number
|
|
57
|
-
export function calculateTargetY (rects: CenterCenterRects): number
|
|
62
|
+
export function calculateTargetX (rects: CenterCenterRects): number
|
|
63
|
+
export function calculateTargetY (rects: CenterCenterRects): number
|
|
64
|
+
}
|
package/src/common/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef {import('
|
|
3
|
-
* @typedef {import('
|
|
4
|
-
* @typedef {import('
|
|
2
|
+
* @typedef {import('center-center/common').ScrollingElement} ScrollingElement
|
|
3
|
+
* @typedef {import('center-center/common').CenterCenterRect} CenterCenterRect
|
|
4
|
+
* @typedef {import('center-center/common').CenterCenterRects} CenterCenterRects
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -104,7 +104,10 @@ export function createSVGElementRect (element) {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
|
-
* Creates `Rects`
|
|
107
|
+
* Creates a collection of `Rects` describing
|
|
108
|
+
* 1. The viewport
|
|
109
|
+
* 2. The DOM container element
|
|
110
|
+
* 3. The DOM target element
|
|
108
111
|
*
|
|
109
112
|
* @param {Element} container
|
|
110
113
|
* @param {Element} target
|
|
@@ -119,7 +122,10 @@ export function createDOMRects (container, target) {
|
|
|
119
122
|
}
|
|
120
123
|
|
|
121
124
|
/**
|
|
122
|
-
* Creates `Rects`
|
|
125
|
+
* Creates a collection of `Rects` describing
|
|
126
|
+
* 1. The viewport
|
|
127
|
+
* 2. The DOM container element
|
|
128
|
+
* 3. The SVG target element
|
|
123
129
|
*
|
|
124
130
|
* @param {Element} container
|
|
125
131
|
* @param {SVGElement} target
|
|
@@ -174,7 +180,7 @@ export function getRectHeight ({ height }) {
|
|
|
174
180
|
}
|
|
175
181
|
|
|
176
182
|
/**
|
|
177
|
-
* Destructures the viewport `Rect`
|
|
183
|
+
* Destructures the viewport `Rect` from a collection of `Rects`
|
|
178
184
|
*
|
|
179
185
|
* @param {CenterCenterRects} rects
|
|
180
186
|
* @returns {CenterCenterRect}
|
|
@@ -184,7 +190,7 @@ export function getViewportRect ({ viewport }) {
|
|
|
184
190
|
}
|
|
185
191
|
|
|
186
192
|
/**
|
|
187
|
-
* Destructures the container `Rect`
|
|
193
|
+
* Destructures the container `Rect` from a collection of `Rects`
|
|
188
194
|
*
|
|
189
195
|
* @param {CenterCenterRects} rects
|
|
190
196
|
* @returns {CenterCenterRect}
|
|
@@ -194,7 +200,7 @@ export function getContainerRect ({ container }) {
|
|
|
194
200
|
}
|
|
195
201
|
|
|
196
202
|
/**
|
|
197
|
-
* Destructures the target `Rect`
|
|
203
|
+
* Destructures the target `Rect` from a collection of `Rects`
|
|
198
204
|
*
|
|
199
205
|
* @param {CenterCenterRects} rects
|
|
200
206
|
* @returns {CenterCenterRect}
|
|
@@ -206,7 +212,7 @@ export function getTargetRect ({ target }) {
|
|
|
206
212
|
/**
|
|
207
213
|
* Calculates the `x` coordinate of a `Rect`
|
|
208
214
|
*
|
|
209
|
-
* @param {CenterCenterRect}
|
|
215
|
+
* @param {CenterCenterRect} rect
|
|
210
216
|
* @returns {number}
|
|
211
217
|
*/
|
|
212
218
|
export function calculateRectX ({ left: l, width: w }) {
|
|
@@ -216,7 +222,7 @@ export function calculateRectX ({ left: l, width: w }) {
|
|
|
216
222
|
/**
|
|
217
223
|
* Calculates the `y` coordinate of a `Rect`
|
|
218
224
|
*
|
|
219
|
-
* @param {CenterCenterRect}
|
|
225
|
+
* @param {CenterCenterRect} rect
|
|
220
226
|
* @returns {number}
|
|
221
227
|
*/
|
|
222
228
|
export function calculateRectY ({ top: t, height: h }) {
|
|
@@ -226,7 +232,7 @@ export function calculateRectY ({ top: t, height: h }) {
|
|
|
226
232
|
/**
|
|
227
233
|
* Calculates the right-most boundary `x` coordinate of the target in the container
|
|
228
234
|
*
|
|
229
|
-
* The range
|
|
235
|
+
* The range is inclusive from left-most 0 to right-most boundary `x`
|
|
230
236
|
*
|
|
231
237
|
* @param {CenterCenterRects} rects
|
|
232
238
|
* @returns {number}
|
|
@@ -241,7 +247,7 @@ export function calculateBoundaryX (rects) {
|
|
|
241
247
|
/**
|
|
242
248
|
* Calculates the bottom-most boundary `y` coordinate of the target in the container
|
|
243
249
|
*
|
|
244
|
-
* The range
|
|
250
|
+
* The range is inclusive from top-most 0 to bottom-most boundary `y`
|
|
245
251
|
*
|
|
246
252
|
* @param {CenterCenterRects} rects
|
|
247
253
|
* @returns {number}
|
package/src/dom/index.d.mts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
declare module 'center-center/dom' {
|
|
2
|
+
import {
|
|
3
|
+
type CenterCenterRects
|
|
4
|
+
} from 'center-center/common'
|
|
4
5
|
|
|
5
|
-
export function createRects (container: Element, target: Element): CenterCenterRects
|
|
6
|
-
export function calculateLeft (rects: CenterCenterRects): number
|
|
7
|
-
export function calculateTop (rects: CenterCenterRects): number
|
|
6
|
+
export function createRects (container: Element, target: Element): CenterCenterRects
|
|
7
|
+
export function calculateLeft (rects: CenterCenterRects): number
|
|
8
|
+
export function calculateTop (rects: CenterCenterRects): number
|
|
9
|
+
}
|
package/src/dom/index.mjs
CHANGED
package/src/index.d.mts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
declare module 'center-center' {
|
|
2
|
+
export type {
|
|
3
|
+
ScrollingElement,
|
|
4
|
+
CenterCenterRects,
|
|
5
|
+
CenterCenterRect
|
|
6
|
+
} from 'center-center/common'
|
|
6
7
|
|
|
7
|
-
export {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from '#center-center'
|
|
8
|
+
export {
|
|
9
|
+
createRects,
|
|
10
|
+
calculateLeft,
|
|
11
|
+
calculateTop
|
|
12
|
+
} from '#center-center'
|
|
13
|
+
}
|
package/src/svg/index.d.mts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
declare module 'center-center/svg' {
|
|
2
|
+
import {
|
|
3
|
+
type CenterCenterRects
|
|
4
|
+
} from 'center-center/common'
|
|
4
5
|
|
|
5
|
-
export function createRects (container: Element, target: SVGElement): CenterCenterRects
|
|
6
|
-
export function calculateX (rects: CenterCenterRects): number
|
|
7
|
-
export function calculateX (rects: CenterCenterRects): number
|
|
6
|
+
export function createRects (container: Element, target: SVGElement): CenterCenterRects
|
|
7
|
+
export function calculateX (rects: CenterCenterRects): number
|
|
8
|
+
export function calculateX (rects: CenterCenterRects): number
|
|
9
|
+
}
|
package/src/svg/index.mjs
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
declare module 'center-center/svg/view-box' {
|
|
2
|
+
export const X: number
|
|
3
|
+
export const Y: number
|
|
4
|
+
export const W: number
|
|
5
|
+
export const H: number
|
|
6
|
+
|
|
7
|
+
function toRound (s: string): number
|
|
8
|
+
|
|
9
|
+
export function getXFor (svg: Element): number
|
|
10
|
+
|
|
11
|
+
export function getYFor (svg: Element): number
|
|
12
|
+
|
|
13
|
+
export function getWFor (svg: Element): number
|
|
14
|
+
|
|
15
|
+
export function getHFor (svg: Element): number
|
|
16
|
+
|
|
17
|
+
export function getX (viewBox: number[]): number
|
|
18
|
+
|
|
19
|
+
export function getY (viewBox: number[]): number
|
|
20
|
+
|
|
21
|
+
export function getW (viewBox: number[]): number
|
|
22
|
+
|
|
23
|
+
export function getH (viewBox: number[]): number
|
|
24
|
+
|
|
25
|
+
export default function getViewBox (svg: Element): number[]
|
|
26
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* An `<svg />` is of type `Element`
|
|
3
3
|
*
|
|
4
|
-
* Its children are of `SVGElement`
|
|
4
|
+
* Its children are of type `SVGElement`
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import debug from 'debug'
|
|
8
8
|
|
|
9
|
-
const log = debug('center-center:view-box')
|
|
9
|
+
const log = debug('center-center:svg:view-box')
|
|
10
10
|
|
|
11
11
|
export const X = 0
|
|
12
12
|
export const Y = 1
|
|
@@ -31,9 +31,9 @@ function toRound (s) {
|
|
|
31
31
|
* @param {Element} svg
|
|
32
32
|
* @returns {number}
|
|
33
33
|
*/
|
|
34
|
-
export function
|
|
34
|
+
export function getXFor (svg) {
|
|
35
35
|
return (
|
|
36
|
-
|
|
36
|
+
getX(getViewBox(svg))
|
|
37
37
|
)
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -43,9 +43,9 @@ export function getViewBoxXFor (svg) {
|
|
|
43
43
|
* @param {Element} svg
|
|
44
44
|
* @returns {number}
|
|
45
45
|
*/
|
|
46
|
-
export function
|
|
46
|
+
export function getYFor (svg) {
|
|
47
47
|
return (
|
|
48
|
-
|
|
48
|
+
getY(getViewBox(svg))
|
|
49
49
|
)
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -55,9 +55,9 @@ export function getViewBoxYFor (svg) {
|
|
|
55
55
|
* @param {Element} svg
|
|
56
56
|
* @returns {number}
|
|
57
57
|
*/
|
|
58
|
-
export function
|
|
58
|
+
export function getWFor (svg) {
|
|
59
59
|
return (
|
|
60
|
-
|
|
60
|
+
getW(getViewBox(svg))
|
|
61
61
|
)
|
|
62
62
|
}
|
|
63
63
|
|
|
@@ -67,9 +67,9 @@ export function getViewBoxWFor (svg) {
|
|
|
67
67
|
* @param {Element} svg
|
|
68
68
|
* @returns {number}
|
|
69
69
|
*/
|
|
70
|
-
export function
|
|
70
|
+
export function getHFor (svg) {
|
|
71
71
|
return (
|
|
72
|
-
|
|
72
|
+
getH(getViewBox(svg))
|
|
73
73
|
)
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -79,9 +79,7 @@ export function getViewBoxHFor (svg) {
|
|
|
79
79
|
* @param {number[]}
|
|
80
80
|
* @returns {number}
|
|
81
81
|
*/
|
|
82
|
-
export function
|
|
83
|
-
[X]: x = 0
|
|
84
|
-
}) {
|
|
82
|
+
export function getX ({ [X]: x = 0 }) {
|
|
85
83
|
return x
|
|
86
84
|
}
|
|
87
85
|
|
|
@@ -91,7 +89,7 @@ export function getViewBoxX ({
|
|
|
91
89
|
* @param {number[]}
|
|
92
90
|
* @returns {number}
|
|
93
91
|
*/
|
|
94
|
-
export function
|
|
92
|
+
export function getY ({ [Y]: y = 0 }) {
|
|
95
93
|
return y
|
|
96
94
|
}
|
|
97
95
|
|
|
@@ -101,7 +99,7 @@ export function getViewBoxY ({ [Y]: y = 0 }) {
|
|
|
101
99
|
* @param {number[]}
|
|
102
100
|
* @returns {number}
|
|
103
101
|
*/
|
|
104
|
-
export function
|
|
102
|
+
export function getW ({ [W]: w = 0 }) {
|
|
105
103
|
return w
|
|
106
104
|
}
|
|
107
105
|
|
|
@@ -111,7 +109,7 @@ export function getViewBoxW ({ [W]: w = 0 }) {
|
|
|
111
109
|
* @param {number[]}
|
|
112
110
|
* @returns {number}
|
|
113
111
|
*/
|
|
114
|
-
export function
|
|
112
|
+
export function getH ({ [H]: h = 0 }) {
|
|
115
113
|
return h
|
|
116
114
|
}
|
|
117
115
|
|