• 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
20Inherited from [BindOptions](ts-universal-attributes-sheet-transition.md#bindoptions).
21| Name             | Type                                      | Mandatory  | Description           |
22| --------------- | ---------------------------------------- | ---- | ------------- |
23| modalTransition | [ModalTransition](ts-types.md#modaltransition10) | No   | Transition mode of the modal. |
24
25## Example
26
27### Example 1
28
29This example applies a custom animation to two modals whose transition type is none.
30
31```ts
32// xxx.ets
33@Entry
34@Component
35struct ModalTransitionExample {
36  @State isShow:boolean = false
37  @State isShow2:boolean = false
38
39  @Builder myBuilder2() {
40    Column() {
41      Button("close modal 2")
42        .margin(10)
43        .fontSize(20)
44        .onClick(()=>{
45          this.isShow2 = false;
46        })
47    }
48    .width('100%')
49    .height('100%')
50  }
51
52  @Builder myBuilder() {
53    Column() {
54      Button("transition modal 2")
55        .margin(10)
56        .fontSize(20)
57        .onClick(()=>{
58          this.isShow2 = true;
59        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Orange, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
60
61      Button("close modal 1")
62        .margin(10)
63        .fontSize(20)
64        .onClick(()=>{
65          this.isShow = false;
66        })
67    }
68    .width('100%')
69    .height('100%')
70    .justifyContent(FlexAlign.Center)
71  }
72
73  build() {
74    Column() {
75      Button("transition modal 1")
76        .onClick(() => {
77          this.isShow = true
78        })
79        .fontSize(20)
80        .margin(10)
81        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
82    }
83    .justifyContent(FlexAlign.Center)
84    .backgroundColor("#ff49c8ab")
85    .width('100%')
86    .height('100%')
87  }
88}
89```
90
91![en-us_full_screen_modal_none_1](figures/en-us_full_screen_modal_none_1.gif)
92
93### Example 2
94
95This example applies a custom animation to two modals whose transition type is none.
96
97```ts
98// xxx.ets
99import curves from '@ohos.curves';
100@Entry
101@Component
102struct ModalTransitionExample {
103  @State  @Watch("isShow1Change") isShow:boolean = false
104  @State  @Watch("isShow2Change") isShow2:boolean = false
105  @State isScale1:number = 1;
106  @State isScale2:number = 1;
107
108  isShow1Change() {
109    this.isShow ? this.isScale1 = 0.95 : this.isScale1 = 1
110  }
111  isShow2Change() {
112    this.isShow2 ? this.isScale2 = 0.95 : this.isScale2 = 1
113  }
114  @Builder myBuilder2() {
115    Column() {
116      Button("close modal 2")
117        .margin(10)
118        .fontSize(20)
119        .onClick(()=>{
120          this.isShow2 = false;
121        })
122    }
123    .width('100%')
124    .height('100%')
125  }
126
127
128  @Builder myBuilder() {
129    Column() {
130      Button("transition modal 2")
131        .margin(10)
132        .fontSize(20)
133        .onClick(()=>{
134          this.isShow2 = true;
135        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Orange, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
136
137      Button("close modal 1")
138        .margin(10)
139        .fontSize(20)
140        .onClick(()=>{
141          this.isShow = false;
142        })
143    }
144    .width('100%')
145    .height('100%')
146    .justifyContent(FlexAlign.Center)
147    .scale({x: this.isScale2, y: this.isScale2})
148    .animation({curve:curves.springMotion()})
149  }
150
151  build() {
152    Column() {
153      Button("transition modal 1")
154        .onClick(() => {
155          this.isShow = true
156        })
157        .fontSize(20)
158        .margin(10)
159        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
160    }
161    .justifyContent(FlexAlign.Center)
162    .backgroundColor("#ff49c8ab")
163    .width('100%')
164    .height('100%')
165    .scale({ x: this.isScale1, y: this.isScale1 })
166    .animation({ curve: curves.springMotion() })
167  }
168}
169```
170
171![en-us_full_screen_modal_none_2](figures/en-us_full_screen_modal_none_2.gif)
172
173### Example 3
174
175This example shows two modals whose transition type is slide-up and slide-down animation.
176
177```ts
178// xxx.ets
179@Entry
180@Component
181struct ModalTransitionExample {
182  @State isShow:boolean = false
183  @State isShow2:boolean = false
184
185  @Builder myBuilder2() {
186    Column() {
187      Button("close modal 2")
188        .margin(10)
189        .fontSize(20)
190        .onClick(()=>{
191          this.isShow2 = false;
192        })
193    }
194    .width('100%')
195    .height('100%')
196  }
197
198  @Builder myBuilder() {
199    Column() {
200      Button("transition modal 2")
201        .margin(10)
202        .fontSize(20)
203        .onClick(()=>{
204          this.isShow2 = true;
205        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.DEFAULT, backgroundColor: Color.Gray, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
206
207      Button("close modal 1")
208        .margin(10)
209        .fontSize(20)
210        .onClick(()=>{
211          this.isShow = false;
212        })
213    }
214    .width('100%')
215    .height('100%')
216    .justifyContent(FlexAlign.Center)
217  }
218
219  build() {
220    Column() {
221      Button("transition modal 1")
222        .onClick(() => {
223          this.isShow = true
224        })
225        .fontSize(20)
226        .margin(10)
227        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.DEFAULT, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
228    }
229    .justifyContent(FlexAlign.Center)
230    .backgroundColor(Color.White)
231    .width('100%')
232    .height('100%')
233  }
234}
235```
236
237![en-us_full_screen_modal_default](figures/en-us_full_screen_modal_default.gif)
238
239### Example 4
240
241This example shows two modals whose transition type is opacity gradient animation.
242
243```ts
244// xxx.ets
245@Entry
246@Component
247struct ModalTransitionExample {
248  @State isShow:boolean = false
249  @State isShow2:boolean = false
250
251  @Builder myBuilder2() {
252    Column() {
253      Button("close modal 2")
254        .margin(10)
255        .fontSize(20)
256        .onClick(()=>{
257          this.isShow2 = false;
258        })
259    }
260    .width('100%')
261    .height('100%')
262    .justifyContent(FlexAlign.Center)
263  }
264
265
266  @Builder myBuilder() {
267    Column() {
268      Button("transition modal 2")
269        .margin(10)
270        .fontSize(20)
271        .onClick(()=>{
272          this.isShow2 = true;
273        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.ALPHA, backgroundColor: Color.Gray, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
274
275      Button("close modal 1")
276        .margin(10)
277        .fontSize(20)
278        .onClick(()=>{
279          this.isShow = false;
280        })
281    }
282    .width('100%')
283    .height('100%')
284    .justifyContent(FlexAlign.Center)
285  }
286
287  build() {
288    Column() {
289      Button("transition modal 1")
290        .onClick(() => {
291          this.isShow = true
292        })
293        .fontSize(20)
294        .margin(10)
295        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.ALPHA, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
296    }
297    .justifyContent(FlexAlign.Center)
298    .backgroundColor(Color.White)
299    .width('100%')
300    .height('100%')
301  }
302}
303```
304
305![en-us_full_screen_modal_alpha](figures/en-us_full_screen_modal_alpha.gif)
306