Files
main/rules-mdc/ui_libraries/component_libraries/tailwind.mdc
T
sanjeed5 5068ef1d22 Add comprehensive MDC files for various technologies and frameworks
Generate a large set of Markdown Configuration (MDC) files covering best practices across multiple domains, including:
- AI/ML libraries (PyTorch, TensorFlow, scikit-learn)
- Backend frameworks (Django, FastAPI, Express, Spring Boot)
- Frontend frameworks (React, Vue, Angular)
- Cloud platforms (AWS, Azure, GCP)
- Development tools (Docker, Kubernetes, Terraform)
- State management libraries
- UI component libraries
- Web technologies and APIs

Enhance project documentation with detailed, standardized best practice guidelines for different technologies and frameworks
2025-02-27 16:58:58 +05:30

92 lines
8.3 KiB
Plaintext

---
description: Provides guidelines and best practices for effectively using Tailwind CSS, focusing on performance, organization, and maintainability. This rule ensures consistent styling and efficient component development across the project.
globs: **/*.{html,js,jsx,ts,tsx,vue,svelte}
---
- **Leverage Tailwind's PurgeCSS:** Configure PurgeCSS properly to remove unused styles in production, drastically reducing the final CSS bundle size. Ensure you configure it to scan all relevant files (HTML, JS/TS components, etc.).
- Configure `tailwind.config.js` with the appropriate `content` array to ensure that PurgeCSS correctly identifies used classes.
- Use the JIT (Just-In-Time) compiler in development for faster build times and complete feature availability.
- **Use Tailwind's Configuration File:** Customize Tailwind's default theme, colors, spacing, and breakpoints in the `tailwind.config.js` file. This ensures consistency across the project and allows for easy updates.
- Define a design system using semantic names for colors and spacing (e.g., `primary-brand`, `space-md`) rather than hardcoding values.
- Utilize Tailwind's theme extension feature to add custom styles without overriding the defaults.
- **Adopt a Mobile-First Approach:** Design components and layouts starting with mobile devices and then progressively enhance them for larger screens using Tailwind's responsive modifiers (e.g., `sm:`, `md:`, `lg:`, `xl:`).
- Avoid deeply nested responsive modifiers. Refactor components to be more adaptable if necessary.
- **Utilize Tailwind UI (if applicable):** If using Tailwind UI, leverage the pre-built components as a starting point and customize them to fit your project's specific needs. Be aware of licensing implications.
- Extract common patterns and modifications into reusable components to avoid repeating code.
- **Optimize for Performance:** Beyond PurgeCSS, be mindful of CSS specificity and avoid overly complex selectors. Use Tailwind's `apply` directive judiciously.
- Use `@layer` directive in your CSS to organize your styles and improve performance.
- Consider CSS extraction and inlining critical CSS for faster initial page load.
- Profile your application's CSS to identify performance bottlenecks.
- **Stay Organized with Components:** Break down complex UIs into smaller, reusable components. Use a consistent naming convention for component files and Tailwind classes.
- Create a component library to store and manage reusable components.
- Use tools like Storybook or Styleguidist to document and showcase components.
- Follow the principles of atomic design when structuring your components.
- **Integrate with a Design System:** Define a clear design system with consistent typography, colors, and spacing. Use Tailwind's configuration file to enforce these standards.
- Document the design system and make it accessible to all team members.
- Use design tokens to manage and update design values across the project.
- **Code Organization and Structure:**
- **Directory Structure:** Organize components into logical directories based on functionality or page sections (e.g., `components/`, `pages/`, `layouts/`).
- **File Naming Conventions:** Use descriptive and consistent file names for components (e.g., `Button.jsx`, `Card.vue`).
- **Module Organization:** Group related components and utilities into modules.
- **Component Architecture:** Favor composition over inheritance when building components. Use functional components and hooks where appropriate.
- **Code Splitting:** Implement code splitting to reduce initial load time. Use dynamic imports to load components on demand.
- **Common Patterns and Anti-patterns:**
- **Design Patterns:** Use the composite pattern for building complex UIs from smaller components. Use the provider pattern for managing global state.
- **Recommended Approaches:** Use Tailwind's utility classes for styling elements. Create custom components for complex or reusable styles.
- **Anti-patterns:** Avoid using inline styles or `!important` declarations. Avoid overly specific CSS selectors.
- **State Management:** Use a state management library (e.g., Redux, Zustand, Vuex) for complex state management. Use React Context or Vue provide/inject for simpler state management.
- **Error Handling:** Implement error boundaries to catch errors in components. Display user-friendly error messages.
- **Performance Considerations:**
- **Optimization Techniques:** Minimize the number of DOM updates. Use memoization to prevent unnecessary re-renders.
- **Memory Management:** Avoid memory leaks by properly cleaning up event listeners and subscriptions.
- **Rendering Optimization:** Use virtualization for rendering large lists. Use the `shouldComponentUpdate` lifecycle method or `React.memo` to prevent unnecessary re-renders.
- **Bundle Size Optimization:** Use code splitting and tree shaking to reduce bundle size. Compress images and other assets.
- **Lazy Loading:** Lazy load images and other resources that are not immediately visible on the page.
- **Security Best Practices:**
- **Common Vulnerabilities:** Be aware of XSS vulnerabilities when rendering user-generated content. Sanitize inputs to prevent XSS attacks.
- **Input Validation:** Validate all user inputs to prevent unexpected behavior.
- **Authentication and Authorization:** Implement secure authentication and authorization mechanisms.
- **Data Protection:** Protect sensitive data by encrypting it both in transit and at rest.
- **Secure API Communication:** Use HTTPS for all API communication. Validate API responses.
- **Testing Approaches:**
- **Unit Testing:** Write unit tests for individual components and functions.
- **Integration Testing:** Write integration tests to ensure that components work together correctly.
- **End-to-End Testing:** Write end-to-end tests to test the entire application flow.
- **Test Organization:** Organize tests into logical directories based on functionality.
- **Mocking and Stubbing:** Use mocking and stubbing to isolate components during testing.
- **Common Pitfalls and Gotchas:**
- **Frequent Mistakes:** Forgetting to configure PurgeCSS. Overusing the `apply` directive. Not using a consistent naming convention.
- **Edge Cases:** Handling different screen sizes and devices. Dealing with browser compatibility issues.
- **Version-Specific Issues:** Be aware of breaking changes in new Tailwind CSS versions.
- **Compatibility Concerns:** Ensure that Tailwind CSS is compatible with your target browsers.
- **Debugging Strategies:** Use browser developer tools to inspect CSS and debug styling issues.
- **Tooling and Environment:**
- **Recommended Development Tools:** Use a code editor with Tailwind CSS IntelliSense. Use a browser with developer tools.
- **Build Configuration:** Configure your build tool (e.g., webpack, Parcel, Vite) to process Tailwind CSS.
- **Linting and Formatting:** Use ESLint and Prettier to enforce code style and prevent errors. Configure a Tailwind CSS plugin for your linter.
- **Deployment Best Practices:** Use a CDN to serve static assets. Configure caching to improve performance.
- **CI/CD Integration:** Integrate Tailwind CSS into your CI/CD pipeline to automate testing and deployment.
- **Naming Conventions:**
- Use BEM (Block, Element, Modifier) or a similar naming convention for custom CSS classes. This helps to maintain consistency and avoid naming conflicts.
- **Accessibility (A11y):**
- Use semantic HTML elements to improve accessibility. Provide alternative text for images. Ensure that your website is navigable using a keyboard.
- **Consider using `theme()` helper function:** When using Javascript to dynamically set tailwind values, you can use the `theme()` helper function from tailwindcss to get theme values for custom classes. This ensures they are based on values you have specified in your `tailwind.config.js` and can be purged properly.
- **Custom Utilities vs Components:** When creating custom styling, decide when to make a utility versus a component. Utilities are best for single purpose styling like font-weight, or color changes. Components should be used for more complex combinations of styling, that require more html structure.
- **Beware of arbitrary values and `!important`:** Using these excessively will reduce your maintainability and reusability of components and CSS.