Skip to content

Commit 08d3e10

Browse files
committed
Update menus article
1 parent a43d1ad commit 08d3e10

File tree

1 file changed

+95
-60
lines changed
  • 16/umbraco-cms/customizing/extending-overview/extension-types

1 file changed

+95
-60
lines changed

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

Lines changed: 95 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,82 @@
1-
# Menu
1+
---
2+
description: >-
3+
Create menus that appear throughout the backoffice, in sidebars, button flyouts, and more.
4+
---
25

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+
# Menus
7+
8+
Menu extensions contain one or more menu item extensions and can be placed throughout the backoffice - in sidebars, flyouts, and more. This article will cover how to create a menu with custom menu items.
69

710
<figure><img src="../../../.gitbook/assets/menu.png" alt="" width="250"><figcaption><p>Menu</p></figcaption></figure>
811

912
## Creating a custom menu
1013

11-
In this section, you can learn how to register and create a custom Menu for the Umbraco backoffice.
12-
13-
### Manifest
14-
15-
The manifest file can be created using either JSON or TypeScript. Both methods are shown below.
14+
Menu extensions can be created using either JSON or TypeScript. Both approaches are shown below.
1615

1716
{% tabs %}
18-
1917
{% tab title="JSON" %}
20-
21-
We can create the manifest using JSON in the `umbraco-package.json`.
22-
18+
{% code title="umbraco-package.json" %}
2319
```json
2420
{
2521
"type": "menu",
2622
"alias": "My.Menu",
2723
"name": "My Menu"
2824
}
2925
```
26+
{% endcode %}
3027
{% endtab %}
31-
3228
{% tab title="TypeScript" %}
29+
Extension authors define the menu manifest, then register it dynamically/during runtime using a [Backoffice Entry Point](../../extending-overview/extension-types/backoffice-entry-point.md) extension.
3330

34-
The manifest can also be written in TypeScript.
35-
36-
For this TypeScript example we used a [Backoffice Entry Point](../../extending-overview/extension-types/backoffice-entry-point.md) extension to register the manifests.
37-
31+
{% code title="my-menu/manifests.ts" %}
3832
```typescript
3933
import type { ManifestMenu } from '@umbraco-cms/backoffice/menu';
4034

41-
const menuManifest: Array<ManifestMenu> = [
35+
export const menuManifest: Array<ManifestMenu> = [
4236
{
4337
type: 'menu',
4438
alias: 'My.Menu',
4539
name: 'My Menu'
4640
}
4741
];
4842
```
43+
{% endcode %}
4944

50-
{% endtab %}
45+
{% code title="entrypoints/entrypoints.ts" %}
46+
```typescript
47+
import type {
48+
UmbEntryPointOnInit,
49+
} from "@umbraco-cms/backoffice/extension-api";
50+
import { umbExtensionsRegistry } from "@umbraco-cms/backoffice/extension-registry";
51+
import { menuManifest } from "./../my-menu/manifests.ts";
5152

53+
export const onInit: UmbEntryPointOnInit = (_host, _extensionRegistry) => {
54+
console.log("Hello from my extension 🎉");
55+
56+
umbExtensionsRegistry.register(menuManifest);
57+
};
58+
```
59+
{% endcode %}
60+
{% endtab %}
5261
{% endtabs %}
5362

54-
# Menu Item
63+
# Menu Items
5564

56-
<figure><img src="../../../.gitbook/assets/menu-item.png" alt="" width="250"><figcaption><p>Menu Item</p></figcaption></figure>
65+
Each menu consists of one or more menu item extensions. Extension authors can create customized menu items.
5766

58-
Menu items are the items that appear in the menu.
67+
<figure><img src="../../../.gitbook/assets/menu-item.png" alt="" width="250"><figcaption><p>Menu Item</p></figcaption></figure>
5968

60-
## Creating a custom menu items
69+
## Creating menu items
6170

62-
In this section, you can learn how to add custom Menu Items to your Umbraco backoffice Menu.
71+
Menu Item extensions can be created using either JSON or TypeScript. Both approaches are shown below.
6372

6473
### Manifest
6574

6675
To add custom menu items, you can define a single MenuItem manifest and link an element to it. In this element, you can fetch the data and render as many menu items as you want based on that data.
6776

68-
The code snippets below show how to declare a new menu item using JSON or TypeScript.
69-
7077
{% tabs %}
71-
7278
{% tab title="JSON" %}
73-
74-
We can create the manifest using JSON in the `umbraco-package.json`.
75-
79+
{% code title="umbraco-package.json" %}
7680
```json
7781
{
7882
"type": "menuItem",
@@ -85,16 +89,18 @@ We can create the manifest using JSON in the `umbraco-package.json`.
8589
}
8690
}
8791
```
88-
92+
{% hint style="info" %}
93+
The `element` parameter is optional. Omitting it will render a menu item styled using Umbraco defaults.
94+
{% endhint %}
95+
{% endcode %}
8996
{% endtab %}
90-
9197
{% tab title="TypeScript" %}
9298

93-
The manifest can also be written in TypeScript.
99+
Extension authors define the menu manifest, then register it dynamically/during runtime using a [Backoffice Entry Point](../../extending-overview/extension-types/backoffice-entry-point.md) extension.
94100

95-
For this TypeScript example we used a [Backoffice Entry Point](../../extending-overview/extension-types/backoffice-entry-point.md) extension to register the manifests.
101+
The `element` attribute will point toward a custom Lit component, an example of which will be in the next section of this article.
96102

