// Modal: the Angular lifecycle half. RCTModalHostView is an ordinary Fabric host node committing // through the same childSet as the rest of the tree (no second JS surface). The style math (the // backdrop override, the container/host styles, the presentationStyle default), the visible gate, // and the iOS keep-alive reducer all live framework-agnostic in @symbiote-native/components and are shared // verbatim with React/Vue; here Angular supplies only the lifecycle: the keep-alive state, armed on // show (ngOnChanges) and dropped by the native dismiss on iOS, reusing renderModal's resolved props. // The user children nest UNDER the container View via . import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, ElementRef, EventEmitter, inject, Input, Output, signal, type DoCheck, type OnChanges, type OnInit, type SimpleChanges, } from '@angular/core'; import { createInitialModalState, isModalVisible, modalReducer, modalVisibilityAction, renderModal, resolveAccessibilityProps, shouldRenderModal, warnAboutModalProps, type IAccessibilityProps, type IAriaProps, type IModalAnimationType, type IModalOrientation, type IModalPresentationStyle, type IModalState, } from '@symbiote-native/components'; import { dlog, isSymbioteEvent, Platform, type IColorValue, type IStyleProp, type ISymbioteEvent, type IViewStyle, } from '@symbiote-native/engine'; import { AccessibilityEventsBase } from '../../accessibility-events'; import { ListScopeProvider, NO_LIST_SCOPE, } from '../virtualized-list/nested-scope'; import { anchorHostStyle, ModalHost, SymbioteHostPropsDirective, SymbioteStyleInputDirective, ViewHost, } from '../../primitives'; export type { IModalAnimationType, IModalPresentationStyle, IModalOrientation, IModalOrientationChangeEvent, } from '@symbiote-native/components'; // Mirrors React's IModalProps minus children (Angular takes children via ). export type IAngularModalProps = IAccessibilityProps & IAriaProps & { visible?: boolean; transparent?: boolean; backdropColor?: IColorValue; animationType?: IModalAnimationType; presentationStyle?: IModalPresentationStyle; supportedOrientations?: ReadonlyArray; hardwareAccelerated?: boolean; statusBarTranslucent?: boolean; navigationBarTranslucent?: boolean; allowSwipeDismissal?: boolean; onShow?: () => void; onDismiss?: () => void; onRequestClose?: () => void; // The engine hands every listener the ISymbioteEvent wrapper, so the orientation is read at // event.nativeEvent.orientation (IModalOrientationChangeEvent describes that payload). onOrientationChange?: (event: ISymbioteEvent) => void; style?: IStyleProp; }; // What the Modal component itself takes as plain @Input()s: the full surface minus the // show/dismiss/close/orientation and accessibility events, which it exposes as real @Output() // EventEmitters instead (see the class below), mirroring the Pressable family's onPress -> press // conversion (adapters/angular/src/components/pressable/index.ts). export type IAngularModalInputs = Omit< IAngularModalProps, | 'onShow' | 'onDismiss' | 'onRequestClose' | 'onOrientationChange' | 'onAccessibilityAction' | 'onAccessibilityTap' | 'onMagicTap' | 'onAccessibilityEscape' >; @Component({ selector: 'Modal', standalone: true, hostDirectives: [ { directive: SymbioteStyleInputDirective, inputs: ['style'] }, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], imports: [ModalHost, ViewHost, SymbioteHostPropsDirective], // The content sits outside the list's scroll, so a list in it is not nested in that list providers: [{ provide: ListScopeProvider, useValue: NO_LIST_SCOPE }], changeDetection: ChangeDetectionStrategy.OnPush, template: ` @if (shouldRender) { } `, }) export class Modal extends AccessibilityEventsBase implements IAngularModalInputs, OnInit, OnChanges, DoCheck { @Output() readonly show = new EventEmitter(); @Output() readonly dismiss = new EventEmitter(); @Output() readonly requestClose = new EventEmitter(); @Input() visible?: boolean; @Input() transparent?: boolean; @Input() backdropColor?: IColorValue; @Input() animationType?: IModalAnimationType; @Input() presentationStyle?: IModalPresentationStyle; @Input() supportedOrientations?: ReadonlyArray; @Input() hardwareAccelerated?: boolean; @Input() statusBarTranslucent?: boolean; @Input() navigationBarTranslucent?: boolean; @Input() allowSwipeDismissal?: boolean; @Input() style?: IStyleProp; @Input() testID?: string; @Output() readonly orientationChange = new EventEmitter(); // The iOS keep-alive (state/modal.ts): after visible→hidden the node stays until the native // onDismiss (`handleDismiss`) drops it. private state: IModalState = createInitialModalState(false); private readonly changeDetector = inject(ChangeDetectorRef); // This component's OWN host — the non-painting anchor `class="..."` at the use site resolves // onto (see anchorHostStyle's doc comment) — NOT the inner `view [style]="containerStyle"` // that hosts the user children; the outer `modal` node (bound via `hostProps` below) is // the real primitive the anchor sits in front of. private readonly elementRef = inject(ElementRef); ngOnInit(): void { this.state = createInitialModalState(isModalVisible(this.visible)); } // RN checks on mount and on every update, a dev build only private warnOnMisuse(): void { warnAboutModalProps({ presentationStyle: this.presentationStyle, transparent: this.transparent, navigationBarTranslucent: this.navigationBarTranslucent, statusBarTranslucent: this.statusBarTranslucent, allowSwipeDismissal: this.allowSwipeDismissal, onRequestClose: this.requestClose.observed, }); } // Bridges the non-reactive @Input fields `hostProps` reads into the reactive graph, so it can // memoize. Plain fields read inside a computed() are UNTRACKED - something must signal "a // dependency changed" or the bag goes stale. Signal inputs would do this natively, but `input()` // is visible only to the AOT compiler and this package's unit suite runs on JIT (see the // `angular-adapter-change-detection` skill, §6); `signal()`/`computed()` are plain runtime APIs. // The keep-alive `state` is deliberately NOT a dependency: it gates `shouldRender`, never the bag. private readonly hostPropsRevision = signal(0); // What the anchor's class-derived style was when the bag was last built (identity, not value). private lastAnchorStyle: unknown; ngOnChanges(changes: SimpleChanges): void { // Before the keep-alive early-return below: this is the single moment Angular has finished // writing every changed @Input, and `hostProps` depends on far more than `visible`. this.hostPropsRevision.update(revision => revision + 1); this.warnOnMisuse(); const visibleChange = changes.visible; // First change is reflected by the ngOnInit seed; only later toggles drive the keep-alive. if (visibleChange === undefined || visibleChange.firstChange) return; // Arms the iOS keep-alive on show; a hide is left to the native dismiss (state/modal.ts). const action = modalVisibilityAction(isModalVisible(this.visible)); if (action === undefined) return; queueMicrotask(() => { this.state = modalReducer(this.state, action); this.changeDetector.markForCheck(); }); } // Modal.js: onDismiss is iOS-only — it drops the keep-alive, then tells the app. handleDismiss(): void { if (Platform.OS !== 'ios') return; this.state = modalReducer(this.state, { type: 'hide' }); this.changeDetector.markForCheck(); this.dismiss.emit(); } get shouldRender(): boolean { const render = shouldRenderModal(isModalVisible(this.visible), this.state); if (!render) dlog('Modal hidden -> no node committed'); return render; } // renderModal owns the backdrop/presentationStyle/style math (shared with React/Vue); the // adapter reads the resolved host + container props off the Descriptor it returns. private get descriptor() { return renderModal({ visible: this.visible, transparent: this.transparent, backdropColor: this.backdropColor, animationType: this.animationType, presentationStyle: this.presentationStyle, supportedOrientations: this.supportedOrientations, hardwareAccelerated: this.hardwareAccelerated, statusBarTranslucent: this.statusBarTranslucent, navigationBarTranslucent: this.navigationBarTranslucent, allowSwipeDismissal: this.allowSwipeDismissal, style: this.style, passthrough: resolveAccessibilityProps(this.accessibilityInputs()), }); } // The anchor's class-derived style is NOT an @Input: `class="..."`/`[ngClass]` at the use site // resolves through the renderer's addClass onto this component's anchor host (see // anchorHostStyle's doc comment), so it never shows up in SimpleChanges and ngOnChanges alone // would leave a later class toggle stranded. ngDoCheck runs at exactly the cadence the old // getter was re-read, and bumps only on a real identity change. ngDoCheck(): void { const anchorStyle = anchorHostStyle(this.elementRef); if (anchorStyle === this.lastAnchorStyle) return; this.lastAnchorStyle = anchorStyle; this.hostPropsRevision.update(revision => revision + 1); } // renderModal's own MODAL_HOST_STYLE (position:'absolute') is the outer modal node's // style; the anchor's class-derived style goes FIRST, that resolved style SECOND — // flattenStyle's later-wins collapse keeps the modal's own style winning over its ambient class. // // A computed(), not a getter: Angular re-reads a template getter on every refresh of this view // and `[symbioteHostProps]` compares by REFERENCE, so a freshly rebuilt (but identical) bag // re-pushes every key through renderer.setProperty -> the engine's prop routing - and this one // re-runs renderModal to build it. readonly hostProps = computed>(() => { this.hostPropsRevision(); const descriptorProps = this.descriptor.props; return { ...descriptorProps, style: [anchorHostStyle(this.elementRef), descriptorProps.style], onAccessibilityAction: this.eventEmitterHandler(this.accessibilityAction), onAccessibilityTap: this.eventEmitterHandler(this.accessibilityTap), onMagicTap: this.eventEmitterHandler(this.magicTap), onAccessibilityEscape: this.eventEmitterHandler(this.accessibilityEscape), }; }); // Through the props bag rather than `[collapsable]="false"`: `view` matches `ViewHost`, a real // component, so a binding it does not declare is NG8002 under `ngc` — and `CUSTOM_ELEMENTS_SCHEMA` // does not rescue a binding on a MATCHED component. Invisible to `tsc` and to vitest; only a real // AOT build says so. Frozen and shared, so the directive's own diff sees one unchanged object. readonly containerProps: Readonly> = Object.freeze({ collapsable: false, }); get containerStyle(): unknown { const container = this.descriptor.children[0]; return typeof container === 'string' ? undefined : container.props.style; } // NOT private: `(orientationChange)="emit(...)"` in the template above calls it, and a template // can only reach public members. `tsc` does not typecheck Angular templates, so `private` here // compiles clean and fails only under `ngc --compilationMode partial`. emit(emitter: EventEmitter, event: unknown): void { if (isSymbioteEvent(event)) emitter.emit(event); } // The four accessibility events are boolean-GATED Fabric events // (`.claude/rules/fabric-boolean-event-gates.md`) — `show`/`dismiss`/`requestClose`/ // `orientationChange` are NOT (Modal's own MODAL_EVENTS), so those stay unconditional template // bindings above. `.observed`-gated, mirroring Pressable's. private eventEmitterHandler( emitter: EventEmitter, ): ((event: unknown) => void) | undefined { return emitter.observed ? event => this.emit(emitter, event) : undefined; } // Typed as the a11y intersection WITH the string index (the bag renderModal spreads into the // host props), so resolveAccessibilityProps's result stays assignable to passthrough. private accessibilityInputs(): IAccessibilityProps & IAriaProps & Record { return { testID: this.testID, ...this.accessibilityInputProps() }; } }