Installation
Basic Usage
tsx
import { Loader } from "@/components/ui/loader"; function MyComponent() { return ( <div> <Loader variant="spinner" size="md" /> </div> );}
Variants
A classic spinning loader with customizable colors.
tsx
<Loader variant="spinner" />Bouncing dots that animate in a wave pattern.
tsx
<Loader variant="bounce" />A pulsing circle with fade effects.
tsx
<Loader variant="pulse" />A morphing loader that changes shape and color.
tsx
<Loader variant="morph" />Sizes
Loaders come in four sizes:
tsx
<Loader size="sm" /> // Small<Loader size="md" /> // Medium (default)<Loader size="lg" /> // Large<Loader size="xl" /> // Extra large
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| variant | string | No | — | The type of loading animation |
| size | string | No | — | The size of the loader |
| color | string | No | — | The color of the loader |
| speed | string | No | — | Animation speed |
| className | string | No | — | Additional CSS classes |
Examples
tsx
function LoadingButton() { const [isLoading, setIsLoading] = useState(false); return ( <Button onClick={() => setIsLoading(true)} disabled={isLoading}> {isLoading ? ( <> <Loader size="sm" className="mr-2" /> Loading... </> ) : ( "Submit" )} </Button> );}
Best Practices
- Appropriate Size: Use smaller loaders for buttons, larger for page loads
- Consistent Placement: Keep loaders in consistent locations across your app
- Meaningful Text: Include descriptive text with loaders when appropriate
- Accessibility: Always provide ARIA labels for screen readers
- Performance: Use lightweight animations for better performance
Accessibility
- ARIA Labels: Proper labels for screen readers
- Reduced Motion: Respect user's motion preferences
- Focus Management: Proper focus handling during loading states
- Color Contrast: Sufficient contrast for visibility
Performance Tips
- CSS Animations: Use CSS animations when possible for better performance
- Reduced Motion: Respect prefers-reduced-motion media query
- Optimized Bundles: Tree-shake unused loader variants
- Lazy Loading: Load heavy animations only when needed
Troubleshooting
Check if Framer Motion is properly installed and the component is wrapped in a motion provider.
Consider using CSS-only animations for better performance on low-end devices.
Ensure proper ARIA labels and test with screen readers.
Check for conflicting CSS classes or Tailwind CSS purging issues.