If the built-in elements do not meet your requirements, you can extend Lynx's capabilities by creating custom native elements. This section will guide you through creating and registering custom elements on Android and iOS platforms.
The implementation of custom native elements can be broken down into several steps, including: declaring and registering elements, creating native views, handling styles and properties, event binding, etc. Let's take a simple custom input element <input> as an example to briefly introduce the implementation process of custom elements.
Where "input" corresponds to the tag name in the front-end DSL. When Lynx Engine parses this tag, it will look for the registered native element and create an instance.
You can use the LYNX_PROP_SETTER macro to listen for property changes passed from the front end and update the native view. For example, handling the value property of the <input> element:
Typically, Lynx Engine automatically calculates and updates the View layout information, so developers do not need to manually handle this. However, in some special cases, such as when additional adjustments to the View are required, you can obtain the latest layout information in the layoutDidFinished callback and apply custom logic.
In some scenarios, the front-end may need to respond to events from custom elements. For example, when the user types in the input box, the front-end might
need to capture and process the input data.
Here is an example of how to send a text input event from the <input> element to the front-end and how the front-end listens for the event.
The client listens to text input callbacks from the native view, and when the text changes, it uses [self.context.eventEmitter dispatchCustomEvent:eventInfo] to send the event to the front-end for handling.
In some cases, the front-end may need to directly manipulate custom elements via imperative APIs. You can make elements support such operations with LYNX_UI_METHOD.
When implementing the focus method, component developers need to return a status code to the frontend to indicate whether the operation was successful. For instance, the frontend call might fail, in which case an appropriate error status should be returned so that the frontend can handle it in the fail callback.
Lynx Engine defines several common error codes, and developers can return the appropriate status code in the method callback:
enum LynxUIMethodErrorCode { kUIMethodSuccess = 0, // Succeeded kUIMethodUnknown, // Unknown error kUIMethodNodeNotFound, // Cannot find corresponding element kUIMethodMethodNotFound, // No corresponding method on this element kUIMethodParamInvalid, // Invalid method parameters kUIMethodSelectorNotSupported, // Selector not supported};
The implementation of custom native elements involves several steps, including: declaring and registering the element, creating native views, handling styles and properties, event binding, etc. Let's take a simple custom input element <input> as an example to briefly introduce the implementation process of a custom element. The complete code can be viewed in LynxExplorer.
Locally registered elements are only available for the current LynxView instance.
Java
Kotlin
LynxViewBuilder lynxViewBuilder = new LynxViewBuilder();lynxViewBuilder.addBehavior(new Behavior("input") { @Override public LynxExplorerInput createUI(LynxContext context) { return new LynxExplorerInput(context); } });
Where "input" corresponds to the tag name in the front-end DSL. When the Lynx Engine encounters this tag, it will look for the registered native element and create an instance.
You can use the @LynxProp annotation to listen for property changes passed from the front-end and update the native view accordingly. For example, handling the value property of the <input> element:
Java
Kotlin
LynxExplorerInput.java
import android.content.Context;import androidx.appcompat.widget.AppCompatEditText;import com.lynx.tasm.behavior.LynxContext;import com.lynx.tasm.behavior.LynxProp;import com.lynx.tasm.behavior.ui.LynxUI;public class LynxExplorerInput extends LynxUI<AppCompatEditText> { public LynxExplorerInput(LynxContext context) { super(context); } @LynxProp(name = "value") public void setValue(String value) { if (!value.equals(mView.getText().toString())) { mView.setText(value); } } @Override protected AppCompatEditText createView(Context context) { AppCompatEditText view = new AppCompatEditText(context); //... return view; }}
Usually, the Lynx Engine will automatically calculate and update the View layout information, so developers don’t need to handle this manually. However, in some special cases, such as when extra adjustments are needed for the View, you can retrieve the latest layout information in the onLayoutUpdated callback and apply custom logic.
Java
Kotlin
LynxExplorerInput.java
import android.content.Context;import androidx.appcompat.widget.AppCompatEditText;import com.lynx.tasm.behavior.LynxContext;import com.lynx.tasm.behavior.LynxProp;import com.lynx.tasm.behavior.ui.LynxUI;public class LynxExplorerInput extends LynxUI<AppCompatEditText> { public LynxExplorerInput(LynxContext context) { super(context); } @Override public void onLayoutUpdated() { super.onLayoutUpdated(); int paddingTop = mPaddingTop + mBorderTopWidth; int paddingBottom = mPaddingBottom + mBorderBottomWidth; int paddingLeft = mPaddingLeft + mBorderLeftWidth; int paddingRight = mPaddingRight + mBorderRightWidth; mView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); } @Override protected AppCompatEditText createView(Context context) { AppCompatEditText view = new AppCompatEditText(context); //... return view; } @LynxProp(name = "value") public void setValue(String value) { if (!value.equals(mView.getText().toString())) { mView.setText(value); } }}
Event handling in native elements is usually done using the @LynxEvent annotation, which binds events between the front-end and native elements. For example, let’s implement a custom onChange event for the <input> element:
Java
Kotlin
LynxExplorerInput.java
import android.content.Context;import android.text.Editable;import android.text.TextWatcher;import androidx.appcompat.widget.AppCompatEditText;import com.lynx.tasm.behavior.LynxContext;import com.lynx.tasm.behavior.LynxProp;import com.lynx.tasm.behavior.ui.LynxUI;import com.lynx.tasm.event.LynxCustomEvent;import java.util.HashMap;import java.util.Map;public class LynxExplorerInput extends LynxUI<AppCompatEditText> { private void emitEvent(String name, Map<String, Object> value) { LynxCustomEvent detail = new LynxCustomEvent(getSign(), name); if (value != null) { for (Map.Entry<String, Object> entry : value.entrySet()) { detail.addDetail(entry.getKey(), entry.getValue()); } } getLynxContext().getEventEmitter().sendCustomEvent(detail); } @Override protected AppCompatEditText createView(Context context) { AppCompatEditText view = new AppCompatEditText(context); view.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { emitEvent("input", new HashMap<String, Object>() { { put("value", s.toString()); } }); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } }); return view; } public LynxExplorerInput(LynxContext context) { super(context); } @Override public void onLayoutUpdated() { super.onLayoutUpdated(); int paddingTop = mPaddingTop + mBorderTopWidth; int paddingBottom = mPaddingBottom + mBorderBottomWidth; int paddingLeft = mPaddingLeft + mBorderLeftWidth; int paddingRight = mPaddingRight + mBorderRightWidth; mView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); } @LynxProp(name = "value") public void setValue(String value) { if (!value.equals(mView.getText().toString())) { mView.setText(value); } }}
On the front-end, you need to bind the relevant input events for the text box. With the following code, the front-end will listen for events sent by the client and process the input data as needed.
Note: Front-end DSL uses bindxxx for event binding, such as bindinput for binding the input event.
#Supporting Direct Element Manipulation (Optional)
In some cases, the front-end may need to directly manipulate custom elements using imperative APIs. You can enable such operations on elements by using @LynxUIMethod.
When implementing the focus method, component developers need to return a status code to the frontend to indicate whether the operation was successful. For instance, the frontend call might fail, in which case an appropriate error status should be returned so that the frontend can handle it in the fail callback.
Lynx Engine predefines some common error codes, and the element developer can return the appropriate status code in the method callback:
enum LynxUIMethodErrorCode { kUIMethodSuccess, // Succeeded kUIMethodUnknown, // Unknown error kUIMethodNodeNotFound, // Cannot find corresponding element kUIMethodMethodNotFound, // No corresponding method on this element kUIMethodParamInvalid, // Invalid method parameters kUIMethodSelectorNotSupported, // Selector not supported}
The way to customize elements in the web can directly refer to Web Components
Once you have completed the development of a custom element, you can use it just like a built-in element. Below is a simple example of using an <input> element:
Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.