Skip to content

Commit c8f11fb

Browse files
nielslyngsoegitbook-bot
authored andcommitted
GITBOOK-6: Update Modal Docs
1 parent 8a316fb commit c8f11fb

File tree

5 files changed

+127
-136
lines changed

5 files changed

+127
-136
lines changed

16/umbraco-cms/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@
158158
* [Workspace Actions](customizing/extending-overview/extension-types/workspaces/workspace-editor-actions.md)
159159
* [Workspace Context](customizing/extending-overview/extension-types/workspaces/workspace-context.md)
160160
* [Workspace Views](customizing/extending-overview/extension-types/workspaces/workspace-views.md)
161-
* [Route Registration](customizing/extending-overview/extension-types/modals/route-registration.md)
162161
* [Menu](customizing/extending-overview/extension-types/menu.md)
163162
* [Header Apps](customizing/extending-overview/extension-types/header-apps.md)
164163
* [Icons](customizing/extending-overview/extension-types/icons.md)
@@ -179,6 +178,7 @@
179178
* [Modals](customizing/extending-overview/extension-types/modals/README.md)
180179
* [Confirm Dialog](customizing/extending-overview/extension-types/modals/confirm-dialog.md)
181180
* [Custom Modals](customizing/extending-overview/extension-types/modals/custom-modals.md)
181+
* [Modal Route Registration](customizing/extending-overview/extension-types/modals/route-registration.md)
182182
* [Extension Kind](customizing/extending-overview/extension-kind.md)
183183
* [Extension Conditions](customizing/extending-overview/extension-conditions.md)
184184
* [Custom Extension types](customizing/extending-overview/custom-extension-type.md)

16/umbraco-cms/customizing/extending-overview/extension-types/modals/README.md

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,55 @@ description: >-
66

77
# Modals
88

9-
{% hint style="warning" %}
10-
This page is a work in progress and may undergo further revisions, updates, or amendments. The information contained herein is subject to change without notice.
11-
{% endhint %}
12-
139
## **Modal Types**
1410

15-
The Dialog modal appears in the middle of the screen. and the Sidebar Modal slide in from the right.
11+
The Dialog modal appears in the middle of the screen and the Sidebar Modal slides in from the right.
1612

17-
## Modal Token
13+
## Open a Modal
1814

19-
For type safety, we recommend that you use Modal Tokens. The Modal Token binds the Modal Type with a Modal Data Type and a Modal Result Type.
15+
A modal can be opened in two ways: either directly at runtime or by registering a route for the modal. Registering a route allows deep-linking to the modal, which may be preferred in certain scenarios. Otherwise, opening the modal directly is a simpler option.
2016

21-
This can also come with defaults, for example, settings for the modal type and size.
17+
### Directly open via the Open Modal method
2218

23-
This is an example of a Modal Token declaration:
19+
<pre class="language-ts" data-title="my-element.ts"><code class="lang-ts">import { customElement, html } from '@umbraco-cms/backoffice/external/lit';
20+
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
21+
import { MY_MODAL_TOKEN } from './my-modal.token.js';
22+
import { umbOpenModal } from '@umbraco-cms/backoffice/modal';
2423

25-
```ts
26-
import { UmbModalToken } from "@umbraco-cms/backoffice/modal";
27-
28-
export type OurSomethingPickerModalData = {
29-
// We do not have any data to parse for this example
30-
};
31-
32-
export type OurSomethingPickerModalValue = {
33-
key: string;
34-
};
35-
36-
export const MY_SOMETHING_PICKER_MODAL = new UmbModalToken<
37-
OurSomethingPickerModalData,
38-
OurSomethingPickerModalValue
39-
>("Our.Modal.SomethingPicker", {
40-
modal: {
41-
type: "sidebar",
42-
size: "small",
43-
},
44-
});
45-
```
24+
<strong>@customElement('my-element')
25+
</strong>class MyElement extends UmbLitElement {
4626

47-
## Make a custom Modal Element
27+
override render() {
28+
return html`
29+
&#x3C;uui-button look="primary" label="Open modal" @click=${this._openModal}>&#x3C;/uui-button>
30+
`;
31+
}
4832

49-
To create your own modal, read the [Implementing a Custom Modal article](custom-modals.md) before proceeding with this article.
33+
private async _openModal() {
34+
const returnedValue = await umbOpenModal(this, MY_MODAL_TOKEN, {
35+
data: {
36+
headline: "My modal headline",
37+
},
38+
}).catch(() => undefined);
39+
}
40+
}
5041

51-
### Basic Usage
42+
declare global {
43+
interface HTMLElementTagNameMap {
44+
'my-element': MyElement;
45+
}
46+
}
47+
</code></pre>
5248

