center-center 0.0.41 → 0.0.43

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/docs/index.html CHANGED
@@ -7,7 +7,10 @@
7
7
  {
8
8
  "imports": {
9
9
  "debug": "https://esm.sh/debug",
10
- "center-center": "/assets/index.mjs"
10
+ "center-center": "/assets/index.mjs",
11
+ "#center-center/common": "/assets/common/index.mjs",
12
+ "#center-center/dom": "/assets/dom/index.mjs",
13
+ "#center-center/svg": "/assets/svg/index.mjs"
11
14
  }
12
15
  }
13
16
  </script>
@@ -15,7 +18,7 @@
15
18
  import debug from 'debug'
16
19
 
17
20
  import {
18
- createDOMRects,
21
+ createRects,
19
22
  calculateLeft,
20
23
  calculateTop
21
24
  } from 'center-center'
@@ -73,7 +76,7 @@
73
76
  log('render')
74
77
 
75
78
  window.requestAnimationFrame(() => {
76
- const rects = createDOMRects(container, target)
79
+ const rects = createRects(container, target)
77
80
  const left = calculateLeft(rects)
78
81
  const top = calculateTop(rects)
79
82
 
package/docs/index.mjs CHANGED
@@ -80,7 +80,16 @@ app
80
80
  res.sendFile('docs/images/favicon.ico', { root: '.' })
81
81
  })
82
82
  .get('/assets/index.mjs', (req, res) => {
83
- res.sendFile('index.mjs', { root: '.' })
83
+ res.sendFile('src/index.mjs', { root: '.' })
84
+ })
85
+ .get('/assets/common/index.mjs', (req, res) => {
86
+ res.sendFile('src/common/index.mjs', { root: '.' })
87
+ })
88
+ .get('/assets/dom/index.mjs', (req, res) => {
89
+ res.sendFile('src/dom/index.mjs', { root: '.' })
90
+ })
91
+ .get('/assets/svg/index.mjs', (req, res) => {
92
+ res.sendFile('src/svg/index.mjs', { root: '.' })
84
93
  })
