Skip to content

Commit 105cfbc

Browse files
Luuk PetersLuuk Peters
authored andcommitted
Update examples in Context API documentation to include both Lit and vanilla JavaScript options, and improve clarity on notification button handler
1 parent 9d4c068 commit 105cfbc

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

16/umbraco-cms/customizing/foundation/context-api/consume-a-context.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ The most straightforward way to consume a context is on an Umbraco Element with
3030
### Get a one-time reference to a context
3131
A one-time reference to a context is for when you want to perform some action and then forget the context. A good example of this is when you want to display a notification in the backoffice when a user performs an action.
3232

33+
The first example uses Lit and that is the way Umbraco builds their elements. If you don't want to use Lit, there is also an example using vanilla Javascript. Both examples don't have any TypeScript specific code, so you either use them in a JavaScript or TypeScript file.
34+
3335
{% tabs %}
34-
{% tab title="Lit element (typescript)" %}
36+
{% tab title="Lit element" %}
3537
```typescript
3638
import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification';
3739
import { html } from '@umbraco-cms/backoffice/external/lit';
@@ -41,11 +43,8 @@ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
4143
//This gives us all the helpers we need to get or consume contexts
4244
export default class ExampleElement extends UmbLitElement {
4345

44-
/**
45-
* Notification handler for the notification button
46-
* @param {Event} event The event that triggered the change
47-
*/
48-
async #notificationButtonClick(event: Event) {
46+
/** Notification handler for the notification button */
47+
async #notificationButtonClick {
4948
//We try to get an instance of the context
5049
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
5150
if (!notificationContext) {
@@ -79,7 +78,7 @@ export default class ExampleElement extends UmbLitElement {
7978
customElements.define('example-element', ExampleElement);
8079
```
8180
{% endtab %}
82-
{% tab title="HTML element (typescript)" %}
81+
{% tab title="HTML element" %}
8382
```typescript
8483
import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification';
8584
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
@@ -93,6 +92,7 @@ export default class ExampleElement extends UmbElementMixin(HTMLElement) {
9392
this.#render();
9493
}
9594

95+
/** Renders the element **/
9696
#render() {
9797
if (!this.shadowRoot)
9898
return;
@@ -107,7 +107,9 @@ export default class ExampleElement extends UmbElementMixin(HTMLElement) {
107107
button?.addEventListener('click', this.#notificationButtonClick.bind(this));
108108
}
109109

110-
async #notificationButtonClick(event: Event) {
110+
/** Notification handler for the notification button */
111+
async #notificationButtonClick {
112+
//We try to get an instance of the context
111113
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
112114
if (!notificationContext) {
113115
throw new Error('Notification context not found!');
@@ -119,6 +121,8 @@ export default class ExampleElement extends UmbElementMixin(HTMLElement) {
119121
message: "The notification button was clicked successfully!"
120122
}
121123
});
124+
125+
//The notification is sent, now forget the context
122126
}
123127
}
124128

0 commit comments

Comments
 (0)