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

# `<overlay>`

APISummary
`<overlay>` 是一个不参与页面布局的承载容器。它本身不占据任何空间，也不需要设置宽度和高度。

`<overlay>`被渲染时，其内部的内容完全脱离 Lynx 文档流，被提升到 Lynx 之外的渲染层级进行展示。

`<overlay>`的唯一子节点默认以屏幕尺寸作为宽高基准，推荐将其视为一个独立的绘制容器，用于承载完整的蒙层内容。

:::info

**适用场景**

- 在客户端页面中嵌入 Lynx 子模块时，可以使用`<overlay>`来构建弹窗，达到覆盖整个应用页面的效果。
- 当完全使用 Lynx 来构建一整个页面时，**不建议**使用`<overlay>`，直接使用`position: fixed`即可构建弹窗能力。

:::

## 使用指南

### 必要条件

- `<overlay>`**只能有一个直接子节点**

- `<overlay>`自身必须设置 **`position:fixed`**

### 推荐用法

- 推荐将`<overlay>`的唯一子节点作为 backdrop 容器：
- 即使背景是透明的，也建议显示绘制
- 显示设置尺寸：**`width: 100%; height: 100%`**
- 显示创建独立的 stacking context：**`z-index:0`**

```tsx
<overlay class="overlay">
  <view class="backdrop">
    // Dialog Content
    <Dialog class="dialog-content" />
  </view>
</overlay>
```

```css
.overlay {
  overflow: visible;
  position: fixed;
}

.backdrop {
  width: 100%;
  height: 100%;
  z-index: 0;
}
```

- 如需调整弹窗的视觉尺寸，应在 backdrop 内部点上使用 margin 等方式控制，而不是修改 backdrop 本身的尺寸

```css
.dialog-content {
  margin: 100px;
  position: absolute;
}
```

**下面是一个示例: overlay**

**Bundle:** `dist/base.lynx.bundle`