53-
Consume the `UMB_MODAL_MANAGER_CONTEXT` and then use the modal manager context to open a modal. This example shows how to consume the Modal Manager Context:
49+
The Promise returned by `umbOpenModal` is handled for potential rejection. This occurs when the Model is closed without submitting. Use this behavior to carry out a certain action if the modal is cancelled. In this case, `undefined` is returned when the Modal is cancelled (rejected).
50+
51+
See the [Confirm Modal article](confirm-dialog.md) for an example.
52+
53+
### Directly open via the Modal Manager Context
54+
55+
For full control, open a modal via the **Modal Manager Context.** This is what the Open Modal method uses behind the scenes. Using the context directly gives you a bit more control and understanding of the system.
56+
57+
First, consume the `UMB_MODAL_MANAGER_CONTEXT` , then use the modal manager context to open a modal. The following example shows how to consume the Modal Manager Context:
5458

5559
```ts
5660
import { LitElement } from "@umbraco-cms/backoffice/external/lit";
@@ -72,13 +76,7 @@ export class MyElement extends UmbElementMixin(LitElement) {
7276
}
7377
```
7478

75-
#### Open a modal
76-
77-
A modal can be opened in two ways. Either you register the modal with a route or at runtime open the modal. The initial option allows users to deep-link to the modal, a potential preference in certain cases; otherwise, consider the latter.
78-
79-
#### Directly open a Modal
80-
81-
In this case, we use the Modal Token from above, this takes a key as its data. And if submitted then it returns the new key.
79+
In this case, the modal token from the previous example is used. It accepts a key as input data and returns the new key if the modal is submitted.
8280

8381
```typescript
8482
const modalContext = this.#modalManagerContext?.open(this, MY_SOMETHING_PICKER_MODAL, {
@@ -95,10 +93,14 @@ modalContext
9593
.catch(() => undefined);
9694
```
9795

98-
[See the implementing a Confirm Dialog for a more concrete example.](confirm-dialog.md)
96+
[See the implementing a Confirm Dialog for an example.](confirm-dialog.md)
9997

100-
**Modal Route Registration**
98+
### Modal Route Registration
10199

102100
You can register modals with a route, making it possible to link directly to that specific modal. This also means the user can navigate back and forth in the browser history. This makes it an ideal solution for modals containing an editorial experience.
103101

104-
For a more concrete example, check out the [Implementing a Confirm Dialog article](route-registration.md).
102+
For a more concrete example, check out the [Modal Route Registration article](route-registration.md).
103+
104+
## Make a custom Modal
105+
106+
To create your modal, read the [Implementing a Custom Modal article](custom-modals.md).

16/umbraco-cms/customizing/extending-overview/extension-types/modals/confirm-dialog.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,23 @@ The modal token describes the options that you can pass to the modal. The confir
2525
```typescript
2626
import { html, LitElement, customElement } from "@umbraco-cms/backoffice/external/lit";
2727
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
28-
import { UMB_MODAL_MANAGER_CONTEXT, UMB_CONFIRM_MODAL } from '@umbraco-cms/backoffice/modal';
28+
import { umbOpenModal, UMB_CONFIRM_MODAL } from '@umbraco-cms/backoffice/modal';
2929

3030
@customElement('my-element')
3131
export class MyElement extends UmbElementMixin(LitElement) {
32-
#modalManagerContext?: typeof UMB_MODAL_MANAGER_CONTEXT.TYPE;
33-
34-
constructor() {
35-
super();
36-
this.consumeContext(UMB_MODAL_MANAGER_CONTEXT, (instance) => {
37-
this.#modalManagerContext = instance;
38-
});
39-
}
4032

4133
#onRequestDisable() {
42-
const modalContext = this.#modalManagerContext?.open(
34+
const modalContext = umbOpenModal(
4335
this, UMB_CONFIRM_MODAL,
4436
{
4537
data: {
46-
headline: `${this.localize.term("actions_disable")}`,
47-
content: `${this.localize.term(
48-
"defaultdialogs_confirmdisable"
49-
)}`,
38+
headline: this.localize.term("actions_disable"),
39+
content: this.localize.term("defaultdialogs_confirmdisable"),
5040
color: "danger",
51-
confirmLabel: "Disable",
41+
confirmLabel: this.localize.term("actions_disable"),
5242
}
5343
}
54-
);
55-
modalContext
56-
?.onSubmit()
44+
)
5745
.then(() => {
5846
console.log("User has approved");
5947
})
Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,22 @@
11
---
2-
description: >-
3-
New modals can be added to the system via the extension registry. This article
4-
goes through how this is done.
2+
description: New modals can be added to the system via the extension registry.
53
---
64

75
# Custom Modals
86

9-
{% hint style="warning" %}
10-
This page is a work in progress and may undergo further revisions, updates, or amendments. The information contained herein is subject to change without notice.
11-
{% endhint %}
12-
13-
There are two parts to creating a custom modal:
14-
15-
* First, you need to create a modal element which you need to register in the extension registry.&#x20;
16-
* Second, you need to create and export a modal token.
17-
18-
## Create a modal token
19-
20-
A modal token is a string that identifies a modal. This is the modal extension alias. It is used to open a modal and is also to set default options for the modal. It should also have a unique alias to avoid conflicts with other modals.
21-
22-
```ts
23-
import { UmbModalToken } from '@umbraco-cms/backoffice/modal';
24-
25-
export type MyModalData = {
26-
headline: string;
27-
}
28-
29-
export type MyModalValue = {
30-
myData: string;
31-
}
7+
This article goes through adding new modals to the system. There are three steps to creating a custom modal:
328

