@alikhalilll/a-smart-skeleton 0.0.0-stage → 1.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 +294 -2
- package/dist/_chunks/core.js +2187 -0
- package/dist/_chunks/core.js.map +1 -0
- package/dist/_chunks/index.d.ts +406 -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 +2 -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 +454 -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 +30 -0
- package/src/core/shapes.ts +84 -0
- package/src/core/shimmer.ts +173 -0
- package/src/core/skeleton.ts +43 -0
- package/src/core/snapshot.ts +159 -0
- package/src/core/styles.ts +167 -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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 alikhalilll
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,295 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @alikhalilll/a-smart-skeleton
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Smart, dynamic skeleton loader for Vue 3, Nuxt 3+, and vanilla Web Components. Lays content out unseen, measures typography and geometry down to exact canvas font metrics and clip regions, and reveals real content with smooth animations (smart, sequence, sweep, together).
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@alikhalilll/a-smart-skeleton)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Why `@alikhalilll/a-smart-skeleton`?
|
|
11
|
+
|
|
12
|
+
Handcrafted skeleton placeholders easily drift out of sync with real content, resulting in jarring layout shifts when data arrives.
|
|
13
|
+
|
|
14
|
+
`@alikhalilll/a-smart-skeleton` solves this by:
|
|
15
|
+
|
|
16
|
+
1. **Zero Layout Shift (CLS = 0)**: Content (or a sample schema) is rendered unseen in an off-screen ghost tree. Its geometry is measured, generating matching skeleton bones that occupy the exact layout footprint before first paint.
|
|
17
|
+
2. **Typographic Accuracy**: Using 2D canvas font metrics, text is measured along font lines (`ascender`, `capHeight`, `xHeight`, `baseline`, `descender`). Visual bars follow your actual typography, not generic rectangular gray blocks.
|
|
18
|
+
3. **Clip-Aware Geometry**: Elements nested inside rounded containers or clipped bounds (`overflow: hidden`) compute intersecting bounding boxes (`clipWithin`) so bones mirror real borders and clips.
|
|
19
|
+
4. **Rich Animation Suite**:
|
|
20
|
+
- **Bones animations**: `shimmer` (page-wide band), `group-shimmer` (per-skeleton), `pulse` (soft opacity wave), or `none`.
|
|
21
|
+
- **Reveal modes**: `smart` (intelligent contextual reveal), `together` (fade together), `sequence` (staggered cascade from top to bottom), `sweep` (glossy gradient sweep across the reveal with automatic stall detection), and `instant`.
|
|
22
|
+
- **Height easing**: When content height differs slightly from the skeleton guess, the container smoothly eases to the new height (`280ms`) rather than snapping.
|
|
23
|
+
5. **Stand-in Bones for Images**: Slow images don't block text from revealing. Content reveals on schedule while pending images remain covered by individual stand-in bones until decoded.
|
|
24
|
+
6. **Shape Memory**: Saves layouts under an `id` or auto-key to `localStorage` across user visits, ensuring second visits load instant skeleton shapes before network data resolves.
|
|
25
|
+
7. **SSR Pre-Paint Script**: Bundles a minimal inline script (`ResizeObserver`-driven) that measures server-rendered skeletons after layout and before the very first paint.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pnpm add @alikhalilll/a-smart-skeleton
|
|
33
|
+
# or
|
|
34
|
+
npm install @alikhalilll/a-smart-skeleton
|
|
35
|
+
# or
|
|
36
|
+
yarn add @alikhalilll/a-smart-skeleton
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Import styles in your main entry (if not using the Nuxt module):
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import '@alikhalilll/a-smart-skeleton/styles.css';
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Nuxt 3 / 4 Setup
|
|
48
|
+
|
|
49
|
+
Add the module to `nuxt.config.ts`:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
export default defineNuxtConfig({
|
|
53
|
+
modules: ['@alikhalilll/a-smart-skeleton/nuxt'],
|
|
54
|
+
aSmartSkeleton: {
|
|
55
|
+
prefix: 'A', // Optional prefix
|
|
56
|
+
styles: true, // Automatically injects styles.css
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Components auto-imported in Nuxt:
|
|
62
|
+
|
|
63
|
+
- `<ASmartSkeleton>`
|
|
64
|
+
- `<ASmartSkeletonBox>`
|
|
65
|
+
- `<ASmartSkeletonSuspense>`
|
|
66
|
+
- `<ASmartSkeletonProvider>`
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Vue 3 Usage
|
|
71
|
+
|
|
72
|
+
### Basic Data-Driven Skeleton
|
|
73
|
+
|
|
74
|
+
When `data` is `null` or `undefined`, the skeleton measures the `sample` data. Once `data` resolves, it smoothly reveals the real content.
|
|
75
|
+
|
|
76
|
+
```vue
|
|
77
|
+
<script setup lang="ts">
|
|
78
|
+
import { ref } from 'vue';
|
|
79
|
+
import { ASmartSkeleton } from '@alikhalilll/a-smart-skeleton';
|
|
80
|
+
|
|
81
|
+
interface User {
|
|
82
|
+
name: string;
|
|
83
|
+
role: string;
|
|
84
|
+
avatar: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const SAMPLE_USER: User = {
|
|
88
|
+
name: 'Alex Johnson',
|
|
89
|
+
role: 'Senior Software Engineer',
|
|
90
|
+
avatar: '',
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const user = ref<User | null>(null);
|
|
94
|
+
|
|
95
|
+
// Simulating fetch
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
user.value = {
|
|
98
|
+
name: 'Sarah Connor',
|
|
99
|
+
role: 'Cyberdyne Systems Lead',
|
|
100
|
+
avatar: 'https://example.com/avatar.jpg',
|
|
101
|
+
};
|
|
102
|
+
}, 1500);
|
|
103
|
+
</script>
|
|
104
|
+
|
|
105
|
+
<template>
|
|
106
|
+
<ASmartSkeleton :data="user" :sample="SAMPLE_USER" id="user-profile" label="Loading user profile">
|
|
107
|
+
<template #default="{ data }">
|
|
108
|
+
<div class="user-card">
|
|
109
|
+
<img :src="data.avatar" alt="Avatar" class="avatar" />
|
|
110
|
+
<div class="meta">
|
|
111
|
+
<h3>{{ data.name }}</h3>
|
|
112
|
+
<p>{{ data.role }}</p>
|
|
113
|
+
</div>
|
|
114
|
+
</div>
|
|
115
|
+
</template>
|
|
116
|
+
</ASmartSkeleton>
|
|
117
|
+
</template>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
### Reveal Modes
|
|
123
|
+
|
|
124
|
+
Select how content replaces its skeleton using the `reveal` prop:
|
|
125
|
+
|
|
126
|
+
```vue
|
|
127
|
+
<!-- Smart contextual reveal (default) -->
|
|
128
|
+
<ASmartSkeleton :data="user" :sample="SAMPLE_USER" reveal="smart">
|
|
129
|
+
...
|
|
130
|
+
</ASmartSkeleton>
|
|
131
|
+
|
|
132
|
+
<!-- Glossy sweep animation across the element -->
|
|
133
|
+
<ASmartSkeleton :data="user" :sample="SAMPLE_USER" reveal="sweep">
|
|
134
|
+
...
|
|
135
|
+
</ASmartSkeleton>
|
|
136
|
+
|
|
137
|
+
<!-- Top-to-bottom sequence cascade -->
|
|
138
|
+
<ASmartSkeleton :data="user" :sample="SAMPLE_USER" reveal="sequence">
|
|
139
|
+
...
|
|
140
|
+
</ASmartSkeleton>
|
|
141
|
+
|
|
142
|
+
<!-- Simple concurrent crossfade -->
|
|
143
|
+
<ASmartSkeleton :data="user" :sample="SAMPLE_USER" reveal="together">
|
|
144
|
+
...
|
|
145
|
+
</ASmartSkeleton>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Listen for when the reveal animation completes via `@reveal`:
|
|
149
|
+
|
|
150
|
+
```vue
|
|
151
|
+
<ASmartSkeleton :data="user" :sample="SAMPLE_USER" @reveal="onRevealed">
|
|
152
|
+
...
|
|
153
|
+
</ASmartSkeleton>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
### States: Empty & Error
|
|
159
|
+
|
|
160
|
+
Handle error and empty states directly inside the skeleton boundary:
|
|
161
|
+
|
|
162
|
+
```vue
|
|
163
|
+
<ASmartSkeleton :data="items" :empty="items?.length === 0" :error="error">
|
|
164
|
+
<template #default="{ data }">
|
|
165
|
+
<ItemList :items="data" />
|
|
166
|
+
</template>
|
|
167
|
+
|
|
168
|
+
<template #empty>
|
|
169
|
+
<p>No items found in this collection.</p>
|
|
170
|
+
</template>
|
|
171
|
+
|
|
172
|
+
<template #error="{ error }">
|
|
173
|
+
<p class="error">Failed to load items: {{ error }}</p>
|
|
174
|
+
</template>
|
|
175
|
+
</ASmartSkeleton>
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
### Standalone Skeleton Box
|
|
181
|
+
|
|
182
|
+
For manual placeholder boxes, use `<ASmartSkeletonBox>`:
|
|
183
|
+
|
|
184
|
+
```vue
|
|
185
|
+
<template>
|
|
186
|
+
<div class="card">
|
|
187
|
+
<ASmartSkeletonBox width="100%" height="200px" radius="8px" />
|
|
188
|
+
<ASmartSkeletonBox width="70%" height="1.2em" radius="4px" />
|
|
189
|
+
<ASmartSkeletonBox width="40%" height="1em" radius="4px" />
|
|
190
|
+
</div>
|
|
191
|
+
</template>
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
### Suspense Integration
|
|
197
|
+
|
|
198
|
+
`<ASmartSkeletonSuspense>` handles Vue's `<Suspense>` boundary lifecycle, keeping an exact clone of the fallback skeleton in place to crossfade smoothly once async setup finishes:
|
|
199
|
+
|
|
200
|
+
```vue
|
|
201
|
+
<template>
|
|
202
|
+
<ASmartSkeletonSuspense id="async-quote" reveal="smart">
|
|
203
|
+
<AsyncQuoteComponent />
|
|
204
|
+
<template #skeleton>
|
|
205
|
+
<QuoteCard :quote="SAMPLE_QUOTE" />
|
|
206
|
+
</template>
|
|
207
|
+
</ASmartSkeletonSuspense>
|
|
208
|
+
</template>
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
### Global Provider & Theming
|
|
214
|
+
|
|
215
|
+
Configure colors, speed, animations, font metric edges, and CSP nonce across your whole app:
|
|
216
|
+
|
|
217
|
+
```vue
|
|
218
|
+
<template>
|
|
219
|
+
<ASmartSkeletonProvider
|
|
220
|
+
mode="system"
|
|
221
|
+
:theme="{
|
|
222
|
+
light: { base: 'rgb(0 0 0 / 0.07)', highlight: 'rgb(255 255 255 / 0.6)' },
|
|
223
|
+
dark: { base: 'rgb(255 255 255 / 0.08)', highlight: 'rgb(255 255 255 / 0.1)' },
|
|
224
|
+
radius: '6px',
|
|
225
|
+
animation: 'shimmer',
|
|
226
|
+
speed: 'slow',
|
|
227
|
+
text: { top: 'ascender', bottom: 'descender' },
|
|
228
|
+
}"
|
|
229
|
+
reveal="smart"
|
|
230
|
+
:debug="false"
|
|
231
|
+
>
|
|
232
|
+
<App />
|
|
233
|
+
</ASmartSkeletonProvider>
|
|
234
|
+
</template>
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## Web Components / Custom Element
|
|
240
|
+
|
|
241
|
+
Use the `<a-smart-skeleton-reveal>` custom element in standard HTML without Vue:
|
|
242
|
+
|
|
243
|
+
```html
|
|
244
|
+
<script type="module" src="@alikhalilll/a-smart-skeleton/element"></script>
|
|
245
|
+
|
|
246
|
+
<a-smart-skeleton-reveal loading id="article-preview" reveal="smart">
|
|
247
|
+
<article>
|
|
248
|
+
<h2>Article Title Headline</h2>
|
|
249
|
+
<p>Detailed body paragraph explaining the context of the article.</p>
|
|
250
|
+
</article>
|
|
251
|
+
</a-smart-skeleton-reveal>
|
|
252
|
+
|
|
253
|
+
<script>
|
|
254
|
+
const el = document.querySelector('a-smart-skeleton-reveal');
|
|
255
|
+
setTimeout(() => {
|
|
256
|
+
el.show(`
|
|
257
|
+
<article>
|
|
258
|
+
<h2>Real Fetched Title</h2>
|
|
259
|
+
<p>This is the real body text loaded over the wire.</p>
|
|
260
|
+
</article>
|
|
261
|
+
`);
|
|
262
|
+
}, 2000);
|
|
263
|
+
</script>
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## Author Hints & Attributes
|
|
269
|
+
|
|
270
|
+
Customize measuring behavior with attributes:
|
|
271
|
+
|
|
272
|
+
- `ask-ignore` / `[ask-ignore]`: Skip an element and its children during measurement.
|
|
273
|
+
- `ask-block` / `[ask-block]`: Force an element to be measured as a solid rectangle block rather than text lines.
|
|
274
|
+
- `ask-own` / `[ask-own]`: Subtree provides its own custom skeleton bones; measuring stops at this boundary.
|
|
275
|
+
- `ask-xray` / `[ask-xray]`: Debug mode that exposes the ghost tree through translucent bones.
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
## CSS Variables
|
|
280
|
+
|
|
281
|
+
| Variable | Default | Description |
|
|
282
|
+
| ------------------- | ------------------------- | --------------------------------------------------------------- |
|
|
283
|
+
| `--ask-base` | `rgb(128 128 128 / 0.2)` | Base bone background color |
|
|
284
|
+
| `--ask-highlight` | `rgb(255 255 255 / 0.12)` | Shimmer sweep highlight color |
|
|
285
|
+
| `--ask-radius` | `0.25em` | Default border-radius for text lines and standalone boxes |
|
|
286
|
+
| `--ask-animation` | `shimmer` | Animation: `shimmer`, `group-shimmer`, `pulse`, `none` |
|
|
287
|
+
| `--ask-speed` | `slow` | Animation speed: `slow`, `fast` |
|
|
288
|
+
| `--ask-text-top` | `ascender` | Top typographic boundary (`ascender`, `cap-height`, `x-height`) |
|
|
289
|
+
| `--ask-text-bottom` | `descender` | Bottom typographic boundary (`baseline`, `descender`) |
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
MIT © [alikhalilll](https://github.com/alikhalilll)
|