Backoffice Notifications
Learn how to show notifications in the Umbraco backoffice, and which notifications the backoffice shows for server responses.
Notifications are short messages that appear over the backoffice UI. They are also known as toasts. Use them to confirm that an action succeeded, or to tell the user about a problem.
The Notification Context shows the notifications. The context is global, so you can consume it from any element or controller in the backoffice.
Show a Notification
Consume the context with the UMB_NOTIFICATION_CONTEXT token. Then call peek() with a color and an options object. The following element copies a link to the clipboard and shows the result:
import { customElement, html, property } from "@umbraco-cms/backoffice/external/lit";
import { UmbLitElement } from "@umbraco-cms/backoffice/lit-element";
import { UMB_NOTIFICATION_CONTEXT } from "@umbraco-cms/backoffice/notification";
@customElement("my-copy-link-button")
export class MyCopyLinkButtonElement extends UmbLitElement {
@property()
url = "";
#notificationContext?: typeof UMB_NOTIFICATION_CONTEXT.TYPE;
constructor() {
super();
this.consumeContext(UMB_NOTIFICATION_CONTEXT, (context) => {
this.#notificationContext = context;
});
}
async #copy() {
try {
await navigator.clipboard.writeText(this.url);
this.#notificationContext?.peek("positive", {
data: { headline: "Link copied", message: "The link is on your clipboard." },
});
} catch {
this.#notificationContext?.peek("danger", {
data: { headline: "Link not copied", message: "The browser blocked access to the clipboard." },
});
}
}
override render() {
return html`<uui-button look="primary" label="Copy link" @click=${this.#copy}></uui-button>`;
}
}
declare global {
interface HTMLElementTagNameMap {
"my-copy-link-button": MyCopyLinkButtonElement;
}
}To get the context once instead of subscribing to it, see the Consume a Context article.
Peek and Stay
The context has two methods that show a notification:
peek()shows a notification that closes after six seconds. Set thedurationoption to change the time.stay()shows a notification that stays open until the user or your code closes it.
Every notification has a close button. Hovering over or focusing a notification pauses the timers of all notifications.
Use stay() for a message that the user must act on. You can also use it to show the progress of a task, and close it from code when the task ends.
Both methods return a handler for the notification. Call close() on the handler to close the notification. The following controller shows a notification while an import runs:
Create the controller with a host, for example new MyProductImportController(this) in an element.
Colors
The first argument of peek() and stay() sets the color of the notification:
default
Neutral information.
positive
An action that succeeded.
warning
Something that the user should check.
danger
An error, or an action that failed.
Options
The options object takes these properties:
data: The content of the notification. See the Notification Data section.duration: The time in milliseconds before apeek()notification closes. Setdurationtonullto keep the notification open.stay()ignores this option.elementName: The name of a custom element that renders the notification instead of the default layout. The element gets thedataandnotificationHandlerproperties. Screen readers announcedata.message, so include amessageindata.
Notification Data
The default layout reads these properties from data:
headline: An optional title.message: The text of the notification. The message renders as plain text, so HTML markup shows as text. Line breaks (\n) are kept. Screen readers announce the message, not the headline.
Show an Error
The umbPeekError function shows a notification for an error. The notification is danger unless you set color. Besides headline and message, the function takes these properties:
detail: Text below the message. Long text moves to a dialog, which the user opens with a button.errors: Validation errors, keyed by field name. The user opens the errors in a dialog with a button.
To show your own headline, handle the error of a request yourself and show it with umbPeekError. Pass disableNotifications: true, so tryExecute does not show its own notification as well. See the Failed Requests section.
Notifications From Server Responses
The backoffice shows notifications for server responses when a request fails, and when the server sends event messages.
Failed Requests
tryExecute shows a notification when a request fails. The notification shows the title and detail of the problem details in the response. Some failed requests show no notification. For the full rules, see the Executing Requests article.
To handle the error yourself, pass disableNotifications: true to tryExecute. You can then show your own notification, for example with umbPeekError.
When the session has expired, the Umbraco HTTP Client asks the user to log in again. For a request that is not a GET request, it then shows a warning that asks the user to try again. disableNotifications does not hide this warning.
Event Messages
On the server, notification handlers can add event messages, for example when they cancel an operation. For an example, see the ContentService Notifications article.
Controllers that inherit from ManagementApiControllerBase return these messages in the Umb-Notifications response header. To add the header to another controller, apply the [AppendEventMessages] attribute from the Umbraco.Cms.Api.Management.Filters namespace. The header is not added to responses for GET requests.
The Umbraco HTTP Client shows a notification for each message. The category of the message is the headline, and the type sets the color:
Error
danger
Warning
warning
Success
positive
Info or Default
default
The client also shows these notifications when tryExecute has disableNotifications set. A generated client shows them too when you configure it with configureClient(), as described in the Custom Generated Client article. A request with the Fetch API does not show them.
The backoffice renders the message as plain text, so HTML markup in an event message shows as text.
Further Reading
Last updated
Was this helpful?