33-
export const MY_MODAL_TOKEN = new UmbModalToken<MyModalData, MyModalValue>('My.Modal', {
34-
modal: {
35-
type: 'sidebar',
36-
size: 'small'
37-
}
38-
});
39-
```
9+
* Create a modal element
10+
* Declare an Extension Manifest
11+
* (Optional) Create a Modal Token&#x20;
4012

41-
A modal token is a generic type that takes two type arguments. The first is the type of the data that is passed to the modal when it is opened. The second is the type of the value that is returned when the modal is closed.
13+
After completing these steps, refer to the example on how to open the modal.
4214

4315
## Create a modal element
4416

45-
A modal element is a web component that is used to render the modal. It should implement the `UmbModalExtensionElement` interface. The modal context is injected into the element when the modal is opened in the `modalContext` property. The modal context is used to close the modal, update the value and submit the modal.
17+
A modal element is a web component that is used to render a modal. It should implement the `UmbModalExtensionElement` interface. The modal context is injected into the element when the modal is opened in the `modalContext` property. The modal context is used to close the modal, update the value and submit the modal.
4618

47-
Additionally, the modal element can see its data parameters through the `modalContext` property. In this example, the modal data is of type `MyModalData` and the modal value is of type `MyModalValue`. The modal context is of type `UmbModalContext<MyModalData, MyModalValue>`. We are using the data to render a headline and the value to update the value and submit the modal.
19+
Additionally, the modal element can see its data parameters through the `modalContext` property. In this example, the modal data is of type `MyModalData` , and the modal value is of type `MyModalValue`. The modal context is of type `UmbModalContext<MyModalData, MyModalValue>`. We are using the data to render a headline and the value to update the value and submit the modal.
4820

4921
{% code title="my-modal.element.ts" %}
5022
```ts
@@ -55,7 +27,7 @@ import type { UmbModalContext } from '@umbraco-cms/backoffice/modal';
5527
import type { MyModalData, MyModalValue } from './my-modal.token.js';
5628

