React coherence

From Vrooli/Vrooli · View interactive version

React coherence is an AI agent skill from the GitHub repository Vrooli/Vrooli. Prioritize maintaining **architectural coherence** across this React codebase so it stays easy to change, safe to extend, and straightforward to restyle.

How to use this skill

Skill file

Steer focus: React Coherence

Prioritize maintaining architectural coherence across this React codebase so it stays easy to change, safe to extend, and straightforward to restyle.

Your goal is to keep the app:

Do not break functionality, regress tests, or introduce unrelated product features. All changes must maintain or improve structural quality.

Required reading:


**0. Why This Skill Exists**

React codebases drift over time when each feature is built in isolation.

Common drift patterns:

The root cause is usually the same: no shared mental model for where code belongs and how style contracts are owned.

This skill defines that shared model.


**0.5 Keyboard Shortcut Coherence**

One keyboard shortcut manager per app shell. Do not scatter document/window keydown listeners across unrelated components — route all app-level shortcut handling through a single central hook.

For the full implementation shape — including iframe relay via @vrooli/iframe-bridge, focus-aware suppression, the local-first/relay-on-noop pattern, and the canonical file slot — see vrooli-ui-interop section 4 (slot [G]).


**1. State Architecture (Scope-Driven, Not Dogmatic)**

Use the smallest state mechanism that correctly matches the state scope.

flowchart TD
    C["Components<br/>(render state, trigger intents)"]
    L["Local UI<br/>useState"]
    F["Feature-Local<br/>Reducer/Store"]
    A["App-Wide<br/>Store"]
    S["Server State<br/>React Query"]
    C --> L
    C --> F
    C --> A
    C --> S
**State Location Decision Table**

| Question | If YES | If NO |

|----------|--------|-------|

| Is it only for one component subtree and ephemeral? | Local useState / useReducer | Continue |

| Is it shared within one surface/feature only? | Feature-local hook/store | Continue |

| Is it shared across multiple surfaces/routes? | App-wide store | Continue |

| Does it come from network/server and need cache/invalidation? | Server state (React Query preferred) | Continue |

| Does it trigger expensive side effects repeatedly? | Add guardrails (rate limiting/dedup/debounce) | Continue |

**State Principles**
  1. Prefer local state first for local concerns.
  2. Promote to feature-local, then app-wide, only when sharing needs are real.
  3. Keep server state in query/mutation primitives, not ad-hoc local mirrors.
  4. Avoid store sprawl and god stores.
  5. State model should reflect user flows, not implementation convenience.
**Store Pattern (When Store Is Needed)**
interface SettingsStore {
  density: "comfortable" | "compact";
  setDensity: (v: "comfortable" | "compact") => void;
}

const useSettingsStore = create<SettingsStore>((set) => ({
  density: "comfortable",
  setDensity: (v) => set({ density: v }),
}));

const density = useSettingsStore((s) => s.density);
const setDensity = useSettingsStore((s) => s.setDensity);
// Anti-pattern: unrelated concerns in one store
interface AppStore {
  auth: unknown;
  theme: unknown;
  formDrafts: unknown;
  jobRuns: unknown;
  modalState: unknown;
}
**Side-Effect Guardrails (Store/Hook/Service)**

Any path that creates remote side effects should include guardrails:

Use the same principle whether the logic lives in a store, hook, or service.


**2. Sharing Decision Tree**

Before implementing new behavior:

flowchart TD
    A["Need to implement behavior?"]
    B["Search for existing implementation<br/>(components/hooks/services/stores)"]
    C{"Found?"}
    D["Reuse/extend existing code"]
    E{"Used in 2+ places<br/>(or clearly soon will be)?"}
    F["Extract to shared<br/>with clear API"]
    G["Keep local to<br/>surface/feature"]

    A --> B --> C
    C -- Yes --> D
    C -- No --> E
    E -- Yes --> F
    E -- No --> G
**Before Writing New Code, Always Search**
# Components
rg "function .*Button|function .*Card|function .*Modal" --type tsx

# Hooks
rg "function use[A-Z]" --type tsx --type ts

# Services
rg "export .*fetch|export .*request|export .*query" src --type ts

# Stores
rg "create<.*>" src --type ts

If similar code exists, extend it instead of duplicating.


**3. Code Organization (Design-System Ready)**

