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 /guide/native-plugins/elements.md.

Write a native element

A native element is a tag your templates use that the platform's own control draws, such as a UISwitch and an Android Switch, a map or a camera preview. Add it to a native plugin, then type it for Angular's template checker.

Write the element

An element is a LynxUI subclass that makes the platform's view:

  • Attributes your template sets arrive in setters.
  • Events go out with LynxDetailEvent.

On iOS

Swift can't use Lynx's LYNX_PROP_SETTER macro. So the class lists its attributes in propSetterLookUp(), each with a setter that takes the value and requestReset:

native/greeter/ios/NativeSwitch.swift
import Lynx
import UIKit

final class NativeSwitch: LynxUI<UISwitch> {
  @objc static func propSetterLookUp() -> [[String]] {
    [["checked", NSStringFromSelector(#selector(setChecked(_:requestReset:)))]]
  }

  override func createView() -> UISwitch? {
    let control = UISwitch()
    control.addTarget(self, action: #selector(flipped), for: .valueChanged)
    return control
  }

  @objc func setChecked(_ value: Any?, requestReset: Bool) {
    view().isOn = requestReset ? false : (value as? Bool ?? false)
  }

  @objc private func flipped() {
    context?.eventEmitter?.send(
      LynxDetailEvent(name: "change", targetSign: sign, detail: ["checked": view().isOn]))
  }
}

On Android

An attribute's setter is marked @LynxProp.

Give LynxDetailEvent a map Lynx can add to, such as a HashMap. Lynx adds keys of its own, and a Kotlin mapOf() throws.

native/greeter/android/com/example/greeter/NativeSwitch.kt
package com.example.greeter

import android.content.Context
import android.widget.Switch
import com.lynx.tasm.behavior.LynxContext
import com.lynx.tasm.behavior.LynxProp
import com.lynx.tasm.behavior.ui.LynxUI
import com.lynx.tasm.event.LynxDetailEvent

class NativeSwitch(context: LynxContext) : LynxUI<Switch>(context) {
    private var setting = false

    override fun createView(context: Context): Switch =
        Switch(context).apply {
            setOnCheckedChangeListener { _, checked ->
                if (!setting) {
                    lynxContext.eventEmitter.sendCustomEvent(
                        LynxDetailEvent(sign, "change", hashMapOf<String, Any>("checked" to checked)),
                    )
                }
            }
        }

    @LynxProp(name = "checked")
    fun setChecked(checked: Boolean) {
        setting = true
        mView.isChecked = checked
        setting = false
    }
}

Register it and size it

Name the tag and its class in the plugin's elements, for each platform:

angular.json
"ios": { "elements": { "native-switch": "NativeSwitch" } },
"android": {
  "packageName": "com.example.greeter",
  "elements": { "native-switch": "NativeSwitch" }
}

See Make a plugin for the rest of the entry, and the tags you can't use.

An element is laid out like a <view>. Give it a size with CSS. It doesn't measure itself from its view.

Type the element

The runtime draws a tag it doesn't know as a <view>. So tell it the tag is a native element with registerNativeElement().

Then type it:

  • A directive gives the template checker its attributes.
  • HTMLElementTagNameMap gives it its events.
src/app/native-switch.ts
import { Directive } from '@angular/core';
import {
  LynxElementAttributes,
  type LynxEvent,
  type LynxTagElement,
  type WithPrefixes,
  registerNativeElement,
} from '@blotch/angular-lynx';

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

registerNativeElement('native-switch');

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

Import the directive where you use the element. ng test then checks the binding's type and types $event:

<native-switch
  style="width: 52px; height: 32px"
  [checked]="on()"
  (bindchange)="on.set($event.detail.checked)"
/>

Keep the screens that use it to the apps you build. Lynx Explorer and the web preview don't have your element. See Elsewhere, your plugin isn't there.

Show its children somewhere else

An element can show the Angular elements inside it in a view of the platform's own, outside the page. For example, a system sheet, a popover or a dialog.

Lynx sends a touch to the elements it finds from the page's root view. So children whose views you move out draw, but never get a tap.

To keep them interactive, subclass HostElementUI instead of LynxUI. See Show an element's children in a sheet.