5729
@customElement('my-dialog')
58-
export default class MyDialogElement
30+
export class MyDialogElement
5931
extends UmbLitElement
6032
implements UmbModalExtensionElement<MyModalData, MyModalValue> {
6133

@@ -84,57 +56,84 @@ export default class MyDialogElement
8456
`;
8557
}
8658
}
59+
60+
export const element = MyDialogElement;
8761
```
8862
{% endcode %}
8963

90-
## Register in the extension registry
64+
The class must be exported as an `element` or `default` for the Extension Registry to be able to pick up the class.
65+
66+
## Declare an Extension Manifest
9167

9268
The modal element needs to be registered in the extension registry. This is done by defining the modal in the manifest file. The `element` property should point to the file that contains the modal element.
9369

94-
```json
70+
```typescript
9571
{
96-
"type": "modal",
97-
"alias": "My.Modal",
98-
"name": "My Modal",
99-
"element": "../path/to/my-modal.element.js"
72+
type: 'modal',
73+
alias: 'My.Modal',
74+
name: 'My Modal',
75+
element: '../path/to/my-modal.element.js'
10076
}
10177
```
10278

103-
## Open the modal
79+
## Create a modal token
80+
81+
{% hint style="info" %}
82+
For type safety, it's recommended to use Modal Tokens. Using a Modal Token gives knowledge about the data that can be parsed to the Modal and as well as the type of the value coming back when submitted.
83+
{% endhint %}
10484

105-
To open the modal, you need to consume the `UmbModalManagerContext` and then use the modal manager context to open a modal. This example shows how to consume the Modal Manager Context:
85+
A Modal Token works as a constant that identifies the modal. It is used to open the modal and knows the types of the modal data and modal value. As well, it can contain a preset, containing default data and modal options.
86+
87+
The first argument passed to `UmbModalToken` is the extension alias; the second is the preset configuration.
10688

107-
{% code title="my-element.ts" %}
10889
```ts
109-
import { customElement, html } from '@umbraco-cms/backoffice/external/lit';
90+
import { UmbModalToken } from '@umbraco-cms/backoffice/modal';
91+
92+
export type MyModalData = {
93+
headline: string;
94+
}
95+
96+
export type MyModalValue = {
97+
myData: string;
98+
}
99+
100+
export const MY_MODAL_TOKEN = new UmbModalToken<MyModalData, MyModalValue>('My.Modal', {
101+
modal: {
102+
type: 'sidebar',
103+
size: 'small'
104+
}
105+
});
106+
```
107+
108+
A modal token is a generic type that takes two arguments(`<MyModalData, MyModalValue>`):
109+
110+
* The first defines the type of data passed to the modal when opened.
111+
* The second defines the type of value returned when the modal is submitted.
112+
113+
## Open the modal
114+
115+
To open the modal, call the `umbOpenModal` method with the Modal Token and data of choice:
116+
117+
<pre class="language-ts" data-title="my-element.ts"><code class="lang-ts">import { customElement, html } from '@umbraco-cms/backoffice/external/lit';
110118
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
111119
import { MY_MODAL_TOKEN } from './my-modal.token.js';
112-
import { UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal';
113-
114-
@customElement('my-element')
115-
class MyElement extends UmbLitElement {
116-
#modalManagerContext?: typeof UMB_MODAL_MANAGER_CONTEXT.TYPE;
117-
118-
constructor() {
119-
super();
120-
this.consumeContext(UMB_MODAL_MANAGER_CONTEXT, (instance) => {
121-
this.#modalManagerContext = instance;
122-
// modalManagerContext is now ready to be used.
123-
});
124-
}
120+
import { umbOpenModal } from '@umbraco-cms/backoffice/modal';
121+
122+
<strong>@customElement('my-element')
123+
</strong>class MyElement extends UmbLitElement {
125124

126125
override render() {
127126
return html`
128-
<uui-button look="primary" label="Open modal" @click=${this._openModal}></uui-button>
127+
&#x3C;uui-button look="primary" label="Open modal" @click=${this._openModal}>&#x3C;/uui-button>
129128
`;
130129
}
131130

132-
private _openModal() {
133-
this.#modalManagerContext?.open(this, MY_MODAL_TOKEN, {
131+
private async _openModal() {
132+
const returnedValue = await umbOpenModal(this, MY_MODAL_TOKEN, {
134133
data: {
135134
headline: "My modal headline",
136135
},
137-
});
136+
}).catch(() => undefined);
138137
}
139138
}
140139

@@ -143,5 +142,6 @@ declare global {
143142
'my-element': MyElement;
144143
}
145144
}
146-
```
147-
{% endcode %}
145+
</code></pre>
146+
147+
The Promise of `umbOpenModal` is caught if it gets rejected. This is because if the Model gets closed without submission, the Promise is rejected. YThis can be used to carry out a certain action if the modal is cancelled. In this case, `undefined` is returned when the Modal is cancelled (rejected).

16/umbraco-cms/customizing/extending-overview/extension-types/modals/route-registration.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# Route Registration
1+
---
2+
description: >-
3+
You can register modals with a route, making it possible to link directly to
4+
that specific modal. This also means the user can navigate back and forth in
5+
the browser history
6+
---
27

3-
{% hint style="warning" %}
4-
This page is a work in progress and may undergo further revisions, updates, or amendments. The information contained herein is subject to change without notice.
5-
{% endhint %}
6-
7-
You can register modals with a route, making it possible to link directly to that specific modal. This also means the user can navigate back and forth in the browser history.&#x20;
8+
# Modal Route Registration
89

910
A modal can be registered via the `UmbModalRouteRegistrationController`. The registration accepts a modal token (or extension alias).
1011

@@ -30,7 +31,7 @@ The registration holds an instance of its `UmbModalHandler` when the modal is ac
3031

3132
**Additional features of the route Registration:**
3233

33-
* Adds unique parts to the path.&#x20;
34+
* Adds unique parts to the path.
3435
* A modal registered in a dashboard can be setup in few steps
3536
* A modal registered in a property editor needs to become specific for the property and the variant of that property.
3637
* Builds some data for the setup.

0 commit comments

Comments
 (0)