• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Modal Transition
2
3You can bind a full-screen modal to a component through the **bindContentCover** attribute. Better yet, with the **ModalTransition** parameter, you can apply a transition effect for when the component is inserted or deleted.
4
5>  **NOTE**
6>
7>  The APIs of this module are supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.
8>
9>  Switching between landscape and portrait modes is not supported.
10>
11>  Route hopping is not supported.
12
13## Attributes
14
15| Name              | Parameter                                      | Description                                    |
16| ---------------- | ---------------------------------------- | ---------------------------------------- |
17| bindContentCover | isShow: boolean,<br>builder: [CustomBuilder](ts-types.md#custombuilder8),<br>options?: [ContentCoverOptions](#contentcoveroptions) | Binds a modal to the component, which can be displayed when the component is touched. The content of the modal is customizable. The transition type can be set to none, slide-up and slide-down animation, and opacity gradient animation.<br> **isShow**: whether to display the modal.<br>Since API version 10, this parameter supports two-way binding through [$$](../../quick-start/arkts-two-way-sync.md).<br>**builder**: content of the modal.<br> **options**: optional attributes of the modal.|
18## ContentCoverOptions
19| Name             | Type                                      | Mandatory  | Description           |
20| --------------- | ---------------------------------------- | ---- | ------------- |
21| modalTransition | [ModalTransition](ts-types.md#modaltransition10) | No   | Transition mode of the modal. |
22| backgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No   | Background color of the modal. |
23| onAppear        | () => void                               | No   | Callback invoked when the modal appears.|
24| onDisappear     | () => void                               | No   | Callback invoked when the modal disappears.|
25
26## Example
27
28### Example 1
29
30This example applies a custom animation to two modals whose transition type is none.
31
32```ts
33// xxx.ets
34@Entry
35@Component
36struct ModalTransitionExample {
37  @State isShow:boolean = false
38  @State isShow2:boolean = false
39
40  @Builder myBuilder2() {
41    Column() {
42      Button("close modal 2")
43        .margin(10)
44        .fontSize(20)
45        .onClick(()=>{
46          this.isShow2 = false;
47        })
48    }
49    .width('100%')
50    .height('100%')
51  }
52
53  @Builder myBuilder() {
54    Column() {
55      Button("transition modal 2")
56        .margin(10)
57        .fontSize(20)
58        .onClick(()=>{
59          this.isShow2 = true;
60        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Orange, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
61
62      Button("close modal 1")
63        .margin(10)
64        .fontSize(20)
65        .onClick(()=>{
66          this.isShow = false;
67        })
68    }
69    .width('100%')
70    .height('100%')
71    .justifyContent(FlexAlign.Center)
72  }
73
74  build() {
75    Column() {
76      Button("transition modal 1")
77        .onClick(() => {
78          this.isShow = true
79        })
80        .fontSize(20)
81        .margin(10)
82        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
83    }
84    .justifyContent(FlexAlign.Center)
85    .backgroundColor("#ff49c8ab")
86    .width('100%')
87    .height('100%')
88  }
89}
90```
91
92![en-us_full_screen_modal_none_1](figures/en-us_full_screen_modal_none_1.gif)
93
94### Example 2
95
96This example applies a custom animation to two modals whose transition type is none.
97
98```ts
99// xxx.ets
100import curves from '@ohos.curves';
101@Entry
102@Component
103struct ModalTransitionExample {
104  @State  @Watch("isShow1Change") isShow:boolean = false
105  @State  @Watch("isShow2Change") isShow2:boolean = false
106  @State isScale1:number = 1;
107  @State isScale2:number = 1;
108  @State flag: boolean = true
109  @State show: string = 'show'
110
111  isShow1Change() {
112    this.isShow ? this.isScale1 = 0.95 : this.isScale1 = 1
113  }
114  isShow2Change() {
115    this.isShow2 ? this.isScale2 = 0.95 : this.isScale2 = 1
116  }
117  @Builder myBuilder2() {
118    Column() {
119      Button("close modal 2")
120        .margin(10)
121        .fontSize(20)
122        .onClick(()=>{
123          this.isShow2 = false;
124        })
125    }
126    .width('100%')
127    .height('100%')
128  }
129
130
131  @Builder myBuilder() {
132    Column() {
133      Button("transition modal 2")
134        .margin(10)
135        .fontSize(20)
136        .onClick(()=>{
137          this.isShow2 = true;
138        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Orange, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
139
140      Button("close modal 1")
141        .margin(10)
142        .fontSize(20)
143        .onClick(()=>{
144          this.isShow = false;
145        })
146    }
147    .width('100%')
148    .height('100%')
149    .justifyContent(FlexAlign.Center)
150    .scale({x: this.isScale2, y: this.isScale2})
151    .animation({curve:curves.springMotion()})
152  }
153
154  build() {
155    Column() {
156      Button("transition modal 1")
157        .onClick(() => {
158          this.isShow = true
159        })
160        .fontSize(20)
161        .margin(10)
162        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
163    }
164    .justifyContent(FlexAlign.Center)
165    .backgroundColor("#ff49c8ab")
166    .width('100%')
167    .height('100%')
168    .scale({ x: this.isScale1, y: this.isScale1 })
169    .animation({ curve: curves.springMotion() })
170  }
171}
172```
173
174![en-us_full_screen_modal_none_2](figures/en-us_full_screen_modal_none_2.gif)
175
176### Example 3
177
178This example shows two modals whose transition type is slide-up and slide-down animation.
179
180```ts
181// xxx.ets
182@Entry
183@Component
184struct ModalTransitionExample {
185  @State isShow:boolean = false
186  @State isShow2:boolean = false
187
188  @Builder myBuilder2() {
189    Column() {
190      Button("close modal 2")
191        .margin(10)
192        .fontSize(20)
193        .onClick(()=>{
194          this.isShow2 = false;
195        })
196    }
197    .width('100%')
198    .height('100%')
199  }
200
201  @Builder myBuilder() {
202    Column() {
203      Button("transition modal 2")
204        .margin(10)
205        .fontSize(20)
206        .onClick(()=>{
207          this.isShow2 = true;
208        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.DEFAULT, backgroundColor: Color.Gray, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
209
210      Button("close modal 1")
211        .margin(10)
212        .fontSize(20)
213        .onClick(()=>{
214          this.isShow = false;
215        })
216    }
217    .width('100%')
218    .height('100%')
219    .justifyContent(FlexAlign.Center)
220  }
221
222  build() {
223    Column() {
224      Button("transition modal 1")
225        .onClick(() => {
226          this.isShow = true
227        })
228        .fontSize(20)
229        .margin(10)
230        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.DEFAULT, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
231    }
232    .justifyContent(FlexAlign.Center)
233    .backgroundColor(Color.White)
234    .width('100%')
235    .height('100%')
236  }
237}
238```
239
240![en-us_full_screen_modal_default](figures/en-us_full_screen_modal_default.gif)
241
242### Example 4
243
244This example shows two modals whose transition type is opacity gradient animation.
245
246```ts
247// xxx.ets
248@Entry
249@Component
250struct ModalTransitionExample {
251  @State isShow:boolean = false
252  @State isShow2:boolean = false
253
254  @Builder myBuilder2() {
255    Column() {
256      Button("close modal 2")
257        .margin(10)
258        .fontSize(20)
259        .onClick(()=>{
260          this.isShow2 = false;
261        })
262    }
263    .width('100%')
264    .height('100%')
265    .justifyContent(FlexAlign.Center)
266  }
267
268
269  @Builder myBuilder() {
270    Column() {
271      Button("transition modal 2")
272        .margin(10)
273        .fontSize(20)
274        .onClick(()=>{
275          this.isShow2 = true;
276        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.ALPHA, backgroundColor: Color.Gray, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
277
278      Button("close modal 1")
279        .margin(10)
280        .fontSize(20)
281        .onClick(()=>{
282          this.isShow = false;
283        })
284    }
285    .width('100%')
286    .height('100%')
287    .justifyContent(FlexAlign.Center)
288  }
289
290  build() {
291    Column() {
292      Button("transition modal 1")
293        .onClick(() => {
294          this.isShow = true
295        })
296        .fontSize(20)
297        .margin(10)
298        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.ALPHA, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
299    }
300    .justifyContent(FlexAlign.Center)
301    .backgroundColor(Color.White)
302    .width('100%')
303    .height('100%')
304  }
305}
306```
307
308![en-us_full_screen_modal_alpha](figures/en-us_full_screen_modal_alpha.gif)
309