• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Page-Level Dialog Box
2By default, ArkUI dialog boxes are displayed at the global level, meaning the dialog box node is a subnode of the root node of the page and appears above all route and navigation pages in the application. If a dialog box is not explicitly closed using the **close** API during a route redirection, it will remain visible on the next page.
3
4Since API version 15, you can use a page-level dialog box that disappears with the previous routing page during page switching and reappears when the user returns to the previous page.
5
6> **NOTE**
7>
8> The page-level capability only takes effect when the dialog box is in non-subwindow mode, that is, the **showInSubWindow** parameter is set to **false** or is not set.
9>
10> Page-level dialog boxes are typically used in conjunction with navigation and routing capabilities. For more details, see [Component Navigation and Page Routing Overview](arkts-navigation-introduction.md).
11>
12> Before using a page-level dialog box, familiarize yourself with the basic dialog box usage in [Dialog Box Overview](arkts-base-dialog-overview.md).
13
14
15## Setting Page-Level Dialog Box Parameters
16
17> **NOTE**
18>
19> For details about the variables, see [Example](#example).
20
21To enable the page-level capability for a dialog box, set [levelMode](../reference/apis-arkui/js-apis-promptAction.md#levelmode15) in the dialog box's **options** parameter to **LevelMode.EMBEDDED**.
22
23When the dialog box is displayed, the current page is automatically obtained, and the dialog box node is mounted to this page. As a result, the dialog box appears above all navigation pages under the current page.
24
25```ts
26promptAction.openCustomDialog({
27  builder: () => {
28    this.customDialogComponent()
29  },
30  levelMode: LevelMode.EMBEDDED, // Enable the page-level dialog box.
31})
32```
33
34## Displaying a Dialog Box on a Specified Page
35
36To display a dialog box on a specific page, use the **levelUniqueId** parameter, which is the node ID on the target page. When this parameter is set, the system automatically queries the navigation page corresponding to the node ID and mounts the dialog box to that page.
37
38In the following example, a **Text** node is used as a reference node on a specific page. The [getFrameNodeById](../reference/apis-arkui/js-apis-arkui-UIContext.md#getframenodebyid12) API obtains the node, and the [getUniqueId](../reference/apis-arkui/js-apis-arkui-frameNode.md#getuniqueid12) API obtains the internal ID of the node, which is then passed as the value of **levelUniqueId**.
39
40```ts
41Text(this.message).id("test_text")
42  .onClick(() => {
43    const node: FrameNode | null = this.getUIContext().getFrameNodeById("test_text") || null;
44    promptAction.openCustomDialog({
45      builder: () => {
46        this.customDialogComponent()
47      },
48      levelMode: LevelMode.EMBEDDED, // Enable the page-level dialog box.
49      levelUniqueId: node?.getUniqueId(), // Set the ID of any node on the target page.
50    })
51  })
52```
53
54## Customizing the Page-Level Dialog Box Mask Style
55
56If a mask is configured for a dialog box, its scope is adjusted based on the page level. By default, the mask covers the display area (page or navigation page) where the dialog box's parent node is located, but it does not cover the status bar or navigation bar. To extend the mask to cover the status bar and navigation bar, set [immersiveMode](../reference/apis-arkui/js-apis-promptAction.md#immersivemode15) to **ImmersiveMode.EXTEND**.
57
58```ts
59Text(this.message).id("test_text")
60  .onClick(() => {
61    const node: FrameNode | null = this.getUIContext().getFrameNodeById("test_text") || null;
62    promptAction.openCustomDialog({
63      builder: () => {
64        this.customDialogComponent()
65      },
66      levelMode: LevelMode.EMBEDDED, // Enable the page-level dialog box.
67      levelUniqueId: node?.getUniqueId(), // Set the ID of any node on the target page.
68      immersiveMode: ImmersiveMode.EXTEND, // Extend the mask to cover the status bar and navigation bar.
69    })
70  })
71```
72
73## Interaction Logic
74
75The interaction logic for some dialog boxes on the page still follows the following interaction policies:
76
771. Handling of the swipe gesture: When the user swipes back to the previous page, if a dialog box is displayed, it will be closed first, and the gesture will end. To return to the previous page, the user needs to perform the gesture gesture again.
78
792. By default, clicking the dialog box mask closes the dialog box. Clicking outside the mask does not close the dialog box.
80
81## Example
82```ts
83// Index.ets
84import { promptAction, LevelMode, ImmersiveMode, router } from '@kit.ArkUI'
85
86let customDialogId: number = 0
87
88@Builder
89function customDialogBuilder() {
90  Column() {
91    Text('Custom dialog Message').fontSize(20).height(100)
92    Row() {
93      Button("Next").onClick(() => {
94        // Perform route redirection within the dialog box.
95        router.pushUrl({url: 'pages/Next'})
96      })
97      Blank().width(50)
98      Button("Close").onClick(() => {
99        promptAction.closeCustomDialog(customDialogId)
100      })
101    }
102  }.padding(20)
103}
104
105@Entry
106@Component
107struct Index {
108  @State message: string = 'Hello World'
109
110  @Builder
111  customDialogComponent() {
112    customDialogBuilder()
113  }
114
115  build() {
116    Row() {
117      Column() {
118        Text(this.message).id("test_text")
119          .fontSize(50)
120          .fontWeight(FontWeight.Bold)
121          .onClick(() => {
122            const node: FrameNode | null = this.getUIContext().getFrameNodeById("test_text") || null;
123            promptAction.openCustomDialog({
124              builder: () => {
125                this.customDialogComponent()
126              },
127              levelMode: LevelMode.EMBEDDED, // Enable the page-level dialog box.
128              levelUniqueId: node?.getUniqueId(), // Set the ID of any node on the target page.
129              immersiveMode: ImmersiveMode.EXTEND, // Extend the mask to cover the status bar and navigation bar.
130            }).then((dialogId: number) => {
131              customDialogId = dialogId
132            })
133          })
134      }
135      .width('100%')
136    }
137    .height('100%')
138  }
139}
140```
141```ts
142// Next.ets
143import { router } from '@kit.ArkUI'
144
145@Entry
146@Component
147struct Next {
148  @State message: string = 'Back'
149
150  build() {
151    Row() {
152      Column() {
153        Button(this.message)
154          .fontSize(20)
155          .fontWeight(FontWeight.Bold)
156          .onClick(() => {
157            router.back()
158          })
159      }
160      .width('100%')
161    }
162    .height('100%')
163  }
164}
165```
166![embedded_dialog](figures/embedded_dialog.gif)
167