@danielgindi/dom-utils 1.0.10 → 2.0.0
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/README.md +13 -7
- package/dist/lib.cjs.js +318 -434
- package/dist/lib.cjs.js.map +1 -1
- package/dist/lib.cjs.min.js +2 -2
- package/dist/lib.cjs.min.js.map +1 -1
- package/dist/lib.es6.js +977 -1093
- package/dist/lib.es6.js.map +1 -1
- package/dist/lib.es6.min.js +2 -2
- package/dist/lib.es6.min.js.map +1 -1
- package/dist/lib.umd.js +318 -434
- package/dist/lib.umd.js.map +1 -1
- package/dist/lib.umd.min.js +2 -2
- package/dist/lib.umd.min.js.map +1 -1
- package/dist/types/Css.d.ts +130 -0
- package/dist/types/Dom.d.ts +33 -0
- package/dist/types/DomCompat.d.ts +55 -0
- package/dist/types/DomEventsSink.d.ts +20 -0
- package/dist/types/ScrollHelper.d.ts +39 -0
- package/dist/types/Touches.d.ts +14 -0
- package/dist/types/index.d.ts +7 -0
- package/eslint.config.mjs +47 -111
- package/lib/{Css.js → Css.ts} +448 -434
- package/lib/{Dom.js → Dom.ts} +132 -132
- package/lib/{DomCompat.js → DomCompat.ts} +276 -276
- package/lib/{DomEventsSink.js → DomEventsSink.ts} +70 -68
- package/lib/{ScrollHelper.js → ScrollHelper.ts} +158 -158
- package/lib/{Touches.js → Touches.ts} +84 -84
- package/package.json +28 -18
- package/tsconfig.json +17 -0
- package/tsconfig.scripts.json +10 -0
- package/tsconfig.tests.json +10 -0
- /package/lib/{index.js → index.ts} +0 -0
package/lib/{Css.js → Css.ts}
RENAMED
|
@@ -1,434 +1,448 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {Element} el
|
|
3
|
-
* @param {string[]} props
|
|
4
|
-
* @returns {Object<string, string>}
|
|
5
|
-
*/
|
|
6
|
-
const getCssProps = function (el, props) {
|
|
7
|
-
const style = getComputedStyle(el)
|
|
8
|
-
let res = {};
|
|
9
|
-
for (let prop of props) {
|
|
10
|
-
let val = style[prop];
|
|
11
|
-
|
|
12
|
-
if ((prop === 'width' || prop === 'height') && val === 'auto' && el instanceof HTMLElement) {
|
|
13
|
-
val = el['
|
|
14
|
-
|
|
15
|
-
if (style.boxSizing !== 'border-box') {
|
|
16
|
-
if (prop === 'width') {
|
|
17
|
-
val -= parseFloat(style.paddingLeft || 0);
|
|
18
|
-
val -= parseFloat(style.paddingRight || 0);
|
|
19
|
-
val -= parseFloat(style.borderLeftWidth || 0);
|
|
20
|
-
val -= parseFloat(style.borderRightWidth || 0);
|
|
21
|
-
} else {
|
|
22
|
-
val -= parseFloat(style.paddingTop || 0);
|
|
23
|
-
val -= parseFloat(style.paddingBottom || 0);
|
|
24
|
-
val -= parseFloat(style.borderTopWidth || 0);
|
|
25
|
-
val -= parseFloat(style.borderBottomWidth || 0);
|
|
26
|
-
}
|
|
27
|
-
if (val < 0)
|
|
28
|
-
val = 0;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
val
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
res[prop] = val;
|
|
35
|
-
}
|
|
36
|
-
return res;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* @param {ElementCSSInlineStyle} el
|
|
41
|
-
* @param {Object<string, string>} props
|
|
42
|
-
*/
|
|
43
|
-
const setCssProps = function (el, props) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
/**boolean*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
body[`
|
|
78
|
-
doc[`
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
let
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
*
|
|
193
|
-
* @
|
|
194
|
-
* @param {
|
|
195
|
-
* @param {boolean} [
|
|
196
|
-
* @
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
*
|
|
206
|
-
* @
|
|
207
|
-
* @
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
*
|
|
219
|
-
* @
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
*
|
|
225
|
-
* @
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
*
|
|
237
|
-
* @
|
|
238
|
-
* @param {
|
|
239
|
-
* @
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
*
|
|
248
|
-
* @
|
|
249
|
-
* @param {
|
|
250
|
-
* @param {
|
|
251
|
-
* @param {boolean} [
|
|
252
|
-
* @param {boolean} [
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
*
|
|
260
|
-
* @
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
let
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
duration
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @param {Element} el
|
|
3
|
+
* @param {string[]} props
|
|
4
|
+
* @returns {Object<string, string>}
|
|
5
|
+
*/
|
|
6
|
+
const getCssProps = function (el: Element, props: string[]): Record<string, string> {
|
|
7
|
+
const style = getComputedStyle(el) as CSSStyleDeclaration & Record<string, string>;
|
|
8
|
+
let res: Record<string, string> = {};
|
|
9
|
+
for (let prop of props) {
|
|
10
|
+
let val: string | number = style[prop];
|
|
11
|
+
|
|
12
|
+
if ((prop === 'width' || prop === 'height') && val === 'auto' && el instanceof HTMLElement) {
|
|
13
|
+
val = el[prop === 'width' ? 'offsetWidth' : 'offsetHeight'];
|
|
14
|
+
|
|
15
|
+
if (style.boxSizing !== 'border-box') {
|
|
16
|
+
if (prop === 'width') {
|
|
17
|
+
val -= parseFloat(style.paddingLeft || '0');
|
|
18
|
+
val -= parseFloat(style.paddingRight || '0');
|
|
19
|
+
val -= parseFloat(style.borderLeftWidth || '0');
|
|
20
|
+
val -= parseFloat(style.borderRightWidth || '0');
|
|
21
|
+
} else {
|
|
22
|
+
val -= parseFloat(style.paddingTop || '0');
|
|
23
|
+
val -= parseFloat(style.paddingBottom || '0');
|
|
24
|
+
val -= parseFloat(style.borderTopWidth || '0');
|
|
25
|
+
val -= parseFloat(style.borderBottomWidth || '0');
|
|
26
|
+
}
|
|
27
|
+
if (val < 0)
|
|
28
|
+
val = 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
val = val + 'px';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
res[prop] = val as string;
|
|
35
|
+
}
|
|
36
|
+
return res;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {ElementCSSInlineStyle} el
|
|
41
|
+
* @param {Object<string, string>} props
|
|
42
|
+
*/
|
|
43
|
+
const setCssProps = function (el: ElementCSSInlineStyle, props: Record<string, unknown>): void {
|
|
44
|
+
const style = el.style as CSSStyleDeclaration & Record<string, string>;
|
|
45
|
+
for (let [key, value] of Object.entries(props))
|
|
46
|
+
style[key] = (value === undefined || value === null) ? '' : String(value);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const generateGetElementSizeFn = function (pseudo: boolean, dim: 'width' | 'height', dimCased: 'Width' | 'Height', startDim: string, endDim: string) {
|
|
50
|
+
// The exported wrappers below provide precise signatures for these dynamic arguments.
|
|
51
|
+
return (...args: any[]) => {
|
|
52
|
+
let /**Element|ElementCSSInlineStyle*/el = args[0],
|
|
53
|
+
/**string*/pseudoSelector,
|
|
54
|
+
/**boolean*/paddings,
|
|
55
|
+
/**boolean*/borders,
|
|
56
|
+
/**boolean*/margins;
|
|
57
|
+
|
|
58
|
+
if (pseudo) {
|
|
59
|
+
pseudoSelector = args[1];
|
|
60
|
+
paddings = !!args[2];
|
|
61
|
+
borders = !!args[3];
|
|
62
|
+
margins = !!args[4];
|
|
63
|
+
} else {
|
|
64
|
+
paddings = !!args[1];
|
|
65
|
+
borders = !!args[2];
|
|
66
|
+
margins = !!args[3];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if ((/**@type Window*/el) === window) {
|
|
70
|
+
return (/**@type Window*/el).document.documentElement[`client${dimCased}`];
|
|
71
|
+
}
|
|
72
|
+
else if (el.nodeType === 9) { // document
|
|
73
|
+
const doc = (/**@type Document*/el).documentElement;
|
|
74
|
+
const body = (/**@type Document*/el).body;
|
|
75
|
+
|
|
76
|
+
return Math.max(
|
|
77
|
+
body[`scroll${dimCased}`],
|
|
78
|
+
doc[`scroll${dimCased}`],
|
|
79
|
+
body[`offset${dimCased}`],
|
|
80
|
+
doc[`offset${dimCased}`],
|
|
81
|
+
doc[`client${dimCased}`],
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
let value;
|
|
86
|
+
let style: (CSSStyleDeclaration & Record<string, string>) | undefined;
|
|
87
|
+
let includesPadding = false, includesBorders = false;
|
|
88
|
+
|
|
89
|
+
if (!pseudo && ('getBoundingClientRect' in el)) {
|
|
90
|
+
value = el.getBoundingClientRect()[dim];
|
|
91
|
+
includesPadding = true;
|
|
92
|
+
includesBorders = true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (value === undefined || margins || includesPadding !== paddings || includesBorders !== borders) {
|
|
96
|
+
style = (pseudo ? getComputedStyle(el, pseudoSelector) : getComputedStyle(el)) as CSSStyleDeclaration & Record<string, string>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (value === undefined) {
|
|
100
|
+
let raw = style[dim];
|
|
101
|
+
if (raw === 'auto') { // computedStyle is no good here, probably an 'inline' element
|
|
102
|
+
value = el[`client${dimCased}`]; // take clientWidth/clientHeight (damn it, it's rounded)
|
|
103
|
+
includesPadding = true;
|
|
104
|
+
} else {
|
|
105
|
+
value = parseFloat(raw);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (style.boxSizing === 'border-box') {
|
|
109
|
+
includesPadding = true;
|
|
110
|
+
includesBorders = true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (paddings !== includesPadding) {
|
|
115
|
+
let totalPadding = parseFloat(style[`padding-${startDim}`] || '0') +
|
|
116
|
+
parseFloat(style[`padding-${endDim}`] || '0');
|
|
117
|
+
if (paddings) value += totalPadding;
|
|
118
|
+
else value -= totalPadding;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (borders !== includesBorders) {
|
|
122
|
+
let totalBorders = parseFloat(style[`border-${startDim}-width`] || '0') +
|
|
123
|
+
parseFloat(style[`border-${endDim}-width`] || '0');
|
|
124
|
+
if (borders) value += totalBorders;
|
|
125
|
+
else value -= totalBorders;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (value < 0)
|
|
129
|
+
value = 0;
|
|
130
|
+
|
|
131
|
+
if (margins) {
|
|
132
|
+
let totalMargins = parseFloat(style[`margin-${startDim}`] || '0') +
|
|
133
|
+
parseFloat(style[`margin-${endDim}`] || '0');
|
|
134
|
+
value += totalMargins;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const generateSetElementSizeFn = (dim: 'width' | 'height', dimCased: 'Width' | 'Height', startDim: string, endDim: string) => {
|
|
143
|
+
return (/**Element|ElementCSSInlineStyle*/el: any,
|
|
144
|
+
/**number*/value: number,
|
|
145
|
+
paddings = false, borders = false, margins = false) => {
|
|
146
|
+
|
|
147
|
+
if ((/**@type Window*/el) === window) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (el.nodeType === 9) { // document
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const style = getComputedStyle(el) as CSSStyleDeclaration & Record<string, string>;
|
|
156
|
+
|
|
157
|
+
let includesPaddingAndBorders = false;
|
|
158
|
+
|
|
159
|
+
if (style.boxSizing === 'border-box') {
|
|
160
|
+
includesPaddingAndBorders = true;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
paddings = !!paddings;
|
|
164
|
+
borders = !!borders;
|
|
165
|
+
margins = !!margins;
|
|
166
|
+
|
|
167
|
+
if (margins)
|
|
168
|
+
value -= (parseFloat(style[`margin-${startDim}`]) || 0) + (parseFloat(style[`margin-${endDim}`]) || 0);
|
|
169
|
+
|
|
170
|
+
if (paddings !== includesPaddingAndBorders) {
|
|
171
|
+
let totalPadding = parseFloat(style[`padding-${startDim}`] || '0') +
|
|
172
|
+
parseFloat(style[`padding-${endDim}`] || '0');
|
|
173
|
+
if (paddings) value -= totalPadding;
|
|
174
|
+
else value += totalPadding;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (borders !== includesPaddingAndBorders) {
|
|
178
|
+
let totalBorders = (parseFloat(style[`border-${startDim}-width`]) || 0) +
|
|
179
|
+
(parseFloat(style[`border-${endDim}-width`]) || 0);
|
|
180
|
+
if (borders) value -= totalBorders;
|
|
181
|
+
else value += totalBorders;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (value < 0)
|
|
185
|
+
value = 0;
|
|
186
|
+
|
|
187
|
+
el.style[dim] = value + 'px';
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Gets the width of an element, with fractions
|
|
193
|
+
* @function getElementWidth
|
|
194
|
+
* @param {Element} el
|
|
195
|
+
* @param {boolean} [paddings=false] - include paddings
|
|
196
|
+
* @param {boolean} [borders=false] - include borders
|
|
197
|
+
* @param {boolean} [margins=false] - include margins
|
|
198
|
+
* @returns {number}
|
|
199
|
+
*/
|
|
200
|
+
const getElementWidth = generateGetElementSizeFn(false, 'width', 'Width', 'left', 'right') as (
|
|
201
|
+
el: Element | Document | Window, paddings?: boolean, borders?: boolean, margins?: boolean
|
|
202
|
+
) => number;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Gets the width of an element, with fractions
|
|
206
|
+
* @function getElementHeight
|
|
207
|
+
* @param {Element} el
|
|
208
|
+
* @param {boolean} [paddings=false] - include paddings
|
|
209
|
+
* @param {boolean} [borders=false] - include borders
|
|
210
|
+
* @param {boolean} [margins=false] - include margins
|
|
211
|
+
* @returns {number}
|
|
212
|
+
*/
|
|
213
|
+
const getElementHeight = generateGetElementSizeFn(false, 'height', 'Height', 'top', 'bottom') as (
|
|
214
|
+
el: Element | Document | Window, paddings?: boolean, borders?: boolean, margins?: boolean
|
|
215
|
+
) => number;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Gets the width of an element, with fractions
|
|
219
|
+
* @function getPseudoElementWidth
|
|
220
|
+
* @param {Element} el
|
|
221
|
+
* @param {string} pseudo
|
|
222
|
+
* @param {boolean} [paddings=false] - include paddings
|
|
223
|
+
* @param {boolean} [borders=false] - include borders
|
|
224
|
+
* @param {boolean} [margins=false] - include margins
|
|
225
|
+
* @returns {number}
|
|
226
|
+
*/
|
|
227
|
+
const getPseudoElementWidth = generateGetElementSizeFn(true, 'width', 'Width', 'left', 'right') as (
|
|
228
|
+
el: Element, pseudo: string, paddings?: boolean, borders?: boolean, margins?: boolean
|
|
229
|
+
) => number;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Gets the width of an element, with fractions
|
|
233
|
+
* @function getPseudoElementHeight
|
|
234
|
+
* @param {Element} el
|
|
235
|
+
* @param {string} pseudo
|
|
236
|
+
* @param {boolean} [paddings=false] - include paddings
|
|
237
|
+
* @param {boolean} [borders=false] - include borders
|
|
238
|
+
* @param {boolean} [margins=false] - include margins
|
|
239
|
+
* @returns {number}
|
|
240
|
+
*/
|
|
241
|
+
const getPseudoElementHeight = generateGetElementSizeFn(true, 'height', 'Height', 'top', 'bottom') as (
|
|
242
|
+
el: Element, pseudo: string, paddings?: boolean, borders?: boolean, margins?: boolean
|
|
243
|
+
) => number;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Sets the width of an element
|
|
247
|
+
* @function setElementWidth
|
|
248
|
+
* @param {Element} el
|
|
249
|
+
* @param {number} value
|
|
250
|
+
* @param {boolean} [paddings=false] - include paddings
|
|
251
|
+
* @param {boolean} [borders=false] - include borders
|
|
252
|
+
* @param {boolean} [margins=false] - include margins
|
|
253
|
+
*/
|
|
254
|
+
const setElementWidth = generateSetElementSizeFn('width', 'Width', 'left', 'right') as (
|
|
255
|
+
el: ElementCSSInlineStyle, value: number, paddings?: boolean, borders?: boolean, margins?: boolean
|
|
256
|
+
) => void;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Sets the width of an element, with fractions
|
|
260
|
+
* @function setElementHeight
|
|
261
|
+
* @param {Element} el
|
|
262
|
+
* @param {number} value
|
|
263
|
+
* @param {boolean} [paddings=false] - include paddings
|
|
264
|
+
* @param {boolean} [borders=false] - include borders
|
|
265
|
+
* @param {boolean} [margins=false] - include margins
|
|
266
|
+
*/
|
|
267
|
+
const setElementHeight = generateSetElementSizeFn('height', 'Height', 'top', 'bottom') as (
|
|
268
|
+
el: ElementCSSInlineStyle, value: number, paddings?: boolean, borders?: boolean, margins?: boolean
|
|
269
|
+
) => void;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Find offset of an element relative to the document, considering scroll offsets
|
|
273
|
+
* @param {Element} el
|
|
274
|
+
* @returns {{top: number, left: number}}
|
|
275
|
+
*/
|
|
276
|
+
const getElementOffset = (el: Element): { top: number; left: number } => {
|
|
277
|
+
if (!el.getClientRects().length) {
|
|
278
|
+
return { top: 0, left: 0 };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
let rect = el.getBoundingClientRect();
|
|
282
|
+
let view = el.ownerDocument.defaultView;
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
top: rect.top + view.pageYOffset,
|
|
286
|
+
left: rect.left + view.pageXOffset,
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Calculates the anchored position relative to this element. <br />
|
|
292
|
+
* The return value will include an `left`/`top` position on left/top axis, <br />
|
|
293
|
+
* an `right`/`bottom` position which is the exact opposite, <br />
|
|
294
|
+
* and the `xSpec`/`ySpec` after normalizing rtl based values (`'start'`/`'end'`).
|
|
295
|
+
* @param {Element} el? - the target element to base the calculations on. If `size` and `rtl` are specified - then this argument is optional.
|
|
296
|
+
* @param {'left'|'center'|'right'|'start'|'end'|string|number} xSpec - horizontal anchor specification (could be either `'left'|'center'|'right'|'start'|'end'` or a percentage `'50%'` or a fixed decimal `Number`)
|
|
297
|
+
* @param {'top'|'center'|'bottom'|string|number} ySpec - vertical anchor specification (could be either `'top'|'center'|'bottom'` or a percentage `'50%'` or a fixed decimal `Number`)
|
|
298
|
+
* @param {{width: number, height: number}?} size - can be specified if already known, to avoid extra dom calculations. This is the outer size of the element, including padding and borders.
|
|
299
|
+
* @param {boolean?} [rtl] - if not specified and values like `'start'`/`'end'` are use, then `rtl` is auto detected
|
|
300
|
+
* @returns {{left: number, top: number, right: number, bottom: number, xSpec: string|number, ySpec: string|number}}
|
|
301
|
+
*/
|
|
302
|
+
const anchoredPosition = function (el: Element | undefined, xSpec: string | number, ySpec: string | number, size?: { width: number; height: number }, rtl?: boolean | null): { left: number; top: number; right: number; bottom: number; xSpec: string | number; ySpec: string | number } {
|
|
303
|
+
if (!size) {
|
|
304
|
+
size = {
|
|
305
|
+
width: getElementWidth(el, true, true),
|
|
306
|
+
height: getElementWidth(el, true, true),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (xSpec === 'start' || xSpec === 'end') {
|
|
311
|
+
if ((/**@type boolean|null*/rtl) === null || rtl === undefined) {
|
|
312
|
+
rtl = getComputedStyle(el).direction === 'rtl';
|
|
313
|
+
}
|
|
314
|
+
xSpec = xSpec === 'start' ? (rtl ? 'right' : 'left') : (rtl ? 'left' : 'right');
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let x, y;
|
|
318
|
+
let xInverted, yInverted;
|
|
319
|
+
|
|
320
|
+
if (xSpec === 'right') {
|
|
321
|
+
x = size.width;
|
|
322
|
+
xInverted = 0;
|
|
323
|
+
}
|
|
324
|
+
else if (xSpec === 'center') {
|
|
325
|
+
x = size.width / 2;
|
|
326
|
+
xInverted = x;
|
|
327
|
+
}
|
|
328
|
+
else if (xSpec === 'left') {
|
|
329
|
+
x = 0;
|
|
330
|
+
xInverted = size.width;
|
|
331
|
+
}
|
|
332
|
+
else if (xSpec && (typeof xSpec === 'string') && xSpec.charAt(xSpec.length - 1) === '%') {
|
|
333
|
+
let percent = ((parseFloat(xSpec) || 0) / 100);
|
|
334
|
+
x = size.width * percent;
|
|
335
|
+
xInverted = size.width * -percent;
|
|
336
|
+
|
|
337
|
+
if ((/**@type boolean|null*/rtl) === null || rtl === undefined)
|
|
338
|
+
rtl = getComputedStyle(el).direction === 'rtl';
|
|
339
|
+
|
|
340
|
+
if (rtl) {
|
|
341
|
+
x = size.width - x;
|
|
342
|
+
xInverted = size.width - x;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
x = parseFloat(String(xSpec)) || 0;
|
|
347
|
+
xInverted = -x;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (ySpec === 'bottom') {
|
|
351
|
+
y = size.height;
|
|
352
|
+
yInverted = 0;
|
|
353
|
+
}
|
|
354
|
+
else if (ySpec === 'center') {
|
|
355
|
+
y = size.height / 2;
|
|
356
|
+
yInverted = y;
|
|
357
|
+
}
|
|
358
|
+
else if (ySpec === 'top') {
|
|
359
|
+
y = 0;
|
|
360
|
+
yInverted = size.height;
|
|
361
|
+
}
|
|
362
|
+
else if (ySpec && (typeof ySpec === 'string') && ySpec.charAt(ySpec.length - 1) === '%') {
|
|
363
|
+
let percent = ((parseFloat(ySpec) || 0) / 100);
|
|
364
|
+
y = size.height * percent;
|
|
365
|
+
yInverted = size.height * -percent;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
y = parseFloat(String(ySpec)) || 0;
|
|
369
|
+
yInverted = -y;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return {
|
|
373
|
+
left: x,
|
|
374
|
+
top: y,
|
|
375
|
+
right: xInverted,
|
|
376
|
+
bottom: yInverted,
|
|
377
|
+
xSpec: xSpec,
|
|
378
|
+
ySpec: ySpec,
|
|
379
|
+
};
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const timeToMs = (value?: string): number | undefined => {
|
|
383
|
+
if (!value) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
let isMs = value.endsWith('ms');
|
|
388
|
+
return parseFloat(value) * (isMs ? 1 : 1000);
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const isTimeString = (value: string): boolean => {
|
|
392
|
+
return /^-?(0?\.)?\d+m?s$/.test(value);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
*
|
|
397
|
+
* @param {string} transition
|
|
398
|
+
* @return {{duration: number, delay: number, name: string, timingFunction: string?}}
|
|
399
|
+
*/
|
|
400
|
+
const parseSingleTransition = (transition: string): { duration?: number; delay?: number; name: string; timingFunction?: string } => {
|
|
401
|
+
let parts = transition.split(/\s+/),
|
|
402
|
+
name = parts[0],
|
|
403
|
+
duration = parts[1],
|
|
404
|
+
timingFunctionOrDelay = parts[2],
|
|
405
|
+
delay = parts[3];
|
|
406
|
+
|
|
407
|
+
if (isTimeString(timingFunctionOrDelay)) {
|
|
408
|
+
return {
|
|
409
|
+
delay: timeToMs(timingFunctionOrDelay),
|
|
410
|
+
duration: timeToMs(duration),
|
|
411
|
+
name: name,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return {
|
|
416
|
+
delay: timeToMs(delay),
|
|
417
|
+
duration: timeToMs(duration),
|
|
418
|
+
name: name,
|
|
419
|
+
timingFunction: timingFunctionOrDelay,
|
|
420
|
+
};
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
*
|
|
425
|
+
* @param {string} transition
|
|
426
|
+
* @return {{duration: number, delay: number, name: string, timingFunction: string?}[]}
|
|
427
|
+
*/
|
|
428
|
+
const parseTransition = (transition: string): { duration?: number; delay?: number; name: string; timingFunction?: string }[] => {
|
|
429
|
+
if (!transition) return [];
|
|
430
|
+
return (transition.match(/([^,()]|\([^)]*\))+/g) ?? []).map(value => value.trim()).filter(value => value.length > 0).map(parseSingleTransition);
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
export {
|
|
434
|
+
getCssProps,
|
|
435
|
+
setCssProps,
|
|
436
|
+
getElementWidth,
|
|
437
|
+
getElementHeight,
|
|
438
|
+
setElementWidth,
|
|
439
|
+
setElementHeight,
|
|
440
|
+
getPseudoElementWidth,
|
|
441
|
+
getPseudoElementHeight,
|
|
442
|
+
getElementOffset,
|
|
443
|
+
anchoredPosition,
|
|
444
|
+
parseTransition,
|
|
445
|
+
parseSingleTransition,
|
|
446
|
+
isTimeString,
|
|
447
|
+
timeToMs,
|
|
448
|
+
};
|