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/hosted-children.md.

Show an element's children in a sheet

A plugin's native 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. This page shows how to keep those children interactive there.

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.

Use HostElementUI

Subclass HostElementUI instead of LynxUI:

  • showChildren(in:) moves the element's view into your container, with its children and its own background, border and shadow. It dispatches their touches from there.
  • returnChildren() puts it back.

In between, bindtap, bindtouchmove and gestures on the children work as they do on the page.

On iOS

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

final class NativeSheet: HostElementUI, UISheetPresentationControllerDelegate {
  private var sheet: UIViewController?
  // The app's `open`, which may turn true again while the sheet animates away.
  private var open = false
  private var dismissing = false

  @objc static func propSetterLookUp() -> [[String]] {
    [["open", NSStringFromSelector(#selector(setOpen(_:requestReset:)))]]
  }

  @objc func setOpen(_ value: Any?, requestReset: Bool) {
    open = requestReset ? false : (value as? Bool ?? false)
    if open, sheet == nil {
      present()
    } else if !open, let sheet, !dismissing {
      dismissing = true
      sheet.dismiss(animated: true) { [weak self] in self?.closed() }
    }
  }

  // An `open` that starts true: present once the view is in a window.
  override func viewDidMoveToWindow() {
    guard view().window != nil, open, sheet == nil else { return }
    DispatchQueue.main.async { [weak self] in
      guard let self, self.open, self.sheet == nil else { return }
      self.present()
    }
  }

  // The app removed the element: the sheet goes with it, without a `close`.
  override func willMove(toSuperComponent newSuperUI: LynxUI<UIView>?) {
    super.willMove(toSuperComponent: newSuperUI)
    guard newSuperUI == nil else { return }
    open = false
    if let sheet, !dismissing {
      dismissing = true
      sheet.dismiss(animated: true) { [weak self] in self?.closed() }
    }
  }

  private func present() {
    guard var top = view().window?.rootViewController else { return }
    while let presented = top.presentedViewController { top = presented }
    let sheet = UIViewController()
    sheet.view.backgroundColor = .systemBackground
    sheet.sheetPresentationController?.detents = [.medium(), .large()]
    sheet.sheetPresentationController?.delegate = self
    showChildren(in: sheet.view)
    top.present(sheet, animated: true)
    self.sheet = sheet
  }

  private func closed() {
    guard sheet != nil else { return }
    sheet = nil
    dismissing = false
    returnChildren()
    if open { present() }
  }

  // A swipe down closed it: tell the app, whose `open` is still true.
  func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
    open = false
    closed()
    context?.eventEmitter?.send(LynxDetailEvent(name: "close", targetSign: sign, detail: [:]))
  }
}

On Android

HostElementUI is a UIView (Lynx's), and showChildren(container) takes a ViewGroup:

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

import android.widget.FrameLayout
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.lynx.tasm.behavior.LynxContext
import com.lynx.tasm.behavior.LynxProp
import com.lynx.tasm.event.LynxDetailEvent
import dev.angularlynx.shell.HostElementUI

class NativeSheet(context: LynxContext) : HostElementUI(context) {
    private var dialog: BottomSheetDialog? = null

    // The app's `open`, which may turn true again before the dismiss listener runs.
    private var open = false

    // The app closed it: Dialog posts the dismiss listener, so this lasts until it runs.
    private var closing = false

    @LynxProp(name = "open")
    fun setOpen(open: Boolean) {
        this.open = open
        val dialog = dialog
        if (open && dialog == null) {
            // An `open` that starts true: wait until Lynx has put the view on the page.
            if (mView.isAttachedToWindow) {
                show()
            } else {
                mView.post { if (this.open && this.dialog == null) show() }
            }
        } else if (!open && dialog != null && !closing) {
            closing = true
            dialog.dismiss()
        }
    }

    // The app removed the element: the sheet goes with it, without a `close`.
    override fun destroy() {
        open = false
        val sheet = dialog
        if (sheet != null && !closing) {
            closing = true
            sheet.dismiss()
        }
        super.destroy()
    }

    private fun show() {
        val sheet = BottomSheetDialog(mView.context)
        val content = FrameLayout(sheet.context)
        showChildren(content)
        sheet.setContentView(content)
        sheet.setOnDismissListener {
            dialog = null
            returnChildren()
            if (closing) {
                closing = false
                if (open) show()
            } else {
                // A swipe down, a tap outside or back closed it: tell the app.
                open = false
                lynxContext.eventEmitter.sendCustomEvent(LynxDetailEvent(sign, "close", hashMapOf<String, Any>()))
            }
        }
        sheet.show()
        dialog = sheet
    }
}

Use it in a template

<native-sheet
  class="absolute left-0 w-full"
  style="top: 100%"
  [open]="open()"
  (bindclose)="open.set(false)"
>
  <text>Tapped {{ taps() }} times</text>
  <view (bindtap)="taps.set(taps() + 1)"><text>Tap me</text></view>
</native-sheet>

Rules for the element

Give it the container's width

The element is laid out on the page like any other, and keeps the width and height it has there. So give it the container's width, and keep it out of the way: below the page, or position: absolute off screen.

Close your container when the element goes

Lynx removes the element's view, but knows nothing of your sheet or dialog. Close it yourself, as the code above does:

  • On iOS, when willMove(toSuperComponent:) gets nil.
  • On Android, in destroy().

Lynx tells only the element it removes. So close the sheet before you remove one of its ancestors:

  • Set open to false in an earlier update than the one that removes the ancestor.
  • The same update isn't enough. Angular doesn't refresh a view it's removing, so the sheet never gets open false, and it stays on screen.

Present once there's somewhere to present

Lynx sets props while it creates the element, before its view is on the page. On iOS, viewDidMoveToWindow() says when it's in a window.

Style the element as you would on the page

On iOS and Android, its background, border, box shadow and outline go into the container with it. On iOS, they follow a change of style while it's there.

  • Give it its shadow or outline before you open the container. On Android, a first one added while it's open puts the element back on the page until the container closes.
  • A shadow or outline that reaches outside the element can be cut off at the container's edges.

Touches read as on the page

  • x and y on a child's touch are measured from the child.
  • pageX and pageY are where the finger is from the Lynx view's top left.

They hold in any container, including one whose window doesn't fill the screen, such as a PopupWindow or a dialog in the middle of the screen.