skeleton-load 0.0.0-stage → 0.1.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.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +455 -2
  3. package/dist/cli.js +255 -0
  4. package/dist/index.cjs +1347 -0
  5. package/dist/index.cjs.map +1 -0
  6. package/dist/index.d.cts +12 -0
  7. package/dist/index.d.ts +12 -0
  8. package/dist/index.js +1338 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/next/index.cjs +425 -0
  11. package/dist/next/index.cjs.map +1 -0
  12. package/dist/next/index.d.cts +19 -0
  13. package/dist/next/index.d.ts +19 -0
  14. package/dist/next/index.js +420 -0
  15. package/dist/next/index.js.map +1 -0
  16. package/dist/plugins/vite.cjs +413 -0
  17. package/dist/plugins/vite.cjs.map +1 -0
  18. package/dist/plugins/vite.d.cts +40 -0
  19. package/dist/plugins/vite.d.ts +40 -0
  20. package/dist/plugins/vite.js +408 -0
  21. package/dist/plugins/vite.js.map +1 -0
  22. package/dist/plugins/webpack.cjs +411 -0
  23. package/dist/plugins/webpack.cjs.map +1 -0
  24. package/dist/plugins/webpack.d.cts +43 -0
  25. package/dist/plugins/webpack.d.ts +43 -0
  26. package/dist/plugins/webpack.js +406 -0
  27. package/dist/plugins/webpack.js.map +1 -0
  28. package/dist/react/index.cjs +1387 -0
  29. package/dist/react/index.cjs.map +1 -0
  30. package/dist/react/index.d.cts +48 -0
  31. package/dist/react/index.d.ts +48 -0
  32. package/dist/react/index.js +1382 -0
  33. package/dist/react/index.js.map +1 -0
  34. package/dist/singleton-D0608W4u.d.cts +74 -0
  35. package/dist/singleton-D4bvf9fp.d.ts +74 -0
  36. package/dist/skeleton-load.auto.js +328 -0
  37. package/dist/skeleton-load.css +326 -0
  38. package/dist/skeleton-load.record.js +21 -0
  39. package/dist/types-DNYMIyOJ.d.cts +73 -0
  40. package/dist/types-DNYMIyOJ.d.ts +73 -0
  41. package/dist/vanilla.cjs +1356 -0
  42. package/dist/vanilla.cjs.map +1 -0
  43. package/dist/vanilla.d.cts +23 -0
  44. package/dist/vanilla.d.ts +23 -0
  45. package/dist/vanilla.js +1353 -0
  46. package/dist/vanilla.js.map +1 -0
  47. package/dist/vue/index.cjs +1379 -0
  48. package/dist/vue/index.cjs.map +1 -0
  49. package/dist/vue/index.d.cts +145 -0
  50. package/dist/vue/index.d.ts +145 -0
  51. package/dist/vue/index.js +1373 -0
  52. package/dist/vue/index.js.map +1 -0
  53. package/package.json +104 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 youss
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,456 @@
1
- # Temporary Holding Version
1
+ <div align="center">
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ # skeleton-load
4
+
5
+ **Loading states that look like the page they replace.**
6
+
7
+ One script tag. Every image and video gets a skeleton automatically. Everything else takes one attribute.
8
+
9
+ `~8 kB gzip` · `zero dependencies` · `TypeScript` · `MIT`
10
+
11
+ </div>
12
+
13
+ ---
14
+
15
+ ## Why
16
+
17
+ Most skeleton libraries ask you to rebuild your layout as grey boxes by hand, and most of them still shift when the real content lands.
18
+
19
+ `skeleton-load` takes the opposite approach:
20
+
21
+ - **Media is automatic.** Every `<img>`, `<video>` and `<iframe>` is detected, covered with a shimmer, and its exact box is preserved — so the page never jumps when the file arrives.
22
+ - **Containers take one attribute.** Mark a region with `data-skeleton`, and the engine paints the right shape inside it.
23
+ - **No guessing.** It never invents skeletons for content that already exists. You stay in control.
24
+ - **Timing is designed.** Fast loads never flash (120 ms delay), short skeletons stay on screen long enough to read (300 ms minimum), and there is a safety valve so nothing can hang forever.
25
+
26
+ ---
27
+
28
+ ## Demo
29
+
30
+ The repository ships with a full demo: product grid, dashboard, editorial page and an SPA with client-side navigation.
31
+
32
+ ```bash
33
+ git clone <repo>
34
+ cd skeleton-load
35
+ npm install
36
+ npm run build
37
+ npm run demo # http://localhost:4173
38
+ ```
39
+
40
+ The demo server intentionally delays its images (`/demo/slow/:id`) so you can watch the skeletons work.
41
+
42
+ ---
43
+
44
+ ## Install
45
+
46
+ ```bash
47
+ npm install skeleton-load
48
+ ```
49
+
50
+ Or use the CDN build with no build step:
51
+
52
+ ```html
53
+ <script src="https://cdn.jsdelivr.net/npm/skeleton-load/dist/skeleton-load.auto.js" defer></script>
54
+ ```
55
+
56
+ That single line starts the engine on `DOMContentLoaded` and covers every image, video and iframe on the page.
57
+
58
+ ---
59
+
60
+ ## The whole integration, in three lines
61
+
62
+ ```html
63
+ <script src="https://cdn.jsdelivr.net/npm/skeleton-load/dist/skeleton-load.auto.js" defer></script>
64
+
65
+ <main data-skeleton="auto">
66
+ <img src="/hero.jpg" alt="Hero"> <!-- automatic -->
67
+ <section id="products"></section> <!-- skeleton via auto -->
68
+ </main>
69
+ ```
70
+
71
+ ### What is automatic
72
+
73
+ | Element | What happens |
74
+ | --- | --- |
75
+ | `<img>` | Shimmer appears over the image box until `load` / `error`. |
76
+ | `<video>` | Shimmer until the first frame is ready. |
77
+ | `<iframe>` | Shimmer until the frame finishes loading. |
78
+
79
+ Lazy media (`loading="lazy"`) is only painted when it is close to the viewport.
80
+
81
+ ### What needs an attribute
82
+
83
+ Anything that is not media — cards, product grids, tables, text, charts — gets a deterministic shape from one attribute:
84
+
85
+ ```html
86
+ <div id="products" data-skeleton="grid" data-skeleton-cols="3" data-skeleton-count="6"></div>
87
+ ```
88
+
89
+ ---
90
+
91
+ ## Element API
92
+
93
+ ### Shapes
94
+
95
+ | `data-skeleton` | Result |
96
+ | --- | --- |
97
+ | `auto` | Mirrors the real children of the container, or falls back to stacked blocks when empty. Best default. |
98
+ | `card` | Media block + title + two lines + footer. |
99
+ | `grid` | A grid of card cells. Pair with `data-skeleton-cols` and `data-skeleton-count`. |
100
+ | `list` | Rows with an avatar and two lines. Pair with `data-skeleton-count`. |
101
+ | `text` | N paragraphs of lines. Pair with `data-skeleton-lines`. |
102
+ | `table` | Faithful table: header, avatar + two-line owner cell, three data cells. |
103
+ | `media` | A single aspect-ratio media block. |
104
+ | `avatar` | A centred circle. |
105
+ | `chart` | A bar chart silhouette. |
106
+ | `profile` | Avatar + two centred lines. |
107
+ | `off` | Opt the subtree out completely. |
108
+
109
+ ### Attributes
110
+
111
+ | Attribute | Applies to | Description |
112
+ | --- | --- | --- |
113
+ | `data-skeleton="<shape>"` | any element | The shape to paint. `""` / `"true"` / `"1"` mean `auto`. |
114
+ | `data-skeleton-state="loading"` | any element | Force the skeleton on, even if the element has content. |
115
+ | `data-skeleton-state="done"` | any element | Reveal immediately and remove the skeleton. |
116
+ | `data-skeleton-preset="shimmer\|pulse\|static"` | any element | Override the animation for this subtree. Inherited. |
117
+ | `data-skeleton-theme="light\|dark\|auto"` | any element | Override tone detection. |
118
+ | `data-skeleton-lines="3"` | text / list | Number of lines (1–12). |
119
+ | `data-skeleton-count="6"` | grid / list / table | Number of repeated rows or cells (1–24). |
120
+ | `data-skeleton-cols="3"` | grid | Grid columns (1–6). |
121
+ | `data-skeleton-ratio="16/9"` | card / grid / media | Aspect ratio of the media block. |
122
+ | `data-skeleton-h="250"` | any element | Reserve a minimum height in px while loading. |
123
+ | `data-skeleton-radius="12"` | any element | Override the placeholder corner radius in px. |
124
+ | `data-skeleton-depth="2"` | auto | Mirror two levels of children instead of one. |
125
+ | `data-skeleton-skip` | any element | Never skeleton this element or its subtree. |
126
+ | `data-skeleton-lazy="true"` | media | Treat the media as lazy even without `loading="lazy"`. |
127
+
128
+ ### Driving it from your own state
129
+
130
+ The attribute you set is the attribute the engine listens to. Practically, that means a single boolean in your app maps to a single attribute:
131
+
132
+ ```html
133
+ <div id="panel" data-skeleton="card" data-skeleton-state="loading"></div>
134
+ ```
135
+
136
+ ```js
137
+ const panel = document.getElementById('panel')
138
+ panel.setAttribute('data-skeleton-state', data ? 'done' : 'loading')
139
+ ```
140
+
141
+ The engine observes the attribute. No `refresh()` call is required.
142
+
143
+ ---
144
+
145
+ ## JavaScript API
146
+
147
+ ```js
148
+ import { startSkeleton, skeletonLoad } from 'skeleton-load'
149
+
150
+ startSkeleton({ preset: 'pulse', delay: 100 })
151
+
152
+ // later
153
+ skeletonLoad.hide(element)
154
+ skeletonLoad.refresh(newSection)
155
+ skeletonLoad.setPreset('static')
156
+ ```
157
+
158
+ Or take full control with an isolated instance:
159
+
160
+ ```js
161
+ import { createSkeletonLoad } from 'skeleton-load'
162
+
163
+ const engine = createSkeletonLoad({
164
+ preset: 'shimmer',
165
+ delay: 120,
166
+ onShow: (el) => console.log('skeleton on', el),
167
+ })
168
+
169
+ engine.start()
170
+ engine.show(document.querySelector('#zone'))
171
+ engine.hide(document.querySelector('#zone'))
172
+ engine.stop()
173
+ engine.destroy()
174
+ ```
175
+
176
+ ### `SkeletonOptions`
177
+
178
+ | Option | Default | Description |
179
+ | --- | --- | --- |
180
+ | `preset` | `"shimmer"` | `shimmer`, `pulse` or `static`. |
181
+ | `theme` | `"auto"` | `auto` derives the tone from the nearest opaque background; `light` / `dark` force it. |
182
+ | `delay` | `120` | Don't paint for loads faster than this (ms). Prevents flashing. |
183
+ | `minVisible` | `300` | Once painted, keep it visible at least this long (ms). |
184
+ | `maxWait` | `5000` | Safety net; force the skeleton to end after this long. `0` disables. |
185
+ | `autoMedia` | `true` | Automatically skeleton `<img>`, `<video>` and `<iframe>`. |
186
+ | `observe` | `true` | Watch the DOM for late content and SPA navigation. |
187
+ | `css` | `true` | Inject the stylesheet. Set `false` if you import `skeleton-load/styles.css`. |
188
+ | `nonce` | `""` | CSP nonce for the injected `<style>` tag. |
189
+ | `root` | `document` | Scan a subtree instead of the whole document. |
190
+ | `debug` | `false` | Outline every skeleton and log matches. |
191
+ | `respectReducedMotion` | `true` | Honour `prefers-reduced-motion` and `prefers-reduced-data`. |
192
+ | `onShow` | — | Called when a skeleton becomes visible. |
193
+ | `onHide` | — | Called when a skeleton is removed. |
194
+
195
+ You can also configure the auto build from the page:
196
+
197
+ ```html
198
+ <script>
199
+ window.SKELETON_LOAD_CONFIG = { preset: 'pulse', delay: 150 }
200
+ </script>
201
+ <script src="/dist/skeleton-load.auto.js" defer></script>
202
+ ```
203
+
204
+ Set `autoStart: false` to load the runtime without starting it.
205
+
206
+ ---
207
+
208
+ ## React
209
+
210
+ ```tsx
211
+ import { Skeleton } from 'skeleton-load/react'
212
+
213
+ export function Product({ product, loading }: Props) {
214
+ return (
215
+ <Skeleton loading={loading} shape="card" ratio="4/3" height={320}>
216
+ <article className="product">
217
+ <img src={product.image} alt={product.name} />
218
+ <h3>{product.name}</h3>
219
+ </article>
220
+ </Skeleton>
221
+ )
222
+ }
223
+ ```
224
+
225
+ | Prop | Type | Description |
226
+ | --- | --- | --- |
227
+ | `loading` | `boolean` | When `true`, the skeleton is shown. |
228
+ | `shape` | `Shape` | Defaults to `auto`. |
229
+ | `preset` | `Preset` | Per-element animation. |
230
+ | `lines` / `ratio` / `height` | `number` / `string` / `number` | Same as the data attributes. |
231
+ | `as` | `ElementType` | Rendered element. Defaults to `div`. |
232
+
233
+ `useSkeleton(loading, options)` returns the raw attributes when you don't want a wrapper:
234
+
235
+ ```tsx
236
+ import { useSkeleton } from 'skeleton-load/react'
237
+
238
+ function Panel({ loading, children }) {
239
+ const attrs = useSkeleton(loading, { shape: 'list', lines: 4 })
240
+ return <section {...attrs}>{children}</section>
241
+ }
242
+ ```
243
+
244
+ `startSkeleton()` is available if you want to boot the engine without rendering a `Skeleton`.
245
+
246
+ ---
247
+
248
+ ## Next.js
249
+
250
+ The React adapter is all you need — the engine boots on mount and injects the stylesheet itself:
251
+
252
+ ```tsx
253
+ // app/products/page.tsx
254
+ 'use client'
255
+ import { Skeleton } from 'skeleton-load/react'
256
+
257
+ export default function Products({ products, loading }: Props) {
258
+ return (
259
+ <Skeleton loading={loading} shape="grid" ratio="4/3" height={360}>
260
+ {products.map((p) => <ProductCard key={p.id} product={p} />)}
261
+ </Skeleton>
262
+ )
263
+ }
264
+ ```
265
+
266
+ For static exports or custom HTML pipelines you can also wrap the config:
267
+
268
+ ```js
269
+ // next.config.mjs
270
+ import { withSkeleton } from 'skeleton-load/next'
271
+
272
+ export default withSkeleton({}, { preset: 'shimmer', delay: 120 })
273
+ ```
274
+
275
+ > `withSkeleton` injects the runtime into HTML emitted through webpack. On the App Router, prefer the component above — it works with streaming and does not depend on the HTML build step.
276
+
277
+ ---
278
+
279
+ ## Vue / Nuxt
280
+
281
+ ```ts
282
+ // main.ts
283
+ import { createApp } from 'vue'
284
+ import { SkeletonLoad } from 'skeleton-load/vue'
285
+ import App from './App.vue'
286
+
287
+ createApp(App).use(SkeletonLoad).mount('#app')
288
+ ```
289
+
290
+ ```vue
291
+ <template>
292
+ <Skeleton :loading="pending" shape="grid" :lines="3" :height="280">
293
+ <ProductCard v-for="p in products" :key="p.id" :product="p" />
294
+ </Skeleton>
295
+ </template>
296
+ ```
297
+
298
+ Props: `loading`, `shape`, `preset`, `lines`, `ratio`, `height`, `tag`.
299
+
300
+ For Nuxt, register the plugin client-side (`plugins/skeleton.client.ts`) so the engine never runs during SSR.
301
+
302
+ ---
303
+
304
+ ## Vite
305
+
306
+ ```ts
307
+ // vite.config.ts
308
+ import { defineConfig } from 'vite'
309
+ import { skeletonLoad } from 'skeleton-load/vite'
310
+
311
+ export default defineConfig({
312
+ plugins: [
313
+ skeletonLoad({
314
+ preset: 'shimmer',
315
+ delay: 120,
316
+ autoMark: true,
317
+ }),
318
+ ],
319
+ })
320
+ ```
321
+
322
+ The plugin injects the critical CSS into `index.html`, inlines the runtime, and adds the config object. Options: `preset`, `theme`, `delay`, `minVisible`, `maxWait`, `autoMedia`, `observe`, `debug`, `runtime` (`"inline" | "external" | "none"`), `src`, `css`, `nonce`, `autoMark`.
323
+
324
+ ---
325
+
326
+ ## Webpack
327
+
328
+ ```js
329
+ // webpack.config.js
330
+ const { SkeletonLoadPlugin } = require('skeleton-load/webpack')
331
+
332
+ module.exports = {
333
+ plugins: [new SkeletonLoadPlugin({ preset: 'shimmer', autoMark: true })],
334
+ }
335
+ ```
336
+
337
+ It hooks `processAssets` and injects the same snippet into every emitted `.html` asset. Idempotent.
338
+
339
+ ---
340
+
341
+ ## CSS
342
+
343
+ Import the stylesheet once if you disabled runtime injection:
344
+
345
+ ```js
346
+ import 'skeleton-load/styles.css'
347
+ ```
348
+
349
+ ### Presets
350
+
351
+ | Preset | Look |
352
+ | --- | --- |
353
+ | `shimmer` | A highlight sweeps across every block. Default. |
354
+ | `pulse` | Blocks fade in and out together. |
355
+ | `static` | No animation; a calm grey block. |
356
+
357
+ ### Design tokens
358
+
359
+ Every visual is driven by CSS variables on `.skl-overlay`, so you can retheme without touching the JS:
360
+
361
+ ```css
362
+ .skl-overlay {
363
+ --skl-base: rgba(17, 20, 28, 0.1);
364
+ --skl-highlight: rgba(255, 255, 255, 0.72);
365
+ --skl-surface: #ffffff;
366
+ --skl-border: rgba(17, 20, 28, 0.08);
367
+ --skl-line: rgba(17, 20, 28, 0.08);
368
+ --skl-solid: #f1f1f6;
369
+ --skl-radius: 8px;
370
+ --skl-gap: 10px;
371
+ }
372
+
373
+ /* Or scope it to one region */
374
+ .dark-panel .skl-overlay {
375
+ --skl-base: rgba(255, 255, 255, 0.12);
376
+ --skl-highlight: rgba(255, 255, 255, 0.24);
377
+ }
378
+ ```
379
+
380
+ The engine detects whether the surface behind an element is light or dark and sets `data-skl-tone="dark"` automatically, so skeletons stay visible on dark themes without configuration.
381
+
382
+ ---
383
+
384
+ ## CLI
385
+
386
+ ```bash
387
+ npx skeleton-load init
388
+ npx skeleton-load record http://localhost:3000
389
+ npx skeleton-load inject --dir dist
390
+ ```
391
+
392
+ - **`init`** detects Next, Vite, Nuxt, React or plain HTML and prints the exact setup for your project.
393
+ - **`record`** starts a tiny dev server; paste one line into DevTools, then click any element on your page to copy the right `data-skeleton` snippet. It works on any site, including production.
394
+ - **`inject`** adds the runtime and CSS to every built `.html` file. Idempotent — safe to re-run after a rebuild.
395
+
396
+ ---
397
+
398
+ ## How it avoids layout shift
399
+
400
+ 1. Media keeps its intrinsic box. The overlay is positioned over the `<img>` and never affects layout.
401
+ 2. Empty containers are measured: the engine paints the skeleton off-screen, reads its natural height, and reserves it as `min-height` on the host. When the content arrives, the reserved height is removed and the reveal happens in place.
402
+ 3. Content is hidden with `visibility`, not `display`, so the layout is already correct while the skeleton plays.
403
+ 4. `delay` prevents a skeleton from flashing on warm caches; `minVisible` prevents a jarring sub-frame appearance.
404
+
405
+ Measured in the bundled demo: shop `CLS ≈ 0.019`, dashboard `CLS ≈ 0.011` — both well under the "good" threshold of 0.1.
406
+
407
+ ---
408
+
409
+ ## Accessibility & performance
410
+
411
+ - Overlays are `aria-hidden` and `pointer-events: none`; screen readers never see them.
412
+ - `prefers-reduced-motion: reduce` disables the shimmer and the reveal animation.
413
+ - `prefers-reduced-data: reduce` drops the shimmer gradient.
414
+ - Animations pause when the tab is hidden.
415
+ - `forced-colors` mode falls back to `CanvasText`.
416
+ - Nothing runs until the DOM is ready; the engine is a single shared instance (`Symbol.for('skeleton-load:default')`), so multiple bundles on one page never double-paint.
417
+
418
+ ## Browser support
419
+
420
+ Chrome/Edge 88+, Firefox 89+, Safari 15.4+ (anything with `color-mix` and `ResizeObserver`). Older browsers get a flat grey skeleton instead of a gradient.
421
+
422
+ ---
423
+
424
+ ## Development
425
+
426
+ ```bash
427
+ npm install
428
+ npm run build # prebuild → tsup → postbuild (IIFE + CLI + CSS)
429
+ npm test # 16 engine tests in jsdom
430
+ npm run typecheck
431
+ npm run size
432
+ npm run demo
433
+ ```
434
+
435
+ Architecture:
436
+
437
+ - `src/core/` — engine, config, shape builders, singleton, generated CSS.
438
+ - `src/react`, `src/vue`, `src/next` — thin adapters over the same engine.
439
+ - `src/plugins/` — Vite/Webpack injection; they embed a prebuilt IIFE runtime.
440
+ - `src/cli/` — `init`, `inject`, `record`.
441
+ - `demo/` — the four-page demo and a static server with artificial latency.
442
+
443
+ Build order matters: `scripts/prebuild.mjs` generates `src/core/css.generated.ts` and `src/plugins/runtime.generated.ts` before `tsup` compiles.
444
+
445
+ ## Publishing
446
+
447
+ ```bash
448
+ npm login
449
+ npm publish
450
+ ```
451
+
452
+ `prepublishOnly` runs the build and the test suite first.
453
+
454
+ ## License
455
+
456
+ MIT © youss