react-auto-skeleton-magic
v1.0.0
Published
Automatic skeleton loader generator for React
Maintainers
Readme
react-auto-skeleton-loader
A zero-config, "magic" skeleton loader generator for React. Wrap your component, and it automatically generates a skeleton layout matching your actual UI structure.
📦 Installation
npm install react-auto-skeleton-loader
# or
yarn add react-auto-skeleton-loader🚀 Usage
- Import the component and styles.
- Wrap your target component with
<AutoSkeleton>. - Pass
loading={true}to see the skeleton.
Important: Your component must render its layout structure (even with dummy data/text) for the skeleton to measure it. The library makes the content invisible and draws skeletons over it.
import { AutoSkeleton } from 'react-auto-skeleton-loader';
import 'react-auto-skeleton-loader/dist/style.css';
function UserProfile({ isLoading, user }) {
// Even when loading, render the layout!
// Use safe checks (user?.name) or dummy data.
return (
<AutoSkeleton loading={isLoading}>
<div className="card">
<img
src={user?.avatar || '/placeholder.png'}
className="avatar"
alt="avatar"
/>
<h3>{user?.name || 'User Name'}</h3>
<p>{user?.bio || 'Short user bio goes here...'}</p>
<button>Follow</button>
</div>
</AutoSkeleton>
);
}⚙️ Props
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| loading | boolean | Required | When true, hides children and shows skeleton overlay. |
| children| ReactNode| Required | The component layout to analyze. |
| animate | boolean | true | Enable/disable shimmer animation. |
🛠 How it Works
- Wraps your children in a relative container.
- Hides the children using
visibility: hiddenopacity 0 (so they retain layout size). - Analyzes the DOM using
BoundingClientRectto find images, headings, buttons, and text blocks. - Overlays absolute positioned skeleton blocks that match the exact geometry of your elements.
- Updates automatically if the layout changes (via
ResizeObserverandMutationObserver).
⚠️ Limitations
- Render Required: Your component must be mounted and render DOM elements for them to be measured. If your component returns
nullwhen loading, no skeleton will appear. - Complex styles: Elements with complex shapes (clip-path) or transforms might not be perfectly matched, though standard transforms usually trigger correct bounding boxes.
- Leaf Nodes: The analyzer focuses on "leaf" content (text, images, inputs). Container divs usually aren't drawn unless they have specific backgrounds, to avoid clutter.
👨💻 Author
Vedant Yengupatla
- Website: https://vedant-dev.netlify.app
- GitHub: @vedantnotfound
🤝 Contributing
Contributions, issues, and feature requests are welcome!
Feel free to check logic in src/hooks/useSkeletonAnalyzer.ts if you want to improve the heuristics.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
Distributed under the MIT License. See LICENSE for more information.
