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:
To make a component's host an element other than <view>, call registerHostElement() instead.
API
registerHostElement()
Makes a component's host element the Lynx element you name. Otherwise every host is a <view>.
- 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()
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.
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 asdiv. 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
What every element directive in LYNX_ELEMENTS extends:
- the attributes every element has, 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:
- Declare each attribute with
declare, as the runtime's elements do, so the class has no field of its own. The input sets it, andngOnChanges()sends it to the element. - Its events go in
LynxTagElement.
Types
HostElementTag
The elements registerHostElement() can make a host.
LynxTagElement
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:
Related
- Elements:
LYNX_ELEMENTSand the attributes every element takes. - Write a native element: register a native element in your app's iOS and Android code.
- Content projection: when to make a host another element.
