// VirtualizedList authoring directives: the Angular-faithful equivalent of React/Vue // `renderItem` / `ListHeaderComponent` / `ListFooterComponent` / `ListEmptyComponent` / // `ItemSeparatorComponent`. React/Vue pass an element-returning callback; that does NOT // translate to Angular, where per-item content is a TEMPLATE. So the app supplies each // slot as an `` carrying one of these structural directives, and the list // stamps the windowed slice through them. The agnostic windowing surface stays shared, // only the cell-AUTHORING shape is framework-specific. // // Authoring API (what FlatList / SectionList build on, and what the app imports): // // … // … // … // … // … // // // `vListItem` carries a static ngTemplateContextGuard so `let-item` infers as ItemT and // `let-index` / `let-separators` are typed — the Angular twin of `renderItem`'s typed info arg. // // VListOutletDirective is the INTERNAL renderer the list uses to stamp those templates. It is // a core-only reimplementation of `@angular/common`'s NgTemplateOutlet (the adapter depends on // @angular/core ONLY — see package.json), built on ViewContainerRef / TemplateRef from // @angular/core: create on template change, update the embedded view's context in place on a // context change (no churn of the cell's own view across windowing recomputes). import { Directive, Input, TemplateRef, ViewContainerRef, inject, reflectComponentType, type ComponentRef, type EmbeddedViewRef, type OnChanges, type OnDestroy, type SimpleChanges, type Type, } from '@angular/core'; import { dlog, isDebug, type IViewStyle } from '@symbiote-native/engine'; import { countAngular } from '../../diagnostics'; import type { ISeparators } from '@symbiote-native/components'; let vListOutletInstanceCounter = 0; function nextOutletInstanceId(): number { vListOutletInstanceCounter += 1; return vListOutletInstanceCounter; } // The context a `vListItem` template receives, mirroring RN's renderItem info arg // ({ item, index, separators }). `$implicit` is the item, so `let-item` (no key) binds it. export type IVListItemContext = { $implicit: ItemT; index: number; separators: ISeparators; }; // The context a `vListSeparator` template receives, mirroring RN's ItemSeparatorComponent props // (the highlight flag the cell toggles + the items flanking the gap). `$implicit` is the highlight // flag, so `let-highlighted` binds it. export type IVListSeparatorContext = { $implicit: boolean; highlighted: boolean; leadingItem?: ItemT; trailingItem?: ItemT; // RN lets a row drive arbitrary separator props via separators.updateProps; they ride here. [key: string]: unknown; }; // The context a `vListCell` template receives, RN's `CellRendererComponent` props // The template draws the wrapper and stamps the item and its separator through `content` // The item is `unknown`, a wrapping list hands it an entry or a row, not its own item type export type IVListCellContext = { $implicit: unknown; cellKey: string; index: number; style: IViewStyle | undefined; // Wire both onto the wrapper: `(layout)="cell.layout($event)"`, `(focus)="cell.focus()"` layout: (event: unknown) => void; focus: () => void; // Stamp it with `[vListOutlet]="cell.content" [vListOutletContext]="cell.contentContext"` content: TemplateRef | undefined; contentContext: unknown; }; function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null; } // `` — the per-cell content. The static guard makes the `let-` bindings // typed at the call site (let-item: ItemT, let-index: number, let-separators: ISeparators). @Directive({ selector: '[vListItem]', standalone: true }) export class VListItemDirective { readonly templateRef = inject>>(TemplateRef); static ngTemplateContextGuard( _dir: VListItemDirective, _ctx: unknown, ): _ctx is IVListItemContext { return true; } } // `` — rendered once above the cells (RN ListHeaderComponent). @Directive({ selector: '[vListHeader]', standalone: true }) export class VListHeaderDirective { readonly templateRef = inject(TemplateRef); } // `` — rendered once below the cells (RN ListFooterComponent). @Directive({ selector: '[vListFooter]', standalone: true }) export class VListFooterDirective { readonly templateRef = inject(TemplateRef); } // `` — rendered in place of the cells when the list is empty // (RN ListEmptyComponent). @Directive({ selector: '[vListEmpty]', standalone: true }) export class VListEmptyDirective { readonly templateRef = inject(TemplateRef); } // `` — rendered between cells (RN ItemSeparatorComponent). The guard // types the `let-` bindings against the separator props. @Directive({ selector: '[vListSeparator]', standalone: true }) export class VListSeparatorDirective { readonly templateRef = inject>>(TemplateRef); static ngTemplateContextGuard( _dir: VListSeparatorDirective, _ctx: unknown, ): _ctx is IVListSeparatorContext { return true; } } // `` replaces the view around each cell (RN CellRendererComponent) // It must wire `layout` and `focus` itself, as RN's custom cell must wire `onLayout` @Directive({ selector: '[vListCell]', standalone: true }) export class VListCellDirective { readonly templateRef = inject>(TemplateRef); static ngTemplateContextGuard( _dir: VListCellDirective, _ctx: unknown, ): _ctx is IVListCellContext { return true; } } // Core-only NgTemplateOutlet twin. The adapter cannot import @angular/common's NgTemplateOutlet // (not a dependency), so the list stamps templates through this: it creates the embedded view when // the template changes and updates the live view's context IN PLACE on a context change, so a // windowing recompute (a fresh context object every CD) refreshes the cell without tearing it down. @Directive({ selector: '[vListOutlet]', standalone: true }) export class VListOutletDirective implements OnChanges, OnDestroy { @Input({ alias: 'vListOutlet' }) templateRef?: TemplateRef; @Input({ alias: 'vListOutletContext' }) context?: C; // RN's `ListItemComponent`: stamped instead of the template, its inputs are the item context @Input({ alias: 'vListOutletComponent' }) component?: Type; private viewRef: EmbeddedViewRef | null = null; private componentRef: ComponentRef | null = null; private readonly viewContainer = inject(ViewContainerRef); private readonly instanceId = nextOutletInstanceId(); ngOnChanges(changes: SimpleChanges): void { if ( changes['templateRef'] !== undefined || changes['component'] !== undefined ) { if (isDebug()) dlog( `Angular VListOutlet#${this.instanceId} templateRef CHANGED (was=${changes['templateRef']?.previousValue !== undefined} now=${this.templateRef !== undefined}) -> clear + recreate`, ); this.viewContainer.clear(); countAngular('outletCreates'); this.componentRef = null; this.viewRef = null; if (this.component !== undefined) { this.componentRef = this.viewContainer.createComponent(this.component); this.writeComponentInputs(this.componentRef); return; } this.viewRef = this.templateRef === undefined ? null : this.viewContainer.createEmbeddedView( this.templateRef, this.context, ); return; } if (this.componentRef !== null) { countAngular('outletUpdates'); this.writeComponentInputs(this.componentRef); return; } if (this.viewRef !== null && this.context !== undefined) { countAngular('outletUpdates'); if (isDebug()) dlog( `Angular VListOutlet#${this.instanceId} context updated, markForCheck`, ); this.updateContext(this.viewRef.context, this.context); this.viewRef.markForCheck(); } } ngOnDestroy(): void { countAngular('outletDestroys'); if (isDebug()) dlog(`Angular VListOutlet#${this.instanceId} destroyed`); this.viewContainer.clear(); } // The item context becomes the component's inputs, only those it declares: RN hands a // `ListItemComponent` item, index and separators, and one may not read all three private writeComponentInputs(ref: ComponentRef): void { const { context } = this; if (!isRecord(context)) return; const declared = new Set( reflectComponentType(ref.componentType)?.inputs.map( input => input.propName, ), ); const inputs = { item: context['$implicit'], index: context['index'], separators: context['separators'], }; for (const [name, value] of Object.entries(inputs)) { if (declared.has(name)) ref.setInput(name, value); } } // Copy the new context's fields onto the live embedded view's context object (whose identity the // consumer's `let-` bindings already read), dropping keys that disappeared. The generic `C` can't // be index-WRITTEN even once narrowed (TS2862), so the copy runs in a non-generic helper the // guarded records flow into — no cast (NgTemplateOutlet does the same copy via `as any`). private updateContext(viewContext: C, nextContext: C): void { if (!isRecord(viewContext) || !isRecord(nextContext)) return; copyContextFields(viewContext, nextContext); } } function copyContextFields( target: Record, source: Record, ): void { for (const key of Object.keys(target)) { if (!(key in source)) delete target[key]; } for (const key of Object.keys(source)) { target[key] = source[key]; } }