Best Practices
Relevant source files This document provides general guidelines and recommendations for effectively using the @ui8kit/core component library. It covers strategies for component selection, variant system usage, semantic HTML practices, accessibility, performance optimization, and composition patterns. For common development patterns and typical usage examples, see Basic Workflow. For handling edge cases requiring custom solutions, see Advanced Workflow.Component Selection Strategy
Choosing the right component layer is fundamental to writing maintainable code. The three-layer architecture provides clear boundaries for different levels of abstraction.Decision Flow for Component Selection
Layer 1: Core Primitives Usage
Use core primitives when you need maximum control over layout and semantic structure:| Component | Use When | Example Scenario |
|---|---|---|
Block | Need semantic HTML5 element with variants | <Block component="section">, <Block component="nav"> |
Box | Need generic container without semantic meaning | Wrapper divs, flex items, grid cells |
Grid | Need CSS Grid layout | Product grids, image galleries, card layouts |
Flex | Need flexbox layout with explicit control | Custom toolbars, complex alignments |
Stack | Need vertical/horizontal stacking with gap | Form fields, button groups, content sections |
Layer 2: UI Components Usage
Use UI components for pre-styled, ready-to-use elements:Layer 3: Layout Components Usage
Use layout components for structural page organization:Variant System Usage
The variant system eliminates ~80% of className usage through 12 composable variants. Follow these guidelines to maximize its effectiveness.Variant Application Flow
Variant Composition Guidelines
| Variant Category | Props | Values | Use Case |
|---|---|---|---|
| Spacing | p, px, py, pt, pb, pl, pr, m, mx, my, mt, mb, ml, mr | none, xs, sm, md, lg, xl, 2xl, auto | Control padding and margins |
| Colors | bg, c, borderColor | Design system colors | Background, text, border colors |
| Layout | w, h, minH, maxW, position | auto, full, screen, fit, min, max | Control dimensions and positioning |
| Typography | size, weight, align, leading | Font-specific values | Text styling |
| Effects | rounded, shadow, border | Preset values | Visual enhancements |
When to Use className
The variant system covers ~80% of styling needs. UseclassName for:
- Animations and transitions (not covered by variants)
- Pseudo-classes like
:hover,:focus,:active - Custom grid/flex patterns beyond basic layouts
- Project-specific utility classes not in the design system
Semantic HTML Best Practices
The library emphasizes semantic HTML5 elements for accessibility and SEO. Always use the most appropriate semantic element.Semantic Element Selection
Semantic HTML Examples
Accessibility Best Practices
Ensure all components are accessible to users with disabilities by following WCAG guidelines.Core Accessibility Principles
| Principle | Implementation | Example |
|---|---|---|
| Keyboard Navigation | Ensure all interactive elements are keyboard accessible | Use semantic <button> instead of <div onClick> |
| ARIA Labels | Provide labels for screen readers | aria-label, aria-labelledby, aria-describedby |
| Focus Management | Visible focus indicators | :focus-visible styles |
| Color Contrast | Minimum 4.5:1 ratio for text | Use design system colors |
| Alt Text | Descriptive text for images | Always provide alt prop |
Form Accessibility
Performance Optimization
Follow these patterns to maintain optimal performance in large applications.Component Composition vs Prop Drilling
Memoization Guidelines
Avoid Unnecessary Re-renders
Composition Patterns
The library supports multiple composition patterns. Choose the appropriate pattern for your use case.Pattern Comparison
Compound Components Pattern
Use for flexible component structures with predefined sub-components:Prop Forwarding Pattern
All UI components forward variant props from the base components:Content Hooks Pattern
Use for dynamic rendering in layout components:Common Anti-Patterns
Avoid these common mistakes when using the library.Anti-Pattern 1: Overusing className
Anti-Pattern 2: Bypassing Semantic HTML
Anti-Pattern 3: Recreating Existing Components
Anti-Pattern 4: Excessive Nesting
Anti-Pattern 5: Ignoring data-class Attributes
Summary Checklist
Use this checklist when building interfaces with @ui8kit/core:- Component Selection: Use the appropriate layer (Core/UI/Layout) for each use case
- Variant System: Prefer variants over className for ~80% of styling needs
- Semantic HTML: Use appropriate semantic elements via
componentprop onBlock - Accessibility: Include ARIA labels, keyboard navigation, and focus management
- Performance: Compose components naturally, avoid prop drilling
- Composition: Use compound components, prop forwarding, and content hooks appropriately
- Data Attributes: Utilize
data-classattributes for DOM targeting and testing - Type Safety: Leverage TypeScript types for prop validation
- Avoid Anti-Patterns: Don’t overuse className, bypass semantics, or recreate components