Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu: Added extra examples + explanation #6456

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 145 additions & 9 deletions 14/umbraco-cms/customizing/extending-overview/extension-types/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,173 @@
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.
{% endhint %}

## Menu

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

```typescript
**JSON Manifest:**
Yinzy00 marked this conversation as resolved.
Show resolved Hide resolved
```json
{
"type": "menu",
"alias": "My.Menu",
"name": "My Menu"
}
```

### Menu Item <a href="#menu-item" id="menu-item"></a>
**Typescript Manifest:**

Check warning on line 18 in 14/umbraco-cms/customizing/extending-overview/extension-types/menu.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [UmbracoDocs.Names] We prefer TypeScript over 'Typescript' Raw Output: {"message": "[UmbracoDocs.Names] We prefer TypeScript over 'Typescript'", "location": {"path": "14/umbraco-cms/customizing/extending-overview/extension-types/menu.md", "range": {"start": {"line": 18, "column": 3}}}, "severity": "WARNING"}
```typescript
import { ManifestMenu } from "@umbraco-cms/backoffice/extension-registry";

const menuManifest: Array<ManifestMenu> = [
{
type: 'menu',
alias: 'My.Menu',
name: 'My Menu'
}
];
```

## Menu Item

<figure><img src="../../../.gitbook/assets/menu-item.png" alt="" width="250"><figcaption><p>Menu Item</p></figcaption></figure>

```typescript
Menu items are the items that appear in the menu.

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.

The code snippets below show how to declare a new menu item using JSON or Typescript.

Check warning on line 39 in 14/umbraco-cms/customizing/extending-overview/extension-types/menu.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [UmbracoDocs.Names] We prefer TypeScript over 'Typescript' Raw Output: {"message": "[UmbracoDocs.Names] We prefer TypeScript over 'Typescript'", "location": {"path": "14/umbraco-cms/customizing/extending-overview/extension-types/menu.md", "range": {"start": {"line": 39, "column": 75}}}, "severity": "WARNING"}

### JSON Manifest

```json
{
"type": "menuItem",
"alias": "My.MenuItem",
"name": "My Menu Item",
"element": "./menu-items.ts",
"meta": {
"label": "My Menu Item",
"menus": ["My.Menu"]
}
}
```

### **Tree Menu Item**
### Typescript Manifest

Check warning on line 56 in 14/umbraco-cms/customizing/extending-overview/extension-types/menu.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [UmbracoDocs.Names] We prefer TypeScript over 'Typescript' Raw Output: {"message": "[UmbracoDocs.Names] We prefer TypeScript over 'Typescript'", "location": {"path": "14/umbraco-cms/customizing/extending-overview/extension-types/menu.md", "range": {"start": {"line": 56, "column": 5}}}, "severity": "WARNING"}

```typescript
const menuItemManifest: Array<ManifestMenuItem> = [
{
type: 'menuItem',
alias: 'My.MenuItem',
name: 'My Menu Item',
meta: {
label: 'My Menu Item',
menus: ["My.Menu"]
},
element: () => import('./menu-items.ts')
}
];
```

### The UI Element
sofietoft marked this conversation as resolved.
Show resolved Hide resolved

#### Rendering menu items with Umbraco's UI menu item component

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.

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}`.

**Example:**

```tsx
<uui-menu-item label="Menu Item 1" has-children>
<uui-menu-item label="Nested Menu Item 1"></uui-menu-item>
<uui-menu-item label="Nested Menu Item 2"></uui-menu-item>
</uui-menu-item>
```

#### Custom menu item element example

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.

**menu-items.ts:**
```typescript
import { UmbMenuItemElement } from '@umbraco-cms/backoffice/extension-registry';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { html, TemplateResult } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { MyMenuItemResponseModel, MyMenuResource } from '../../../api';

const elementName = 'my-menu-item';

