// createAnimatedComponent for @symbiote-native/angular. React/Vue have a runtime HOC // (`createAnimatedComponent(Component)` builds a fresh wrapped component on the fly); Angular // can't — the AOT-under-Metro build compiles every @Component ahead of time and ships no JIT // compiler to Hermes, so a runtime `Component({...})(class)` would throw. The AOT-safe // equivalent is an EXPLICIT set of standalone components — AnimatedView / AnimatedText / // AnimatedImage / AnimatedScrollView — each a thin @Component over the matching host primitive, // sharing one abstract lifecycle base. The mechanism (build an AnimatedProps leaf, reduce // animated props to current values for the first paint, bind the leaf to the committed host // node, swap leaves on re-render, tear down on destroy) lives once in AnimatedComponentBase. // // The framework-agnostic half (reduceProps / readPassthroughStyle / resolveHostNode / // AnimatedProps / attachNativeEventHandler / isNativeAnimatedAvailable) comes from // @symbiote-native/engine, shared with React and Vue. The per-frame path NEVER goes through // Angular change detection: value.setValue / animation -> flushValue -> AnimatedProps.update() // -> setNativeProps(node), same as the other adapters. import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, Directive, ElementRef, inject, signal, ViewChild, type AfterViewInit, type DoCheck, type OnChanges, type OnDestroy, type Type, } from '@angular/core'; import { isNativeAnimatedAvailable, isSymbioteNode, reduceProps, readPassthroughStyle, resolveHostNode, type ISymbioteNode, } from '@symbiote-native/engine'; import { Text, View } from '../../components'; import { anchorHostStyle, ImageHost, SymbioteHostPropsDirective, SymbioteStyleInputDirective, TextHost, ViewHost, } from '../../primitives'; import { FlatList } from '../../components/flat-list'; import { IMAGE_INPUTS, IMAGE_OUTPUTS, ImageBase, isImageEventCallback, resolveImageProps, } from '../../components/image-shared'; import type { IGatedAccessibilityEvent } from '../../gate-demand'; import { SectionList } from '../../components/section-list'; import { AnimatedLeafBinder } from './animated-leaf-binder'; // The intrinsic `createAnimatedComponent` accepts in place of the deleted `Image` component. const IMAGE_TAG = 'image'; // Same move for the deleted `ScrollView` component — the vertical axis's own tag stands in for // the class identity the generic dispatcher used to compare against. const SCROLL_VIEW_TAG = 'scroll-view'; // RN's prop carrying explicit (already-rasterized) values that override the animated prop in // the COMMITTED props (sticky-header passthrough). Named once so the directive input and the // reconcile agree. const PASSTHROUGH_PROP = 'passthroughAnimatedPropExplicitValues'; // The inputs every animated wrapper accepts. `style` is the (possibly animated) style — the // common path (TouchableOpacity passes `[style, { opacity }]`). `animatedProps` is the generic // escape hatch for any OTHER prop that may hold an AnimatedNode or an Animated.event handler, // plus static host props to forward (the React/Vue `rest`). Each concrete @Component re-lists // these (Angular's inputs-on-base convention, mirroring Switch/Image). // `testID` is listed alongside them rather than left to `animatedProps`, which is the documented // escape hatch for everything else. Every other component in this adapter takes it as an ordinary // input, and React's `createAnimatedComponent` spreads `...props`, so `` // is what an app writes — it compiled (a static attribute on a component tag is never checked), // landed on the non-painting anchor host and committed nowhere. export const ANIMATED_INPUTS = [ 'style', 'testID', 'animatedProps', PASSTHROUGH_PROP, ]; export const ANIMATED_IMAGE_INPUTS = [ ...IMAGE_INPUTS, 'animatedProps', PASSTHROUGH_PROP, ]; // The shared lifecycle for every animated wrapper. Concrete subclasses add ONLY a @Component // decorator (selector + the host-primitive template) — no behavior. Plain fields (no @Input // here): the inputs are declared on each concrete via ANIMATED_INPUTS, the adapter convention. // @Directive() (no selector) is the Angular-sanctioned decorator for an abstract base that // declares lifecycle hooks — without it ngtsc rejects the inherited hooks (NG2007). @Directive() export abstract class AnimatedComponentBase implements AfterViewInit, DoCheck, OnChanges, OnDestroy { style: unknown; testID: string | undefined; animatedProps: Record | undefined; passthroughAnimatedPropExplicitValues: unknown; // This component's OWN host — every Animated* wrapper is a non-painting ANCHOR // (ANCHOR_HOST_COMPONENTS in renderer.ts), so a `class="..."` at the use site resolves onto // THIS element, never the real inner host primitive one level down (see anchorHostStyle's doc // comment in primitives/shared.ts — the same reason ScrollView's own scrollProps merges it in). private readonly elementRef = inject(ElementRef); // The directive sitting on the inner host primitive; its `node` is the committed SymbioteNode // the leaf binds to. Inherited by the decorated subclass (Angular collects base-class queries). @ViewChild(SymbioteHostPropsDirective) private hostDirective?: SymbioteHostPropsDirective; // The leaf-lifecycle orchestration (build/attach/swap/bind/detach an AnimatedProps leaf) is a // Pure Fabrication shared with AnimatedImage, which cannot extend this class (it must extend // ImageBase — see the file header). `resolveNode` stays a resolver, not a captured node: the // committed node only exists after `hostDirective` is populated post-view-init. private readonly leafBinder = new AnimatedLeafBinder( () => this.hostNode(), this.constructor.name, ); // Set once the view (and the inner host directive) exists; ngOnChanges before then is a no-op. private viewReady = false; // The concrete props for the host element each change-detection pass: animated entries reduced // to their current value, then the passthrough style layered on top (last wins via the style // array, which the commit layer flattens) so the ShadowTree carries the current transform. get reducedProps(): Record { const reduced = reduceProps(this.mergedProps()); const passthroughStyle = readPassthroughStyle( this.passthroughAnimatedPropExplicitValues, ); if (passthroughStyle !== undefined) { reduced['style'] = reduced['style'] === undefined ? passthroughStyle : [reduced['style'], passthroughStyle]; } reduced['style'] = [this.anchorStyle(), reduced['style']]; return reduced; } // The anchor's class-derived style, polled rather than read live inside `reducedProps`. // // Two different writers land style on this component's anchor and NEITHER goes through // ngOnChanges: `class=` / `[class.x]` / `[ngClass]` at the use site (via the renderer's // addClass/removeClass -> commitClassStyle), and a `[style]` binding from a composing parent // that Angular writes as a host property because `style` is not declared as an @Input here. // Reading them live in the getter worked at CREATION only - nothing dirties THIS view when the // anchor changes, so the getter was never re-read and the update was lost. Device-visible as a // TouchableOpacity whose `class` toggled after mount never repainted. // // ngDoCheck runs during the PARENT's refresh even when this view is skipped, and the signal // write is what then marks this view for refresh. `signal.set`'s own Object.is makes an // unchanged poll a no-op, so a steady screen costs one property read per pass and nothing else. private readonly anchorStyle = signal(undefined); ngDoCheck(): void { this.anchorStyle.set(anchorHostStyle(this.elementRef)); } ngAfterViewInit(): void { this.viewReady = true; this.reconcile(); } ngOnChanges(): void { this.reconcile(); } ngOnDestroy(): void { this.leafBinder.destroy(); } // A non-stable [animatedProps]/[style] input (a fresh object literal in the template) pushes // a new reference on every change-detection pass of the HOST view, re-triggering this on every // unrelated interaction elsewhere in the app — DEBUG=1 turns this into a visible tick count in // AnimatedLeafBinder.reconcile, so an unexpectedly high rate for a component nobody is // animating is the tell (see AnimatedScrollView's stable-reference fix in examples/angular/App.ts). private reconcile(): void { if (!this.viewReady) return; const props = this.mergedProps(); const hasPassthroughAnimatedValues = this.passthroughAnimatedPropExplicitValues !== null && this.passthroughAnimatedPropExplicitValues !== undefined; const wantsNative = hasPassthroughAnimatedValues && isNativeAnimatedAvailable(); this.leafBinder.reconcile(props, wantsNative); } // Merge the dedicated `style` input over the generic `animatedProps` bag into one props map — // the equivalent of React/Vue `rest`. `style` wins so `[style]` and `[animatedProps].style` // never disagree. private mergedProps(): Record { const base = this.animatedProps ?? {}; const merged: Record = this.style === undefined ? { ...base } : { ...base, style: this.style }; // Only when set, so an unset input cannot clear a `testID` the app put in `animatedProps`. if (this.testID !== undefined) merged.testID = this.testID; return merged; } // The committed host node held by IDENTITY. resolveHostNode unwraps an imperative scroll // handle to the underlying SymbioteNode (View / Text already hand back the node directly); // the isSymbioteNode guard keeps us off a cast. private hostNode(): ISymbioteNode | null { const resolved = resolveHostNode(this.hostDirective?.node); return isSymbioteNode(resolved) ? resolved : null; } } @Component({ selector: 'AnimatedView, symbiote-animated-view', standalone: true, hostDirectives: [ { directive: SymbioteStyleInputDirective, inputs: ['style'] }, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], imports: [SymbioteHostPropsDirective, ViewHost], inputs: ANIMATED_INPUTS, template: ` `, changeDetection: ChangeDetectionStrategy.OnPush, }) export class AnimatedView extends AnimatedComponentBase {} @Component({ selector: 'AnimatedText, symbiote-animated-text', standalone: true, hostDirectives: [ { directive: SymbioteStyleInputDirective, inputs: ['style'] }, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], imports: [SymbioteHostPropsDirective, TextHost], inputs: ANIMATED_INPUTS, template: ` `, changeDetection: ChangeDetectionStrategy.OnPush, }) export class AnimatedText extends AnimatedComponentBase {} @Component({ selector: 'AnimatedImage, symbiote-animated-image', standalone: true, hostDirectives: [ { directive: SymbioteStyleInputDirective, inputs: ['style'] }, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], imports: [SymbioteHostPropsDirective, ImageHost], inputs: ANIMATED_IMAGE_INPUTS, outputs: IMAGE_OUTPUTS, template: ` `, changeDetection: ChangeDetectionStrategy.OnPush, }) export class AnimatedImage extends ImageBase implements AfterViewInit, OnChanges, OnDestroy { animatedProps: Record | undefined; passthroughAnimatedPropExplicitValues: unknown; // AnimatedImage is a non-painting ANCHOR too (ANCHOR_HOST_COMPONENTS) — see the matching // comment on AnimatedComponentBase.elementRef. private readonly elementRef = inject(ElementRef); @ViewChild(SymbioteHostPropsDirective) private hostDirective?: SymbioteHostPropsDirective; // Same Pure Fabrication AnimatedComponentBase holds — see its matching comment. AnimatedImage // can't extend AnimatedComponentBase (it must extend ImageBase, see the file header), so it // gets the leaf-lifecycle via composition instead of duplicating it. private readonly leafBinder = new AnimatedLeafBinder( () => this.hostNode(), this.constructor.name, ); private viewReady = false; private gateInto( props: Record, name: IGatedAccessibilityEvent, key: string, ): void { const carried = props[key]; props[key] = this.gatedAccessibilityHandler( name, isImageEventCallback(carried) ? carried : undefined, ); } get animatedImageProps(): Record { const reduced = reduceProps(this.mergedProps()); const passthroughStyle = readPassthroughStyle( this.passthroughAnimatedPropExplicitValues, ); if (passthroughStyle !== undefined) { reduced['style'] = reduced['style'] === undefined ? passthroughStyle : [reduced['style'], passthroughStyle]; } const resolved = resolveImageProps(reduced); resolved['style'] = [anchorHostStyle(this.elementRef), resolved['style']]; // The four gated accessibility events, which used to be template bindings on the host tag and // therefore lit their Fabric flags on every AnimatedImage in the app // (`.claude/rules/fabric-boolean-event-gates.md`). Routed through the base's gate instead, so // both channels still reach the app — the `onX` callback carried in the animated prop bag, and // the `@Output()` an app may bind on (IMAGE_OUTPUTS declares all four). this.gateInto(resolved, 'accessibilityAction', 'onAccessibilityAction'); this.gateInto(resolved, 'accessibilityTap', 'onAccessibilityTap'); this.gateInto(resolved, 'magicTap', 'onMagicTap'); this.gateInto(resolved, 'accessibilityEscape', 'onAccessibilityEscape'); return resolved; } // The anchor's class-derived style, polled rather than read live inside `reducedProps`. // // Two different writers land style on this component's anchor and NEITHER goes through // ngOnChanges: `class=` / `[class.x]` / `[ngClass]` at the use site (via the renderer's // addClass/removeClass -> commitClassStyle), and a `[style]` binding from a composing parent // that Angular writes as a host property because `style` is not declared as an @Input here. // Reading them live in the getter worked at CREATION only - nothing dirties THIS view when the // anchor changes, so the getter was never re-read and the update was lost. Device-visible as a // TouchableOpacity whose `class` toggled after mount never repainted. // // ngDoCheck runs during the PARENT's refresh even when this view is skipped, and the signal // write is what then marks this view for refresh. `signal.set`'s own Object.is makes an // unchanged poll a no-op, so a steady screen costs one property read per pass and nothing else. private readonly anchorStyle = signal(undefined); ngDoCheck(): void { this.anchorStyle.set(anchorHostStyle(this.elementRef)); } ngAfterViewInit(): void { this.viewReady = true; this.reconcile(); } ngOnChanges(): void { this.reconcile(); } ngOnDestroy(): void { this.leafBinder.destroy(); } private reconcile(): void { if (!this.viewReady) return; const props = this.mergedProps(); const hasPassthroughAnimatedValues = this.passthroughAnimatedPropExplicitValues !== null && this.passthroughAnimatedPropExplicitValues !== undefined; const wantsNative = hasPassthroughAnimatedValues && isNativeAnimatedAvailable(); this.leafBinder.reconcile(props, wantsNative); } private mergedProps(): Record { const props: Record = { ...(this.animatedProps ?? {}) }; for (const [key, value] of Object.entries(this.imageInputProps)) { if (value !== undefined) props[key] = value; } return props; } private hostNode(): ISymbioteNode | null { const resolved = resolveHostNode(this.hostDirective?.node); return isSymbioteNode(resolved) ? resolved : null; } } @Component({ selector: 'AnimatedScrollView, symbiote-animated-scroll-view', standalone: true, hostDirectives: [ { directive: SymbioteStyleInputDirective, inputs: ['style'] }, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], imports: [SymbioteHostPropsDirective], inputs: ANIMATED_INPUTS, template: ` `, changeDetection: ChangeDetectionStrategy.OnPush, }) export class AnimatedScrollView extends AnimatedComponentBase { // Nothing left to override: `registerScrollViewBehavior()` owns the content node, base style // and sticky seam for this SAME `scroll-view` tag. Building `` here too would // give the node two owners; `` lands under the one the behavior already builds. } // List components are already explicit AOT-compiled Angular components with their own full input // surface and inner ScrollView composition. Expose them in the Animated namespace without runtime // synthesis; app code passes animated style/onScroll through the existing list inputs. export const AnimatedFlatList = FlatList; export const AnimatedSectionList = SectionList; // Surface-parity shim for the React/Vue `createAnimatedComponent(Component)`. Angular cannot // synthesize a component at runtime (no JIT under AOT/Metro — see the file header), so this maps // the built-in primitives to their pre-authored wrappers and refuses anything else with a // pointer to the real Angular idiom: author a standalone @Component extending // AnimatedComponentBase. export function createAnimatedComponent(base: unknown): Type { if (base === View) return AnimatedView; if (base === Text) return AnimatedText; // The TAG, not a component identity: `ScrollView`/`Image` are gone as classes now // (`components/scroll-view-props.ts`, `modules/image`), so there is no class to compare // against — the same move React made (`createAnimatedComponent('image')`). A tag string is also // what portable app code can pass on every adapter. if (base === SCROLL_VIEW_TAG) return AnimatedScrollView; if (base === IMAGE_TAG) return AnimatedImage; throw new Error( 'createAnimatedComponent: Angular cannot synthesize a component at runtime (no JIT compiler ' + 'under AOT/Metro). Author an explicit standalone @Component extending AnimatedComponentBase ' + "instead. The built-in primitives View / Text and the tags 'scroll-view' / 'image' map to " + 'AnimatedView / AnimatedText / AnimatedScrollView / AnimatedImage.', ); }