ts-universal-attributes-popup.md 11.0 KB
Newer Older
Z
zengyawen 已提交
1
# Popup Control
Z
zengyawen 已提交
2

E
esterzhou 已提交
3
You can bind a popup to a component, specifying its content, interaction logic, and display status.
Z
zengyawen 已提交
4

5 6 7
>  **NOTE**
>
>  This attribute is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9


E
ester.zhou 已提交
10 11 12
## APIs


E
ester.zhou 已提交
13 14
| Name       | Type                                    | Description                                      |
| --------- | ---------------------------------------- | ---------------------------------------- |
E
ester.zhou 已提交
15
| bindPopup | show: boolean,<br>popup: [PopupOptions](#popupoptions) \| [CustomPopupOptions](#custompopupoptions8)<sup>8+</sup> | Binds a popup to the component.<br>**show**: whether to show the popup. The default value is **false**, indicating that the popup is hidden. As the popup can be displayed only after building of all pages is completed, **show** cannot be set to **true** during page building. Otherwise, the display position and shape of the popup will be incorrect.<br>**popup**: parameters of the popup.|
E
ester.zhou 已提交
16 17 18

## PopupOptions

E
ester.zhou 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
| Name                                 | Type                                                        | Mandatory| Description                                                        |
| ------------------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| message                               | string                                                       | Yes  | Content of the popup message.                                              |
| placementOnTop<sup>(deprecated)</sup> | boolean                                                      | No  | Whether to display the popup above the component.<br/>Default value: **false**<br>**NOTE**<br>This API is deprecated since API version 10. You are advised to use **placement** instead. |
| primaryButton                         | {<br>value: string,<br>action: () =&gt; void<br>} | No  | Primary button.<br>**value**: text of the primary button in the popup.<br>**action**: callback for clicking the primary button.|
| secondaryButton                       | {<br>value: string,<br>action: () =&gt; void<br>} | No  | Secondary button.<br>**value**: text of the secondary button in the popup.<br>**action**: callback for clicking the secondary button.|
| onStateChange                         | (event: { isVisible: boolean }) =&gt; void | No  | Callback for the popup status change event.<br>**isVisible**: whether the popup is visible. |
| arrowOffset<sup>9+</sup>              | [Length](ts-types.md#length)                                 | No  | Offset of the popup arrow relative to the popup.<br>When the arrow is at the top or bottom of the popup: The value **0** indicates that the arrow is located on the leftmost, and any other value indicates the distance from the arrow to the leftmost; the arrow is centered by default.<br/>When the arrow is on the left or right side of the popup: The value indicates the distance from the arrow to the top; the arrow is centered by default.<br/>When the popup is displayed on either edge of the screen, it will automatically deviate leftward or rightward to stay within the safe area. When the value is **0**, the arrow always points to the bound component. |
| showInSubWindow<sup>9+</sup>          | boolean                                                      | No  | Whether to show the popup in the subwindow.<br/>Default value: **false**                  |
| mask<sup>10+</sup>                    | boolean \| [ResourceColor](ts-types.md#resourcecolor) | No  | Whether to apply a mask to the popup. The value **true** means to apply a transparent mask to the popup, **false** means not to apply a mask to the popup, and a color value means to apply a mask in the corresponding color to the popup.|
| messageOptions<sup>10+</sup>          | [PopupMessageOptions](#popupmessageoptions10)                | No  | Parameters of the popup message.                                      |
| targetSpace<sup>10+</sup>             | [Length](ts-types.md#length)                                 | No  | Space between the popup and the target.                                     |
| placement<sup>10+</sup>               | [Placement](ts-appendix-enums.md#placement8)                 | No  | Position of the popup relative to the target. The default value is **Placement.Bottom**.<br>If both **placementOnTop** and **placement** are set, the latter prevails.|
E
ester.zhou 已提交
32 33
| offset<sup>10+</sup>                  | [Position](ts-types.md#position8)                            | No  | Offset of the popup relative to the display position specified by **placement**.<br>**NOTE**<br>This parameter cannot be set in percentage.|
| enableArrow<sup>10+</sup>             | boolean                                                      | No  | Whether to display the arrow.<br>Default value: **true**|
E
ester.zhou 已提交
34 35 36

## PopupMessageOptions<sup>10+</sup>

E
ester.zhou 已提交
37 38 39 40
| Name       | Type                                      | Mandatory  | Description         |
| --------- | ---------------------------------------- | ---- | ----------- |
| textColor | [ResourceColor](ts-types.md#resourcecolor) | No   | Text color of the popup message.|
| font      | [Font](ts-types.md#Font)                 | No   | Font attributes of the popup message.|
E
ester.zhou 已提交
41 42
## CustomPopupOptions<sup>8+</sup>

E
ester.zhou 已提交
43 44
| Name                          | Type                                      | Mandatory  | Description                                      |
| ---------------------------- | ---------------------------------------- | ---- | ---------------------------------------- |
E
ester.zhou 已提交
45
| builder                      | [CustomBuilder](ts-types.md#custombuilder8) | Yes   | Popup builder.<br>**NOTE**<br>The **popup** attribute is a universal attribute. A custom popup does not support display of another popup. The **position** attribute cannot be used for the first-layer container under the builder. If the **position** attribute is used, the popup will not be displayed.                             |
E
ester.zhou 已提交
46 47 48 49 50 51 52
| placement                    | [Placement](ts-appendix-enums.md#placement8) | No   | Preferred position of the popup. If the set position is insufficient for holding the popup, it will be automatically adjusted.<br>Default value: **Placement.Bottom**|
| popupColor                   | [ResourceColor](ts-types.md#resourcecolor) | No   | Color of the popup.                                |
| enableArrow                  | boolean                                  | No   | Whether to display an arrow.<br>Since API version 9, if the position set for the popup is not large enough, the arrow will not be displayed. For example, if **placement** is set to **Left**, but the popup height (80 vp) is less than the sum of the arrow width (32 vp) and diameter of popup rounded corner (48 vp), the arrow will not be displayed.<br>Default value: **true**|
| autoCancel                   | boolean                                  | No   | Whether to automatically close the popup when an operation is performed on the page.<br>Default value: **true**           |
| onStateChange                | (event: { isVisible: boolean }) =&gt; void | No   | Callback for the popup status change event.<br/>**isVisible**: whether the popup is visible.           |
| arrowOffset<sup>9+</sup>     | [Length](ts-types.md#length)             | No   | Offset of the popup arrow relative to the popup.<br/>When the arrow is at the top or bottom of the popup: The value **0** indicates that the arrow is located on the leftmost, and any other value indicates the distance from the arrow to the leftmost; the arrow is centered by default.<br/>When the arrow is on the left or right side of the popup: The value indicates the distance from the arrow to the top; the arrow is centered by default.<br/>When the popup is displayed on either edge of the screen, it will automatically deviate leftward or rightward to stay within the safe area. When the value is **0**, the arrow always points to the bound component. |
| showInSubWindow<sup>9+</sup> | boolean                                  | No   | Whether to show the popup in the subwindow. The default value is **false**.                   |
E
ester.zhou 已提交
53
| maskColor<sup>(deprecated)</sup> | [ResourceColor](ts-types.md#resourcecolor)   | No  | Color of the popup mask.<br>**NOTE**<br>This parameter is deprecated since API version 10. You are advised to use **mask** instead.|
E
ester.zhou 已提交
54 55
| mask<sup>10+</sup>           | boolean \| [ResourceColor](ts-types.md#resourcecolor) | No   | Whether to apply a mask to the popup. The value **true** means to apply a transparent mask to the popup, **false** means not to apply a mask to the popup, and a color value means to apply a mask in the corresponding color to the popup.|
| targetSpace<sup>10+</sup>    | [Length](ts-types.md#length)             | No   | Space between the popup and the target.                          |
E
ester.zhou 已提交
56
| offset<sup>10+</sup>         | [Position](ts-types.md#position8)                            | No  | Offset of the popup relative to the display position specified by **placement**.<br>**NOTE**<br>This parameter cannot be set in percentage.|
Z
zengyawen 已提交
57

Z
zengyawen 已提交
58
## Example
59 60
```ts
// xxx.ets
Z
zengyawen 已提交
61 62 63 64
@Entry
@Component
struct PopupExample {
  @State handlePopup: boolean = false
Z
zengyawen 已提交
65 66
  @State customPopup: boolean = false

E
esterzhou 已提交
67
  // Popup builder
Z
zengyawen 已提交
68 69
  @Builder popupBuilder() {
    Row({ space: 2 }) {
E
esterzhou 已提交
70
      Image($r("app.media.image")).width(24).height(24).margin({ left: -5 })
71
      Text('Custom Popup').fontSize(10)
E
esterzhou 已提交
72
    }.width(100).height(50).padding(5)
Z
zengyawen 已提交
73
  }
Z
zengyawen 已提交
74 75

  build() {
Z
zengyawen 已提交
76
    Flex({ direction: FlexDirection.Column }) {
E
esterzhou 已提交
77 78
      // PopupOptions for setting the popup
      Button('PopupOptions')
Z
zengyawen 已提交
79 80 81 82
        .onClick(() => {
          this.handlePopup = !this.handlePopup
        })
        .bindPopup(this.handlePopup, {
E
esterzhou 已提交
83
          message: 'This is a popup with PopupOptions',
Z
zengyawen 已提交
84
          placementOnTop: true,
E
esterzhou 已提交
85
          showInSubWindow:false,
Z
zengyawen 已提交
86
          primaryButton: {
E
esterzhou 已提交
87
            value: 'confirm',
Z
zengyawen 已提交
88 89
            action: () => {
              this.handlePopup = !this.handlePopup
E
esterzhou 已提交
90 91 92 93 94 95 96
              console.info('confirm Button click')
            }
          },
          // Secondary button
          secondaryButton: {
            value: 'cancel',
            action: () => {
E
ester.zhou 已提交
97
              this.handlePopup = !this.handlePopup
E
esterzhou 已提交
98
              console.info('cancel Button click')
Z
zengyawen 已提交
99 100 101
            }
          },
          onStateChange: (e) => {
Z
zhaoxinyu 已提交
102
            console.info(JSON.stringify(e.isVisible))
E
esterzhou 已提交
103 104 105
            if (!e.isVisible) {
              this.handlePopup = false
            }
Z
zengyawen 已提交
106 107
          }
        })
E
esterzhou 已提交
108 109
        .position({ x: 100, y: 50 })

Z
zengyawen 已提交
110

E
esterzhou 已提交
111 112
      // CustomPopupOptions for setting the popup
      Button('CustomPopupOptions')
Z
zengyawen 已提交
113 114 115 116 117
        .onClick(() => {
          this.customPopup = !this.customPopup
        })
        .bindPopup(this.customPopup, {
          builder: this.popupBuilder,
E
esterzhou 已提交
118
          placement: Placement.Top,
E
ester.zhou 已提交
119
          mask: {color:'0x33000000'},
E
esterzhou 已提交
120
          popupColor: Color.Yellow,
Z
zengyawen 已提交
121
          enableArrow: true,
E
esterzhou 已提交
122
          showInSubWindow: false,
Z
zengyawen 已提交
123 124 125 126 127 128
          onStateChange: (e) => {
            if (!e.isVisible) {
              this.customPopup = false
            }
          }
        })
E
esterzhou 已提交
129
        .position({ x: 80, y: 200 })
Z
zengyawen 已提交
130 131 132 133 134
    }.width('100%').padding({ top: 5 })
  }
}
```

E
esterzhou 已提交
135
![figures/popup.gif](figures/popup.gif)