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
- Effect was intentionally not updating widget input to "preserve component internal state"
- Destroy/create cycle caused
NG0950: Input "widgetId" is requirederrors - 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
libs/libs-widgets/widget-builder/src/lib/components/widget-live-preview/widget-live-preview.component.ts- Added
lastRenderedPropertiesfield - Changed effect to call
updateWidgetInput - Added new
updateWidgetInput()method
- Added
Testing
All test cases passed:
- ✅ Card widget generation with correct initial properties
- ✅ Real-time title edit (instant update, no flicker)
- ✅ Multiple property changes (title, subtitle, colors, spacing)
- ✅ 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! 🎉