Component Style Guide
This document outlines the recommended patterns and best practices for building components in the BuildAppsWith platform.Table of Contents
- Folder Structure
- Component Organization
- Import Standards
- Component Patterns
- Accessibility
- Magic UI Components
- Performance Considerations
Folder Structure
Components are organized following a domain-first approach:See the Folder Structure Guide for more details on the overall project organization.
Component Organization
UI Component Hierarchy
Components follow a hierarchical organization:- Core UI Components: Foundational building blocks (buttons, inputs, etc.)
- Composite UI Components: Combinations of core components that are reused across domains
- Domain-specific UI Components: Components tailored to a specific domain’s needs
Component File Structure
Each component file should follow this structure:Import Standards
Using Barrel Exports
Always import components from barrel exports (index.ts files) rather than directly from component files:Benefits of Barrel Exports
Using barrel exports provides several advantages:- Simplified Imports: Fewer import statements and shorter paths
- Encapsulation: Implementation details can change without affecting imports
- Refactoring Support: Components can be moved within their domain without breaking imports
- Cleaner Code: Reduces import clutter at the top of files
Import Order
Organize imports in the following order, with a blank line between each group:- External Libraries: React, framer-motion, etc.
- Internal Utilities: Utilities and hooks from
/liband/hooks - Internal Components: Components from
/components - Types: Type imports if separate from component imports
Component Patterns
Client vs. Server Components
- Use server components by default for better performance
- Add
"use client";directive only when needed (hooks, event handlers, browser APIs) - Keep client components as small as possible
Props Interface Pattern
- Use TypeScript interfaces for component props
- Document complex props with JSDoc comments
- Use optional props with default values where appropriate
- Put props interface directly above the component
Styling Approach
- Use Tailwind CSS for styling
- Use the
cn()utility for conditional class names - Compose styles with component composition rather than complex class logic
- Use CSS variables for themeable values
Accessibility
All components must be accessible according to WCAG 2.1 AA standards:- Use semantic HTML elements
- Include proper ARIA attributes when needed
- Support keyboard navigation
- Ensure sufficient color contrast
- Provide text alternatives for non-text content
- Support screen readers
Reduced Motion
Support users who prefer reduced motion:Magic UI Components
BuildAppsWith uses a component library called Magic UI which is built on top of shadcn/ui. These components follow consistent patterns:- Composition-based: Components are composed rather than configured
- Style-flexible: Uses Tailwind classes for styling
- Accessible: Built with accessibility in mind
- Form Components: Form components use React Hook Form integration
Using Magic UI Components
Performance Considerations
Component Optimization
- Use
memosparingly and only when needed (with performance tests) - Avoid unnecessary re-renders by keeping state as local as possible
- Use
useCallbackfor functions passed to child components - Use
useMemofor expensive computations
Code Splitting
- Use dynamic imports for large components or libraries
- Lazy load components not needed for initial render
- Prioritize core functionality loading
Image Optimization
- Always use the
Imagecomponent from Next.js for automatic optimization - Specify width and height to prevent layout shifts
- Use responsive images with appropriate sizes
Code Formatting and Standards
BuildAppsWith uses industry-standard tools to maintain code quality:- Prettier for consistent code formatting
- ESLint for code quality analysis
- TypeScript for type safety
New components should have at least 90% test coverage using Vitest and React Testing Library.