ts-universal-attributes-popup.md 6.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 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 21 22 23 24 25

## PopupOptions

| Name                     | Type                                               | Mandatory   | Description                                         |
| -------------------------| ------------------------------------------------| -----| ----------------------------------------- |
| message                  | string                                          | Yes   | Content of the popup message.                                    |
| placementOnTop           | boolean                                         | No   | Whether to display the popup above the component. The default value is **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.|
| onStateChange            | (event: { isVisible: boolean }) =&gt; void | No   | Callback for the popup status change event. The parameter **isVisible** indicates whether the popup is visible.     |
E
ester.zhou 已提交
26 27
| arrowOffset<sup>9+</sup> | [Length](ts-types.md#length)                                 | No  | Offset of the popup arrow in the popup window. When residing above or below the popup, the arrow is offset to the left by default. When residing on the left or right side of the popup, the arrow is offset to the top by default.|
| showInSubWindow<sup>9+</sup> | boolean | No| Whether to show the popup in the subwindow. The default value is **false**.|
E
ester.zhou 已提交
28 29 30 31 32

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

| Name                      | Type                      | Mandatory    | Description                                                |
| -------------------------| ------------------------- | ---- | ---------------------------------------------------- |
E
esterzhou 已提交
33
| builder                  | [CustomBuilder](ts-types.md#custombuilder8)  | Yes  | Popup builder.                                         |
E
ester.zhou 已提交
34 35 36 37 38 39
| 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**    |
| maskColor                | [ResourceColor](ts-types.md#resourcecolor)  | No  | Color of the popup mask.                                         |
| 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 location set for the popup arrow is not large enough, the arrow will not be displayed. For example, if **placement** is set to **Left** and the popup height is less than the arrow width (32 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. The parameter **isVisible** indicates whether the popup is visible.|
E
ester.zhou 已提交
40 41
| arrowOffset<sup>9+</sup> | [Length](ts-types.md#length) | No| Offset of the popup arrow in the popup window. When residing above or below the popup, the arrow is offset to the left by default. When residing on the left or right side of the popup, the arrow is offset to the top by default.|
| showInSubWindow<sup>9+</sup> | boolean | No| Whether to show the popup in the subwindow. The default value is **false**.|
Z
zengyawen 已提交
42 43


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

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

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

Z
zengyawen 已提交
96

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

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