```tsx {19-72}
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { root, useState } from "@lynx-js/react";

const App = () => {
  const [visible, setVisible] = useState(true);

  return (
    <view
      style={{
        linearOrientation: "vertical",
        width: "100%",
        height: "100%",
        padding: "10px",
      }}
    >
      <overlay visible={visible} style={{ position: "fixed" }}>
        <view
          style={{
            width: "100%",
            height: "100%",
            backgroundColor: "rgba(0, 0, 0, 0.5)",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
          }}
          bindtap={() => setVisible(false)}
        >
          <view
            catchtap={() => console.log("tap inside the overlay")}
            style={{
              width: "80%",
              height: "50%",
              backgroundColor: "white",
              display: "flex",
              justifyContent: "center",
              alignItems: "center",
              borderRadius: "10px",
            }}
          >
            <text
              style={{
                fontSize: "30px",
                color: "red",
                textAlign: "center",
                width: "max-content",
              }}
            >
              Biz Content
            </text>
            <text
              catchtap={() => setVisible(false)}
              style={{
                position: "absolute",
                fontSize: "20px",
                color: "white",
                textAlign: "center",
                left: "20px",
                right: "20px",
                bottom: "20px",
                backgroundColor: "green",
                padding: "10px",
                borderRadius: "5px",
              }}
            >
              Dismiss
            </text>
          </view>
        </view>
      </overlay>
      <view
        bindtap={() => setVisible(true)}
        style={{
          width: "100%",
          padding: "10px",
          backgroundColor: "#12345678",
          borderRadius: "10px",
        }}
      >
        <text
          style={{
            fontSize: "30px",
            color: "red",
            textAlign: "center",
            width: "max-content",
          }}
        >
          Open the overlay
        </text>
      </view>
    </view>
  );
};

root.render(<App />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.0



## 属性

### `ios-enable-swipe-back`

iOS
```tsx
// @默认值: false
'ios-enable-swipe-back'?: boolean;
```

当覆盖层显示时，'true' 允许向右滑动关闭当前页面，'false' 则不允许。

### `level`

Android
iOS
Desktop
Harmony
```tsx
// @默认值: 1
level?: 4 | 2 | 1 | 3;
```

引入了层的概念，分为四个级别。层的值越大，越靠近底部。默认情况下为第一级。层按 1 到 4 的顺序排列。指定要显示的层，不受显示顺序的影响。在每个层内，按照“后进先出”的逻辑排列。覆盖层显示时无法动态调整层，只能在隐藏时进行调整。

### `mode`

iOS
```tsx
// @默认值: 'window'
mode?: string;
```

指定覆盖层内容在 iOS 上的层级。window：挂载在窗口上，即应用的顶层；top：挂载在 topViewController 上；page：挂载在 UINavigationController 上；others：在客户端中自定义类名。

### `visible`

Android
iOS
Desktop
Harmony
```tsx
// @默认值: false
visible?: boolean;
```

控制覆盖层是否显示。

## 事件

前端可以绑定相应的事件回调来监听元素的运行时行为，如下所示。

### `binddismissoverlay`

Android
iOS
Desktop
Harmony
```tsx
binddismissoverlay = (e: BaseEvent) => {};
```

覆盖层隐藏时的回调。

### `binderror`

Android
2.18

```tsx
binderror = (e: OverlayErrorEvent) => {};
```

| 字段 | 类型 | 是否可选 | 默认值 | 平台 | 起始版本 | 描述 |
| --- | --- | --- | --- | --- | --- | --- |
| errorCode | 字符串 | 否 | – | Android |  | 错误码：0 - 正常显示；非 0 - 无法显示，你可以尝试使用下面提到的预加载方案来适配容器以解决问题。 |
| errorMsg | 字符串 | 否 | – | Android |  | 错误信息 |

触摸覆盖层时的回调

### `bindoverlaytouch`

Android
iOS
Desktop
```tsx
bindoverlaytouch = (e: OverlayTouchEvent) => {};
```

| 字段 | 类型 | 是否可选 | 默认值 | 平台 | 起始版本 | 描述 |
| --- | --- | --- | --- | --- | --- | --- |
| state | OverlayTouchState | 否 | – | Android iOS Desktop |  | 触摸状态 |
| x | 数字 | 否 | – | Android iOS Desktop |  | 相对于窗口的 x 位置，单位为 px |
| y | 数字 | 否 | – | Android iOS Desktop |  | 相对于窗口的 y 位置，单位为 px |

触摸覆盖层时的回调

### `bindrequestclose`

Android
Desktop
Harmony
```tsx
bindrequestclose = (e: BaseEvent) => {};
```

点击返回按钮时的回调。

### `bindshowoverlay`

Android
iOS
Desktop
Harmony
```tsx
bindshowoverlay = (e: BaseEvent) => {};
```

覆盖层显示时的回调

## 兼容性

**Compatibility Table**

**Query:** `elements.overlay`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/elements/overlay.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| overlay | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes |
| attributes | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes |
| attributes.visible | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes |
| attributes.level | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | No |
| attributes.ios-enable-swipe-back | No | No | Yes (3.5) | No | No | No |
| attributes.mode | No | Yes (4.0) | Yes (3.5) | No | No | No |
| attributes.android-navigation-bar-style | Yes (4.1) | No | No | No | No | No |
| attributes.android-overlay-scope | Yes (4.3) | No | No | No | No | No |
| events.binddismissoverlay | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes |
| events.binderror | No | No | No | No | No | No |
| events.bindoverlaytouch | Yes (3.5) | Yes (4.0) | Yes (3.5) | Yes (3.5) | Yes (3.5) | No |
| events.bindrequestclose | Yes (3.5) | Yes (3.5) | No | Yes (3.5) | Yes (3.5) | No |
| events.bindshowoverlay | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes (3.5) | Yes |
| methods.boundingClientRect | Yes (3.5) | Yes (4.0) | Yes (4.0) | No | No | Yes |
| methods.requestAccessibilityFocus | Yes (3.5) | No | Yes (3.5) | No | No | No |
| methods.scrollIntoView | Yes (3.5) | Yes (3.5) | Yes (3.5) | No | No | Yes |
| methods.setFocus | No | No | No | No | No | No |
| methods.takeScreenshot | Yes (3.5) | Yes (3.5) | Yes (3.5) | No | No | No |

**`overlay`**

overlay

**`attributes`**

attributes

**`attributes.visible`**

<code>visible</code>

**`attributes.level`**

<code>level</code>

**`attributes.ios-enable-swipe-back`**

<code>ios-enable-swipe-back</code>

**`attributes.mode`**

<code>mode</code>

**`attributes.android-navigation-bar-style`**

<code>android-navigation-bar-style</code>

**`attributes.android-overlay-scope`**

<code>android-overlay-scope</code>

**`events.binddismissoverlay`**

<code>binddismissoverlay</code>

**`events.binderror`**

<code>binderror</code>

**`events.bindoverlaytouch`**

<code>bindoverlaytouch</code>

**`events.bindrequestclose`**

<code>bindrequestclose</code>

**`events.bindshowoverlay`**

<code>bindshowoverlay</code>

**`methods.boundingClientRect`**

<code>boundingClientRect</code>

**`methods.requestAccessibilityFocus`**

<code>requestAccessibilityFocus</code>

**`methods.scrollIntoView`**

<code>scrollIntoView</code>

- **Web — Supported (version unknown):** Uses the browser element method.

**`methods.setFocus`**

<code>setFocus</code>

**`methods.takeScreenshot`**

<code>takeScreenshot</code>