@customElement(elementName)
class MyMenuItems extends UmbLitElement implements UmbMenuItemElement {
@state()
private _items: MyMenuItemResponseModel[] = []; // Store fetched items
@state()
private _loading: boolean = true; // Track loading state
@state()
private _error: string | null = null; // Track any errors

constructor() {
super();
this.fetchInitialItems(); // Start fetching on component load
}

// Fetch initial items
async fetchInitialItems() {
try {
this._loading = true;
this._items = ((await MyMenuResource.getMenuApiV1()).items); // Fetch root-level items
} catch (e) {
this._error = 'Error fetching items';
} finally {
this._loading = false;
}
}

// Render items
renderItems(items: MyMenuItemResponseModel[]): TemplateResult {
return html`
${items.map(element => html`
<uui-menu-item label="${element.name}" ?has-children=${element.hasChildren}>
${element.type === 1
? html`<uui-icon slot="icon" name="icon-folder"></uui-icon>`
: html`<uui-icon slot="icon" name="icon-autofill"></uui-icon>`}
<!-- recursively render children -->
${element.hasChildren ? this.renderItems(element.children) : ''}
</uui-menu-item>
`)}
`;
}

// Main render function
render() {
if (this._loading) {
return html`<uui-loader></uui-loader>`;
}

if (this._error) {
return html`<uui-menu-item active disabled label="Could not load form tree!">
</uui-menu-item>`;
}

// Render items if loading is done and no error occurred
return html`${this.renderItems(this._items)}`;
}
}

export { MyMenuItems as element };

declare global {
interface HTMLElementTagNameMap {
[elementName]: MyMenuItems;
}
}

```

## Tree Menu Item

#### **Manifest**
### Manifest

```typescript
// it will be something like this
Expand All @@ -50,14 +186,14 @@
}
```

#### **Default Element**
#### Default Element

```typescript
// get interface
interface UmbTreeMenuItemElement {}
```

### **Adding menu items to an existing menu**
### Adding menu items to an existing menu

The backoffice comes with a couple of menus.

Expand Down
150 changes: 143 additions & 7 deletions 15/umbraco-cms/customizing/extending-overview/extension-types/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,173 @@
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.
{% endhint %}

## Menu

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

```typescript
**JSON Manifest:**
```json
{
"type": "menu",
"alias": "My.Menu",
"name": "My Menu"
}
```

### Menu Item <a href="#menu-item" id="menu-item"></a>
**Typescript Manifest:**

Check warning on line 18 in 15/umbraco-cms/customizing/extending-overview/extension-types/menu.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [UmbracoDocs.Names] We prefer TypeScript over 'Typescript' Raw Output: {"message": "[UmbracoDocs.Names] We prefer TypeScript over 'Typescript'", "location": {"path": "15/umbraco-cms/customizing/extending-overview/extension-types/menu.md", "range": {"start": {"line": 18, "column": 3}}}, "severity": "WARNING"}
```typescript
import { ManifestMenu } from "@umbraco-cms/backoffice/extension-registry";

const menuManifest: Array<ManifestMenu> = [
{
type: 'menu',
alias: 'My.Menu',
name: 'My Menu'
}
];
```


## Menu Item <a href="#menu-item" id="menu-item"></a>

<figure><img src="../../../.gitbook/assets/menu-item.png" alt="" width="250"><figcaption><p>Menu Item</p></figcaption></figure>

