Skip to content

Commit 1dde0e3

Browse files
authored
docs: improved modal docs (#137)
1 parent 4f30ed1 commit 1dde0e3

File tree

1 file changed

+125
-27
lines changed

1 file changed

+125
-27
lines changed

content/guide/navigation/modals.md

Lines changed: 125 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,143 @@ description: Navigation using modals - detached from the current backstack.
44
contributos:
55
- Ombuweb
66
- rigor789
7+
- flipperlite
78
---
89

910
## Showing a modal
1011

1112
To show a modal, call the [showModal](https://docs.nativescript.org/api/class/ViewCommon#showmodal) method on a [View](https://docs.nativescript.org/api/class/View) instance and pass it the path to the modal view file:
1213

14+
<!-- note: we need a "Open In StackBlitz" button, and also put these in an official NativeScript collection to link on the docs. -->
15+
<!-- A strongly typed, working, Typescript application can be [found here](https://stackblitz.com/edit/nativescript-stackblitz-templates-3bvo1g?file=app%2Fdetails-page.ts,app%2Fdetails-page.xml,app%2Fmain-page.ts). -->
16+
1317
```xml
14-
<Button text="View details" tap="onTap" />
18+
<Button text="Show a Modal" tap="onShowModal" />
1519
```
1620

1721
```ts
18-
onTap(args: EventData) {
19-
const button = args.object as Button
20-
button.showModal("details-page")
22+
import { EventData, View, ShowModalOptions } from '@nativescript/core'
23+
24+
export function onShowModal(args: EventData) {
25+
const view = args.object as View
26+
const options: ShowModalOptions = {
27+
// ...
28+
}
29+
view.showModal('details-page', options)
2130
}
2231
```
2332

33+
:::warning Note
34+
If your modal does not appear when tapping the button, confirm you set the correct path and the modal page exists. `showModal` does not throw an error when the provided path doesn't exist.
35+
:::
36+
2437
## Closing a modal
2538

26-
To close a modal, call the `closeModal` method of any `View` from the modal. Optionally pass it data you want to access in the parent page:
39+
To close a modal, call the `closeModal` method of any `View` from the modal.
40+
41+
For passing data back to the parent, see [Passing Data](#passing-data).
2742

2843
```xml
29-
<Button text="Close" tap="onTap" />
44+
<Button text="Close a Modal" tap="onCloseModal"/>
3045
```
3146

3247
```ts
33-
onTap(args: EventData) {
34-
const button = args.object as Button
35-
button.closeModal({
36-
name: 'John Doe - EDITED',
37-
})
48+
export function onCloseModal(args: EventData) {
49+
const view = args.object as View
50+
view.closeModal()
3851
}
3952
```
4053

41-
## Passing data to and from a modal
54+
## Passing data {#passing-data}
55+
56+
Modals are often used for prompting the user for input, this requires passing data between the parent and the modal.
4257

43-
Passing data can be done by passing a `context` in the `showModal` options parameter.
58+
### From parent to modal
59+
60+
To pass data to the modal, provide it in the `context` field of the ShowModalOptions:
4461

4562
```ts
63+
// main-page.ts
64+
import { ShowModalOptions } from '@nativescript/core'
65+
66+
// optionally use strong types
67+
import { ExampleModalContext } from './details-page'
68+
4669
const options: ShowModalOptions = {
47-
context: { name: 'John Doe' },
48-
closeCallback(args: { name: string }) {
49-
console.log('Modal closed', args.name)
50-
},
70+
context: {
71+
name: 'John Doe',
72+
} as ExampleModalContext,
5173
}
52-
// ...
74+
5375
button.showModal('details-page', options)
5476
```
5577

56-
To access the data in the modal, handle the `shownModally` event, and access it via `args.context`.
78+
In the modal page, listen to the `shownModally` event to access the `context`:
5779

5880
```xml
81+
<!-- details-page.xml -->
82+
<Page shownModally="onShownModally"/>
83+
```
84+
85+
```ts
86+
// details-page.ts
87+
import { ShownModallyData } from '@nativescript/core'
88+
89+
// optional strong type for the context
90+
export type ExampleModalContext = {
91+
name: string
92+
}
93+
94+
export function onShownModally(args: ShownModallyData) {
95+
const context = args.context as ExampleModalContext
96+
console.log(context.name) // 'John Doe'
97+
}
98+
```
99+
100+
### From modal to parent
101+
102+
When closing a modal, you can optionally pass data back to the parent. To do so, provide a `closeCallback` option in the ShowModalOptions:
103+
104+
```ts
105+
// main-page.ts
106+
107+
import { ShowModalOptions } from '@nativescript/core'
108+
109+
// optionally use strong types
110+
import { ExampleModalContext, ExampleModalResult } from './details-page'
111+
112+
const options: ShowModalOptions = {
113+
context: {
114+
name: 'John Doe',
115+
} as ExampleModalContext,
116+
closeCallback(result?: ExampleModalResult) {
117+
if (result) {
118+
console.log(`Modal returned: ${result.newName}`) // 'Modal returned: Jane Doe'
119+
return
120+
}
121+
console.log('Modal was cancelled.')
122+
},
123+
}
124+
125+
button.showModal('details-page', options)
126+
```
127+
128+
In the modal page, listen to the `shownModally` event to access the `context`.
129+
130+
::: code-group
131+
132+
```xml [details-page.xml]
59133
<Page shownModally="onShownModally">
60134
<StackLayout>
135+
<!-- ... -->
61136
<TextField text="{{ name }}" />
62-
<Button text="Close" tap="onClose"/>
137+
<Button text="Change Name" tap="onChangeName"></Button>
138+
<Button text="Cancel" tap="onCancel"></Button>
63139
</StackLayout>
64140
</Page>
65141
```
66142

67-
```ts
143+
```ts [details-page.ts]
68144
import {
69145
fromObject,
70146
Page,
@@ -73,17 +149,39 @@ import {
73149
EventData,
74150
} from '@nativescript/core'
75151

152+
// optional strong type for the context
153+
export type ExampleModalContext = {
154+
name: string
155+
}
156+
157+
export type ExampleModalResult = {
158+
newName: string
159+
}
160+
76161
export function onShownModally(args: ShownModallyData) {
77162
const page = args.object as Page
78-
const context = fromObject({
79-
...args.context,
80-
onClose(args: EventData) {
163+
const incomingContext = args.context as ExampleModalContext
164+
165+
const bindingContext = fromObject({
166+
...incomingContext,
167+
onChangeName(args: EventData) {
81168
const button = args.object as Button
82169
button.closeModal({
83-
name: context.name, // 'John Doe - EDITED'
84-
})
170+
newName: bindingContext.name, // 'Jane Doe'
171+
} as ExampleModalResult)
172+
},
173+
onCancel(args: EventData) {
174+
const view = args.object as View
175+
view.closeModal()
85176
},
86177
})
87-
page.bindingContext = context
178+
page.bindingContext = bindingContext
88179
}
89180
```
181+
182+
:::
183+
184+
## Additional Resources
185+
186+
- [NativeScript XML Data Binding](/guide/data-binding)
187+
- [TextField](/ui/text-field)

0 commit comments

Comments
 (0)