85
94
  .get('/', (req, res) => {
86
95
  res.sendFile('docs/index.html', { root: '.' })
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "center-center",
3
- "version": "0.0.41",
3
+ "version": "0.0.43",
4
4
  "description": "Position an element at the center center of a container",
5
- "main": "./index.mjs",
5
+ "main": "./src/index.mjs",
6
6
  "type": "module",
7
- "types": "./index.d.mts",
7
+ "types": "./src/index.d.mts",
8
8
  "author": {
9
9
  "name": "Jonathan Perry for Sequence Media Limited",
10
10
  "email": "sequencemedia@sequencemedia.net",
@@ -52,6 +52,16 @@
52
52
  "typescript": "^5.4.2"
53
53
  },
54
54
  "imports": {
55
- "#center-center": "./index.mjs"
55
+ "#center-center/common": "./src/common/index.mjs",
56
+ "#center-center/dom": "./src/dom/index.mjs",
57
+ "#center-center/svg": "./src/svg/index.mjs",
58
+ "#center-center": "./src/index.mjs"
59
+ },
60
+ "exports": {
61
+ "./common": "./src/common/index.mjs",
62
+ "./dom": "./src/dom/index.mjs",
63
+ "./svg/view-box": "./src/svg/view-box/index.mjs",
64
+ "./svg": "./src/svg/index.mjs",
65
+ ".": "./src/index.mjs"
56
66
  }
57
67
  }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * The document `scrollingElement`
3
+ */
4
+ export interface ScrollingElement {
5
+ scrollLeft: number
6
+ scrollTop: number
7
+ }
8
+
9
+ /**
10
+ * A Center Center `Rect`
11
+ */
12
+ export interface CenterCenterRect {
13
+ left: number
14
+ top: number
15
+ width: number
16
+ height: number
17
+ right: number
18
+ bottom: number
19
+ }
20
+
21
+ /**
22
+ * Center Center `Rects`
23
+ */
24
+ export interface CenterCenterRects {
25
+ viewport: CenterCenterRect
26
+ container: CenterCenterRect
27
+ target: CenterCenterRect
28
+ }
29
+
30
+ export function getScrollingElement (): ScrollingElement
31
+
32
+ export function createViewportRect (): CenterCenterRect
33
+ export function createDOMElementRect (element: Element): CenterCenterRect
34
+ export function createSVGElementRect (element: SVGElement): CenterCenterRect
35
+ export function createDOMRects (container: Element, target: Element): CenterCenterRect
36
+ export function createSVGRects (container: Element, target: SVGElement): CenterCenterRect
37
+
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
42
+
43
+ export function getViewportRect (rects: CenterCenterRects): CenterCenterRect
44
+ export function getContainerRect (rects: CenterCenterRects): CenterCenterRect
45
+ export function getTargetRect (rects: CenterCenterRects): CenterCenterRect
46
+
47
+ export function calculateRectX (rect: CenterCenterRect): number
48
+ export function calculateRectY (rect: CenterCenterRect): number
49
+
50
+ export function calculateBoundaryX (rects: CenterCenterRects): number
51
+ export function calculateBoundaryY (rects: CenterCenterRects): number
52
+
53
+ export function calculateContainerX (rects: CenterCenterRects): number
54
+ export function calculateContainerY (rects: CenterCenterRects): number
55
+
56
+ export function calculateTargetX (rects: CenterCenterRects): number
57
+ export function calculateTargetY (rects: CenterCenterRects): number
@@ -1,7 +1,7 @@
1
1
  /**
2
- * @typedef {import('.').ScrollingElement} ScrollingElement
3
- * @typedef {import('.').CenterCenterRect} CenterCenterRect
4
- * @typedef {import('.').CenterCenterRects} CenterCenterRects
2
+ * @typedef {import('./index.mjs').ScrollingElement} ScrollingElement
3
+ * @typedef {import('./index.mjs').CenterCenterRect} CenterCenterRect
4
+ * @typedef {import('./index.mjs').CenterCenterRects} CenterCenterRects
5
5
  */
6
6
 
7
7
  /**
@@ -21,46 +21,6 @@ export function getScrollingElement () {
21
21
  }
22
22
  }
23
23
 
24
- /**
25
- * Destructures the `left` field from a `Rect`
26
- *
27
- * @param {CenterCenterRect}
28
- * @returns {number}
29
- */
30
- export function getRectLeft ({ left }) {
31
- return left
32
- }
33
-
34
- /**
35
- * Destructures the `top` field from a `Rect`
36
- *
37
- * @param {CenterCenterRect}
38
- * @returns {number}
39
- */
40
- export function getRectTop ({ top }) {
41
- return top
42
- }
43
-
44
- /**
45
- * Destructures the `width` field from a `Rect`
46
- *
47
- * @param {CenterCenterRect}
48
- * @returns {number}
49
- */
50
- export function getRectWidth ({ width }) {
51
- return width
52
- }
53
-
54
- /**
55
- * Destructures the `height` field from a `Rect`
56
- *
57
- * @param {CenterCenterRect}
58
- * @returns {number}
59
- */
60
- export function getRectHeight ({ height }) {
61
- return height
62
- }
63
-
64
24
  /**
65
25
  * Creates a `Rect` for the viewport
66
26
  *
@@ -88,18 +48,26 @@ export function createViewportRect () {
88
48
  }
89
49
 
90
50
  /**
91
- * Creates a `Rect` for an SVG element
51
+ * Creates a `Rect` for a DOM element
92
52
  *
93
- * @param {SVGElement} element
53
+ * @param {Element} element
94
54
  * @returns {CenterCenterRect}
95
55
  */
96
- export function createSVGElementRect (element) {
56
+ export function createDOMElementRect (element) {
97
57
  const {
98
- x: left,
99
- y: top,
58
+ scrollLeft,
59
+ scrollTop
60
+ } = getScrollingElement()
61
+
62
+ const {
63
+ x,
64
+ y,
100
65
  width,
101
66
  height
102
- } = element.getBBox()
67
+ } = element.getBoundingClientRect()
68
+
69
+ const left = (scrollLeft + x)
70
+ const top = (scrollTop + y)
103
71
 
104
72
  return {
105
73
  left,
@@ -112,26 +80,18 @@ export function createSVGElementRect (element) {
112
80
  }
113
81
 
114
82
  /**
115
- * Creates a `Rect` for a DOM element
83
+ * Creates a `Rect` for an SVG element
116
84
  *
117
- * @param {Element} element
85
+ * @param {SVGElement} element
118
86
  * @returns {CenterCenterRect}
119
87
  */
120
- export function createDOMElementRect (element) {
121
- const {
122
- scrollLeft,
123
- scrollTop
124
- } = getScrollingElement()
125
-
88
+ export function createSVGElementRect (element) {
126
89
  const {
127
- x,
128
- y,
90
+ x: left,
91
+ y: top,
129
92
  width,
130
93
  height
131
- } = element.getBoundingClientRect()
132
-
133
- const left = (scrollLeft + x)
134
- const top = (scrollTop + y)
94
+ } = element.getBBox()
135
95
 
136
96
  return {
137
97
  left,
@@ -144,63 +104,103 @@ export function createDOMElementRect (element) {
144
104
  }
145
105
 
146
106
  /**
147
- * Creates `Rects` for the viewport as well as the DOM container and SVG target elements
107
+ * Creates `Rects` for the viewport as well as the DOM container and DOM target elements
148
108
  *
149
109
  * @param {Element} container
150
- * @param {SVGElement} target
110
+ * @param {Element} target
151
111
  * @returns {CenterCenterRects}
152
112
  */
153
- export function createSVGRects (container, target) {
113
+ export function createDOMRects (container, target) {
154
114
  return {
155
115
  viewport: createViewportRect(),
156
116
  container: createDOMElementRect(container),
157
- target: createSVGElementRect(target)
117
+ target: createDOMElementRect(target)
158
118
  }
159
119
  }
160
120
 
161
121
  /**
162
- * Creates `Rects` for the viewport as well as the DOM container and DOM target elements
122
+ * Creates `Rects` for the viewport as well as the DOM container and SVG target elements
163
123
  *
164
124
  * @param {Element} container
165
- * @param {Element} target
125
+ * @param {SVGElement} target
166
126
  * @returns {CenterCenterRects}
167
127
  */
168
- export function createDOMRects (container, target) {
128
+ export function createSVGRects (container, target) {
169
129
  return {
170
130
  viewport: createViewportRect(),
171
131
  container: createDOMElementRect(container),
172
- target: createDOMElementRect(target)
132
+ target: createSVGElementRect(target)
173
133
  }
174
134
  }
175
135
 
176
136
  /**
177
- * Calculates the right-most boundary `x` coordinate of the target in the container
137
+ * Destructures the `left` field from a `Rect`
178
138
  *
179
- * The range for target `x` is from left-most 0 to right-most boundary, inclusive
139
+ * @param {CenterCenterRect} rect
140
+ * @returns {number}
141
+ */
142
+ export function getRectLeft ({ left }) {
143
+ return left
144
+ }
145
+
146
+ /**
147
+ * Destructures the `top` field from a `Rect`
180
148
  *
181
- * @param {CenterCenterRects} rects
149
+ * @param {CenterCenterRect} rect
182
150
  * @returns {number}
183
151
  */
184
- export function calculateBoundaryX (rects) {
185
- const containerW = getRectWidth(getContainerRect(rects))
186
- const targetW = getRectWidth(getTargetRect(rects))
152
+ export function getRectTop ({ top }) {
153
+ return top
154
+ }
187
155
 
188
- return (containerW - targetW)
156
+ /**
157
+ * Destructures the `width` field from a `Rect`
158
+ *
159
+ * @param {CenterCenterRect} rect
160
+ * @returns {number}
161
+ */
162
+ export function getRectWidth ({ width }) {
163
+ return width
189
164
  }
190
165
 
191
166
  /**
192
- * Calculates the bottom-most boundary `y` coordinate of the target in the container
167
+ * Destructures the `height` field from a `Rect`
193
168
  *
194
- * The range for target `y` is from top-most 0 to bottom-most boundary, inclusive
169
+ * @param {CenterCenterRect} rect
170
+ * @returns {number}
171
+ */
172
+ export function getRectHeight ({ height }) {
173
+ return height
174
+ }
175
+
176
+ /**
177
+ * Destructures the viewport `Rect`
195
178
  *
196
179
  * @param {CenterCenterRects} rects
197
- * @returns {number}
180
+ * @returns {CenterCenterRect}
198
181
  */
199
- export function calculateBoundaryY (rects) {
200
- const containerH = getRectHeight(getContainerRect(rects))
201
- const targetH = getRectHeight(getTargetRect(rects))
182
+ export function getViewportRect ({ viewport }) {
183
+ return viewport
184
+ }
202
185
 
203
- return (containerH - targetH)
186
+ /**
187
+ * Destructures the container `Rect`
188
+ *
189
+ * @param {CenterCenterRects} rects
190
+ * @returns {CenterCenterRect}
191
+ */
192
+ export function getContainerRect ({ container }) {
193
+ return container
194
+ }
195
+
196
+ /**
197
+ * Destructures the target `Rect`
198
+ *
199
+ * @param {CenterCenterRects} rects
200
+ * @returns {CenterCenterRect}
201
+ */
202
+ export function getTargetRect ({ target }) {
203
+ return target
204
204
  }
205
205
 
206
206
  /**
@@ -223,6 +223,36 @@ export function calculateRectY ({ top: t, height: h }) {
223
223
  return (t + (h / 2))
224
224
  }
225
225
 
226
+ /**
227
+ * Calculates the right-most boundary `x` coordinate of the target in the container
228
+ *
229
+ * The range for target `x` is from left-most 0 to right-most boundary, inclusive
230
+ *
231
+ * @param {CenterCenterRects} rects
232
+ * @returns {number}
233
+ */
234
+ export function calculateBoundaryX (rects) {
235
+ const containerW = getRectWidth(getContainerRect(rects))
236
+ const targetW = getRectWidth(getTargetRect(rects))
237
+
238
+ return (containerW - targetW)
239
+ }
240
+
241
+ /**
242
+ * Calculates the bottom-most boundary `y` coordinate of the target in the container
243
+ *
244
+ * The range for target `y` is from top-most 0 to bottom-most boundary, inclusive
245
+ *
246
+ * @param {CenterCenterRects} rects
247
+ * @returns {number}
248
+ */
249
+ export function calculateBoundaryY (rects) {
250
+ const containerH = getRectHeight(getContainerRect(rects))
251
+ const targetH = getRectHeight(getTargetRect(rects))
252
+
253
+ return (containerH - targetH)
254
+ }
255
+
226
256
  /**
227
257
  * Calculates the `x` coordinate of the container
228
258
  *
@@ -296,101 +326,3 @@ export function calculateTargetY (rects) {
296
326
 
297
327
  return (y - (h / 2))
298
328
  }
299
-
300
- /**
301
- * Destructures the viewport `Rect`
302
- *
303
- * @param {CenterCenterRects} rects
304
- * @returns {CenterCenterRect}
305
- */
306
- export function getViewportRect ({ viewport }) {
307
- return viewport
308
- }
309
-
310
- /**
311
- * Destructures the container `Rect`
312
- *
313
- * @param {CenterCenterRects} rects
314
- * @returns {CenterCenterRect}
315
- */
316
- export function getContainerRect ({ container }) {
317
- return container
318
- }
319
-
320
- /**
321
- * Destructures the target `Rect`
322
- *
323
- * @param {CenterCenterRects} rects
324
- * @returns {CenterCenterRect}
325
- */
326
- export function getTargetRect ({ target }) {
327
- return target
328
- }
329
-
330
- /**
331
- * Calculates the target `x` position
332
- *
333
- * @param {CenterCenterRects} rects
334
- * @param {number | undefined} viewBoxW
335
- * @param {number | undefined} scale
336
- * @returns {number}
337
- */
338
- export function calculateX (rects, viewBoxW = 0, scale = 1) {
339
- const targetX = calculateRectX(getTargetRect(rects))
340
- const containerX = calculateContainerX(rects)
341
- const w = getRectWidth(getContainerRect(rects))
342
- const ratio = (viewBoxW / w)
343
-
344
- return (
345
- targetX - ((containerX * ratio) / scale)
346
- )
347
- }
348
-
349
- /**
350
- * Calculates the target `y` position
351
- *
352
- * @param {CenterCenterRects} rects
353
- * @param {number | undefined} viewBoxH
354
- * @param {number | undefined} scale
355
- * @returns {number}
356
- */
357
- export function calculateY (rects, viewBoxH = 0, scale = 1) {
358
- const targetY = calculateRectY(getTargetRect(rects))
359
- const containerY = calculateContainerY(rects)
360
- const h = getRectHeight(getContainerRect(rects))
361
- const ratio = (viewBoxH / h)
362
-
363
- return (
364
- targetY - ((containerY * ratio) / scale)
365
- )
366
- }
367
-
368
- /**
369
- * Calculates the target `left` position
370
- *
371
- * @param {CenterCenterRects} rects
372
- * @returns {number}
373
- */
374
- export function calculateLeft (rects) {
375
- const boundary = calculateBoundaryX(rects)
376
- const x = calculateTargetX(rects)
377
-
378
- return (
379
- Math.max(0, Math.min(boundary, x))
380
- )
381
- }
382
-
383
- /**
384
- * Calculates the target `top` position
385
- *
386
- * @param {CenterCenterRects} rects
387
- * @returns {number}
388
- */
389
- export function calculateTop (rects) {
390
- const boundary = calculateBoundaryY(rects)
391
- const y = calculateTargetY(rects)
392
-
393
- return (
394
- Math.max(0, Math.min(boundary, y))
395
- )
396
- }
@@ -0,0 +1,7 @@
1
+ import {
2
+ type CenterCenterRects
3
+ } from '#center-center/common'
4
+
5
+ export function createRects (container: Element, target: Element): CenterCenterRects
6
+ export function calculateLeft (rects: CenterCenterRects): number
7
+ export function calculateTop (rects: CenterCenterRects): number
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @typedef {import('./common/index.mjs').ScrollingElement} ScrollingElement
3
+ * @typedef {import('./common/index.mjs').CenterCenterRect} CenterCenterRect
4
+ * @typedef {import('./common/index.mjs').CenterCenterRects} CenterCenterRects
5
+ */
6
+
7
+ import {
8
+ calculateBoundaryX,
9
+ calculateBoundaryY,
10
+ calculateTargetX,
11
+ calculateTargetY
12
+ } from '#center-center/common'
13
+
14
+ export {
15
+ createDOMRects as createRects
16
+ } from '#center-center/common'
17
+
18
+ /**
19
+ * Calculates the target `left` position
20
+ *
21
+ * @param {CenterCenterRects} rects
22
+ * @returns {number}
23
+ */
24
+ export function calculateLeft (rects) {
25
+ const boundary = calculateBoundaryX(rects)
26
+ const x = calculateTargetX(rects)
27
+
28
+ return (
29
+ Math.max(0, Math.min(boundary, x))
30
+ )
31
+ }
32
+
33
+ /**
34
+ * Calculates the target `top` position
35
+ *
36
+ * @param {CenterCenterRects} rects
37
+ * @returns {number}
38
+ */
39
+ export function calculateTop (rects) {
40
+ const boundary = calculateBoundaryY(rects)
41
+ const y = calculateTargetY(rects)
42
+
43
+ return (
44
+ Math.max(0, Math.min(boundary, y))
45
+ )
46
+ }
@@ -0,0 +1,11 @@
1
+ export type {
2
+ ScrollingElement,
3
+ CenterCenterRects,
4
+ CenterCenterRect
5
+ } from '#center-center/common'
6
+
7
+ export {
8
+ createRects,
9
+ calculateLeft,
10
+ calculateTop
11
+ } from '#center-center'
package/src/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ export {
2
+ createRects,
3
+ calculateLeft,
4
+ calculateTop
5
+ } from '#center-center/dom'
@@ -0,0 +1,7 @@
1
+ import {
2
+ type CenterCenterRects
3
+ } from '#center-center/common'
4
+
5
+ export function createRects (container: Element, target: SVGElement): CenterCenterRects
6
+ export function calculateX (rects: CenterCenterRects): number
7
+ export function calculateX (rects: CenterCenterRects): number
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @typedef {import('../common/index.mjs').ScrollingElement} ScrollingElement
3
+ * @typedef {import('../common/index.mjs').CenterCenterRect} CenterCenterRect
4
+ * @typedef {import('../common/index.mjs').CenterCenterRects} CenterCenterRects
5
+ */
6
+
7
+ import {
8
+ getTargetRect,
9
+ calculateRectX,
10
+ calculateRectY,
11
+ calculateContainerX,
12
+ calculateContainerY,
13
+ getContainerRect,
14
+ getRectWidth,
15
+ getRectHeight
16
+ } from '#center-center/common'
17
+
18
+ export {
19
+ createSVGRects as createRects
20
+ } from '#center-center/common'
21
+
22
+ /**
23
+ * Calculates the target `x` position
24
+ *
25
+ * @param {CenterCenterRects} rects
26
+ * @param {number | undefined} viewBoxW
27
+ * @param {number | undefined} scale
28
+ * @returns {number}
29
+ */
30
+ export function calculateX (rects, viewBoxW = 0, scale = 1) {
31
+ const targetX = calculateRectX(getTargetRect(rects))
32
+ const containerX = calculateContainerX(rects)
33
+ const w = getRectWidth(getContainerRect(rects))
34
+ const ratio = (viewBoxW / w)
35
+
36
+ return (
37
+ targetX - ((containerX * ratio) / scale)
38
+ )
39
+ }
40
+
41
+ /**
42
+ * Calculates the target `y` position
43
+ *
44
+ * @param {CenterCenterRects} rects
45
+ * @param {number | undefined} viewBoxH
46
+ * @param {number | undefined} scale
47
+ * @returns {number}
48
+ */
49
+ export function calculateY (rects, viewBoxH = 0, scale = 1) {
50
+ const targetY = calculateRectY(getTargetRect(rects))
51
+ const containerY = calculateContainerY(rects)
52
+ const h = getRectHeight(getContainerRect(rects))
53
+ const ratio = (viewBoxH / h)
54
+
55
+ return (
56
+ targetY - ((containerY * ratio) / scale)
57
+ )
58
+ }
package/index.d.mts DELETED
@@ -1,43 +0,0 @@
1
- export function getParentElementForOffsetLeft (element: SVGElement): Element
2
- export function getParentElementForOffsetTop (element: SVGElement): Element
3
- export function getLeft (element: Element | SVGElement): number
4
- export function getTop (element: Element | SVGElement): number
5
- export function getScrollingElement (): ScrollingElement
6
- export function getViewportRect (): CenterCenterRect
7
- export function getDOMElementRect (element: Element): CenterCenterRect
8
- export function getSVGElementRect (element: SVGElement): CenterCenterRect
9
- export function getElementRect (element: Element | SVGElement): CenterCenterRect
10
- export function getDOMRects (container: Element, target: Element): CenterCenterRects
11
- export function getSVGRects (container: Element, target: SVGElement): CenterCenterRects
12
- export function getRects (container: Element, target: Element | SVGElement): CenterCenterRects
13
- export function calculateLeft (rects: CenterCenterRects): number
14
- export function calculateTop (rects: CenterCenterRects): number
15
-
16
- /**
17
- * The document `scrollingElement`
18
- */
19
- export interface ScrollingElement {
20
- scrollLeft: number
21
- scrollTop: number
22
- }
23
-
24
- /**
25
- * A Center Center `Rect`
26
- */
27
- export interface CenterCenterRect {
28
- left: number
29
- top: number
30
- width: number
31
- height: number
32
- right: number
33
- bottom: number
34
- }
35
-
36
- /**
37
- * Center Center `Rects`
38
- */
39
- export interface CenterCenterRects {
40
- viewport: CenterCenterRect
41
- container: CenterCenterRect
42
- target: CenterCenterRect
43
- }
File without changes