1# Custom Dialog Box 2 3A custom dialog box is a dialog box you customize by using APIs of the **CustomDialogController** class. You can set the style and content to your preference for a custom dialog box. 4 5> **NOTE** 6> 7> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## APIs 11 12CustomDialogController(value: CustomDialogControllerOptions) 13 14Defines a custom dialog box. 15 16> **NOTE** 17> 18> No parameters of the custom dialog box can be dynamically updated. 19 20**System capability**: SystemCapability.ArkUI.ArkUI.Full 21 22**Parameters** 23 24| Name| Type | Mandatory| Description | 25| ------ | ------------------------------------------------------------ | ---- | ---------------------- | 26| value | [CustomDialogControllerOptions](#customdialogcontrolleroptions) | Yes | Parameters of the custom dialog box.| 27 28## CustomDialogControllerOptions 29 30| Name | Type | Mandatory | Description | 31| ----------------------------- | ---------------------------------------- | ---- | ---------------------------------------- | 32| builder | [CustomDialog](../../ui/arkts-common-components-custom-dialog.md) | Yes | Builder of the custom dialog box content. | 33| cancel | () => void | No | Callback invoked when the dialog box is closed after the Back key, the ESC key, or the mask is clicked.| 34| autoCancel | boolean | No | Whether to close the dialog box when the overlay is clicked. The value **true** means to close the dialog box when the overlay is clicked, and **false** means the opposite.<br>Default value: **true**| 35| alignment | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment) | No | Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**| 36| offset | [Offset](ts-types.md#offset) | No | Offset of the dialog box relative to the alignment position. | 37| customStyle | boolean | No | Whether to use a custom style for the dialog box.<br>**false** (default): The dialog box automatically adapts its width to the grid system and its height to the child components; the maximum height is 90% of the container height; the rounded corner is 24 vp.<br>**true**: The dialog box automatically adapts its width to the child components; the rounded corner is 0; the background color is transparent.| 38| gridCount<sup>8+</sup> | number | No | Number of [grid columns](../../ui/arkts-layout-development-grid-layout.md) occupied by the dialog box.<br>The default value is subject to the window size, and the maximum value is the maximum number of columns supported by the system. If this parameter is set to an invalid value, the default value is used.| 39| maskColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | No | Custom mask color.<br>Default value: **0x33000000** | 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| openAnimation<sup>10+</sup> | [AnimateParam](ts-explicit-animation.md#animateparam) | No | Parameters for defining the open animation of the dialog box.<br>**NOTE**<br>**iterations**: The default value is **1**, indicating that the animation is played once; any other value evaluates to the default value.<br>**playMode**: The default value is **PlayMode.Normal**; any other value evaluates to the default value.| 42| closeAnimation<sup>10+</sup> | [AnimateParam](ts-explicit-animation.md#animateparam) | No | Parameters for defining the close animation of the dialog box.<br>**NOTE**<br>**iterations**: The default value is **1**, indicating that the animation is played once; any other value evaluates to the default value.<br>**playMode**: The default value is **PlayMode.Normal**; any other value evaluates to the default value.<br>For page transition, you are advised to use the default close animation. | 43| showInSubWindow<sup>10+</sup> | boolean | No | Whether to display a dialog box in a subwindow.<br>Default value: **false**, indicating that the dialog box is not displayed in the subwindow<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**.| 44| backgroundColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the dialog box.<br>**NOTE**<br>If the content builder also has the background color set, the background color set here will be overridden by the background color of the content builder.| 45| cornerRadius<sup>10+</sup> | [BorderRadiuses](ts-types.md#borderradiuses9) \| [Dimension](ts-types.md#dimension10) | No | Radius of the rounded corners of the background.<br>You can set separate radiuses for the four rounded corners.<br>Default value: **{ topLeft: '24vp', topRight: '24vp', bottomLeft: '24vp', bottomRight: '24vp' }**<br>**NOTE**<br>This attribute must be used together with the [borderRadius](ts-universal-attributes-border.md#attributes) attribute.| 46 47> **NOTE** 48> 49> - Pressing the Back or ESC key closes the dialog box. 50> - Use the custom dialog box to contain simple information only. Do not use it as a page. If the dialog box's height is too large, it may be partly blocked by the soft keyboard (if any), which is automatically raised when displayed. 51 52## CustomDialogController 53 54### Objects to Import 55 56```ts 57dialogController : CustomDialogController | null = new CustomDialogController(CustomDialogControllerOptions) 58``` 59> **NOTE** 60> 61> **CustomDialogController** is effective only when it is a member variable of the **@CustomDialog** and **@Component** decorated struct and is defined in the **@Component** decorated struct. For details, see the following example. 62 63### open 64open(): void 65 66 67Opens the content of the custom dialog box. This API can be called multiple times. If the dialog box displayed in a subwindow, no new subwindow is allowed. 68 69 70### close 71close(): void 72 73 74Closes the custom dialog box. If the dialog box is closed, this API does not take effect. 75 76 77## Example 78 79```ts 80// xxx.ets 81@CustomDialog 82struct CustomDialogExampleTwo { 83 controllerTwo?: CustomDialogController 84 85 build() { 86 Column() { 87 Text('I'm the second dialog box.') 88 .fontSize(30) 89 .height(100) 90 Button ('Close Second Dialog Box') 91 .onClick(() => { 92 if (this.controllerTwo != undefined) { 93 this.controllerTwo.close() 94 } 95 }) 96 .margin(20) 97 } 98 } 99} 100 101@CustomDialog 102struct CustomDialogExample { 103 @Link textValue: string 104 @Link inputValue: string 105 dialogControllerTwo: CustomDialogController | null = new CustomDialogController({ 106 builder: CustomDialogExampleTwo(), 107 alignment: DialogAlignment.Bottom, 108 offset: { dx: 0, dy: -25 } }) 109 controller?: CustomDialogController 110 // You can pass in multiple other controllers in the CustomDialog to open one or more other CustomDialogs in the CustomDialog. In this case, you must place the controller pointing to the self behind all controllers. 111 cancel: () => void = () => { 112 } 113 confirm: () => void = () => { 114 } 115 116 build() { 117 Column() { 118 Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 }) 119 TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%') 120 .onChange((value: string) => { 121 this.textValue = value 122 }) 123 Text('Are you sure you want to change text?').fontSize(16).margin({ bottom: 10 }) 124 Flex({ justifyContent: FlexAlign.SpaceAround }) { 125 Button('No') 126 .onClick(() => { 127 if (this.controller != undefined) { 128 this.controller.close() 129 this.cancel() 130 } 131 }).backgroundColor(0xffffff).fontColor(Color.Black) 132 Button('Yes') 133 .onClick(() => { 134 if (this.controller != undefined) { 135 this.inputValue = this.textValue 136 this.controller.close() 137 this.confirm() 138 } 139 }).backgroundColor(0xffffff).fontColor(Color.Red) 140 }.margin({ bottom: 10 }) 141 142 Button ('Open Second Dialog Box') 143 .onClick(() => { 144 if (this.dialogControllerTwo != null) { 145 this.dialogControllerTwo.open() 146 } 147 }) 148 .margin(20) 149 }.borderRadius(10) 150 // When using the border or cornerRadius attribute, use it together with the borderRadius attribute. 151 } 152} 153 154@Entry 155@Component 156struct CustomDialogUser { 157 @State textValue: string = '' 158 @State inputValue: string = 'Click Me' 159 dialogController: CustomDialogController | null = new CustomDialogController({ 160 builder: CustomDialogExample({ 161 cancel: ()=> { this.onCancel() }, 162 confirm: ()=> { this.onAccept() }, 163 textValue: $textValue, 164 inputValue: $inputValue 165 }), 166 cancel: this.exitApp, 167 autoCancel: true, 168 alignment: DialogAlignment.Bottom, 169 offset: { dx: 0, dy: -20 }, 170 gridCount: 4, 171 customStyle: false, 172 backgroundColor: 0xd9ffffff, 173 cornerRadius: 10, 174 }) 175 176 // Set dialogController to null when the custom component is about to be destructed. 177 aboutToDisappear() { 178 this.dialogController = null // Set dialogController to null. 179 } 180 181 onCancel() { 182 console.info('Callback when the first button is clicked') 183 } 184 185 onAccept() { 186 console.info('Callback when the second button is clicked') 187 } 188 189 exitApp() { 190 console.info('Click the callback in the blank area') 191 } 192 193 build() { 194 Column() { 195 Button(this.inputValue) 196 .onClick(() => { 197 if (this.dialogController != null) { 198 this.dialogController.open() 199 } 200 }).backgroundColor(0x317aff) 201 }.width('100%').margin({ top: 5 }) 202 } 203} 204``` 205 206 207