center-center 0.0.39 → 0.0.40
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 +2 -2
- package/index.mjs +178 -40
- package/package.json +2 -2
package/docs/index.html
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
import debug from 'debug'
|
|
16
16
|
|
|
17
17
|
import {
|
|
18
|
-
|
|
18
|
+
createRects,
|
|
19
19
|
calculateLeft,
|
|
20
20
|
calculateTop
|
|
21
21
|
} from 'center-center'
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
log('render')
|
|
74
74
|
|
|
75
75
|
window.requestAnimationFrame(() => {
|
|
76
|
-
const rects =
|
|
76
|
+
const rects = createRects(container, target)
|
|
77
77
|
const left = calculateLeft(rects)
|
|
78
78
|
const top = calculateTop(rects)
|
|
79
79
|
|
package/index.mjs
CHANGED
|
@@ -113,11 +113,51 @@ export function getScrollingElement () {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
/**
|
|
116
|
-
*
|
|
116
|
+
* Destructures the `left` field from a `Rect`
|
|
117
|
+
*
|
|
118
|
+
* @param {CenterCenterRect}
|
|
119
|
+
* @returns {number}
|
|
120
|
+
*/
|
|
121
|
+
export function getRectLeft ({ left }) {
|
|
122
|
+
return left
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Destructures the `top` field from a `Rect`
|
|
127
|
+
*
|
|
128
|
+
* @param {CenterCenterRect}
|
|
129
|
+
* @returns {number}
|
|
130
|
+
*/
|
|
131
|
+
export function getRectTop ({ top }) {
|
|
132
|
+
return top
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Destructures the `width` field from a `Rect`
|
|
137
|
+
*
|
|
138
|
+
* @param {CenterCenterRect}
|
|
139
|
+
* @returns {number}
|
|
140
|
+
*/
|
|
141
|
+
export function getRectWidth ({ width }) {
|
|
142
|
+
return width
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Destructures the `height` field from a `Rect`
|
|
147
|
+
*
|
|
148
|
+
* @param {CenterCenterRect}
|
|
149
|
+
* @returns {number}
|
|
150
|
+
*/
|
|
151
|
+
export function getRectHeight ({ height }) {
|
|
152
|
+
return height
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Creates a `Rect` for the viewport
|
|
117
157
|
*
|
|
118
158
|
* @returns {CenterCenterRect}
|
|
119
159
|
*/
|
|
120
|
-
export function
|
|
160
|
+
export function createViewportRect () {
|
|
121
161
|
const {
|
|
122
162
|
innerWidth: width,
|
|
123
163
|
innerHeight: height
|
|
@@ -139,12 +179,12 @@ export function getViewportRect () {
|
|
|
139
179
|
}
|
|
140
180
|
|
|
141
181
|
/**
|
|
142
|
-
*
|
|
182
|
+
* Creates a `Rect` for an SVG element
|
|
143
183
|
*
|
|
144
184
|
* @param {Element} element
|
|
145
185
|
* @returns {CenterCenterRect}
|
|
146
186
|
*/
|
|
147
|
-
export function
|
|
187
|
+
export function createSVGElementRect (element) {
|
|
148
188
|
const {
|
|
149
189
|
x: left,
|
|
150
190
|
y: top,
|
|
@@ -163,12 +203,12 @@ export function getSVGElementRect (element) {
|
|
|
163
203
|
}
|
|
164
204
|
|
|
165
205
|
/**
|
|
166
|
-
*
|
|
206
|
+
* Creates a `Rect` for a DOM element
|
|
167
207
|
*
|
|
168
208
|
* @param {Element} element
|
|
169
209
|
* @returns {CenterCenterRect}
|
|
170
210
|
*/
|
|
171
|
-
export function
|
|
211
|
+
export function createDOMElementRect (element) {
|
|
172
212
|
const {
|
|
173
213
|
scrollLeft,
|
|
174
214
|
scrollTop
|
|
@@ -195,118 +235,216 @@ export function getDOMElementRect (element) {
|
|
|
195
235
|
}
|
|
196
236
|
|
|
197
237
|
/**
|
|
198
|
-
*
|
|
238
|
+
* Creates a `Rect` for a DOM element
|
|
199
239
|
*
|
|
200
240
|
* @param {Element} element
|
|
201
241
|
* @returns {CenterCenterRect}
|
|
202
242
|
*/
|
|
203
|
-
export function
|
|
243
|
+
export function createElementRect (element) {
|
|
204
244
|
return (
|
|
205
|
-
|
|
245
|
+
createDOMElementRect(element)
|
|
206
246
|
)
|
|
207
247
|
}
|
|
208
248
|
|
|
209
249
|
/**
|
|
210
|
-
*
|
|
250
|
+
* Creates `Rects` for the viewport as well as the DOM container and DOM target elements
|
|
211
251
|
*
|
|
212
252
|
* @param {Element} container
|
|
213
253
|
* @param {Element} target
|
|
214
254
|
* @returns {CenterCenterRects}
|
|
215
255
|
*/
|
|
216
|
-
export function
|
|
256
|
+
export function createSVGRects (container, target) {
|
|
217
257
|
return {
|
|
218
|
-
viewport:
|
|
219
|
-
container:
|
|
220
|
-
target:
|
|
258
|
+
viewport: createViewportRect(),
|
|
259
|
+
container: createDOMElementRect(container),
|
|
260
|
+
target: createSVGElementRect(target)
|
|
221
261
|
}
|
|
222
262
|
}
|
|
223
263
|
|
|
224
264
|
/**
|
|
225
|
-
*
|
|
265
|
+
* Creates `Rects` for the viewport as well as the DOM container and DOM target elements
|
|
226
266
|
*
|
|
227
267
|
* @param {Element} container
|
|
228
268
|
* @param {Element} target
|
|
229
269
|
* @returns {CenterCenterRects}
|
|
230
270
|
*/
|
|
231
|
-
export function
|
|
271
|
+
export function createDOMRects (container, target) {
|
|
232
272
|
return {
|
|
233
|
-
viewport:
|
|
234
|
-
container:
|
|
235
|
-
target:
|
|
273
|
+
viewport: createViewportRect(),
|
|
274
|
+
container: createDOMElementRect(container),
|
|
275
|
+
target: createDOMElementRect(target)
|
|
236
276
|
}
|
|
237
277
|
}
|
|
238
278
|
|
|
239
279
|
/**
|
|
240
|
-
*
|
|
280
|
+
* Creates `Rects` for the viewport as well as the container and target elements
|
|
241
281
|
*
|
|
242
282
|
* @param {Element} container
|
|
243
283
|
* @param {Element} target
|
|
244
284
|
* @returns {CenterCenterRects}
|
|
245
285
|
*/
|
|
246
|
-
export function
|
|
286
|
+
export function createRects (container, target) {
|
|
247
287
|
return (
|
|
248
|
-
|
|
288
|
+
createDOMRects(container, target)
|
|
249
289
|
)
|
|
250
290
|
}
|
|
251
291
|
|
|
252
292
|
/**
|
|
253
|
-
* Calculates the
|
|
293
|
+
* Calculates the boundary `x` coordinate of the target in the container
|
|
294
|
+
*
|
|
295
|
+
* @param {CenterCenterRects}
|
|
296
|
+
* @returns {number}
|
|
297
|
+
*/
|
|
298
|
+
export function calculateBoundaryX (rects) {
|
|
299
|
+
const containerW = getRectWidth(getContainerRect(rects))
|
|
300
|
+
const targetW = getRectWidth(getTargetRect(rects))
|
|
301
|
+
|
|
302
|
+
return (containerW - targetW)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Calculates the boundary `y` coordinate of the target in the container
|
|
307
|
+
*
|
|
308
|
+
* @param {CenterCenterRects}
|
|
309
|
+
* @returns {number}
|
|
310
|
+
*/
|
|
311
|
+
export function calculateBoundaryY (rects) {
|
|
312
|
+
const containerH = getRectHeight(getContainerRect(rects))
|
|
313
|
+
const targetH = getRectHeight(getTargetRect(rects))
|
|
314
|
+
|
|
315
|
+
return (containerH - targetH)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Calculates the `x` coordinate of the container
|
|
254
320
|
*
|
|
255
321
|
* @param {CenterCenterRects}
|
|
256
322
|
* @returns {number}
|
|
257
323
|
*/
|
|
258
|
-
export function
|
|
324
|
+
export function calculateContainerX ({
|
|
259
325
|
viewport: {
|
|
260
326
|
left: viewportL,
|
|
261
327
|
right: viewportR
|
|
262
328
|
},
|
|
263
329
|
container: {
|
|
264
330
|
left: containerL,
|
|
265
|
-
width: containerW,
|
|
266
331
|
right: containerR
|
|
267
|
-
},
|
|
268
|
-
target: {
|
|
269
|
-
width: targetW
|
|
270
332
|
}
|
|
271
333
|
}) {
|
|
272
334
|
const l = Math.max(viewportL, containerL)
|
|
273
335
|
const r = Math.min(viewportR, containerR)
|
|
274
336
|
const w = (r - l)
|
|
275
337
|
const x = (l - containerL)
|
|
276
|
-
const boundary = (containerW - targetW)
|
|
277
338
|
|
|
278
|
-
return (
|
|
279
|
-
Math.max(0, Math.min(boundary, x + ((w / 2) - (targetW / 2))))
|
|
280
|
-
)
|
|
339
|
+
return (x + (w / 2))
|
|
281
340
|
}
|
|
282
341
|
|
|
283
342
|
/**
|
|
284
|
-
* Calculates the
|
|
343
|
+
* Calculates the `y` coordinate of the container
|
|
285
344
|
*
|
|
286
345
|
* @param {CenterCenterRects}
|
|
287
346
|
* @returns {number}
|
|
288
347
|
*/
|
|
289
|
-
export function
|
|
348
|
+
export function calculateContainerY ({
|
|
290
349
|
viewport: {
|
|
291
350
|
top: viewportT,
|
|
292
351
|
bottom: viewportB
|
|
293
352
|
},
|
|
294
353
|
container: {
|
|
295
354
|
top: containerT,
|
|
296
|
-
height: containerH,
|
|
297
355
|
bottom: containerB
|
|
298
|
-
},
|
|
299
|
-
target: {
|
|
300
|
-
height: targetH
|
|
301
356
|
}
|
|
302
357
|
}) {
|
|
303
358
|
const t = Math.max(viewportT, containerT)
|
|
304
359
|
const b = Math.min(viewportB, containerB)
|
|
305
360
|
const h = (b - t)
|
|
306
361
|
const y = (t - containerT)
|
|
307
|
-
|
|
362
|
+
|
|
363
|
+
return (y + (h / 2))
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Calculates the `x` coordinate of the target
|
|
368
|
+
*
|
|
369
|
+
* @param {CenterCenterRects}
|
|
370
|
+
* @returns {number}
|
|
371
|
+
*/
|
|
372
|
+
export function calculateTargetX (rects) {
|
|
373
|
+
const x = calculateContainerX(rects)
|
|
374
|
+
const w = getRectWidth(getTargetRect(rects))
|
|
375
|
+
|
|
376
|
+
return (x - (w / 2))
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Calculates the `y` coordinate of the target
|
|
381
|
+
*
|
|
382
|
+
* @param {CenterCenterRects}
|
|
383
|
+
* @returns {number}
|
|
384
|
+
*/
|
|
385
|
+
export function calculateTargetY (rects) {
|
|
386
|
+
const y = calculateContainerY(rects)
|
|
387
|
+
const h = getRectHeight(getTargetRect(rects))
|
|
388
|
+
|
|
389
|
+
return (y - (h / 2))
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Destructures the viewport `Rect`
|
|
394
|
+
*
|
|
395
|
+
* @param {CenterCenterRects}
|
|
396
|
+
* @returns {CenterCenterRect}
|
|
397
|
+
*/
|
|
398
|
+
export function getViewportRect ({ viewport }) {
|
|
399
|
+
return viewport
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Destructures the container `Rect`
|
|
404
|
+
*
|
|
405
|
+
* @param {CenterCenterRects}
|
|
406
|
+
* @returns {CenterCenterRect}
|
|
407
|
+
*/
|
|
408
|
+
export function getContainerRect ({ container }) {
|
|
409
|
+
return container
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Destructures the target `Rect`
|
|
414
|
+
*
|
|
415
|
+
* @param {CenterCenterRects}
|
|
416
|
+
* @returns {CenterCenterRect}
|
|
417
|
+
*/
|
|
418
|
+
export function getTargetRect ({ target }) {
|
|
419
|
+
return target
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Calculates the target `left` position
|
|
424
|
+
*
|
|
425
|
+
* @param {CenterCenterRects}
|
|
426
|
+
* @returns {number}
|
|
427
|
+
*/
|
|
428
|
+
export function calculateLeft (rects) {
|
|
429
|
+
const boundary = calculateBoundaryX(rects)
|
|
430
|
+
const x = calculateTargetX(rects)
|
|
431
|
+
|
|
432
|
+
return (
|
|
433
|
+
Math.max(0, Math.min(boundary, x))
|
|
434
|
+
)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Calculates the target `top` position
|
|
439
|
+
*
|
|
440
|
+
* @param {CenterCenterRects}
|
|
441
|
+
* @returns {number}
|
|
442
|
+
*/
|
|
443
|
+
export function calculateTop (rects) {
|
|
444
|
+
const boundary = calculateBoundaryY(rects)
|
|
445
|
+
const y = calculateTargetY(rects)
|
|
308
446
|
|
|
309
447
|
return (
|
|
310
|
-
Math.max(0, Math.min(boundary, y
|
|
448
|
+
Math.max(0, Math.min(boundary, y))
|
|
311
449
|
)
|
|
312
450
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "center-center",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.40",
|
|
4
4
|
"description": "Position an element at the center center of a container",
|
|
5
5
|
"main": "./index.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"mocha": "^10.3.0",
|
|
50
50
|
"nodemon": "^3.1.0",
|
|
51
51
|
"npm-run-all": "^4.1.5",
|
|
52
|
-
"typescript": "^5.
|
|
52
|
+
"typescript": "^5.4.2"
|
|
53
53
|
},
|
|
54
54
|
"imports": {
|
|
55
55
|
"#center-center": "./index.mjs"
|