97-
{% code title="manifest.ts" overflow="wrap" lineNumbers="true" %}
103+
{% code title="my-menu/manifests.ts" %}
98104
```typescript
99105
const menuItemManifest: Array<ManifestMenuItem> = [
100106
{
@@ -105,37 +111,51 @@ const menuItemManifest: Array<ManifestMenuItem> = [
105111
label: 'My Menu Item',
106112
menus: ["My.Menu"]
107113
},
108-
element: () => import('./menu-items.ts')
109114
}
110115
];
111116
```
112117
{% endcode %}
113118

119+
{% code title="entrypoints/entrypoints.ts" %}
120+
```typescript
121+
import type {
122+
UmbEntryPointOnInit,
123+
} from "@umbraco-cms/backoffice/extension-api";
124+
import { umbExtensionsRegistry } from "@umbraco-cms/backoffice/extension-registry";
125+
import { menuItemManifest } from "./../my-menu/manifests.ts";
114126

115-
{% endtab %}
127+
export const onInit: UmbEntryPointOnInit = (_host, _extensionRegistry) => {
128+
console.log("Hello from my extension 🎉");
116129

130+
umbExtensionsRegistry.register(menuItemManifest);
131+
};
132+
```
133+
{% endcode %}
134+
{% endtab %}
117135
{% endtabs %}
118136

119-
### The UI Element
137+
### Custom menu items
120138

121-
#### Rendering menu items with Umbraco's UI menu item component
139+
{% hint style="info" %}
140+
**Note:** Displaying menu item extensions does not require extension authors to create custom menu item subclasss. This step is optional.
141+
{% endhint %}
122142

123-
To render your menu items in Umbraco, you can use the [Umbraco UI Menu Item component](https://uui.umbraco.com/?path=/docs/uui-menu-item--docs). This component allows you to create nested menu structures with a few lines of code.
143+
To render your menu items in Umbraco, extension authors can use the [Umbraco UI Menu Item component](https://uui.umbraco.com/?path=/docs/uui-menu-item--docs). This component enables nested menu structures with a few lines of markup.
124144

125-
By default, you can set the `has-children` attribute to display the caret icon indicating nested items. It will look like this: `?has-children=${bool}`.
145+
`<uui-menu-item>` nodes accept the `has-children` boolean attribute, which will display a caret icon indicating nested items. Tying this boolean attribute to a variable requires using the `?` Lit directive, which would look similar to this: `?has-children=${boolVariable}`.
126146

127147
**Example:**
128148

129-
```tsx
149+
```html
130150
<uui-menu-item label="Menu Item 1" has-children>
131151
<uui-menu-item label="Nested Menu Item 1"></uui-menu-item>
132152
<uui-menu-item label="Nested Menu Item 2"></uui-menu-item>
133153
</uui-menu-item>
134154
```
135155

136-
#### Custom menu item element example
156+
### Custom menu item element example
137157

138-
You can fetch the data and render the menu items using the Lit element above. By putting the result of the fetch in a `@state()`, we can trigger a re-render of the component when the data is fetched.
158+
Custom elements can fetch the data and render menu items using markup, like above. Storing the results of the fetch in a `@state()` variable will trigger a re-render of the component when the value of the variable changes.
139159

140160
{% code title="menu-items.ts" overflow="wrap" lineNumbers="true" %}
141161
```typescript
@@ -211,17 +231,14 @@ declare global {
211231
[elementName]: MyMenuItems;
212232
}
213233
}
214-
215234
```
216235
{% endcode %}
217236

218-
219237
## Tree Menu Item
220238

221239
### Manifest
222240

223241
```json
224-
// it will be something like this
225242
{
226243
"type": "menuItem",
227244
"kind": "tree",
@@ -242,22 +259,40 @@ The default element supports rendering a subtree of menu items.
242259
class UmbMenuItemTreeDefaultElement {}
243260
```
244261

245-
### Adding menu items to an existing menu
262+
## Adding menu items to an existing menu
246263

247-
The backoffice comes with a couple of menus.
264+
Extension authors are able to add their own additional menu items to the menus that ship with Umbraco.
248265

249-
* Content, Media, Settings, Templating, Dictionary, etc.
266+
Some examples of these built-in menus include:
250267

251-
To add a menu item to an existing menu, you can use the `meta.menus` property.
268+
* Content - `Umb.Menu.Content`
269+
* Media - `Umb.Menu.Media`
270+
* Settings - `Umb.Menu.StructureSettings`
271+
* Templating - `Umb.Menu.Templating`
272+
* ...
252273

253-
```typescript
274+
Additional Umbraco menus (nine, total) can be found using the Extension Insights browser and selecting **Menu** from the dropdown.
275+
276+
<figure><img src="../../../.gitbook/assets/extension-types-backoffice-browser.png" alt=""><figcaption><p>Backoffice extension browser</p></figcaption></figure>
277+
278+
### Extending Menus
279+
280+
To add a menu item to an existing menu, use the `meta.menus` property.
281+
282+
{% code title="umbraco-package.json" %}
283+
```json
254284
{
255-
"type": "menuItem",
256-
"alias": "My.MenuItem",
257-
"name": "My Menu Item",
258-
"meta": {
259-
"label": "My Menu Item",
260-
"menus": ["Umb.Menu.Content"]
261-
}
285+
"type": "menuItem",
286+
"alias": "My.MenuItem",
287+
"name": "My Menu Item",
288+
"meta": {
289+
"label": "My Menu Item",
290+
"menus": ["Umb.Menu.Content"]
291+
},
292+
"element": "menu-items.js"
262293
}
263294
```
295+
{% endcode %}
296+
297+
## See Also
298+
* [Section Sidebar](sections/section-sidebar.md) for information on creating menus for navigation within section extensions.

0 commit comments

Comments
 (0)