ts-universal-attributes-popup.md 12.7 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
| 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**|
E
ester.zhou 已提交
47
| popupColor                   | [ResourceColor](ts-types.md#resourcecolor) | No   | Color of the popup.<br>Default value: **'#4d4d4d'**|
E
ester.zhou 已提交
48 49 50 51 52
| 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
E
ester.zhou 已提交
59 60 61

### Example 1

62 63
```ts
// xxx.ets
Z
zengyawen 已提交
64 65 66 67
@Entry
@Component
struct PopupExample {
  @State handlePopup: boolean = false
Z
zengyawen 已提交
68 69
  @State customPopup: boolean = false

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

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

Z
zengyawen 已提交
113

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

E
esterzhou 已提交
138
![figures/popup.gif](figures/popup.gif)
E
ester.zhou 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

### Example 2

```ts
// xxx.ets
@Entry
@Component
struct PopupExample {
  @State handlePopup: boolean = false

  build() {
    Column() {
      Button('PopupOptions')
        .onClick(() => {
          this.handlePopup = !this.handlePopup
        })
        .bindPopup(this.handlePopup, {
          message: 'This is a popup with PopupOptions',
          messageOptions: {
            textColor: Color.Red,
            font: {
              size: '14vp',
              style: FontStyle.Italic,
              weight: FontWeight.Bolder
            }
          },
          placement: Placement.Bottom,
          enableArrow: false,
          targetSpace: '15vp',
          onStateChange: (e) => {
            console.info(JSON.stringify(e.isVisible))
            if (!e.isVisible) {
              this.handlePopup = false
            }
          }
        })
    }.margin(20)
  }
}
```

![](figures/popup_2.png)

### Example 3

```ts
// xxx.ets
@Entry
@Component
struct PopupExample {
  @State customPopup: boolean = false

  // Popup builder
  @Builder popupBuilder() {
    Row() {
      Text('Custom Popup Message').fontSize(10)
    }.height(50).padding(5)
  }

  build() {
    Column() {
      // CustomPopupOptions for setting the popup
      Button('CustomPopupOptions')
        .onClick(() => {
          this.customPopup = !this.customPopup
        })
        .bindPopup(this.customPopup, {
          builder: this.popupBuilder,
          targetSpace: '15vp',
          enableArrow: false,
          onStateChange: (e) => {
            if (!e.isVisible) {
              this.customPopup = false
            }
          }
        })
    }.margin(20)
  }
}
```

![](figures/popup_3.png)