Real-Time Preview Fix

How we fixed instant property updates in the widget builder live preview

Real-Time Preview Fix ⚡

The main achievement of Session 7.


Problem

Properties edited in the property editor weren't updating the live preview. Users could modify values like title, subtitle, colors, etc., but the preview remained static.

Root Causes

  1. Effect was intentionally not updating widget input to "preserve component internal state"
  2. Destroy/create cycle caused NG0950: Input "widgetId" is required errors
  3. Component lifecycle conflicts during rapid re-renders

Solution

Smart Widget Input Update

Update the existing component's widget input instead of destroying/recreating the component.

// Effect watches for property changes
effect(() => {
  const props = this.propertyValues();
  const propsHash = JSON.stringify(props);

  if (
    this.currentComponentRef && 
    propsHash !== this.lastRenderedProperties
  ) {
    this.lastRenderedProperties = propsHash;
    this.updateWidgetInput(props); // ← Update input, don't destroy!
  }
});

New Method

private updateWidgetInput(properties: Record<string, unknown>): void {
  const mappedProperties = this.mapPropertiesToWidget(properties, builderWidget.type);

  const coreWidget: CoreWidget = {
    id: builderWidget.id,
    type: builderWidget.type,
    properties: mappedProperties, // ← Updated properties!
    // ... other fields ...
  };

  // Update widget input to trigger change detection
  this.currentComponentRef.setInput('widget', coreWidget);
}

Benefits

✅ Instant Updates: Properties reflect immediately
✅ No Flicker: Component never destroyed
✅ State Preserved: Animations/transitions maintained
✅ No Errors: widgetId always available
✅ Performance: Minimal change detection overhead


Files Modified


Testing

All test cases passed:

  1. ✅ Card widget generation with correct initial properties
  2. ✅ Real-time title edit (instant update, no flicker)
  3. ✅ Multiple property changes (title, subtitle, colors, spacing)
  4. ✅ Complex widget properties (nested colors, shadows, spacing)

Technical Flow

User edits property
    ↓
propertyGroups signal updated
    ↓
propertyValues signal recomputed
    ↓
Effect triggered
    ↓
Property hash compared
    ↓
updateWidgetInput() called
    ↓
CoreWidget object built with new properties
    ↓
setInput('widget', coreWidget)
    ↓
Angular change detection
    ↓
Widget component updates
    ↓
DOM updates
    ↓
User sees instant preview

This fix makes the widget builder production-ready for real-time editing! 🎉