ts-universal-attributes-popup.md 8.1 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 13 14
## APIs


| Name          | Type                            | Description                                       |
| ---------- | ------------------------------------- | --------------------------------------- |
E
esterzhou 已提交
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.<br>**popup**: parameters of the popup.|
E
ester.zhou 已提交
16 17 18 19 20

## PopupOptions

| Name                     | Type                                               | Mandatory   | Description                                         |
| -------------------------| ------------------------------------------------| -----| ----------------------------------------- |
E
ester.zhou 已提交
21 22 23 24
| message                      | string                                                | Yes  | Content of the popup message.                                    |
| placementOnTop               | boolean                                               | No   | Whether to display the popup above the component.<br/>Default value: **false**            |
| 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.|
E
ester.zhou 已提交
25
| onStateChange                | (event: { isVisible: boolean }) =&gt; void        | No  | Callback for the popup status change event.<br/>**isVisible**: whether the popup is visible. |
E
ester.zhou 已提交
26
| 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. |
E
ester.zhou 已提交
27
| showInSubWindow<sup>9+</sup> | boolean                                           | No  | Whether to show the popup in the subwindow. <br/>Default value: **false** |
E
ester.zhou 已提交
28
| 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.|
E
ester.zhou 已提交
29 30 31 32 33

## CustomPopupOptions<sup>8+</sup>

| Name                      | Type                      | Mandatory    | Description                                                |
| -------------------------| ------------------------- | ---- | ---------------------------------------------------- |
E
ester.zhou 已提交
34 35 36 37 38 39 40
| builder                  | [CustomBuilder](ts-types.md#custombuilder8)  | Yes  | Popup builder.                                         |
| 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 is less than twice the arrow width (64 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. |
E
ester.zhou 已提交
41
| showInSubWindow<sup>9+</sup> | boolean                                      | No  | Whether to show the popup in the subwindow.<br/>Default value: **false** |
E
ester.zhou 已提交
42
| 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.|
Z
zengyawen 已提交
43 44


Z
zengyawen 已提交
45
## Example
46 47
```ts
// xxx.ets
Z
zengyawen 已提交
48 49 50 51
@Entry
@Component
struct PopupExample {
  @State handlePopup: boolean = false
Z
zengyawen 已提交
52 53
  @State customPopup: boolean = false

E
esterzhou 已提交
54
  // Popup builder
Z
zengyawen 已提交
55 56
  @Builder popupBuilder() {
    Row({ space: 2 }) {
E
esterzhou 已提交
57
      Image($r("app.media.image")).width(24).height(24).margin({ left: -5 })
58
      Text('Custom Popup').fontSize(10)
E
esterzhou 已提交
59
    }.width(100).height(50).padding(5)
Z
zengyawen 已提交
60
  }
Z
zengyawen 已提交
61 62

  build() {
Z
zengyawen 已提交
63
    Flex({ direction: FlexDirection.Column }) {
E
esterzhou 已提交
64 65
      // PopupOptions for setting the popup
      Button('PopupOptions')
Z
zengyawen 已提交
66 67 68 69
        .onClick(() => {
          this.handlePopup = !this.handlePopup
        })
        .bindPopup(this.handlePopup, {
E
esterzhou 已提交
70
          message: 'This is a popup with PopupOptions',
Z
zengyawen 已提交
71
          placementOnTop: true,
E
esterzhou 已提交
72
          showInSubWindow:false,
Z
zengyawen 已提交
73
          primaryButton: {
E
esterzhou 已提交
74
            value: 'confirm',
Z
zengyawen 已提交
75 76
            action: () => {
              this.handlePopup = !this.handlePopup
E
esterzhou 已提交
77 78 79 80 81 82 83
              console.info('confirm Button click')
            }
          },
          // Secondary button
          secondaryButton: {
            value: 'cancel',
            action: () => {
E
ester.zhou 已提交
84
              this.handlePopup = !this.handlePopup
E
esterzhou 已提交
85
              console.info('cancel Button click')
Z
zengyawen 已提交
86 87 88
            }
          },
          onStateChange: (e) => {
Z
zhaoxinyu 已提交
89
            console.info(JSON.stringify(e.isVisible))
E
esterzhou 已提交
90 91 92
            if (!e.isVisible) {
              this.handlePopup = false
            }
Z
zengyawen 已提交
93 94
          }
        })
E
esterzhou 已提交
95 96
        .position({ x: 100, y: 50 })

Z
zengyawen 已提交
97

E
esterzhou 已提交
98 99
      // CustomPopupOptions for setting the popup
      Button('CustomPopupOptions')
Z
zengyawen 已提交
100 101 102 103 104
        .onClick(() => {
          this.customPopup = !this.customPopup
        })
        .bindPopup(this.customPopup, {
          builder: this.popupBuilder,
E
esterzhou 已提交
105
          placement: Placement.Top,
E
ester.zhou 已提交
106
          mask: {color: 0x33000000},
E
esterzhou 已提交
107
          popupColor: Color.Yellow,
Z
zengyawen 已提交
108
          enableArrow: true,
E
esterzhou 已提交
109
          showInSubWindow: false,
Z
zengyawen 已提交
110 111 112 113 114 115
          onStateChange: (e) => {
            if (!e.isVisible) {
              this.customPopup = false
            }
          }
        })
E
esterzhou 已提交
116
        .position({ x: 80, y: 200 })
Z
zengyawen 已提交
117 118 119 120 121
    }.width('100%').padding({ top: 5 })
  }
}
```

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