@alikhalilll/a-smart-skeleton 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +356 -2
- package/dist/_chunks/core.js +2314 -0
- package/dist/_chunks/core.js.map +1 -0
- package/dist/_chunks/index.d.ts +431 -0
- package/dist/core/index.cjs +67 -0
- package/dist/core/index.d.cts +2 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/element/index.cjs +47 -0
- package/dist/element/index.cjs.map +1 -0
- package/dist/element/index.d.cts +24 -0
- package/dist/element/index.d.ts +24 -0
- package/dist/element/index.js +46 -0
- package/dist/element/index.js.map +1 -0
- package/dist/index.cjs +512 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +204 -0
- package/dist/index.d.ts +204 -0
- package/dist/index.js +441 -0
- package/dist/index.js.map +1 -0
- package/dist/nuxt/index.cjs +36 -0
- package/dist/nuxt/index.cjs.map +1 -0
- package/dist/nuxt/index.d.cts +30 -0
- package/dist/nuxt/index.d.ts +30 -0
- package/dist/nuxt/index.js +36 -0
- package/dist/nuxt/index.js.map +1 -0
- package/dist/resolver/index.cjs +26 -0
- package/dist/resolver/index.cjs.map +1 -0
- package/dist/resolver/index.d.cts +19 -0
- package/dist/resolver/index.d.ts +19 -0
- package/dist/resolver/index.js +26 -0
- package/dist/resolver/index.js.map +1 -0
- package/dist/styles.css +136 -0
- package/package.json +144 -3
- package/src/components/ASmartSkeleton.vue +228 -0
- package/src/components/ASmartSkeletonBox.vue +23 -0
- package/src/components/ASmartSkeletonProvider.vue +34 -0
- package/src/components/ASmartSkeletonSuspense.vue +142 -0
- package/src/composables/useSmartSkeleton.ts +20 -0
- package/src/composables/useStyles.ts +8 -0
- package/src/core/boot.ts +63 -0
- package/src/core/canvas.ts +7 -0
- package/src/core/constants.ts +76 -0
- package/src/core/debug.ts +154 -0
- package/src/core/dev.ts +53 -0
- package/src/core/fade.ts +490 -0
- package/src/core/ghost.ts +236 -0
- package/src/core/images.ts +28 -0
- package/src/core/index.ts +71 -0
- package/src/core/inline.generated.ts +9 -0
- package/src/core/measure.ts +571 -0
- package/src/core/memory.ts +146 -0
- package/src/core/motion.ts +12 -0
- package/src/core/plan.ts +368 -0
- package/src/core/reveal.ts +89 -0
- package/src/core/reveals.ts +39 -0
- package/src/core/shapes.ts +84 -0
- package/src/core/shimmer.ts +229 -0
- package/src/core/skeleton.ts +43 -0
- package/src/core/snapshot.ts +159 -0
- package/src/core/styles.ts +187 -0
- package/src/core/sweep.ts +203 -0
- package/src/core/theme.ts +162 -0
- package/src/element/index.ts +59 -0
- package/src/index.ts +27 -0
- package/src/nuxt/index.ts +59 -0
- package/src/resolver/index.ts +35 -0
- package/src/types.ts +100 -0
- package/src/vue/Skeleton.ts +275 -0
- package/src/vue/Suspense.ts +157 -0
- package/src/vue/config.ts +78 -0
- package/src/vue/index.ts +7 -0
- package/web-types.json +256 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ATTR_BLOCK,
|
|
3
|
+
ATTR_LAYER,
|
|
4
|
+
ATTR_EXITING,
|
|
5
|
+
ATTR_GHOST,
|
|
6
|
+
ATTR_INACTIVE,
|
|
7
|
+
ATTR_LOADING,
|
|
8
|
+
ATTR_OWN,
|
|
9
|
+
ATTR_REVEAL,
|
|
10
|
+
ATTR_SNAPSHOT,
|
|
11
|
+
GHOSTED,
|
|
12
|
+
UNCOVER,
|
|
13
|
+
} from './constants';
|
|
14
|
+
import { reportLoading } from './debug';
|
|
15
|
+
import { noteSkeleton } from './plan';
|
|
16
|
+
import { warnOnce } from './dev';
|
|
17
|
+
import { measureSkeleton } from './measure';
|
|
18
|
+
import { flushShapes, recallShape } from './shapes';
|
|
19
|
+
import { drawSkeleton } from './shimmer';
|
|
20
|
+
import { autoKey, restore } from './snapshot';
|
|
21
|
+
import { addStyles, removeStyles } from './styles';
|
|
22
|
+
|
|
23
|
+
export type GhostOptions = {
|
|
24
|
+
/** Remembers the shape under this key; without it, one is derived from the markup. */
|
|
25
|
+
id?: string;
|
|
26
|
+
/** The ghost was rendered from real data, so it beats any remembered shape. */
|
|
27
|
+
exact?: boolean;
|
|
28
|
+
/** Its data is still set: a refetch over what's on screen, not a new load. */
|
|
29
|
+
refetch?: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type Source = 'ghost' | 'recall' | 'fallback';
|
|
33
|
+
|
|
34
|
+
const FALLBACK = [100, 90, 60]
|
|
35
|
+
.map(
|
|
36
|
+
(width) =>
|
|
37
|
+
`<div ${ATTR_BLOCK} style="width:${width}%;height:0.75em;margin:0.5em 0;border-radius:0.25em"></div>`
|
|
38
|
+
)
|
|
39
|
+
.join('');
|
|
40
|
+
|
|
41
|
+
// In the document before any script ran: server-rendered, so already painted at the shape it has
|
|
42
|
+
const served = new WeakSet<Element>(
|
|
43
|
+
typeof document === 'undefined' ? [] : document.querySelectorAll(`[${ATTR_LOADING}]`)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const loadingSizes = new WeakMap<
|
|
47
|
+
Element,
|
|
48
|
+
{
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
guessed: boolean;
|
|
52
|
+
/** Each image's height in the skeleton, in document order — 0 for one nothing sizes until it loads. */
|
|
53
|
+
images: number[];
|
|
54
|
+
}
|
|
55
|
+
>();
|
|
56
|
+
|
|
57
|
+
/** The size a reveal's skeleton last measured at, taken once by whatever replaces it. */
|
|
58
|
+
export function takeLoadingSize(reveal: Element | null) {
|
|
59
|
+
if (!reveal) return undefined;
|
|
60
|
+
const size = loadingSizes.get(reveal);
|
|
61
|
+
loadingSizes.delete(reveal);
|
|
62
|
+
return size;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function child(root: HTMLElement, attribute: string) {
|
|
66
|
+
for (const element of root.children) {
|
|
67
|
+
if (element.hasAttribute(attribute)) return element as HTMLElement;
|
|
68
|
+
}
|
|
69
|
+
const element = document.createElement('div');
|
|
70
|
+
element.setAttribute(attribute, '');
|
|
71
|
+
root.append(element);
|
|
72
|
+
return element;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const hasContent = (element: Element) =>
|
|
76
|
+
!!element.firstElementChild || !!element.textContent?.trim();
|
|
77
|
+
|
|
78
|
+
/** Draws a skeleton over content laid out unseen in `root`, so it takes exactly the content's space. */
|
|
79
|
+
export class Ghost {
|
|
80
|
+
private ghost: HTMLElement;
|
|
81
|
+
private snapshot: HTMLElement;
|
|
82
|
+
private layer: HTMLElement;
|
|
83
|
+
private source: Source | null = null;
|
|
84
|
+
private hadContent = false;
|
|
85
|
+
private frame = 0;
|
|
86
|
+
private mutations: MutationObserver;
|
|
87
|
+
private resize: ResizeObserver | null;
|
|
88
|
+
|
|
89
|
+
constructor(
|
|
90
|
+
private root: HTMLElement,
|
|
91
|
+
private options: GhostOptions = {}
|
|
92
|
+
) {
|
|
93
|
+
addStyles();
|
|
94
|
+
(root as HTMLElement & { [GHOSTED]?: boolean })[GHOSTED] = true;
|
|
95
|
+
root.setAttribute(ATTR_LOADING, '');
|
|
96
|
+
root.setAttribute('aria-hidden', 'true');
|
|
97
|
+
root.inert = true;
|
|
98
|
+
|
|
99
|
+
this.ghost = child(root, ATTR_GHOST);
|
|
100
|
+
this.snapshot = child(root, ATTR_SNAPSHOT);
|
|
101
|
+
this.layer = child(root, ATTR_LAYER);
|
|
102
|
+
|
|
103
|
+
this.hadContent = hasContent(this.ghost);
|
|
104
|
+
|
|
105
|
+
this.note();
|
|
106
|
+
this.choose();
|
|
107
|
+
this.measure();
|
|
108
|
+
// Covered paint is marked now; the inline script's stand-in sheet can go
|
|
109
|
+
(root as HTMLElement & { [UNCOVER]?: () => void })[UNCOVER]?.();
|
|
110
|
+
|
|
111
|
+
this.mutations = new MutationObserver(this.schedule);
|
|
112
|
+
this.mutations.observe(this.ghost, {
|
|
113
|
+
childList: true,
|
|
114
|
+
subtree: true,
|
|
115
|
+
characterData: true,
|
|
116
|
+
attributes: true,
|
|
117
|
+
});
|
|
118
|
+
// None in jsdom, which has no layout to follow
|
|
119
|
+
this.resize = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(this.schedule);
|
|
120
|
+
this.resize?.observe(root);
|
|
121
|
+
root.addEventListener('load', this.schedule, true);
|
|
122
|
+
addEventListener('resize', this.schedule);
|
|
123
|
+
document.fonts?.ready.then(this.schedule);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Swaps in fresh options without tearing down the observers or stylesheet refcount. */
|
|
127
|
+
update(options: GhostOptions = {}) {
|
|
128
|
+
this.options = options;
|
|
129
|
+
this.note();
|
|
130
|
+
this.choose();
|
|
131
|
+
this.measure();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private note() {
|
|
135
|
+
const reveal = this.root.closest(`[${ATTR_REVEAL}]`);
|
|
136
|
+
if (!reveal) return;
|
|
137
|
+
noteSkeleton(reveal, {
|
|
138
|
+
served: served.has(this.root),
|
|
139
|
+
refetch: !!this.options.refetch && this.hadContent,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private schedule = () => {
|
|
144
|
+
// Leaving: re-measuring a pane pinned at its last size would redraw what's fading out
|
|
145
|
+
if (this.root.closest(`[${ATTR_EXITING}]`)) return;
|
|
146
|
+
cancelAnimationFrame(this.frame);
|
|
147
|
+
this.frame = requestAnimationFrame(() => {
|
|
148
|
+
const has = hasContent(this.ghost);
|
|
149
|
+
if (has !== this.hadContent) {
|
|
150
|
+
this.hadContent = has;
|
|
151
|
+
this.choose();
|
|
152
|
+
}
|
|
153
|
+
this.measure();
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
private choose() {
|
|
158
|
+
let next: Source = 'fallback';
|
|
159
|
+
let html: string | null = null;
|
|
160
|
+
|
|
161
|
+
// Re-shaping a skeleton already on screen would move the page — keep it
|
|
162
|
+
const settled = this.options.exact || served.has(this.root);
|
|
163
|
+
// A remembered shape is the shown content, which has none of the author's own skeletons
|
|
164
|
+
const own = !!this.ghost.querySelector(`[${ATTR_OWN}]`);
|
|
165
|
+
if ((settled || own) && this.hadContent) {
|
|
166
|
+
next = 'ghost';
|
|
167
|
+
} else {
|
|
168
|
+
const key = this.options.id ?? autoKey(this.ghost);
|
|
169
|
+
if (key) {
|
|
170
|
+
flushShapes(key);
|
|
171
|
+
html = recallShape(key);
|
|
172
|
+
}
|
|
173
|
+
if (html) next = 'recall';
|
|
174
|
+
else if (this.hadContent) next = 'ghost';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (next !== this.source) {
|
|
178
|
+
if (next === 'recall' && html) {
|
|
179
|
+
this.snapshot.replaceChildren(restore(html));
|
|
180
|
+
} else if (next === 'fallback') {
|
|
181
|
+
this.snapshot.innerHTML = FALLBACK;
|
|
182
|
+
warnOnce(
|
|
183
|
+
`fallback:${this.options.id}`,
|
|
184
|
+
`Nothing to draw a skeleton from${this.options.id ? ` for "${this.options.id}"` : ''}. Pass \`sample\` data, or an \`id\` so its shape is remembered after the first load. For a lone placeholder box, use \`SkeletonBox\`.`
|
|
185
|
+
);
|
|
186
|
+
} else {
|
|
187
|
+
this.snapshot.replaceChildren();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
this.ghost.toggleAttribute(ATTR_INACTIVE, next !== 'ghost');
|
|
192
|
+
this.snapshot.toggleAttribute(ATTR_INACTIVE, next === 'ghost');
|
|
193
|
+
this.source = next;
|
|
194
|
+
|
|
195
|
+
reportLoading(
|
|
196
|
+
this.root.closest(`[${ATTR_REVEAL}]`),
|
|
197
|
+
next === 'recall'
|
|
198
|
+
? 'remembered shape'
|
|
199
|
+
: next === 'fallback'
|
|
200
|
+
? 'fallback'
|
|
201
|
+
: this.options.exact
|
|
202
|
+
? 'data'
|
|
203
|
+
: served.has(this.root)
|
|
204
|
+
? 'server render'
|
|
205
|
+
: 'sample'
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private measure() {
|
|
210
|
+
const active = this.source === 'ghost' ? this.ghost : this.snapshot;
|
|
211
|
+
drawSkeleton(this.layer, measureSkeleton(active, this.layer));
|
|
212
|
+
|
|
213
|
+
const reveal = this.root.closest(`[${ATTR_REVEAL}]`);
|
|
214
|
+
if (reveal) {
|
|
215
|
+
loadingSizes.set(reveal, {
|
|
216
|
+
width: this.root.offsetWidth,
|
|
217
|
+
height: this.root.offsetHeight,
|
|
218
|
+
// Drawn from real data, so a sample would have changed nothing
|
|
219
|
+
guessed: !(this.options.exact && this.source === 'ghost'),
|
|
220
|
+
images: [...active.querySelectorAll('img')].map(
|
|
221
|
+
(image) => image.getBoundingClientRect().height
|
|
222
|
+
),
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Bones keep shimmering until they are removed, so a skeleton fading out stays alive
|
|
228
|
+
destroy() {
|
|
229
|
+
cancelAnimationFrame(this.frame);
|
|
230
|
+
this.mutations.disconnect();
|
|
231
|
+
this.resize?.disconnect();
|
|
232
|
+
this.root.removeEventListener('load', this.schedule, true);
|
|
233
|
+
removeEventListener('resize', this.schedule);
|
|
234
|
+
removeStyles();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IMAGE_TIMEOUT } from './constants';
|
|
2
|
+
|
|
3
|
+
const noop = () => {};
|
|
4
|
+
|
|
5
|
+
// Lazy and well off screen, it won't load until scrolled to — nothing to wait for
|
|
6
|
+
const deferred = (image: HTMLImageElement) => {
|
|
7
|
+
if (image.loading !== 'lazy') return false;
|
|
8
|
+
const rect = image.getBoundingClientRect();
|
|
9
|
+
return rect.bottom < -innerHeight || rect.top > innerHeight * 2;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** Images inside still on their way. */
|
|
13
|
+
export const pendingImages = (element: Element) =>
|
|
14
|
+
[...element.querySelectorAll('img')].filter((image) => !image.complete && !deferred(image));
|
|
15
|
+
|
|
16
|
+
/** Loaded and decoded, so its first frame on screen is the picture; failing counts, as nothing more is coming. */
|
|
17
|
+
export function imageReady(image: HTMLImageElement): Promise<void> {
|
|
18
|
+
const loaded = image.complete
|
|
19
|
+
? Promise.resolve()
|
|
20
|
+
: new Promise<void>((resolve) => {
|
|
21
|
+
image.addEventListener('load', () => resolve(), { once: true });
|
|
22
|
+
image.addEventListener('error', () => resolve(), { once: true });
|
|
23
|
+
});
|
|
24
|
+
return Promise.race([
|
|
25
|
+
loaded.then(() => image.decode?.()).then(noop, noop),
|
|
26
|
+
new Promise<void>((resolve) => setTimeout(resolve, IMAGE_TIMEOUT)),
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export {
|
|
2
|
+
ATTR_BAR,
|
|
3
|
+
ATTR_BLOCK,
|
|
4
|
+
ATTR_RECT,
|
|
5
|
+
ATTR_LAYER,
|
|
6
|
+
ATTR_DEBUG,
|
|
7
|
+
ATTR_GHOST,
|
|
8
|
+
ATTR_IGNORE,
|
|
9
|
+
ATTR_LOADING,
|
|
10
|
+
ATTR_OWN,
|
|
11
|
+
ATTR_PANE,
|
|
12
|
+
ATTR_REVEAL,
|
|
13
|
+
ATTR_SNAPSHOT,
|
|
14
|
+
ATTR_STATUS,
|
|
15
|
+
ATTR_XRAY,
|
|
16
|
+
CASCADE_SPREAD,
|
|
17
|
+
DURATIONS,
|
|
18
|
+
FADE_DURATION,
|
|
19
|
+
IMAGE_TIMEOUT,
|
|
20
|
+
RESIZE_DURATION,
|
|
21
|
+
} from './constants';
|
|
22
|
+
export { addStyles, SMART_SKELETON_CSS, removeStyles } from './styles';
|
|
23
|
+
export { INLINE_SCRIPT, INLINE_STUB, inlineScriptTag } from './inline.generated';
|
|
24
|
+
export {
|
|
25
|
+
applyTheme,
|
|
26
|
+
DEFAULT_COLORS,
|
|
27
|
+
DEFAULT_THEME,
|
|
28
|
+
skeletonCSS,
|
|
29
|
+
themeStyleTag,
|
|
30
|
+
themeVariables,
|
|
31
|
+
} from './theme';
|
|
32
|
+
export type {
|
|
33
|
+
SkeletonAnimation,
|
|
34
|
+
SkeletonColors,
|
|
35
|
+
SkeletonMode,
|
|
36
|
+
SkeletonSpeed,
|
|
37
|
+
SkeletonText,
|
|
38
|
+
SkeletonTheme,
|
|
39
|
+
} from './theme';
|
|
40
|
+
export { debugging, setDebug } from './debug';
|
|
41
|
+
export { checkNonce } from './dev';
|
|
42
|
+
export { DEFAULT_TEXT, measureSkeleton } from './measure';
|
|
43
|
+
export type { SkeletonChrome, SkeletonRect, TextBottom, TextTop } from './measure';
|
|
44
|
+
export { rectsOf, drawSkeleton, motionOf, shimmer } from './shimmer';
|
|
45
|
+
export { Ghost } from './ghost';
|
|
46
|
+
export type { GhostOptions } from './ghost';
|
|
47
|
+
export { Memory } from './memory';
|
|
48
|
+
export type { MemoryOptions } from './memory';
|
|
49
|
+
export { forgetShapes } from './shapes';
|
|
50
|
+
export { createSkeletonBox, DEFAULT_SKELETON_BOX_OPTIONS, skeletonBoxStyle } from './skeleton';
|
|
51
|
+
export type { SkeletonBoxOptions } from './skeleton';
|
|
52
|
+
export { boxOf, fadeIn, fadeOut, prefersReducedMotion } from './fade';
|
|
53
|
+
export type { PaneBox } from './fade';
|
|
54
|
+
export {
|
|
55
|
+
byContent,
|
|
56
|
+
byPosition,
|
|
57
|
+
byRow,
|
|
58
|
+
instant,
|
|
59
|
+
planReveal,
|
|
60
|
+
quickly,
|
|
61
|
+
sequence,
|
|
62
|
+
SKELETON_DELAY,
|
|
63
|
+
smart,
|
|
64
|
+
together,
|
|
65
|
+
} from './plan';
|
|
66
|
+
export type { Piece, RevealContext, RevealMode, RevealSpec, Wipe, WipePane } from './plan';
|
|
67
|
+
export { sweep } from './sweep';
|
|
68
|
+
export { exitDuration } from './reveals';
|
|
69
|
+
export { imageReady, pendingImages } from './images';
|
|
70
|
+
export { Reveal } from './reveal';
|
|
71
|
+
export type { RevealOptions } from './reveal';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by scripts/build-inline.mjs from boot.ts — don't edit
|
|
2
|
+
export const INLINE_SCRIPT = "\"use strict\";(()=>{var H=\"ask-ghost\";var G=\"ask-layer\",M=\"ask-rect\";var A=\"ask-covered\",P=\"ask-own\",W=\"ask-ignore\",F=\"ask-block\",z=Symbol.for(\"a-smart-skeleton.ghost\"),U=Symbol.for(\"a-smart-skeleton.uncover\"),X=\"a-smart-skeleton.boot\";var j=\"var(--ask-highlight, rgb(255 255 255 / 0.12))\";var V={shimmer:{slow:4e3,fast:2e3},pulse:{slow:2400,fast:1200}},q=[\"a\",\"b\",\"code\",\"dd\",\"dt\",\"em\",\"h1\",\"h2\",\"h3\",\"h4\",\"h5\",\"h6\",\"i\",\"label\",\"li\",\"p\",\"small\",\"span\",\"strong\",\"td\",\"th\",\"time\"],Y=[\"audio\",\"canvas\",\"embed\",\"iframe\",\"img\",\"input\",\"object\",\"picture\",\"select\",\"svg\",\"textarea\",\"video\"];var Et,K=()=>Et??(Et=/jsdom/.test(navigator.userAgent)?null:document.createElement(\"canvas\").getContext(\"2d\"));var nt=(t,e)=>t?{left:Math.max(t.left,e.left),right:Math.min(t.right,e.right),top:Math.max(t.top,e.top),bottom:Math.min(t.bottom,e.bottom)}:e,k=t=>[[t.left,t.top],[t.right,t.top],[t.right,t.bottom],[t.left,t.bottom]],$=(t,e)=>Math.abs(t[0]-e[0])<1&&Math.abs(t[1]-e[1])<1;function rt(t,e){let r=Math.min(e.right-e.left,e.bottom-e.top),o=n=>n.endsWith(\"%\")?parseFloat(n)/100*r:parseFloat(n)||0;return[o(t.borderTopLeftRadius),o(t.borderTopRightRadius),o(t.borderBottomRightRadius),o(t.borderBottomLeftRadius)]}function At(t,e,r){let o=nt(t,e),n=rt(r,e),c=t?k(t):[],i=k(e),l=k(o).map((b,R)=>$(b,i[R])?n[R]:t&&$(b,c[R])?t.corners[R]:0),{left:s,right:a,top:d,bottom:h}=o;return{left:s,right:a,top:d,bottom:h,corners:l}}function Z(t,e,r,o){if(!t)return r;let n=k(t),c=k(e).map((l,s)=>t.corners[s]>0&&$(l,n[s]));if(!c.some(Boolean))return r;let i=rt(o,e);return c.map((l,s)=>l?Math.max(i[s],t.corners[s]):i[s]).map(l=>`${l}px`).join(\" \")}var O=(t,e)=>{let r=nt(e,t);return r.right-r.left>=2&&r.bottom-r.top>=2?r:null},J={top:\"ascender\",bottom:\"descender\"},w=new Map,st=t=>[...t.getClientRects?.()??[]];function kt(t){let e=`${t.fontStyle} ${t.fontWeight} ${t.fontSize} ${t.fontFamily}`;if(w.has(e))return w.get(e);let r=K(),o=null;if(r){r.font=e;let n=r.measureText(\"H\"),c=i=>r.measureText(i).actualBoundingBoxAscent;o={ascent:n.fontBoundingBoxAscent,descent:n.fontBoundingBoxDescent,ascender:c(\"bdfhkl\"),capHeight:n.actualBoundingBoxAscent,xHeight:c(\"xz\"),descender:r.measureText(\"gjpqy\").actualBoundingBoxDescent}}return w.set(e,o),o}var Q={ascender:\"ascender\",\"cap-height\":\"capHeight\",\"x-height\":\"xHeight\"},tt=(t,e,r,o)=>{let n=t.getPropertyValue(e).trim();return r.includes(n)?n:o};function Ct(t,e,r){let o=kt(e);if(!o){let s=Math.min(r*.72,t.bottom-t.top),a=(t.top+t.bottom)/2;return{barTop:a-s/2,barBottom:a+s/2}}let n=tt(e,\"--ask-text-top\",Object.keys(Q),J.top),c=tt(e,\"--ask-text-bottom\",[\"baseline\",\"descender\"],J.bottom),i=o.ascent+o.descent,l=t.top+(t.bottom-t.top-i)/2+o.ascent;return{barTop:l-o[Q[n]],barBottom:l+(c===\"descender\"?o.descender:0)}}var Mt=new Set(Y),Ot=new Set(q),it=.4,at=/[,/]\\s*0\\)$/,g=(t,e)=>t!==\"\"&&t!==e,N=[\"Top\",\"Right\",\"Bottom\",\"Left\"],wt=t=>N.every(e=>g(t[`border${e}Style`],\"none\")&&g(t[`border${e}Width`],\"0px\")),$t=t=>N.some(e=>g(t[`border${e}Style`],\"none\")&&g(t[`border${e}Width`],\"0px\")),Bt=t=>{let[e=0,r=0,o=0,n=0]=(t.replace(/rgba?\\([^)]*\\)|#\\w+/g,\"\").match(/-?[\\d.]+px/g)??[]).map(parseFloat);return t.includes(\"inset\")&&!o&&!n&&e===0!=(r===0)&&Math.abs(e+r)<=2},ct=t=>g(t,\"none\")&&!t.split(/,(?![^(]*\\))/).every(Bt);function Nt(t){let e=[],r=0,o=0;for(let n=0;n<t.length;n++){let c=t[n];c===\"(\"?r++:c===\")\"?r--:c===\",\"&&r===0&&(e.push(t.slice(o,n)),o=n+1)}return e.push(t.slice(o)),e.map(n=>n.trim()).filter(n=>n&&!n.startsWith(\"url(\")).join(\", \")}var dt=t=>g(t.backgroundColor,\"transparent\")&&!at.test(t.backgroundColor)||g(t.backgroundImage,\"none\")||ct(t.boxShadow)||wt(t);function _t(t){let e={},r=t.backgroundColor;g(r,\"transparent\")&&!at.test(r)&&(e.background=r);let o=t.backgroundImage;if(g(o,\"none\")){let n=Nt(o);n&&(e.background=e.background?`${n}, ${e.background}`:n)}for(let n of N){let c=t[`border${n}Width`],i=t[`border${n}Style`],l=t[`border${n}Color`];if(g(i,\"none\")&&g(c,\"0px\")&&parseFloat(c)>0){let s=`border${n}`;e[s]=`${c} ${i} ${l}`}}return ct(t.boxShadow)&&(e.boxShadow=t.boxShadow),Object.keys(e).length?e:void 0}var B=t=>Mt.has(t.localName),lt=t=>Ot.has(t.localName)&&!t.firstElementChild&&!t.textContent?.trim(),Dt=t=>{let e=document.createTreeWalker(t,NodeFilter.SHOW_TEXT|NodeFilter.SHOW_ELEMENT),r=document.createRange(),o=[],n=0;for(;e.nextNode();){let a=e.currentNode;if(a.nodeType===Node.TEXT_NODE){let d=a.parentElement;if(!a.textContent?.trim()||!d)continue;n++;let h=d.getBoundingClientRect();if(h.width<=1||h.height<=1)continue;r.selectNodeContents(a),o.push(st(r).filter(b=>b.width||b.height))}else lt(a)&&(n++,o.push([...a.getClientRects()]))}if(!o.some(a=>a.length))return n;let c=(parseFloat(getComputedStyle(t).fontSize)||16)*it,i=(a,d)=>Math.min(a.bottom,d.bottom)-Math.max(a.top,d.top)>=Math.min(a.height,d.height)/2&&Math.max(a.left,d.left)-Math.min(a.right,d.right)<=c,l=o.map((a,d)=>d),s=a=>l[a]===a?a:l[a]=s(l[a]);for(let a=0;a<o.length;a++)for(let d=a+1;d<o.length;d++)o[a].some(h=>o[d].some(b=>i(h,b)))&&(l[s(d)]=s(a));return new Set(o.map((a,d)=>s(d))).size},Lt=(t,e)=>t.hasAttribute(F)||B(t)||dt(e)&&Dt(t)<=1;function et(t,e){if(parseFloat(e.borderRadius)||!t.parentElement)return e.borderRadius||\"0px\";let r=t.parentElement,o=getComputedStyle(r),n=t.getBoundingClientRect(),c=r.getBoundingClientRect();return Math.abs(n.width-c.width)<1&&Math.abs(n.height-c.height)<1&&o.overflow!==\"visible\"?o.borderRadius:e.borderRadius||\"0px\"}function ot(t){t.sort((r,o)=>r.left-o.left);let e=[];for(let r of t){let o=e.find(n=>{let c=Math.min(n.bottom,r.bottom)-Math.max(n.top,r.top),i=Math.min(n.bottom-n.top,r.bottom-r.top),l=r.left-n.right;return c>=i/2&&l<=Math.max(n.fontSize,r.fontSize)*it});o?(o.left=Math.min(o.left,r.left),o.right=Math.max(o.right,r.right),o.top=Math.min(o.top,r.top),o.bottom=Math.max(o.bottom,r.bottom),o.fontSize=Math.max(o.fontSize,r.fontSize),o.barTop=Math.min(o.barTop,r.barTop),o.barBottom=Math.max(o.barBottom,r.barBottom)):e.push({...r})}return e.map(r=>{let o=r.barBottom-r.barTop;return{type:\"text\",x:r.left,y:r.barTop,width:r.right-r.left,height:o,radius:r.radius||`${Math.round(o/3)}px`}})}function mt(t,e,{mark:r=!0,onCovered:o}={}){let n=e.getBoundingClientRect(),c=e.offsetWidth?n.width/e.offsetWidth:1,i=e.offsetHeight?n.height/e.offsetHeight:1,l=m=>({left:(m.left-n.left)/c,right:(m.right-n.left)/c,top:(m.top-n.top)/i,bottom:(m.bottom-n.top)/i}),s=[],a=[],d=new Set,h=document.createRange(),b=(m,f,T)=>{let u=O(m,T);if(!u)return;let p=parseFloat(f.fontSize)||16,C=l(u);a.push({...C,fontSize:p,...Ct(C,f,p),radius:f.getPropertyValue(\"--ask-radius\").trim()})},R=(m,f)=>{let T=m.data.search(/\\S/);if(T<0||!m.parentElement)return;h.setStart(m,T),h.setEnd(m,m.data.trimEnd().length);let u=getComputedStyle(m.parentElement);for(let p of st(h))b(p,u,f)},I=(m,f)=>{for(let T of m.childNodes){if(T.nodeType===Node.TEXT_NODE){R(T,f);continue}if(T.nodeType!==Node.ELEMENT_NODE)continue;let u=T;if(u.hasAttribute(W)||u.hasAttribute(P))continue;let p=getComputedStyle(u);if(p.display===\"none\")continue;if(Lt(u,p)){let x=O(u.getBoundingClientRect(),f);if(x){let S=l(x);s.push({type:B(u)?\"media\":\"box\",x:S.left,y:S.top,width:S.right-S.left,height:S.bottom-S.top,radius:Z(f,x,et(u,p),p)})}B(u)||d.add(u);continue}if(lt(u)){for(let x of u.getClientRects())b(x,p,f);continue}if(dt(p)||$t(p)){let x=_t(p);if(x){let S=O(u.getBoundingClientRect(),f);if(S){let y=l(S);s.push({type:\"chrome\",x:y.left,y:y.top,width:y.right-y.left,height:y.bottom-y.top,radius:Z(f,S,et(u,p),p),chrome:x})}}}let C=p.overflowX!==\"visible\"||p.overflowY!==\"visible\";I(u,C?At(f,u.getBoundingClientRect(),p):f)}};if(I(t,null),o?.(d),!r)return[...s,...ot(a)];for(let m of t.querySelectorAll(`[${A}]`))d.has(m)||m.removeAttribute(A);for(let m of d)m.hasAttribute(A)||m.setAttribute(A,\"\");return[...s,...ot(a)]}var vt=t=>typeof matchMedia<\"u\"&&matchMedia(t).matches,It=()=>typeof Element<\"u\"&&typeof Element.prototype.animate==\"function\",ut=()=>!It()||vt(\"(prefers-reduced-motion: reduce)\");var _=t=>t.getAnimations?.()??[];var Ht=.7,Gt=1280,pt=320,Pt=2.5,Wt=.45,Ft=[\"shimmer\",\"group-shimmer\",\"pulse\",\"none\"];function bt(t){let e=getComputedStyle(t),r=(c,i,l)=>{let s=e.getPropertyValue(c).trim();return i.includes(s)?s:l},o=r(\"--ask-animation\",Ft,\"shimmer\"),n=r(\"--ask-speed\",[\"slow\",\"fast\"],\"slow\");return{animation:o,duration:V[o===\"pulse\"?\"pulse\":\"shimmer\"][n]}}var L=20,zt=Math.sin(L*Math.PI/180),gt=Math.cos(L*Math.PI/180),Ut=zt/gt;var Xt=`${90+L}deg`,D=.85,E=12,ht=t=>(1-Math.cos(Math.PI*t))/2;function jt(t){let e=t*Pt,r=(n,c)=>`color-mix(in srgb, ${j} ${(c*100).toFixed(1)}%, transparent) ${(n*t).toFixed(2)}px`,o=[];for(let n=0;n<=E;n++)o.push(r(n/E*D,ht(n/E)));for(let n=1;n<=E;n++)o.push(r(D+n/E*(1-D),ht(1-n/E)));return{width:e/gt,image:`repeating-linear-gradient(${Xt}, ${o.join(\", \")}, transparent ${e.toFixed(2)}px)`}}function Vt(t,e,r=bt(t)){for(let b of _(t))b.cancel();if(t.style.backgroundImage=\"\",t.style.backgroundRepeat=\"\",ut()||typeof t.animate!=\"function\"||r.animation===\"none\")return;let o={duration:r.duration,iterations:1/0};if(r.animation===\"pulse\"){t.animate([{opacity:1,easing:\"ease-in-out\"},{opacity:Wt,easing:\"ease-in-out\"},{opacity:1}],o).startTime=0;return}let n=t.getBoundingClientRect(),c=n.left+scrollX,i=r.animation===\"group-shimmer\"?e??{left:c,top:n.top,width:n.width,height:n.height}:{left:0,top:0,width:document.documentElement.clientWidth||innerWidth,height:innerHeight},l=Math.min(Math.max(i.width*.4,80),pt),{width:s,image:a}=jt(l),d=c-i.left+(n.top-i.top)*Ut,h=r.duration*Ht/(Gt+pt);t.style.backgroundImage=a,t.style.backgroundSize=`${s}px 100%`,t.style.backgroundRepeat=\"repeat-x\",t.animate([{backgroundPosition:`${-d}px 0`},{backgroundPosition:`${s-d}px 0`}],{duration:s*h,iterations:1/0}).startTime=0}var qt=t=>[...t.querySelectorAll(`:scope > [${M}]`)],ft=4,Yt=t=>t.replace(/(\\d+(?:\\.\\d+)?)px/g,(e,r)=>{let o=parseFloat(r);return Number.isFinite(o)&&o<ft?`${ft}px`:`${o}px`});function St(t,e){let r=qt(t),o=t.getBoundingClientRect(),n={left:o.left+scrollX,top:o.top,width:o.width,height:o.height},c=bt(t);e.forEach((i,l)=>{let s=r[l];if(s||(s=document.createElement(\"div\"),s.style.cssText=\"position:absolute;display:block;pointer-events:none;background-repeat:no-repeat\",t.append(s)),s.setAttribute(M,i.type),Object.assign(s.style,{left:`${i.x}px`,top:`${i.y}px`,width:`${i.width}px`,height:`${i.height}px`,borderRadius:i.type===\"chrome\"?i.radius:Yt(i.radius)}),i.type===\"chrome\"){for(let h of _(s))h.cancel();s.style.backgroundImage=\"\",s.style.backgroundSize=\"\",s.style.backgroundRepeat=\"\";let d=i.chrome;s.style.background=d?.background??\"\",s.style.borderTop=d?.borderTop??\"\",s.style.borderRight=d?.borderRight??\"\",s.style.borderBottom=d?.borderBottom??\"\",s.style.borderLeft=d?.borderLeft??\"\",s.style.boxShadow=d?.boxShadow??\"\",s.dataset.motion=\"chrome\";return}s.dataset.motion===\"chrome\"&&(s.style.background=\"\",s.style.borderTop=\"\",s.style.borderRight=\"\",s.style.borderBottom=\"\",s.style.borderLeft=\"\",s.style.boxShadow=\"\");let a=`${Math.round(n.left+i.x)}|${Math.round(i.y)}|${Math.round(i.height)}|${Math.round(n.left)}|${Math.round(n.width)}|${Math.round(n.height)}|${document.documentElement.clientWidth}|${c.animation}|${c.duration}`;s.dataset.motion!==a&&(s.dataset.motion=a,Vt(s,n,c))});for(let i=r.length-1;i>=e.length;i--)r[i].remove()}var xt=t=>t.parentElement?`${xt(t.parentElement)} > :nth-child(${[...t.parentElement.children].indexOf(t)+1})`:\":root\";function v(t){let e=t?.previousElementSibling,r=e?.querySelector(`:scope > [${H}]`),o=e?.querySelector(`:scope > [${G}]`);if(!e||!r||!o)return;let n=typeof CSSStyleSheet==\"function\"&&\"replaceSync\"in CSSStyleSheet.prototype?new CSSStyleSheet:null;n&&(document.adoptedStyleSheets=[...document.adoptedStyleSheets,n],e[U]=()=>{document.adoptedStyleSheets=document.adoptedStyleSheets.filter(s=>s!==n)});let c=s=>n?.replaceSync(s.size?`${[...s].map(xt).join(\", \")} { visibility: hidden !important; }`:\"\"),i=()=>{if(!o.isConnected||e[z])return l.disconnect();r.getClientRects().length&&St(o,mt(r,o,{mark:!1,onCovered:c}))},l=new ResizeObserver(i);l.observe(e),document.fonts?.ready.then(i)}var Rt=self,yt=Symbol.for(X),Tt=Rt[yt];Rt[yt]=v;Array.isArray(Tt)&&Tt.forEach(v);v(document.currentScript);})();";
|
|
3
|
+
|
|
4
|
+
/** For every skeleton after a page's first: calls the boot INLINE_SCRIPT left on window. */
|
|
5
|
+
export const INLINE_STUB = "(s=>{let k=Symbol.for(\"a-smart-skeleton.boot\"),b=self[k];typeof b==\"function\"?b(s):(self[k]=b||[]).push(s)})(document.currentScript)";
|
|
6
|
+
|
|
7
|
+
/** Either, as a whole element — for templates where a literal script tag would be read as the component's own. */
|
|
8
|
+
export const inlineScriptTag = (nonce?: string, stub = false) =>
|
|
9
|
+
`<script${nonce ? ` nonce="${nonce.replace(/"/g, """)}"` : ""}>${stub ? INLINE_STUB : INLINE_SCRIPT}</script>`;
|