Static vs Saved Variants - Critical Fix
Fixed "Variant not found" error by distinguishing between static demo variants and saved user variants
Static vs Saved Variants - Critical Fix
๐ Problem Identified
Error: "Variant not found" when clicking "Edit" on static demo variants (e.g., "primary", "secondary", "danger").
Root Cause: The variant editor was trying to load static mock variants (defined in CoreComponentDetailComponent) from VariantStorageService, which doesn't contain them.
Error Flow
User clicks "Edit" on "Primary" button variant
โ
Navigate to: /widgets/variant-editor/button/primary
โ
VariantBuilderPage tries to load "primary" from storage
โ
โ Error: "Variant not found" (it's a static demo, not in storage)
โ Solution Implemented
1. Added isStatic Flag to ComponentVariant
interface ComponentVariant {
id: string;
name: string;
description: string;
preview: string;
code: string;
properties?: Record<string, any>;
isStatic?: boolean; // NEW: Flag to distinguish static demo variants
}
2. Flag Static Variants in allVariants Computed
// Merge static variants with loaded variants
readonly allVariants = computed((): ComponentVariant[] => {
const data = this.componentData();
const loaded = this.loadedVariants();
// Convert loaded WidgetVariants to ComponentVariant format
const loadedConverted: ComponentVariant[] = loaded.map(v => ({
...v,
isStatic: false // โ
Loaded variants are NOT static
}));
// Mark static variants with isStatic flag
const allVariants: ComponentVariant[] = data.variants.map(v => ({
...v,
isStatic: true // โ
Static demo variants
}));
// Merge without duplicates
for (const variant of loadedConverted) {
if (!allVariants.find(v => v.id === variant.id)) {
allVariants.push(variant);
}
}
return allVariants;
});
3. Conditional Action Buttons in Template
<!-- Only show Edit/Duplicate for saved variants (non-static) -->
@if (!variant.isStatic) {
<button class="btn-action btn-edit" (click)="editVariant(variant)">
<span class="material-icons">edit</span>
Edit
</button>
<button class="btn-action btn-duplicate" (click)="duplicateVariant(variant)">
<span class="material-icons">content_copy</span>
Duplicate
</button>
} @else {
<!-- For static variants, show "Use Template" -->
<button class="btn-action btn-duplicate" (click)="duplicateVariant(variant)">
<span class="material-icons">add_circle</span>
Use Template
</button>
}
4. Visual Indicator for Static Variants
Added a "TEMPLATE" badge to static variants:
<div class="variant-header">
<h4>{{ variant.name }}</h4>
@if (variant.isStatic) {
<span class="badge-template">Template</span>
}
</div>
๐จ User Experience
Before Fix
- All variants showed Edit/Duplicate buttons
- Clicking "Edit" on static variants โ โ "Variant not found" error
- No way to distinguish demo variants from saved variants
After Fix
Static (Demo) Variants:
- โ Display "TEMPLATE" badge
- โ Show "Use Template" button (creates new variant)
- โ No "Edit" button (can't edit demos)
Saved Variants:
- โ No badge
- โ Show "Edit" button (opens full-page editor)
- โ Show "Duplicate" button (creates copy)
๐ Variant Types
Static Variants (Templates)
- Source: Hard-coded in
CoreComponentDetailComponent - Purpose: Demo/examples for users
- Storage: None (in-memory only)
- Actions: Select, Use Template
- Examples: "Primary", "Secondary", "Danger", "Ghost"
Saved Variants (User-Created)
- Source:
VariantStorageService(IndexedDB + Backend) - Purpose: User customizations
- Storage: Persistent (flash-persistence)
- Actions: Select, Edit, Duplicate
- Examples: "My Custom Button", "Test Uni 3"
๐งช Testing
Test Scenario 1: Static Variant
- Navigate to:
http://localhost:5200/space/chrome-space-1/widgets/guide/button - Hover over "Primary" variant card
- โ Should see "TEMPLATE" badge
- โ Should see "Use Template" button
- โ Should NOT see "Edit" button
- Click "Use Template"
- โ Modal opens with pre-filled data for duplication
Test Scenario 2: Saved Variant
- Create a new variant (e.g., "My Button")
- Return to component detail page
- Hover over "My Button" variant card
- โ Should NOT see "TEMPLATE" badge
- โ Should see "Edit" button
- โ Should see "Duplicate" button
- Click "Edit"
- โ Navigates to full-page editor
- โ Variant loads successfully (no error)
๐ Impact
Files Modified
core-component-detail.component.ts(interface, computed property)core-component-detail.component.html(conditional rendering)core-component-detail.component.scss(badge styles)
Benefits
- โ No more errors: Users can't accidentally try to edit demo variants
- โ Clear UX: Visual distinction between templates and saved variants
- โ Better workflow: "Use Template" makes it clear the action creates a new variant
- โ
Type-safe:
isStaticflag provides compile-time safety
๐ Future Enhancements
- Template Library: Expand static variants into a rich template library
- Template Categories: Organize templates by use case (CTA, Navigation, Forms, etc.)
- Import/Export: Allow users to share custom variants as templates
- Template Preview: Interactive preview before using template
- Template Versioning: Track template updates and migrations
๐ Related Documentation
- Widget System Production Refactor
- Variant Module Integration Strategy
- Smart Variant Creator UX Review
โ Summary
Problem: "Variant not found" error when editing static demo variants.
Solution: Added isStatic flag to distinguish templates from saved variants, conditionally showing Edit/Duplicate buttons, and adding visual "TEMPLATE" badge.
Result:
- โ No more errors
- โ Clear UX distinction
- โ Better user workflow
- โ Production-ready reliability
Status: Fixed and tested โ