# Component Styling Guidelines **Version:** 2.0.0 **Last Updated:** 2025-11-19 **Status:** ✅ Complete --- ## Table of Contents 1. [Overview](#overview) 2. [Decision Tree](#decision-tree) 3. [When to Use Global CSS](#when-to-use-global-css) 4. [When to Use Scoped CSS](#when-to-use-scoped-css) 5. [Anti-Patterns](#anti-patterns) 6. [Code Review Checklist](#code-review-checklist) 7. [Examples](#examples) 8. [Best Practices](#best-practices) --- ## Overview ROA2WEB uses a **hybrid CSS architecture** combining: - **Global patterns** (`src/assets/css/`) for reusable components - **Scoped styles** (` ``` #### 2. **Form Elements** ```vue ``` #### 3. **Buttons** ```vue ``` #### 4. **Card Components** ```vue ``` #### 5. **Dashboard Patterns** ```vue ``` #### 6. **Utility Classes** ```vue ``` --- ## When to Use Scoped CSS ### ✅ Scoped CSS is OK For: #### 1. **Component-Specific Layout (Not Reusable)** ```vue ``` **Why it's OK:** This layout is unique to this specific dual-chart component and won't be used elsewhere. #### 2. **Third-Party Library Integration (Chart.js, etc.)** ```vue ``` **Why it's OK:** Chart.js has specific requirements that are component-specific and not reusable patterns. #### 3. **Complex Component State Styling** ```vue ``` **Why it's OK:** This state machine is unique to this visualization component. #### 4. **Print Styles (Component-Specific)** ```vue ``` **Why it's OK:** Print styles for specific components that have unique printing requirements. --- ## Anti-Patterns ### ❌ NEVER Do These Things #### 1. **Using `:deep()` for PrimeVue** ```vue ``` **Why it's bad:** - Creates specificity issues - Overrides break easily - Not maintainable - Causes cascading problems **✅ Correct approach:** All PrimeVue styling goes in `src/assets/css/vendor/primevue-overrides.css`: ```css /* vendor/primevue-overrides.css */ .p-inputtext { border: 2px solid var(--color-primary); padding: 1rem; } .p-datatable .p-datatable-thead { background: var(--color-bg-secondary); } ``` #### 2. **Hardcoding Colors/Sizes** ```vue ``` #### 3. **Duplicating Existing Patterns** ```vue ``` #### 4. **Using `!important` in Scoped Styles** ```vue ``` **Exception:** `!important` is allowed in: - `vendor/primevue-overrides.css` (to override PrimeVue) - Print styles (`@media print`) - Third-party library overrides (documented reason required) #### 5. **Creating Custom Form/Button Base Styles** ```vue ``` --- ## Code Review Checklist ### Before Submitting PR - [ ] **No duplicate patterns** - Checked `docs/CSS_PATTERNS.md` first - [ ] **No `:deep()` in components** - PrimeVue styles in `vendor/primevue-overrides.css` - [ ] **Design tokens used** - No hardcoded colors, sizes, or spacing - [ ] **No `!important` in scoped styles** - Except documented exceptions - [ ] **Scoped styles < 50 lines** - If more, consider extracting to global - [ ] **No form/button/card base styles** - Use existing global patterns - [ ] **Responsive tested** - Works on mobile (375px), tablet (768px), desktop (1920px) - [ ] **Build successful** - `npm run build` passes - [ ] **Playwright tests pass** - `npm run test:e2e` passes - [ ] **CSS is necessary** - Not adding CSS just because "it looks different" ### Review Questions **For Scoped CSS:** 1. Is this styling truly unique to this component? 2. Could this be used in 2+ places? → Make it global 3. Can this use existing patterns? → Use global CSS 4. Is this < 50 lines? → If not, extract to global 5. Does it have a documented reason? → Add comment **For Global CSS:** 1. Is this pattern used in 2+ places? 2. Will this be reused in the future? 3. Does it fit existing pattern categories? 4. Is it generic enough for reuse? --- ## Examples ### ✅ Example #1: Good Use of Global CSS ```vue ``` **Why it's good:** - Zero duplicate CSS - Uses established patterns - Maintainable - Consistent with other cards --- ### ✅ Example #2: Good Use of Scoped CSS ```vue ``` **Why it's good:** - Unique layout specific to this component - Uses design tokens - Chart.js integration requires specific CSS - Responsive - Well-commented --- ### ❌ Example #3: Bad - Duplicate Pattern ```vue ``` --- ## Best Practices ### 1. **Check Pattern Library First** Before writing ANY CSS, check: 1. [CSS Patterns Library](./CSS_PATTERNS.md) 2. `src/assets/css/` directories 3. Ask yourself: "Has this been done before?" ### 2. **Use Design Tokens** ```vue ``` ### 3. **Keep Scoped CSS Minimal** **Rule of thumb:** If scoped styles > 50 lines, extract to global pattern. ```vue ``` ### 4. **Document Scoped CSS Rationale** ```vue ``` ### 5. **Responsive Design** Use global responsive patterns when possible: ```vue ``` --- ## Migration Guide ### Migrating Existing Components **Step 1:** Identify duplicate patterns ```bash # Search for common patterns grep -r "background: var(--color-bg)" src/components/ grep -r "border: 1px solid" src/components/ grep -r ":deep(.p-" src/components/ ``` **Step 2:** Check Pattern Library - Review `docs/CSS_PATTERNS.md` - Find matching global patterns **Step 3:** Replace with Global CSS ```vue ``` **Step 4:** Test - Build: `npm run build` - E2E tests: `npm run test:e2e` - Visual inspection on all breakpoints --- ## Getting Help - **Pattern Library:** [docs/CSS_PATTERNS.md](./CSS_PATTERNS.md) - **Form Guidelines:** [docs/FORM_TEMPLATE.md](./FORM_TEMPLATE.md) - **Design Tokens:** [docs/DESIGN_TOKENS.md](./DESIGN_TOKENS.md) - **Questions?** Ask in #frontend-css channel - **New pattern needed?** Follow [docs/STYLING_GUIDELINES.md](./STYLING_GUIDELINES.md) --- **Last Updated:** 2025-11-19 **Version:** 2.0.0 (Post CSS Refactoring) **Maintained By:** Frontend Team