• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Popup
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @liyi0309-->
5<!--Designer: @liyi0309-->
6<!--Tester: @lxl007-->
7<!--Adviser: @HelloCrease-->
8
9The **Popup** component is used to display popups in a specific style.
10
11>  **NOTE**
12>
13>  - This component is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version.
14>  - Use this component with the custom popup features in [popup control](ts-universal-attributes-popup.md) for best results.
15
16## Modules to Import
17
18```ts
19import { Popup, PopupOptions, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI';
20```
21
22##  Child Components
23
24Not supported
25
26## Popup
27
28Popup(options: PopupOptions): void
29
30**Decorator**: @Builder
31
32**Atomic service API**: This API can be used in atomic services since API version 12.
33
34**System capability**: SystemCapability.ArkUI.ArkUI.Full
35
36**Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly.
37
38**Parameters**
39
40| Name | Type                         | Mandatory| Description                 |
41| ------- | ----------------------------- | ---- | --------------------- |
42| options | [PopupOptions](#popupoptions) | Yes  | Parameters of the popup.|
43
44## PopupOptions
45
46Defines the style parameters of the popup.
47
48**System capability**: SystemCapability.ArkUI.ArkUI.Full
49
50**Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly.
51
52| Name       | Type      | Mandatory       | Description                           |
53| ----------- | ---------- | ------| --------------------------------- |
54| icon      | [PopupIconOptions](#popupiconoptions)                        | No  | Icon of the popup.<br>**NOTE**<br>The icon is not displayed when **size** is set to an invalid value or **0**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
55| title     | [PopupTextOptions](#popuptextoptions)                        | No  | Title of the popup.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
56| message   | [PopupTextOptions](#popuptextoptions)                        | Yes  | Content of the popup.<br>**NOTE**<br>**fontWeight** is not available for **messages**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
57| showClose | boolean \| [Resource](ts-types.md#resource)                | No  | Whether to show the close button. The value **true** means to show the close icon, and **false** means the opposite. If the value is of the **Resource** type, the corresponding icon is displayed.<br>Default value: **true**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
58| onClose   | () => void                                                   | No  | Callback for the popup close button.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
59| buttons   | [[PopupButtonOptions](#popupbuttonoptions)?,[PopupButtonOptions](#popupbuttonoptions)?] | No  | Buttons of the popup. A maximum of two buttons can be set.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
60| direction<sup>12+</sup> | [Direction](ts-appendix-enums.md#direction)                                             | No                               | Layout direction.<br>Default value: **Direction.Auto**<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
61| maxWidth<sup>18+</sup> | [Dimension](ts-types.md#dimension10)                                             | No                               | Maximum width of the popup. This API allows the popup to display with a custom width.<br>**NOTE**<br>When using resource references, ensure that the parameter type matches the attribute method type. **maxWidth** accepts numeric values (both floating-point and integer values), such as **$r('app.float.maxWidth')** and **$r('app.integer.maxWidth')**.<br>Default value: 400 vp<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
62
63## PopupTextOptions
64
65Provides text style settings.
66
67**Atomic service API**: This API can be used in atomic services since API version 12.
68
69**System capability**: SystemCapability.ArkUI.ArkUI.Full
70
71**Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly.
72
73| Name      | Type                                                        | Mandatory| Description        |
74| ---------- | ------------------------------------------------------------ | ---- | ------------------ |
75| text       | [ResourceStr](ts-types.md#resourcestr)                       | Yes  | Text content.    |
76| fontSize   | number \| string \| [Resource](ts-types.md#resource)         | No  | Text font size.<br>Default value: **$r('sys.float.ohos_id_text_size_body2')**<br>The string value must be convertible to a number (for example, **'10'**) or include a length unit (for example, **'10px'**); percentage-based strings are not supported.<br>Value range of number values: (0, +∞)|
77| fontColor  | [ResourceColor](ts-types.md#resourcecolor)                   | No  | Text font color.<br>Default value: **$r('sys.color.ohos_id_color_text_secondary')**|
78| fontWeight | number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string | No  | Text font weight.<br>For the number type, the value ranges from 100 to 900, at an interval of 100. A larger value indicates a heavier font weight. The default value is **400**.<br>For the string type, only strings of the number type are supported, for example, **"400"**, **"bold"**, **"bolder"**, **"lighter"**, **"regular"**, and **"medium"**, which correspond to the enumerated values in **FontWeight**.<br>Default value: **FontWeight.Regular**|
79
80## PopupButtonOptions
81
82Defines the button attributes and events.
83
84**Atomic service API**: This API can be used in atomic services since API version 12.
85
86**System capability**: SystemCapability.ArkUI.ArkUI.Full
87
88**Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly.
89
90| Name     | Type                                                | Mandatory| Description                |
91| --------- | ---------------------------------------------------- | ---- | ---------------------- |
92| text      | [ResourceStr](ts-types.md#resourcestr)               | Yes  | Text of the button.        |
93| action    | () => void                                           | No  | Click callback of the button.|
94| fontSize  | number \| string \| [Resource](ts-types.md#resource) | No  | Font size of the button text.<br>Default value: **$r('sys.float.ohos_id_text_size_button2')**<br>The string value must be convertible to a number (for example, **'10'**) or include a length unit (for example, **'10px'**); percentage-based strings are not supported.<br>If an invalid value is provided, the default value is used.|
95| fontColor | [ResourceColor](ts-types.md#resourcecolor)           | No  | Font color of the button text.<br>Default value: **$r('sys.color.ohos_id_color_text_primary_activated')**|
96
97##  PopupIconOptions
98
99Defines the attributes of the icon (in the upper left corner).
100
101**Atomic service API**: This API can be used in atomic services since API version 12.
102
103**System capability**: SystemCapability.ArkUI.ArkUI.Full
104
105**Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly.
106
107| Name        | Type                                                        | Mandatory| Description                            |
108| ------------ | ------------------------------------------------------------ | ---- | ---------------------------------- |
109| image        | [ResourceStr](ts-types.md#resourcestr)                       | Yes  | Icon content.                    |
110| width        | [Dimension](ts-types.md#dimension10)                         | No  | Icon width.<br>Default value: **32VP**|
111| height       | [Dimension](ts-types.md#dimension10)                         | No  | Icon height.<br>Default value: **32VP**|
112| fillColor    | [ResourceColor](ts-types.md#resourcecolor)                   | No  | Icon fill color. This property applies only to an SVG image.|
113| borderRadius | [Length](ts-types.md#length) \| [BorderRadiuses](ts-types.md#borderradiuses9) | No  | Rounded corner of the icon.<br>Default value: **$r('sys.float.ohos_id_corner_radius_default_s')** |
114
115## Example
116
117### Example 1: Setting the Popup Style
118
119This example demonstrates how to customize the style of a popup by configuring **PopupIconOptions**, **PopupTextOptions**, and **PopupButtonOptions**.
120
121```ts
122// xxx.ets
123import { Popup, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI';
124
125@Entry
126@Component
127struct PopupExample {
128  build() {
129    Row() {
130      // Define a popup.
131      Popup({
132        // Set the icon through PopupIconOptions.
133        icon: {
134          // Replace $r('app.media.icon') with the image resource file you use.
135          image: $r('app.media.icon'),
136          width: 32,
137          height: 32,
138          fillColor: Color.White,
139          borderRadius: 16
140        } as PopupIconOptions,
141        // Set the text through PopupTextOptions.
142        title: {
143          text: 'This is a popup with PopupOptions',
144          fontSize: 20,
145          fontColor: Color.Black,
146          fontWeight: FontWeight.Normal
147        } as PopupTextOptions,
148        // Set the text through PopupTextOptions.
149        message: {
150          text: 'This is the message',
151          fontSize: 15,
152          fontColor: Color.Black
153        } as PopupTextOptions,
154        showClose: false,
155        onClose: () => {
156          console.info('close Button click');
157        },
158        // Set the button through PopupButtonOptions.
159        buttons: [{
160          text: 'confirm',
161          action: () => {
162            console.info('confirm button click');
163          },
164          fontSize: 15,
165          fontColor: Color.Black,
166        },
167          {
168            text: 'cancel',
169            action: () => {
170              console.info('cancel button click');
171            },
172            fontSize: 15,
173            fontColor: Color.Black
174          },] as [PopupButtonOptions?, PopupButtonOptions?]
175      })
176    }
177    .width(300)
178    .height(200)
179    .borderWidth(2)
180    .justifyContent(FlexAlign.Center)
181  }
182}
183```
184
185![](figures/popup_7.png)
186
187### Example 2: Implementing an RTL Layout
188This example shows how to implement a right-to-left (RTL) layout for a popup by configuring **direction**.
189
190```ts
191// xxx.ets
192import { Popup, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI';
193
194@Entry
195@Component
196struct PopupPage {
197  @State currentDirection: Direction = Direction.Rtl;
198
199  build() {
200    Column() {
201      // Define a popup.
202      Popup({
203        // Set the icon through PopupIconOptions.
204        direction: this.currentDirection,
205        icon: {
206          // Replace $r('app.media.icon') with the image resource file you use.
207          image: $r('app.media.icon'),
208          width: 32,
209          height: 32,
210          fillColor: Color.White,
211          borderRadius: 16,
212        } as PopupIconOptions,
213        // Set the text through PopupTextOptions.
214        title: {
215          text: 'This is a popup with PopupOptions',
216          fontSize: 20,
217          fontColor: Color.Black,
218          fontWeight: FontWeight.Normal,
219
220        } as PopupTextOptions,
221        // Set the text through PopupTextOptions.
222        message: {
223          text: 'This is the message',
224          fontSize: 15,
225          fontColor: Color.Black,
226        } as PopupTextOptions,
227        showClose: true,
228        onClose: () => {
229          console.info('close Button click');
230        },
231        // Set the button through PopupButtonOptions.
232        buttons: [{
233          text: 'confirm',
234          action: () => {
235            console.info('confirm button click');
236          },
237          fontSize: 15,
238          fontColor: Color.Black,
239
240        },
241          {
242            text: 'cancel',
243            action: () => {
244              console.info('cancel button click');
245            },
246            fontSize: 15,
247            fontColor: Color.Black,
248          },] as [PopupButtonOptions?, PopupButtonOptions?],
249      })
250
251    }
252    .justifyContent(FlexAlign.Center)
253    .width('100%')
254    .height('100%')
255  }
256}
257```
258
259![](figures/popup_8.png)
260
261### Example 3: Setting the Custom Width
262This example demonstrates how to set the custom width for a popup using **maxWidth**.
263
264```ts
265// xxx.ets
266import { Popup, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI';
267
268@Entry
269@Component
270struct PopupPage {
271  @State currentDirection: Direction = Direction.Rtl;
272
273  build() {
274    Row() {
275      // Define a popup.
276      Popup({
277        // Set the custom width.
278        maxWidth: '50%',
279        // Set the icon through PopupIconOptions.
280        icon: {
281          // Replace $r('app.media.startIcon') with the image resource file you use.
282          image: $r('app.media.startIcon'),
283          width: 32,
284          height: 32,
285          fillColor: Color.White,
286          borderRadius: 16,
287        } as PopupIconOptions,
288        // Set the text through PopupTextOptions.
289        message: {
290          text: 'This is the message. This is the message. This is the message. This is the message.',
291          fontSize: 15,
292          fontColor: Color.Black
293        } as PopupTextOptions,
294        showClose: false,
295        onClose: () => {
296          console.info('close Button click');
297        },
298        // Set the button through PopupButtonOptions.
299        buttons: [{
300          text: 'OK',
301          action: () => {
302            console.info('confirm button click');
303          },
304          fontSize: 15,
305          fontColor: Color.Black,
306        },
307          {
308            text: 'cancel',
309            action: () => {
310              console.info('cancel button click');
311            },
312            fontSize: 15,
313            fontColor: Color.Black
314          },] as [PopupButtonOptions?, PopupButtonOptions?]
315      })
316    }
317    .width(400)
318    .height(200)
319    .borderWidth(2)
320    .justifyContent(FlexAlign.Center)
321  }
322}
323```
324
325![](figures/popup_9.png)
326