> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# 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](/guide/native-plugins/index.md)'s `<native-switch>`, takes three steps:

```typescript title="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()`](#registerhostelement) instead.

## API

### `registerHostElement()`

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

Makes a component's [host element](/guide/content-projection.md#make-the-host-the-element) the Lynx element you name. Otherwise every host is a `<view>`.

```typescript title="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()`

```typescript
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](/guide/native-plugins/index.md)'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`.

```typescript title="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()`](#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](/guide/glossary.md#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`

```typescript
@Directive()
class LynxElementAttributes implements OnChanges {}
```

What every element directive in `LYNX_ELEMENTS` extends:

- the [attributes every element has](/api/angular-lynx/elements/index.md#attributes-on-every-element), as inputs
- `ngOnChanges()`, which sets each input on the element

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

```typescript title="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`](#lynxtagelement).**

## Types

### `HostElementTag`

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

The elements `registerHostElement()` can make a host.

### `LynxTagElement`

```typescript
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](/api/angular-lynx/element-events.md#withprefixes)) adds the `catch`, `capture-` and `global-` forms of each `bind` name:

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

## Related

- [Elements](/api/angular-lynx/elements/index.md): `LYNX_ELEMENTS` and the attributes every element takes.
- [Write a native element](/guide/native-plugins/elements.md): register a native element in your app's iOS and Android code.
- [Content projection](/guide/content-projection.md#make-the-host-the-element): when to make a host another element.
