auto-skeleton-react 1.0.1 → 1.0.2
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/dist/auto-skeleton-react.js +66 -66
- package/package.json +1 -1
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { jsx as p, jsxs as
|
|
2
|
-
import { useMemo as fe, useRef as ye, useState as Se, useLayoutEffect as
|
|
3
|
-
function
|
|
1
|
+
import { jsx as p, jsxs as ge } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo as fe, useRef as ye, useState as Se, useLayoutEffect as oe } from "react";
|
|
3
|
+
function de(e) {
|
|
4
4
|
var r;
|
|
5
|
-
return e ? (r = e.hasAttribute) != null && r.call(e, "data-no-skeleton") ? !0 :
|
|
5
|
+
return e ? (r = e.hasAttribute) != null && r.call(e, "data-no-skeleton") ? !0 : de(e.parentElement) : !1;
|
|
6
6
|
}
|
|
7
|
-
function
|
|
7
|
+
function ce(e, r, i = 0) {
|
|
8
8
|
if (i > r.maxDepth) return null;
|
|
9
|
-
if (
|
|
9
|
+
if (de(e)) {
|
|
10
10
|
const c = e.getBoundingClientRect(), l = window.getComputedStyle(e), o = {};
|
|
11
|
-
for (const
|
|
12
|
-
o[
|
|
11
|
+
for (const u of e.attributes)
|
|
12
|
+
o[u.name] = u.value;
|
|
13
13
|
return {
|
|
14
14
|
rect: c,
|
|
15
15
|
style: l,
|
|
@@ -27,10 +27,10 @@ function he(e, r, i = 0) {
|
|
|
27
27
|
let v = t;
|
|
28
28
|
if (R && n) {
|
|
29
29
|
let c = 0, l = 0;
|
|
30
|
-
const o = e.parentElement,
|
|
30
|
+
const o = e.parentElement, u = o ? window.getComputedStyle(o) : null;
|
|
31
31
|
let x = 0;
|
|
32
32
|
if (o) {
|
|
33
|
-
const d = o.getBoundingClientRect(), H = parseFloat((
|
|
33
|
+
const d = o.getBoundingClientRect(), H = parseFloat((u == null ? void 0 : u.paddingLeft) || "0"), L = parseFloat((u == null ? void 0 : u.paddingRight) || "0");
|
|
34
34
|
x = d.width - H - L;
|
|
35
35
|
}
|
|
36
36
|
const b = n.style.width, C = n.style.height;
|
|
@@ -78,7 +78,7 @@ function he(e, r, i = 0) {
|
|
|
78
78
|
const A = [];
|
|
79
79
|
for (const c of e.children)
|
|
80
80
|
if (c instanceof HTMLElement) {
|
|
81
|
-
const l =
|
|
81
|
+
const l = ce(c, r, i + 1);
|
|
82
82
|
l && A.push(l);
|
|
83
83
|
}
|
|
84
84
|
return {
|
|
@@ -90,11 +90,11 @@ function he(e, r, i = 0) {
|
|
|
90
90
|
children: A
|
|
91
91
|
};
|
|
92
92
|
}
|
|
93
|
-
const
|
|
93
|
+
const he = ["TABLE", "THEAD", "TBODY", "TFOOT", "TR"], pe = ["TH", "TD"];
|
|
94
94
|
function me(e) {
|
|
95
95
|
var S, m;
|
|
96
96
|
const { tagName: r, textContent: i, children: t, style: s, rect: y } = e;
|
|
97
|
-
if (y.width * y.height < 100 ||
|
|
97
|
+
if (y.width * y.height < 100 || he.includes(r) || pe.includes(r)) return !1;
|
|
98
98
|
if (r.match(/^(IMG|BUTTON|INPUT|TEXTAREA|SELECT|SVG)$/) || i != null && i.trim() && t.length === 0) return !0;
|
|
99
99
|
if (t.length === 1 && (i != null && i.trim())) {
|
|
100
100
|
const f = t[0];
|
|
@@ -121,7 +121,7 @@ function B(e, r) {
|
|
|
121
121
|
const l = m;
|
|
122
122
|
let o = 1;
|
|
123
123
|
if (l === "text") {
|
|
124
|
-
const
|
|
124
|
+
const u = parseFloat(t.lineHeight), x = parseFloat(t.fontSize), b = u || x * 1.2;
|
|
125
125
|
b > 0 && i.height > b && (o = Math.ceil(i.height / b));
|
|
126
126
|
}
|
|
127
127
|
return {
|
|
@@ -152,7 +152,7 @@ function B(e, r) {
|
|
|
152
152
|
}
|
|
153
153
|
};
|
|
154
154
|
}
|
|
155
|
-
if (
|
|
155
|
+
if (he.includes(s)) {
|
|
156
156
|
const l = S.map((o) => B(o, r)).filter((o) => o.type !== "skip");
|
|
157
157
|
return {
|
|
158
158
|
type: s.toLowerCase(),
|
|
@@ -177,7 +177,7 @@ function B(e, r) {
|
|
|
177
177
|
}
|
|
178
178
|
};
|
|
179
179
|
}
|
|
180
|
-
if (
|
|
180
|
+
if (pe.includes(s)) {
|
|
181
181
|
const l = S.map((o) => B(o, r)).filter((o) => o.type !== "skip");
|
|
182
182
|
return {
|
|
183
183
|
type: s.toLowerCase(),
|
|
@@ -250,8 +250,8 @@ function B(e, r) {
|
|
|
250
250
|
).reduce((l, o) => o[1] > l[1] ? o : l), k = v[1] > 30 ? v[0] : "text";
|
|
251
251
|
let A = 1;
|
|
252
252
|
if (k === "text") {
|
|
253
|
-
const l = parseFloat(t.lineHeight), o = parseFloat(t.fontSize),
|
|
254
|
-
|
|
253
|
+
const l = parseFloat(t.lineHeight), o = parseFloat(t.fontSize), u = l || o * 1.2;
|
|
254
|
+
u > 0 && i.height > u && (A = Math.ceil(i.height / u));
|
|
255
255
|
}
|
|
256
256
|
return {
|
|
257
257
|
type: k,
|
|
@@ -290,7 +290,7 @@ function be({
|
|
|
290
290
|
return T(e, r, i, t);
|
|
291
291
|
}
|
|
292
292
|
function T(e, r, i, t, s) {
|
|
293
|
-
var a, w, R, v, k, A, c, l, o,
|
|
293
|
+
var a, w, R, v, k, A, c, l, o, u, x, b, C, d, H, L, I, z, G, E, j, D, M, O, W, _, P, U, V, $, J, X, q, Y, K, Q, Z, ee, te, ie, re, se, le, ne, ae;
|
|
294
294
|
if (e.type === "passthrough")
|
|
295
295
|
return e.passthroughHtml ? /* @__PURE__ */ p(
|
|
296
296
|
"div",
|
|
@@ -311,35 +311,35 @@ function T(e, r, i, t, s) {
|
|
|
311
311
|
tableLayout: (R = e.preservedStyles) == null ? void 0 : R.tableLayout
|
|
312
312
|
},
|
|
313
313
|
children: (v = e.children) == null ? void 0 : v.map(
|
|
314
|
-
(h,
|
|
314
|
+
(h, g) => T(h, r, i, t, g)
|
|
315
315
|
)
|
|
316
316
|
},
|
|
317
317
|
s
|
|
318
318
|
);
|
|
319
319
|
if (e.type === "thead")
|
|
320
320
|
return /* @__PURE__ */ p("thead", { children: (k = e.children) == null ? void 0 : k.map(
|
|
321
|
-
(h,
|
|
321
|
+
(h, g) => T(h, r, i, t, g)
|
|
322
322
|
) }, s);
|
|
323
323
|
if (e.type === "tbody")
|
|
324
324
|
return /* @__PURE__ */ p("tbody", { children: (A = e.children) == null ? void 0 : A.map(
|
|
325
|
-
(h,
|
|
325
|
+
(h, g) => T(h, r, i, t, g)
|
|
326
326
|
) }, s);
|
|
327
327
|
if (e.type === "tr")
|
|
328
328
|
return /* @__PURE__ */ p("tr", { children: (c = e.children) == null ? void 0 : c.map(
|
|
329
|
-
(h,
|
|
329
|
+
(h, g) => T(h, r, i, t, g)
|
|
330
330
|
) }, s);
|
|
331
331
|
if (e.type === "th" || e.type === "td") {
|
|
332
|
-
const h = e.type,
|
|
332
|
+
const h = e.type, g = {
|
|
333
333
|
padding: (l = e.preservedStyles) == null ? void 0 : l.padding,
|
|
334
334
|
borderBottom: (o = e.preservedStyles) == null ? void 0 : o.borderBottom,
|
|
335
|
-
backgroundColor: (
|
|
335
|
+
backgroundColor: (u = e.preservedStyles) == null ? void 0 : u.backgroundColor,
|
|
336
336
|
textAlign: (x = e.preservedStyles) == null ? void 0 : x.textAlign,
|
|
337
337
|
verticalAlign: (b = e.preservedStyles) == null ? void 0 : b.verticalAlign,
|
|
338
338
|
width: (C = e.preservedStyles) == null ? void 0 : C.width
|
|
339
339
|
};
|
|
340
|
-
return e.children && e.children.length > 0 ? /* @__PURE__ */ p(h, { style:
|
|
341
|
-
(N,
|
|
342
|
-
) }, s) : /* @__PURE__ */ p(h, { style:
|
|
340
|
+
return e.children && e.children.length > 0 ? /* @__PURE__ */ p(h, { style: g, children: e.children.map(
|
|
341
|
+
(N, ue) => T(N, r, i, t, ue)
|
|
342
|
+
) }, s) : /* @__PURE__ */ p(h, { style: g, children: /* @__PURE__ */ p("div", { style: {
|
|
343
343
|
width: "80%",
|
|
344
344
|
height: 16,
|
|
345
345
|
borderRadius: 4,
|
|
@@ -348,7 +348,7 @@ function T(e, r, i, t, s) {
|
|
|
348
348
|
} }) }, s);
|
|
349
349
|
}
|
|
350
350
|
if (e.type === "container" && e.children && e.children.length > 0) {
|
|
351
|
-
const h = ((d = e.preservedStyles) == null ? void 0 : d.border) || "",
|
|
351
|
+
const h = ((d = e.preservedStyles) == null ? void 0 : d.border) || "", g = h && !h.includes("0px") && !h.startsWith("none");
|
|
352
352
|
return /* @__PURE__ */ p(
|
|
353
353
|
"div",
|
|
354
354
|
{
|
|
@@ -360,19 +360,19 @@ function T(e, r, i, t, s) {
|
|
|
360
360
|
// This avoids issues with incorrect height measurement from alt text
|
|
361
361
|
boxSizing: "border-box",
|
|
362
362
|
padding: (H = e.preservedStyles) == null ? void 0 : H.padding,
|
|
363
|
-
border:
|
|
363
|
+
border: g ? (L = e.preservedStyles) == null ? void 0 : L.border : void 0,
|
|
364
364
|
borderRadius: e.borderRadius,
|
|
365
|
-
justifyContent: (
|
|
365
|
+
justifyContent: (I = e.preservedStyles) == null ? void 0 : I.justifyContent,
|
|
366
366
|
alignItems: (z = e.preservedStyles) == null ? void 0 : z.alignItems,
|
|
367
|
-
flexDirection: (
|
|
368
|
-
minHeight: (
|
|
369
|
-
//
|
|
370
|
-
//
|
|
371
|
-
gridTemplateColumns:
|
|
372
|
-
gridTemplateRows: (
|
|
367
|
+
flexDirection: (G = e.preservedStyles) == null ? void 0 : G.flexDirection,
|
|
368
|
+
minHeight: (E = e.preservedStyles) == null ? void 0 : E.minHeight,
|
|
369
|
+
// Preserve measured grid template so responsive breakpoints (e.g. md:grid-cols-*)
|
|
370
|
+
// are kept exactly during skeleton rendering.
|
|
371
|
+
gridTemplateColumns: (j = e.preservedStyles) == null ? void 0 : j.gridTemplateColumns,
|
|
372
|
+
gridTemplateRows: (D = e.preservedStyles) == null ? void 0 : D.gridTemplateRows
|
|
373
373
|
},
|
|
374
374
|
children: e.children.map(
|
|
375
|
-
(
|
|
375
|
+
(F, N) => T(F, r, i, t, N)
|
|
376
376
|
)
|
|
377
377
|
},
|
|
378
378
|
s
|
|
@@ -386,7 +386,7 @@ function T(e, r, i, t, s) {
|
|
|
386
386
|
backgroundColor: r,
|
|
387
387
|
display: "block",
|
|
388
388
|
boxSizing: "border-box",
|
|
389
|
-
margin: (
|
|
389
|
+
margin: (M = e.preservedStyles) == null ? void 0 : M.margin
|
|
390
390
|
};
|
|
391
391
|
return t === "pulse" && (h.animation = "skeleton-pulse 2s ease-in-out infinite"), /* @__PURE__ */ p("div", { style: h, className: "skeleton-image" }, s);
|
|
392
392
|
}
|
|
@@ -399,50 +399,50 @@ function T(e, r, i, t, s) {
|
|
|
399
399
|
minHeight: e.rect.height,
|
|
400
400
|
boxSizing: "border-box",
|
|
401
401
|
// Preserve flex-item properties
|
|
402
|
-
margin: (
|
|
403
|
-
flex: (
|
|
404
|
-
flexGrow: (
|
|
402
|
+
margin: (O = e.preservedStyles) == null ? void 0 : O.margin,
|
|
403
|
+
flex: (W = e.preservedStyles) == null ? void 0 : W.flex,
|
|
404
|
+
flexGrow: (_ = e.preservedStyles) != null && _.flexGrow ? Number(e.preservedStyles.flexGrow) : void 0,
|
|
405
405
|
flexShrink: 0,
|
|
406
406
|
// Prevent flex compression
|
|
407
|
-
flexBasis: (
|
|
408
|
-
alignSelf: (
|
|
407
|
+
flexBasis: (P = e.preservedStyles) == null ? void 0 : P.flexBasis,
|
|
408
|
+
alignSelf: (U = e.preservedStyles) == null ? void 0 : U.alignSelf,
|
|
409
409
|
justifySelf: (V = e.preservedStyles) == null ? void 0 : V.justifySelf,
|
|
410
410
|
// Grid properties
|
|
411
|
-
gridColumn: (
|
|
412
|
-
gridRow: (
|
|
413
|
-
gridArea: (
|
|
411
|
+
gridColumn: ($ = e.preservedStyles) == null ? void 0 : $.gridColumn,
|
|
412
|
+
gridRow: (J = e.preservedStyles) == null ? void 0 : J.gridRow,
|
|
413
|
+
gridArea: (X = e.preservedStyles) == null ? void 0 : X.gridArea,
|
|
414
414
|
// Visual
|
|
415
415
|
borderRadius: e.borderRadius,
|
|
416
416
|
backgroundColor: r,
|
|
417
417
|
// Preserve inline nature
|
|
418
418
|
display: "inline-block",
|
|
419
|
-
verticalAlign: ((
|
|
419
|
+
verticalAlign: ((q = e.preservedStyles) == null ? void 0 : q.verticalAlign) || "middle"
|
|
420
420
|
};
|
|
421
421
|
return t === "pulse" && (h.animation = "skeleton-pulse 2s ease-in-out infinite"), /* @__PURE__ */ p("div", { style: h, className: `skeleton-${e.type}` }, s);
|
|
422
422
|
}
|
|
423
423
|
const n = {
|
|
424
424
|
// Only margin for spacing between siblings
|
|
425
|
-
margin: (
|
|
425
|
+
margin: (Y = e.preservedStyles) == null ? void 0 : Y.margin,
|
|
426
426
|
// Flex-item properties
|
|
427
|
-
flex: (
|
|
428
|
-
flexGrow: (
|
|
429
|
-
flexShrink: (
|
|
430
|
-
flexBasis: (
|
|
431
|
-
alignSelf: (
|
|
427
|
+
flex: (K = e.preservedStyles) == null ? void 0 : K.flex,
|
|
428
|
+
flexGrow: (Q = e.preservedStyles) != null && Q.flexGrow ? Number(e.preservedStyles.flexGrow) : void 0,
|
|
429
|
+
flexShrink: (Z = e.preservedStyles) != null && Z.flexShrink ? Number(e.preservedStyles.flexShrink) : void 0,
|
|
430
|
+
flexBasis: (ee = e.preservedStyles) == null ? void 0 : ee.flexBasis,
|
|
431
|
+
alignSelf: (te = e.preservedStyles) == null ? void 0 : te.alignSelf,
|
|
432
432
|
// Grid-item properties
|
|
433
|
-
gridColumn: (
|
|
434
|
-
gridRow: (
|
|
435
|
-
gridArea: (
|
|
436
|
-
}, S = ((
|
|
433
|
+
gridColumn: (ie = e.preservedStyles) == null ? void 0 : ie.gridColumn,
|
|
434
|
+
gridRow: (re = e.preservedStyles) == null ? void 0 : re.gridRow,
|
|
435
|
+
gridArea: (se = e.preservedStyles) == null ? void 0 : se.gridArea
|
|
436
|
+
}, S = ((le = e.preservedStyles) == null ? void 0 : le.display) === "inline" ? "inline-block" : "block", m = {
|
|
437
437
|
width: e.rect.width,
|
|
438
438
|
height: e.rect.height,
|
|
439
439
|
borderRadius: e.borderRadius,
|
|
440
440
|
backgroundColor: r,
|
|
441
441
|
display: S,
|
|
442
|
-
verticalAlign: ((
|
|
442
|
+
verticalAlign: ((ne = e.preservedStyles) == null ? void 0 : ne.verticalAlign) || "middle"
|
|
443
443
|
};
|
|
444
444
|
if (t === "pulse" && (m.animation = "skeleton-pulse 2s ease-in-out infinite"), e.type === "text" && e.lines && e.lines > 1) {
|
|
445
|
-
const h = parseFloat(((
|
|
445
|
+
const h = parseFloat(((ae = e.preservedStyles) == null ? void 0 : ae.lineHeight) || "1.2"), g = e.rect.height / e.lines;
|
|
446
446
|
return /* @__PURE__ */ p(
|
|
447
447
|
"div",
|
|
448
448
|
{
|
|
@@ -450,14 +450,14 @@ function T(e, r, i, t, s) {
|
|
|
450
450
|
...n,
|
|
451
451
|
display: "flex",
|
|
452
452
|
flexDirection: "column",
|
|
453
|
-
gap: h >
|
|
453
|
+
gap: h > g ? h - g : 4
|
|
454
454
|
},
|
|
455
455
|
children: Array.from({ length: e.lines }).map((F, N) => /* @__PURE__ */ p(
|
|
456
456
|
"div",
|
|
457
457
|
{
|
|
458
458
|
style: {
|
|
459
459
|
...m,
|
|
460
|
-
height:
|
|
460
|
+
height: g,
|
|
461
461
|
width: N === e.lines - 1 ? e.rect.width * 0.7 : e.rect.width
|
|
462
462
|
// Last line shorter
|
|
463
463
|
},
|
|
@@ -492,13 +492,13 @@ function ke({
|
|
|
492
492
|
() => ({ ...ve, ...i }),
|
|
493
493
|
[i]
|
|
494
494
|
), s = ye(null), [y, n] = Se(null);
|
|
495
|
-
return
|
|
495
|
+
return oe(() => {
|
|
496
496
|
if (!e || !s.current)
|
|
497
497
|
return;
|
|
498
498
|
const f = requestAnimationFrame(() => {
|
|
499
499
|
if (s.current)
|
|
500
500
|
try {
|
|
501
|
-
const a =
|
|
501
|
+
const a = ce(s.current, t);
|
|
502
502
|
if (a) {
|
|
503
503
|
const w = B(a, t);
|
|
504
504
|
n(w);
|
|
@@ -508,13 +508,13 @@ function ke({
|
|
|
508
508
|
}
|
|
509
509
|
});
|
|
510
510
|
return () => cancelAnimationFrame(f);
|
|
511
|
-
}, [e, t]),
|
|
511
|
+
}, [e, t]), oe(() => {
|
|
512
512
|
if (e) return;
|
|
513
513
|
const f = setTimeout(() => {
|
|
514
514
|
n(null);
|
|
515
515
|
}, 300);
|
|
516
516
|
return () => clearTimeout(f);
|
|
517
|
-
}, [e]), /* @__PURE__ */
|
|
517
|
+
}, [e]), /* @__PURE__ */ ge("div", { style: { position: "relative" }, children: [
|
|
518
518
|
/* @__PURE__ */ p("style", { children: `
|
|
519
519
|
@keyframes skeleton-pulse {
|
|
520
520
|
0%, 100% { opacity: 1; }
|