```typescript
### What is a Menu Item?

Menu items are the items that appear in the menu.

For adding custom menu items we can define a single MenuItem manifest and link an element to it. In this element we can fetch the data and render as many menu items as we want based on that data.

### JSON Manifest

```json
{
"type": "menuItem",
"alias": "My.MenuItem",
"name": "My Menu Item",
"element": "./menu-items.ts",
"meta": {
"label": "My Menu Item",
"menus": ["My.Menu"]
}
}
```

### **Tree Menu Item**
### Typescript Manifest

Check warning on line 57 in 15/umbraco-cms/customizing/extending-overview/extension-types/menu.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [UmbracoDocs.Names] We prefer TypeScript over 'Typescript' Raw Output: {"message": "[UmbracoDocs.Names] We prefer TypeScript over 'Typescript'", "location": {"path": "15/umbraco-cms/customizing/extending-overview/extension-types/menu.md", "range": {"start": {"line": 57, "column": 5}}}, "severity": "WARNING"}
```typescript
const menuItemManifest: Array<ManifestMenuItem> = [
{
type: 'menuItem',
alias: 'My.MenuItem',
name: 'My Menu Item',
meta: {
label: 'My Menu Item',
menus: ["My.Menu"]
},
element: () => import('./menu-items.ts')
}
];
```

### The Lit Element

#### Rendering menu items with Umbraco's UI menu item component

To render your menu items in Umbraco, you can make use of the powerful [Umbraco UI Menu Item component](https://uui.umbraco.com/?path=/docs/uui-menu-item--docs). This component allows you to easily create nested menu structures with just a few lines of code.

Check warning on line 77 in 15/umbraco-cms/customizing/extending-overview/extension-types/menu.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [UmbracoDocs.Editorializing] Consider removing 'easily' Raw Output: {"message": "[UmbracoDocs.Editorializing] Consider removing 'easily'", "location": {"path": "15/umbraco-cms/customizing/extending-overview/extension-types/menu.md", "range": {"start": {"line": 77, "column": 192}}}, "severity": "WARNING"}

Check warning on line 77 in 15/umbraco-cms/customizing/extending-overview/extension-types/menu.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [UmbracoDocs.Editorializing] Consider removing 'just' Raw Output: {"message": "[UmbracoDocs.Editorializing] Consider removing 'just'", "location": {"path": "15/umbraco-cms/customizing/extending-overview/extension-types/menu.md", "range": {"start": {"line": 77, "column": 234}}}, "severity": "WARNING"}

To display the caret icon indicating nested items, you can set the `has-children` attribute dynamically like this: `?has-children=${bool}`.

**Example:**

```tsx
<uui-menu-item label="Menu Item 1" has-children>
<uui-menu-item label="Nested Menu Item 1"></uui-menu-item>
<uui-menu-item label="Nested Menu Item 2"></uui-menu-item>
</uui-menu-item>
```

#### Custom menu item element example

Using this Lit element we can fetch the data and render the menu items. By putting the result of the fetch in a `@state()`, we can trigger a re-render of the component when the data is fetched.

**menu-items.ts:**
```typescript
import { UmbMenuItemElement } from '@umbraco-cms/backoffice/extension-registry';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { html, TemplateResult } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { MyMenuItemResponseModel, MyMenuResource } from '../../../api';

const elementName = 'my-menu-item';

@customElement(elementName)
class MyMenuItems extends UmbLitElement implements UmbMenuItemElement {
@state()
private _items: MyMenuItemResponseModel[] = []; // Store fetched items
@state()
private _loading: boolean = true; // Track loading state
@state()
private _error: string | null = null; // Track any errors

constructor() {
super();
this.fetchInitialItems(); // Start fetching on component load
}

// Fetch initial items
async fetchInitialItems() {
try {
this._loading = true;
this._items = ((await MyMenuResource.getMenuApiV1()).items); // Fetch root-level items
} catch (e) {
this._error = 'Error fetching items';
} finally {
this._loading = false;
}
}

// Render items
renderItems(items: MyMenuItemResponseModel[]): TemplateResult {
return html`
${items.map(element => html`
<uui-menu-item label="${element.name}" ?has-children=${element.hasChildren}>
${element.type === 1
? html`<uui-icon slot="icon" name="icon-folder"></uui-icon>`
: html`<uui-icon slot="icon" name="icon-autofill"></uui-icon>`}
<!-- recursively render children -->
${element.hasChildren ? this.renderItems(element.children) : ''}
</uui-menu-item>
`)}
`;
}

// Main render function
render() {
if (this._loading) {
return html`<uui-loader></uui-loader>`;
}

if (this._error) {
return html`<uui-menu-item active disabled label="Could not load form tree!">
</uui-menu-item>`;
}

// Render items if loading is done and no error occurred
return html`${this.renderItems(this._items)}`;
}
}

export { MyMenuItems as element };

declare global {
interface HTMLElementTagNameMap {
[elementName]: MyMenuItems;
}
}

```

## **Tree Menu Item**

#### **Manifest**
### **Manifest**

```typescript
// it will be something like this
Expand Down
Loading