For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/angular-lynx/elements/host-and-native.md.

Host and Native Elements

Choose which Lynx element a component's host is, and use native elements your app's own iOS and Android code registers. import { registerHostElement, registerNativeElement, LynxElementAttributes, type HostElementTag, type LynxTagElement } from '@blotch/angular-lynx';

Usage

A native element of your own, such as a native plugin's <native-switch>, takes three steps:

src/app/native-switch.ts
import { Directive } from '@angular/core';
import {
  LynxElementAttributes,
  type LynxEvent,
  type LynxTagElement,
  registerNativeElement,
  type WithPrefixes,
} from '@blotch/angular-lynx';

// 1. Declare its attributes, so the template checker knows them.
@Directive({ selector: 'native-switch', inputs: ['checked'] })
export class NativeSwitch extends LynxElementAttributes {
  declare checked?: boolean;
}

// 2. Create the tag as the host's native element, not a <view>.
registerNativeElement('native-switch');

// 3. Type its events, so $event has the event's type.
declare global {
  interface HTMLElementTagNameMap {
    'native-switch': LynxTagElement<
      WithPrefixes<{ bindchange: LynxEvent<{ readonly checked: boolean }> }>
    >;
  }
}

To make a component's host an element other than <view>, call registerHostElement() instead.

API

registerHostElement()

function registerHostElement(
  component: Type<unknown>,
  element: HostElementTag,
): void;

Makes a component's host element the Lynx element you name. Otherwise every host is a <view>.

src/app/card-title.ts
@Component({
  selector: 'app-card-title',
  host: { class: 'text-lg font-semibold text-foreground' },
  template: '<ng-content />',
})
export class CardTitle {}

registerHostElement(CardTitle, 'text');
  • Call it once, after the class.
  • It applies to each element name in the component's selector, wherever a template uses the component.
  • It throws for a component without an element selector.

registerNativeElement()

function registerNativeElement(...tags: readonly string[]): void;

Has the runtime create each tag as the native element the host registered under that name: a LynxUI of its own. For example, a native plugin's <native-switch>, drawn by a UISwitch on iOS and Android's Switch.

Without it, a tag the runtime doesn't know is a <view>, as every component's host is. Call it once, next to the element's directive or in main.ts.

src/app/native-switch.ts
registerNativeElement('native-switch');

It throws for:

  • a name without a hyphen. Custom elements' names have one, so a tag can't be one of Lynx's own, such as view, or an HTML tag the runtime maps, such as div. Any hyphens HTML allows are fine (native--switch).
  • a name with other characters. It must start with a lowercase letter and hold only lowercase letters, digits and hyphens. Anything else would need escaping in the tag's class and CSS type selectors.
  • the tags the runtime creates itself (blur-view, scroll-view, …).
  • a component's host that registerHostElement() made another element.

Where the host app has no such element:

  • Register only tags the host has. On Android, an element nothing is registered for stops the elements after it from rendering. So a bundle that registers a tag shows less of the page in a host without that element, such as Lynx Explorer.
  • On Lynx for Web, the tag becomes an HTML element of that name. It's a plain inline element that shows its children, unless you define a custom element of that name for the web preview.

LynxElementAttributes

@Directive()
class LynxElementAttributes implements OnChanges {}

What every element directive in LYNX_ELEMENTS extends:

Extend it to declare a native element of your own, so the template checker knows its attributes:

src/app/native-switch.ts
@Directive({ selector: 'native-switch', inputs: ['checked'] })
export class NativeSwitch extends LynxElementAttributes {
  declare checked?: boolean;
}

registerNativeElement('native-switch');
  • Declare each attribute with declare, as the runtime's elements do, so the class has no field of its own. The input sets it, and ngOnChanges() sends it to the element.
  • Its events go in LynxTagElement.

Types

HostElementTag

type HostElementTag =
  'view' | 'text' | 'image' | 'scroll-view' | 'svg' | 'blur-view';

The elements registerHostElement() can make a host.

LynxTagElement

interface LynxTagElement<Events> extends HTMLElement {}

A Lynx tag as Angular's template checker sees it: an HTMLElement whose listeners also know Events, so $event has the event's type.

To give a native element of your own its events, add its tag to HTMLElementTagNameMap. WithPrefixes (see Element events) adds the catch, capture- and global- forms of each bind name:

declare global {
  interface HTMLElementTagNameMap {
    'native-switch': LynxTagElement<
      WithPrefixes<{ bindchange: LynxEvent<{ readonly checked: boolean }> }>
    >;
  }
}