@auto-skeleton/react 0.0.2 → 0.0.3
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 +193 -0
- package/package.json +4 -3
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# auto-skeleton
|
|
2
|
+
|
|
3
|
+
Zero-config skeleton loaders for React. Wraps any component and auto-generates a pixel-accurate skeleton by scanning the real DOM — no manual shape definitions needed.
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
<AutoSkeleton id="profile" loading={isLoading}>
|
|
7
|
+
<ProfileCard />
|
|
8
|
+
</AutoSkeleton>
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
When `loading` is true, the real UI is replaced with a skeleton that mirrors its exact layout.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Packages
|
|
16
|
+
|
|
17
|
+
| Package | Description |
|
|
18
|
+
|---|---|
|
|
19
|
+
| [`@auto-skeleton/react`](https://www.npmjs.com/package/@auto-skeleton/react) | React component and hook |
|
|
20
|
+
| [`@auto-skeleton/core`](https://www.npmjs.com/package/@auto-skeleton/core) | Framework-agnostic DOM scanner |
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @auto-skeleton/react
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { AutoSkeleton } from "@auto-skeleton/react";
|
|
36
|
+
import "@auto-skeleton/react/styles.css";
|
|
37
|
+
|
|
38
|
+
function UserProfile({ userId }: { userId: string }) {
|
|
39
|
+
const { data, isLoading } = useUser(userId);
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<AutoSkeleton id="user-profile" loading={isLoading}>
|
|
43
|
+
<div className="profile">
|
|
44
|
+
<img src={data?.avatar} alt="avatar" />
|
|
45
|
+
<h2>{data?.name}</h2>
|
|
46
|
+
<p>{data?.bio}</p>
|
|
47
|
+
</div>
|
|
48
|
+
</AutoSkeleton>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
On first load, `auto-skeleton` scans the rendered DOM, extracts bone positions, and caches them. Every subsequent load shows the skeleton immediately — no layout shift.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## How it works
|
|
58
|
+
|
|
59
|
+
1. On the first non-loading render, the DOM inside `<AutoSkeleton>` is scanned using a `TreeWalker`
|
|
60
|
+
2. Each visible element is measured with `getBoundingClientRect` and classified as a rect, circle, or text block
|
|
61
|
+
3. Bone positions are stored in memory and `sessionStorage` keyed by `id` + viewport width
|
|
62
|
+
4. When `loading` switches back to `true`, an absolutely-positioned overlay renders the cached bones
|
|
63
|
+
5. The real content is hidden (`visibility: hidden`) so the layout is preserved during loading
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Props
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
<AutoSkeleton
|
|
71
|
+
id="unique-id" // required — used as the cache key
|
|
72
|
+
loading={boolean} // required — controls when the skeleton shows
|
|
73
|
+
className="..." // optional — added to the root wrapper div
|
|
74
|
+
options={...} // optional — see options table below
|
|
75
|
+
>
|
|
76
|
+
{children}
|
|
77
|
+
</AutoSkeleton>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Options
|
|
81
|
+
|
|
82
|
+
| Option | Type | Default | Description |
|
|
83
|
+
|---|---|---|---|
|
|
84
|
+
| `animation` | `"wave" \| "pulse" \| "none"` | `"wave"` | Skeleton animation style |
|
|
85
|
+
| `debug` | `boolean` | `false` | Outline detected bones with dashed borders |
|
|
86
|
+
| `watch` | `boolean` | `true` | Re-scan when layout changes while not loading |
|
|
87
|
+
| `watchDebounceMs` | `number` | `120` | Debounce delay for watcher-triggered re-scans |
|
|
88
|
+
| `cache` | `boolean` | `true` | Cache bones in memory and `sessionStorage` |
|
|
89
|
+
| `minSize` | `number` | `8` | Minimum element size in px to generate a bone for |
|
|
90
|
+
| `ignoreSelectors` | `string[]` | `[]` | CSS selectors for elements to skip during scan |
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Data attribute overrides
|
|
95
|
+
|
|
96
|
+
Fine-tune how individual elements are scanned without changing render logic:
|
|
97
|
+
|
|
98
|
+
| Attribute | Effect |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `data-skeleton-ignore` | Skip this element entirely |
|
|
101
|
+
| `data-skeleton-shape="circle"` | Force a circular bone |
|
|
102
|
+
| `data-skeleton-shape="rect"` | Force a rectangular bone |
|
|
103
|
+
| `data-skeleton-lines="N"` | Force N text lines instead of auto-detecting |
|
|
104
|
+
| `data-skeleton-container` | Treat as a container — scan children instead of creating one large bone |
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
<AutoSkeleton id="profile" loading={loading}>
|
|
108
|
+
<article data-skeleton-container>
|
|
109
|
+
<img src={avatar} data-skeleton-shape="circle" />
|
|
110
|
+
<h2 data-skeleton-lines="1">{name}</h2>
|
|
111
|
+
<p data-skeleton-lines="3">{bio}</p>
|
|
112
|
+
<span data-skeleton-ignore>Live</span>
|
|
113
|
+
</article>
|
|
114
|
+
</AutoSkeleton>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Theming
|
|
120
|
+
|
|
121
|
+
Override CSS variables to match your design system:
|
|
122
|
+
|
|
123
|
+
```css
|
|
124
|
+
:root {
|
|
125
|
+
--as-base: #e4e4e7; /* bone background */
|
|
126
|
+
--as-highlight: rgba(255, 255, 255, 0.9); /* wave shimmer */
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Accessibility
|
|
133
|
+
|
|
134
|
+
- The skeleton overlay is `aria-hidden="true"` — invisible to screen readers
|
|
135
|
+
- Animations are automatically disabled when `prefers-reduced-motion: reduce` is set
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## Debug mode
|
|
140
|
+
|
|
141
|
+
Set `options.debug = true` to visualize detected bones with dashed red outlines while tuning your layout:
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
<AutoSkeleton id="card" loading={loading} options={{ debug: true }}>
|
|
145
|
+
<Card />
|
|
146
|
+
</AutoSkeleton>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Using the core scanner directly
|
|
152
|
+
|
|
153
|
+
For non-React environments, `@auto-skeleton/core` exposes the scanner:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { scanBones } from "@auto-skeleton/core";
|
|
157
|
+
|
|
158
|
+
const bones = scanBones(document.getElementById("root"), {
|
|
159
|
+
minSize: 8,
|
|
160
|
+
ignoreSelectors: [".no-skeleton"]
|
|
161
|
+
});
|
|
162
|
+
// bones: Array<{ x, y, width, height, radius, kind }>
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Browser support
|
|
168
|
+
|
|
169
|
+
Latest 2 versions of Chrome, Edge, Firefox, and Safari (macOS + iOS).
|
|
170
|
+
|
|
171
|
+
Requires `MutationObserver`, `ResizeObserver`, and `sessionStorage`. Falls back to `window resize` events if `ResizeObserver` is unavailable.
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## Contributing
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
git clone https://github.com/riazzahmedm/react-auto-skeleton
|
|
179
|
+
cd react-auto-skeleton
|
|
180
|
+
npm install
|
|
181
|
+
npm run demo:dev # start the demo
|
|
182
|
+
npm test # run tests
|
|
183
|
+
npm run build # build all packages
|
|
184
|
+
npm run perf:scan # run performance benchmark (budget: <80ms avg)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
To release:
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
npm run changeset # describe your changes
|
|
191
|
+
npm run version-packages # bump versions + generate changelogs
|
|
192
|
+
npm run release # publish to npm
|
|
193
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auto-skeleton/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "dist/index.cjs",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"./styles.css": "./dist/styles.css"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
|
-
"dist"
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
17
18
|
],
|
|
18
19
|
"sideEffects": [
|
|
19
20
|
"**/*.css"
|
|
@@ -27,6 +28,6 @@
|
|
|
27
28
|
"react-dom": ">=18"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"@auto-skeleton/core": "0.0.
|
|
31
|
+
"@auto-skeleton/core": "0.0.3"
|
|
31
32
|
}
|
|
32
33
|
}
|