Component Library Implementation Guide
This guide provides practical instructions for implementing components in the BuildAppsWith component library, including best practices, code samples, and troubleshooting tips.Implementation Process
1. Component Planning
Before writing any code, follow these planning steps:- Review Requirements: Understand the component’s purpose, variants, and usage
- Check Existing Components: Verify if the component already exists or can be composed from existing components
- Identify Dependencies: Determine what external libraries or internal utilities are needed
- Define Props Interface: Define the TypeScript interface for component props
2. Component Implementation
Follow these steps when implementing a new component:- Create Component Directory: Follow the domain-based organization pattern
- Write Component Code: Implement the component using TypeScript and React
- Implement Variants: Add support for all required variants
- Add Documentation: Include JSDoc comments for the component and props
- Create Tests: Write comprehensive tests for the component
3. Component Testing
All components should include comprehensive tests:4. Component Documentation
Document your component using JSDoc comments and provide usage examples:Integration with Magic UI Pro
Installing Magic UI Components
To install a Magic UI Pro component:Extending Magic UI Components
To extend a Magic UI Pro component:Common Implementation Patterns
Container/Presenter Pattern
Separate logic from presentation using the container/presenter pattern:Compound Components
Use compound components for complex UI elements:Hooks for Logic
Extract complex logic into custom hooks:Performance Optimization
Memoization
Use React.memo for components that render often but rarely change:Code Splitting
Use dynamic imports to split large components:Troubleshooting
Common Issues and Solutions
| Issue | Cause | Solution |
|---|---|---|
| Component re-renders too often | Unstable props or context values | Use useCallback, useMemo, or React.memo |
| Type errors with props | Incorrect TypeScript types | Define explicit interfaces and use proper union types |
| CSS conflicts with Tailwind | Specificity issues or incorrect class order | Use the cn() utility to merge classes properly |
| Layout shifts during loading | Missing size constraints | Set explicit width/height or aspect ratios |
| Z-index issues with overlays | Incorrect stacking context | Use consistent z-index scale and ensure proper DOM nesting |
Debugging Techniques
-
Component Boundaries: Add outline to see component boundaries
-
Render Counting: Track component renders
-
Prop Debugging: Log props changes
Best Practices
Do’s
- ✅ Use TypeScript for all components
- ✅ Write comprehensive tests
- ✅ Follow accessibility guidelines
- ✅ Document props with JSDoc
- ✅ Consider all error states
- ✅ Use React DevTools for debugging
- ✅ Validate props with PropTypes or TypeScript
Don’ts
- ❌ Mutate props or state directly
- ❌ Use
anytype in TypeScript - ❌ Create unnecessarily complex components
- ❌ Put business logic in UI components
- ❌ Ignore accessibility
- ❌ Use inline styles (use Tailwind instead)