Links

Extending the Dashboard using Umbraco UI library

Now that we have data for our dashboard we might want to make it look prettier. To do this we can use the Umbraco UI library.
This page is a work in progress. It will be updated as the software evolves.

Overview

This is the fourth and final part of the guide to building a Custom Dashboard. This part continues work on the dashboard we built in part three: Adding functionality to the Dashboard. But it goes further to showcase how we can use the UI Library in our extension.

Umbraco UI Library

The Umbraco UI Library is a set of web components and variables that we can use to build Umbraco User Interfaces. It is already part of the Backoffice, which means you can already start to use it.
By using the variables available from the UI Library, you ensure that your extensions are a consistent size with the rest of the backoffice.
Let's start by wrapping uui-box around our render output. This makes our dashboard the same style as the built-in dashboards of Umbraco.
welcome-dashboard.element.ts
render() {
return html`
<uui-box>
// rest of the render code
</uui-box>
`;
}
The uui-box has a headline property. Let's move our headline into the headline property.
welcome-dashboard.element.ts
render() {
return html`
<uui-box headline="${this.localize.term('welcomeDashboard_heading')} ${this._currentUser?.name ?? 'Unknown'}!">
// rest of the render code
</uui-box>
`;
}
The uui-box also has a headline slot if you want to add an element instead. This element can also be styled and contain other elements. So let's say we want to keep using umb-localize instead of localize.term(), we can accomplish this by doing so:
welcome-dashboard.element.ts
render() {
return html`
<uui-box>
<span slot="headline">
<umb-localize key="welcomeDashboard_heading">Welcome</umb-localize>
${this._currentUser?.name ?? 'Unknown'}!
</span>
// rest of the render code
</uui-box>
`;
}
The UI Library also has a lot of variables we can use such as sizes and colors. Let's update our padding to ensure that our element is always consistent with the rest of the backoffice:
welcome-dashboard.element.ts
static styles = [
css`
:host {
display: block;
padding: var(--uui-size-layout-1);
}
...
`,
];
This already looks a lot better!
The dashboard in uui-box
See the entire file: welcome-dashboard.element.ts
welcome-dashboard.element.ts
1
import { UMB_AUTH, UmbLoggedInUser } from "@umbraco-cms/backoffice/auth";
2
import { LitElement, css, html, customElement, state } from "@umbraco-cms/backoffice/external/lit";
3
import { UmbUserDetail, UmbUserRepository } from '@umbraco-cms/backoffice/users';
4
import { UmbElementMixin } from "@umbraco-cms/backoffice/element-api";
5
6
@customElement('my-welcome-dashboard')
7
export class MyWelcomeDashboardElement extends UmbElementMixin(LitElement) {
8
@state()
9
private _currentUser?: UmbLoggedInUser;
10
11
@state()
12
private _userData?: Array<UmbUserDetail>;
13
14
private _auth?: typeof UMB_AUTH.TYPE;
15
16
private _userRepository = new UmbUserRepository(this);
17
18
constructor() {
19
super();
20
this.consumeContext(UMB_AUTH, (instance) => {
21
this._auth = instance;
22
this._observeCurrentUser();
23
});
24
this._getDataFromRepository();
25
}
26
27
//Get the current user
28
private async _observeCurrentUser() {
29
if (!this._auth) return;
30
this.observe(this._auth.currentUser, (currentUser) => {
31
this._currentUser = currentUser;
32
});
33
}
34
35
//Get all users
36
private async _getDataFromRepository() {
37
const { data } = await this._userRepository.requestCollection();
38
this._userData = data?.items;
39
}
40
41
render() {
42
return html`
43
<uui-box>
44
<h1 slot="headline">
45
<umb-localize key="welcomeDashboard_heading">Welcome</umb-localize>
46
${this._currentUser?.name ?? 'Unknown'}!
47
</h1>
48
<div>
49
<p>
50
<umb-localize key="welcomeDashboard_bodytext">
51
This is the Backoffice. From here, you can modify the content, media, and settings of your website.
52
</umb-localize>
53
</p>
54
<p>
55
<umb-localize key="welcomeDashboard_copyright"> © Sample Company 20XX </umb-localize>
56
</p>
57
</div>
58
<div id="users-wrapper">${this._userData?.map((user) => this._renderUser(user))}</div>
59
</uui-box>
60
`;
61
}
62
63
private _renderUser(user: UmbUserDetail) {
64
return html`<div class="user">
65
<div>${user.name}</div>
66
<div>${user.email}</div>
67
<div>${user.state}</div>
68
</div>`;
69
}
70
71
static styles = [
72
css`
73
:host {
74
display: block;
75
padding: var(--uui-size-layout-1);
76
}
77
78
#users-wrapper {
79
border: 1px solid lightgray;
80
}
81
82
.user {
83
padding: 5px 10px;
84
}
85
86
.user:not(:first-child) {
87
border-top: 1px solid lightgray;
88
}
89
`,
90
];
91
}
92
93
export default MyWelcomeDashboardElement;
94
95
declare global {
96
interface HTMLElementTagNameMap {
97
'my-welcome-dashboard': MyWelcomeDashboardElement;
98
}
99
}
Let's try another uui element.
Since we have a lot of information from the users, it could be a good idea to insert it into a proper table. The Umbraco UI (UUI) Library also includes a uui-table, so let's use that.
welcome-dashboard.element.ts
render() {
return html`
<uui-box>
...
<uui-table id="users-wrapper">
<uui-table-row>
<uui-table-head-cell>Name</uui-table-head-cell>
<uui-table-head-cell>Email</uui-table-head-cell>
<uui-table-head-cell>Status</uui-table-head-cell>
</uui-table-row>
${this._userData?.map((user) => this._renderUser(user))}
</uui-table>
</uui-box>
`;
}
private _renderUser(user: UmbUserDetail) {
if (!user) return;
return html`<uui-table-row class="user">
<uui-table-cell>${user.name}</uui-table-cell>
<uui-table-cell>${user.email}</uui-table-cell>
<uui-table-cell>${user.state}</uui-table-cell>
</uui-table-row>`;
}
Since the uui-table and others are handling the table, we can redo our CSS a bit:
welcome-dashboard.element.ts
static styles = [
css`
:host {
display: block;
padding: var(--uui-size-layout-1);
}
uui-table-head-cell {
font-weight: bold;
}
.user:hover,
.user:focus {
cursor: pointer;
background-color: var(--uui-color-surface-alt);
}
`,
];
Your dashboard component should now look like this:
Dashboard with uui-table in use
See the entire file: welcome-dashboard.element.ts
welcome-dashboard.element.ts
1
import { UMB_AUTH, UmbLoggedInUser } from "@umbraco-cms/backoffice/auth";
2
import { LitElement, css, html, customElement, state } from "@umbraco-cms/backoffice/external/lit";
3
import { UmbUserDetail, UmbUserRepository } from '@umbraco-cms/backoffice/users';
4
import { UmbElementMixin } from "@umbraco-cms/backoffice/element-api";
5
6
@customElement('my-welcome-dashboard')
7
export class MyWelcomeDashboardElement extends UmbElementMixin(LitElement) {
8
@state()
9
private _currentUser?: UmbLoggedInUser;
10
11
@state()
12
private _userData?: Array<UmbUserDetail>;
13
14
private _auth?: typeof UMB_AUTH.TYPE;
15
16
private _userRepository = new UmbUserRepository(this);
17
18
constructor() {
19
super();
20
this.consumeContext(UMB_AUTH, (instance) => {
21
this._auth = instance;
22
this._observeCurrentUser();
23
});
24
this._getPagedUserData();
25
}
26
27
//Get the current user
28
private async _observeCurrentUser() {
29
if (!this._auth) return;
30
this.observe(this._auth.currentUser, (currentUser) => {
31
this._currentUser = currentUser;
32
});
33
}
34
35
//Get all users
36
private async _getPagedUserData() {
37
const { data } = await this._userRepository.requestCollection();
38
this._userData = data?.items;
39
}
40
41
render() {
42
return html`
43
<uui-box>
44
<h1 slot="headline">
45
<umb-localize key="welcomeDashboard_heading">Welcome</umb-localize>
46
${this._currentUser?.name ?? 'Unknown'}!
47
</h1>
48
<div>
49
<p>
50
<umb-localize key="welcomeDashboard_bodytext">
51
This is the Backoffice. From here, you can modify the content, media, and settings of your website.
52
</umb-localize>
53
</p>
54
<p>
55
<umb-localize key="welcomeDashboard_copyright"> © Sample Company 20XX </umb-localize>
56
</p>
57
</div>
58
59
<uui-table id="users-wrapper">
60
<uui-table-row>
61
<uui-table-head-cell>Name</uui-table-head-cell>
62
<uui-table-head-cell>Email</uui-table-head-cell>
63
<uui-table-head-cell>Status</uui-table-head-cell>
64
</uui-table-row>
65
${this._userData?.map((user) => this._renderUser(user))}
66
</uui-table>
67
</uui-box>
68
`;
69
}
70
71
private _renderUser(user: UmbUserDetail) {
72
if (!user) return;
73
return html`<uui-table-row class="user">
74
<uui-table-cell>${user.name}</uui-table-cell>
75
<uui-table-cell>${user.email}</uui-table-cell>
76
<uui-table-cell>${user.state}</uui-table-cell>
77
</uui-table-row>`;
78
}
79
80
static styles = [
81
css`
82
:host {
83
display: block;
84
padding: var(--uui-size-layout-1);
85
}
86
87
uui-table-head-cell {
88
font-weight: bold;
89
}
90
91
.user:hover,
92
.user:focus {
93
cursor: pointer;
94
background-color: var(--uui-color-surface-alt);
95
}
96
`,
97
];
98
}
99
100
export default MyWelcomeDashboardElement;
101
102
declare global {
103
interface HTMLElementTagNameMap {
104
'my-welcome-dashboard': MyWelcomeDashboardElement;
105
}
106
}
Challenge (optional): Insert the user.state into a uui-tag that uses different values of the propertieslook and color. The values depend on the state of the user. For example, use look="primary" and color="positive" when the user is Active
See possible solution to the challenge
Remember that you can come to a solution in many different ways! Here is a possible solution to how it could've been solved:
import { UserStateModel } from '@umbraco-cms/backoffice/backend-api';
import { InterfaceColor, InterfaceLook } from '@umbraco-cms/backoffice/external/uui';
...
private _renderUser(user: UmbUserDetail) {
if (!user) return;
const state = this.getLookAndColorFromUserState(user.state);
return html`<uui-table-row class="user">
<uui-table-cell>${user.name}</uui-table-cell>
<uui-table-cell>${user.email}</uui-table-cell>
<uui-table-cell><uui-tag look=${state.look} color=${state.color}>${user.state}</uui-tag></uui-table-cell>
</uui-table-row>`;
}
getLookAndColorFromUserState = (state?: UserStateModel): { look: InterfaceLook; color: InterfaceColor } => {
switch (state) {
case UserStateModel.INACTIVE:
case UserStateModel.INVITED:
return { look: 'primary', color: 'warning' };
case UserStateModel.ACTIVE:
return { look: 'primary', color: 'positive' };
case UserStateModel.DISABLED:
return { look: 'primary', color: 'danger' };
default:
return { look: 'secondary', color: 'default' };
}
};
...
The uui-table-row also have a selectable property. We advise you to continue to experiment with different UUI elements from the UI library to build awesome interfaces!