Skip to main content

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

Sources: README.md64-89 README.md105-120 README.md147-168 src/components/README.md1-19

Layer 1: Core Primitives Usage

Use core primitives when you need maximum control over layout and semantic structure: Sources: README.md81-103 src/components/README.md23-92

Layer 2: UI Components Usage

Use UI components for pre-styled, ready-to-use elements:
Sources: README.md105-145 src/components/README.md94-204

Layer 3: Layout Components Usage

Use layout components for structural page organization:
Sources: README.md147-168 src/layouts/DashLayout.tsx1-99

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

Sources: README.md170-217 src/lib/core-classes.json1-619 scripts/cva-extractor.ts223-260

Variant Composition Guidelines

Sources: README.md171-217 src/components/README.md206-222

When to Use className

The variant system covers ~80% of styling needs. Use className for:
  1. Animations and transitions (not covered by variants)
  2. Pseudo-classes like :hover, :focus, :active
  3. Custom grid/flex patterns beyond basic layouts
  4. Project-specific utility classes not in the design system
Sources: README.md278-304 src/components/README.md237-244

Semantic HTML Best Practices

The library emphasizes semantic HTML5 elements for accessibility and SEO. Always use the most appropriate semantic element.

Semantic Element Selection

Sources: README.md14 src/components/README.md239 src/core/ui/Block.tsx1-15

Semantic HTML Examples

Sources: README.md14 src/components/README.md28-64 src/core/ui/Block.tsx1-15

Accessibility Best Practices

Ensure all components are accessible to users with disabilities by following WCAG guidelines.

Core Accessibility Principles

Sources: README.md14 src/components/ui/Button.tsx1-40 src/components/ui/Image.tsx1-20

Form Accessibility

Sources: src/components/GUIDE_CREATE_FORM.md1-50 src/components/README.md239

Performance Optimization

Follow these patterns to maintain optimal performance in large applications.

Component Composition vs Prop Drilling

Sources: README.md306-338 src/components/ui/Card.tsx1-80

Memoization Guidelines

Sources: src/core/ui/Box.tsx1-30 src/components/ui/Button.tsx1-40

Avoid Unnecessary Re-renders

Sources: README.md341-359

Composition Patterns

The library supports multiple composition patterns. Choose the appropriate pattern for your use case.

Pattern Comparison

Sources: README.md15-17 src/layouts/LayoutBlock.tsx15-22 src/components/ui/Card.tsx1-80

Compound Components Pattern

Use for flexible component structures with predefined sub-components:
Sources: README.md38-59 README.md121-145 src/components/ui/Card.tsx1-80

Prop Forwarding Pattern

All UI components forward variant props from the base components:
Sources: README.md16 src/components/README.md12-18 src/components/ui/Button.tsx1-40

Content Hooks Pattern

Use for dynamic rendering in layout components:
Sources: src/layouts/LayoutBlock.tsx15-22 src/layouts/LayoutBlock.tsx1-50

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

Sources: src/components/README.md223-235 src/components/README.md237-244

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 component prop on Block
  • 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-class attributes 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
Sources: README.md388-402 src/components/README.md237-244