---
name: frontend-patterns
description: "Internal skill. Use cc10x-router for all development tasks."
allowed-tools: Read, Grep, Glob, LSP
---
# Frontend Patterns
## Overview
User interfaces exist to help users accomplish tasks. Every UI decision should make the user's task easier or the interface more accessible.
**Core principle:** Design for user success, not aesthetic preference.
**Violating the letter of this process is violating the spirit of frontend design.**
## Focus Areas (Reference Pattern)
- **React component architecture** (hooks, context, performance)
- **Responsive CSS** with Tailwind/CSS-in-JS
- **State management** (Redux, Zustand, Context API)
- **Frontend performance** (lazy loading, code splitting, memoization)
- **Accessibility** (WCAG compliance, ARIA labels, keyboard navigation)
## Approach (Reference Pattern)
1. **Component-first thinking** - reusable, composable UI pieces
2. **Mobile-first responsive design** - start small, scale up
3. **Performance budgets** - aim for sub-3s load times
4. **Semantic HTML** and proper ARIA attributes
5. **Type safety** with TypeScript when applicable
## Component Output Checklist
**Every frontend deliverable should include:**
- [ ] Complete React component with props interface
- [ ] Styling solution (Tailwind classes or styled-components)
- [ ] State management implementation if needed
- [ ] Basic unit test structure
- [ ] Accessibility checklist for the component
- [ ] Performance considerations and optimizations
**Focus on working code over explanations. Include usage examples in comments.**
## The Iron Law
```
NO UI DESIGN BEFORE USER FLOW IS UNDERSTOOD
```
If you haven't mapped what the user is trying to accomplish, you cannot design UI.
## Design Thinking (Pre-Code)
Before writing any UI code, commit to answers for:
1. **Purpose**: What specific problem does this interface solve?
2. **Tone**: Choose an aesthetic direction and commit to it:
- Brutally minimal, maximalist, retro-futuristic, organic/natural
- Luxury/refined, playful/toy-like, editorial/magazine, brutalist/raw
- Art deco/geometric, soft/pastel, industrial/utilitarian
3. **Constraints**: Framework requirements, performance budget, accessibility level
4. **Differentiation**: What's the ONE thing someone will remember about this UI?
**Key insight:** Bold maximalism and refined minimalism both work. The enemy is indecision and generic defaults.
## Loading State Order (CRITICAL)
**Always handle states in this order:**
```typescript
// CORRECT order
if (error) return ;
if (loading && !data) return ;
if (!data?.items.length) return ;
return ;
```
**Loading State Decision Tree:**
```
Is there an error? → Yes: Show error with retry
→ No: Continue
Is loading AND no data? → Yes: Show loading indicator
→ No: Continue
Do we have data? → Yes, with items: Show data
→ Yes, but empty: Show empty state
→ No: Show loading (fallback)
```
**Golden Rule:** Show loading indicator ONLY when there's no data to display.
## Skeleton vs Spinner
| Use Skeleton When | Use Spinner When |
|-------------------|------------------|
| Known content shape | Unknown content shape |
| List/card layouts | Modal actions |
| Initial page load | Button submissions |
| Content placeholders | Inline operations |
## Motion & Animation
| Rule | Do | Don't |
|------|-----|-------|
| **Reduced motion** | Honor `prefers-reduced-motion` | Ignore user preferences |
| **Properties** | Animate `transform`/`opacity` only | Animate `width`/`height`/`top`/`left` |
| **Transitions** | List properties explicitly | Use `transition: all` |
| **Duration** | 150-300ms for micro-interactions | Too fast (<100ms) or slow (>500ms) |
| **Interruptible** | Allow animation cancellation | Lock UI during animation |
```css
/* CORRECT: Compositor-friendly, respects preferences */
@media (prefers-reduced-motion: no-preference) {
.card { transition: transform 200ms ease-out, opacity 200ms ease-out; }
.card:hover { transform: translateY(-2px); opacity: 0.95; }
}
```
## Error Handling Hierarchy
| Level | Use For |
|-------|---------|
| **Inline error** | Field-level validation |
| **Toast notification** | Recoverable errors, user can retry |
| **Error banner** | Page-level errors, data still partially usable |
| **Full error screen** | Unrecoverable, needs user action |
## Success Criteria Framework
**Every UI must have explicit success criteria:**
1. **Task completion**: Can user complete their goal?
2. **Error recovery**: Can user recover from mistakes?
3. **Accessibility**: Can all users access it?
4. **Performance**: Does it feel responsive?
## Typography Rules
| Rule | Correct | Wrong |
|------|---------|-------|
| Ellipsis | `…` (single character) | `...` (three periods) |
| Quotes | `" "` curly quotes | `" "` straight quotes |
| Units | `10 MB` (non-breaking) | `10 MB` (can break) |
| Shortcuts | `⌘ K` (non-breaking) | `⌘ K` (can break) |
| Loading text | `Loading…` | `Loading...` |
| Numbers in tables | `font-variant-numeric: tabular-nums` | Default proportional |
| Headings | `text-wrap: balance` | Unbalanced line breaks |
| Line length | 65-75 characters max | Unlimited width |
## Content Overflow Handling
**Prevent broken layouts from user-generated content:**
| Scenario | Solution |
|----------|----------|
| Single-line overflow | `truncate` (Tailwind) or `text-overflow: ellipsis` |
| Multi-line overflow | `line-clamp-2` / `line-clamp-3` |
| Long words/URLs | `break-words` or `overflow-wrap: break-word` |
| Flex child truncation | Add `min-w-0` to flex children (critical!) |
| Empty strings/arrays | Show placeholder, not broken UI |
```tsx
{/* Flex truncation pattern - min-w-0 is REQUIRED */}
{user.name}
```
**Test with:** short text, average text, and absurdly long text (50+ characters).
## Universal Questions (Answer First)
**ALWAYS answer before designing/reviewing:**
1. **What is the user trying to accomplish?** - Specific task, not feature
2. **What are the steps?** - Click by click
3. **What can go wrong?** - Every error state
4. **Who might struggle?** - Accessibility needs
5. **What's the existing pattern?** - Project conventions
## User Flow First
**Before any UI work, map the flow:**
```
User Flow: Create Account
1. User lands on signup page
2. User enters email
3. User enters password
4. User confirms password
5. System validates inputs (inline)
6. User clicks submit
7. System processes (loading state)
8. Success: User sees confirmation + redirect
9. Error: User sees error + can retry
```
**For each step, identify:**
- What user sees
- What user does
- What feedback they get
- What can go wrong
## UX Review Checklist
| Check | Criteria | Example Issue |
|-------|----------|---------------|
| **Task completion** | Can user complete goal? | Button doesn't work |
| **Discoverability** | Can user find what they need? | Hidden navigation |
| **Feedback** | Does user know what's happening? | No loading state |
| **Error handling** | Can user recover from errors? | No error message |
| **Efficiency** | Can user complete task quickly? | Too many steps |
**Severity levels:**
- **BLOCKS**: User cannot complete task
- **IMPAIRS**: User can complete but with difficulty
- **MINOR**: Small friction, not blocking
## Accessibility Review Checklist (WCAG 2.1 AA)
| Check | Criterion | How to Verify |
|-------|-----------|---------------|
| **Keyboard** | All interactive elements keyboard accessible | Tab through entire flow |
| **Focus visible** | Current focus clearly visible | Tab and check highlight |
| **Focus order** | Logical tab order | Tab matches visual order |
| **Labels** | All inputs have labels | Check `