• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Action Sheet (ActionSheet)
2
3An action sheet is a dialog box that displays actions a user can take.
4
5>  **NOTE**
6>
7>  The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8>
9> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext).
10>
11> Since API version 10, you can use the [showActionSheet](../apis/js-apis-arkui-UIContext.md#showactionsheet) API in [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext) to obtain the UI context.
12
13## ActionSheet.show
14
15show(value: ActionSheetOptions)
16
17Shows an action sheet in the given settings.
18
19**System capability**: SystemCapability.ArkUI.ArkUI.Full
20
21**Parameters**
22
23| Name| Type                                             | Mandatory| Description.                    |
24| ------ | ------------------------------------------------- | ---- | ------------------------ |
25| value  | [ActionSheetOptions](#actionsheetoptions) | Yes  | Parameters of the action sheet.|
26
27## ActionSheetOptions
28
29| Name     | Type                   | Mandatory | Description                         |
30| ---------- | -------------------------- | ------- | ----------------------------- |
31| title      | [Resource](ts-types.md#resource) \| string | Yes    |  Title of the dialog box.|
32| subtitle<sup>10+</sup> | [ResourceStr](ts-types.md#resourcestr) | No| Subtitle of the dialog box.|
33| message    | [Resource](ts-types.md#resource) \| string | Yes    | Content of the dialog box. |
34| autoCancel | boolean                           | No    | Whether to close the dialog box when the overlay is clicked.<br>Default value: **true**<br>The value **true** means to close the dialog box when the overlay is clicked, and **false** means the opposite.|
35| confirm    | {<br>enabled<sup>10+</sup>?: boolean,<br>defaultFocus<sup>10+</sup>?: boolean,<br>style<sup>10+</sup>?: [DialogButtonStyle](#dialogbuttonstyle10),<br>value: [ResourceStr](ts-types.md#resourcestr),<br>action: () =&gt; void<br>} | No | Information about the confirm button.<br>**enabled**: whether the button responds to clicks. The value **true** means that the button responds to clicks, and **false** means the opposite.<br>Default value: **true**<br>**defaultFocus**: whether the button is the default focus. The value **true** means that the button is the default focus, and **false** means the opposite.<br>Default value: **false**<br>**style**: button style.<br>Default value: **DialogButtonStyle.DEFAULT**<br>**value**: button text.<br>**action**: Button: callback upon button clicking.|
36| cancel     | () =&gt; void           | No    | Callback invoked when the dialog box is closed after the overlay is clicked.  |
37| alignment  | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment) | No    |  Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Bottom**|
38| offset     | {<br>dx: [Length](ts-types.md#length),<br>dy: [Length](ts-types.md#length)<br>} | No     | Offset of the dialog box relative to the alignment position.<br>Default value:<br>1. When alignment is set to **Top**, **TopStart**, or **TopEnd**: {dx: 0,dy: "40vp"}<br>2. When **alignment** is set to any other value: {dx: 0,dy: "-40vp"}|
39| sheets     | Array&lt;[SheetInfo](#sheetinfo)&gt; | Yes      | Options in the dialog box. Each option supports the image, text, and callback.|
40| maskRect<sup>10+</sup> | [Rectangle](ts-methods-alert-dialog-box.md#rectangle8) | No    | Mask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.<br>Default value: **{ x: 0, y: 0, width: '100%', height: '100%' }**|
41| showInSubWindow<sup>11+</sup> | boolean | No| Whether to show the dialog box in a sub-window when the dialog box needs to be displayed outside the main window.<br>Default value: **false**<br>**NOTE**<br>A dialog box whose **showInSubWindow** attribute is **true** cannot trigger the display of another dialog box whose **showInSubWindow** attribute is also **true**.|
42| isModal<sup>11+</sup> | boolean | No| Whether the dialog box is a modal. A modal dialog box has a mask applied, while a non-modal dialog box does not.<br>Default value: **true**|
43| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor)  | No| Backplane color of the dialog box.<br>Default value: **Color.Transparent**|
44| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-appendix-enums.md#blurstyle9) | No| Background blur style of the dialog box.<br>Default value: **BlurStyle.COMPONENT_ULTRA_THICK**|
45
46## SheetInfo
47
48| Name| Type                                                    | Mandatory| Description         |
49| ------ | ------------------------------------------------------------ | ---- | ----------------- |
50| title  | [ResourceStr](ts-types.md#resourcestr) | Yes  | Sheet text.      |
51| icon   | [ResourceStr](ts-types.md#resourcestr) | No  | Sheet icon. By default, no icon is displayed.    |
52| action | ()=&gt;void                                          | Yes  | Callback when the sheet is selected.|
53
54## DialogButtonStyle<sup>10+</sup>
55
56| Name     | Description                             |
57| --------- | --------------------------------- |
58| DEFAULT   | Blue text on white background (black background under the dark theme).|
59| HIGHLIGHT | White text on blue background.                       |
60
61## Example
62
63### Example 1
64
65```ts
66@Entry
67@Component
68struct ActionSheetExample {
69  build() {
70    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
71      Button('Click to Show ActionSheet')
72        .onClick(() => {
73          ActionSheet.show({
74            title: 'ActionSheet title',
75            subtitle: 'ActionSheet subtitle',
76            message: 'message',
77            autoCancel: true,
78            confirm: {
79              defaultFocus: true,
80              value: 'Confirm button',
81              action: () => {
82                console.log('Get Alert Dialog handled')
83              }
84            },
85            cancel: () => {
86              console.log('actionSheet canceled')
87            },
88            alignment: DialogAlignment.Bottom,
89            offset: { dx: 0, dy: -10 },
90            sheets: [
91              {
92                title: 'apples',
93                action: () => {
94                  console.log('apples')
95                }
96              },
97              {
98                title: 'bananas',
99                action: () => {
100                  console.log('bananas')
101                }
102              },
103              {
104                title: 'pears',
105                action: () => {
106                  console.log('pears')
107                }
108              }
109            ]
110          })
111        })
112    }.width('100%')
113    .height('100%')
114  }
115}
116```
117
118![en-us_image_action](figures/en-us_image_action.gif)
119
120### Example 2
121
122```ts
123@Entry
124@Component
125struct ActionSheetExample {
126  build() {
127    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
128      Button('Click to Show ActionSheet')
129        .onClick(() => {
130          ActionSheet.show({
131            title: 'ActionSheet title',
132            subtitle: 'ActionSheet subtitle',
133            message: 'message',
134            autoCancel: true,
135            showInSubWindow: true,
136            isModal: true,
137            confirm: {
138              defaultFocus: true,
139              value: 'Confirm button',
140              action: () => {
141                console.log('Get Alert Dialog handled')
142              }
143            },
144            cancel: () => {
145              console.log('actionSheet canceled')
146            },
147            alignment: DialogAlignment.Center,
148            offset: { dx: 0, dy: -10 },
149            sheets: [
150              {
151                title: 'apples',
152                action: () => {
153                  console.log('apples')
154                }
155              },
156              {
157                title: 'bananas',
158                action: () => {
159                  console.log('bananas')
160                }
161              },
162              {
163                title: 'pears',
164                action: () => {
165                  console.log('pears')
166                }
167              }
168            ]
169          })
170        })
171    }.width('100%')
172    .height('100%')
173  }
174}
175```
176
177![en-us_image_action_showinsubwindow](figures/en-us_image_action_showinsubwindow.jpg)
178