Use a structure that keeps visual-system ownership explicit:

src/
├── shared/
│   ├── theme/                 # Design tokens, themes, typography, motion scales
│   │   ├── tokens.css
│   │   ├── themes/
│   │   │   ├── clean.ts
│   │   │   └── legacy.ts
│   │   ├── typography.ts
│   │   └── motion.ts
│   │
│   ├── ui/
│   │   ├── primitives/        # Button, Card, Input, Badge, Tabs, etc.
│   │   └── composites/        # Panel, Header, EmptyState, FormRow, etc.
│   │
│   ├── components/            # Non-design-system shared widgets
│   ├── hooks/                 # Domain-agnostic hooks
│   ├── stores/                # App-wide stores (only when truly cross-surface)
│   ├── services/              # API/services layer
│   ├── controllers/           # Orchestration layer
│   ├── schemas/               # Validation schemas
│   └── lib/                   # Pure utilities
│
├── surfaces/                  # Feature/page assembly
│   ├── dashboard/
│   ├── search/
│   ├── explorer/
│   └── ...
**Ownership Rules**
  1. shared/theme/* owns tokens and theme values.
  2. shared/ui/primitives/* owns base interactive styling contracts.
  3. shared/ui/composites/* owns repeated composed patterns.
  4. surfaces/* assemble flows and content; they do not define new base primitives.
**What Goes Where**

| Location | Criteria | Examples |

|----------|----------|----------|

| shared/theme/ | Global visual contract | tokens, theme maps, type ramps |

| shared/ui/primitives/ | Lowest-level reusable UI atoms | Button, Card, Input, Badge |

| shared/ui/composites/ | Repeated multi-part UI blocks | PanelHeader, EmptyState, ToolbarRow |

| shared/components/ | Shared non-design-system widgets | ErrorBoundary, route shell wrappers |

| shared/hooks/ | Domain-agnostic hooks | useDebounce, useMediaQuery |

| shared/stores/ | Truly app-wide shared state | settingsStore, modalStore |

| surfaces/X/components/ | Feature-specific render components | SearchResultsList |

| surfaces/X/hooks/ | Feature-specific behavior | useSearchFilters |

**Migration Pattern: Extracting to Shared**
  1. Move file to correct shared location.
  2. Update imports.
  3. Remove feature-only assumptions.
  4. Add exports for discoverability.
  5. Confirm no circular dependencies.

**4. Styling Coherence**

Use this style stack:

flowchart TD
    T["Design Tokens<br/>(CSS variables)"]
    W["Tailwind utility mapping<br/>(optional but consistent)"]
    V["CVA variants for<br/>primitives/composites"]
    U["Surface composition<br/>(no raw palette values)"]
    T --> W --> V --> U
**Token Categories (Required)**

Define and use semantic tokens for:

Example:

:root {
  --color-text-primary: 15 23 42;
  --color-text-muted: 71 85 105;
  --color-surface-base: 248 250 252;
  --color-surface-elevated: 255 255 255;
  --color-border-default: 226 232 240;
  --radius-md: 0.5rem;
  --space-4: 1rem;
  --motion-fast: 150ms;
}
**Variant Rules (CVA)**
  1. Primitives expose semantic variants (primary, secondary, danger).
  2. Avoid one-off style props (padding, raw color strings).
  3. Keep variant count controlled (typically <= 4 per axis).
**Hard Rules**
  1. No raw hex/rgb/hsl in TSX surface markup.
  2. No ad-hoc palette classes in surfaces when a primitive/composite exists.
  3. If two surfaces share a repeated class cluster, extract it.
  4. Focus styles must be explicit and consistent.

**5. Theme Refresh & Design System Migration**

Use this section when the task includes a substantial visual refresh.

**5.1 Pre-Work: Visual Direction Brief (Required)**

Before changing code, capture:

  1. Intent: what should feel different.
  2. References: target style direction.
  3. Constraints: what must not change (flows, selectors, accessibility, test IDs).
  4. Scope: token-only refresh vs layout + component refresh.
**5.2 Migration Strategy (Recommended Sequence)**
  1. Stabilize tokens
  1. Normalize primitives
  1. Migrate high-traffic surfaces first
  1. Migrate remaining surfaces
  1. Retire legacy classes
**5.3 Coexistence Rule During Migration**

Temporary dual styling is allowed only if:

Do not leave permanent mixed styling contracts.

**5.4 Quality Gates for Theme Refresh**

Must verify:

  1. WCAG AA contrast for core text/actions.
  2. Keyboard navigation and focus visibility.
  3. Desktop and mobile layout stability.
  4. Loading/error/empty states still readable and coherent.
  5. Automation selectors remain stable unless intentionally coordinated.
**5.5 Definition of Done (Theme Refresh)**

A theme refresh is complete when:


**6. Common Mechanism Patterns**

Use one canonical pattern for each repeated concern:

| Mechanism | Pattern | Location | Notes |

|-----------|---------|----------|-------|

| Error handling | ErrorBoundary + local recovery UX | shared/components/ | Recovery actions, not dead ends |

| Loading states | Skeleton/Suspense/inline loading contracts | shared + surface | Consistent user feedback |

| API calls | Service layer | shared/services/ | Typed results and centralized error shaping |

| Forms | Local reducer or feature/app store based on scope | surface/shared | Match scope, avoid dogma |

| Modals | Shared modal primitives + optional shared state | shared | Avoid duplicated modal frameworks |

| Toasts/alerts | Single notification mechanism | shared | Do not mix multiple systems |

| Validation | Schema-based at boundaries | shared/schemas/ | Keep UI code lean |


**7. Coherence Audit**

Run this audit at session start, before large feature work, or before a theme refresh.

**Step 1: State Inventory**
rg "useState\(" --type tsx -c | sort -t: -k2 -nr | head -20
rg "create<.*>" --type ts -l
rg "createContext|useContext" --type tsx -l

Red flags:

**Step 2: Duplication Check**
rg "function (Button|Card|Modal|Input|Form)" --type tsx -l
rg "function use(Form|Fetch|Auth|Modal)" --type tsx -l
rg "function (format|parse|validate)" --type ts -l

Red flags:

**Step 3: Styling + Theme Readiness**
# Raw colors in TSX/CSS
rg "#[0-9a-fA-F]{3,8}|rgb\\(|hsl\\(" --type tsx --type css

# Arbitrary spacing
rg "p-\\[|m-\\[|gap-\\[" --type tsx

# Primitive/variant usage
rg "cva\\(" --type tsx -c

# Token usage
rg "var\\(--" src --type css

Red flags:

**Step 4: Architecture Alignment**
ls -la src/shared/
ls -la src/shared/theme src/shared/ui 2>/dev/null
rg "export (function|const) (Button|Card|Input|Badge)" --type tsx -l | grep -v "shared/ui/primitives"

Questions:

**Step 5: Document Findings**

Update or create docs/internal/COHERENCE-NOTES.md:

# Coherence Audit - [Date]

## State
- Current pattern: [local-first / mixed / store-heavy]
- App-wide stores: [list]
- State hotspots: [list]

## Duplication
- Duplicate components: [list]
- Duplicate hooks/services: [list]
- Consolidation candidates: [list]

## Styling System
- Token coverage: [good/partial/none]
- Primitive variant coverage: [good/partial/none]
- Surface-level style debt: [list]

## Theme Refresh Readiness
- Ready now / needs foundation work
- Required prerequisites: [list]

## Priority Actions
1. [...]
2. [...]
3. [...]

**8. Memory Management with Visited Tracker**

Use visited-tracker-tools with:


**9. Relationship to Other Skills**

| Skill | Focus | How it combines with react-coherence |

|-------|-------|--------------------------------------|

| react-stability | Crash prevention | Apply stability guardrails before large refactors |

| experience-architecture-audit | User flow clarity | Coherence handles structure; experience audit handles flow outcomes |

| refactor | Targeted cleanup | Use after coherence audit identifies concrete debt |

| code-cleanup | Dead code removal | Use once architecture direction is stable |

Recommended sequence:

  1. react-coherence audit
  2. react-stability hardening
  3. experience-architecture-audit flow improvements
  4. refactor / code-cleanup

**10. Scenario Constraints**

Do not:

Prefer:


**11. Output Expectations**

You may update:

You must:

For theme refresh tasks, include in your final summary:

  1. Visual direction brief (short)
  2. What was migrated (tokens/primitives/surfaces)
  3. What remains
  4. Risks and follow-ups

Avoid superficial churn (renames/moves only) without real coherence gains.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.