# item

## Overview

Items are destinations that users can navigate to. An item is associated with a single view in your app. When a user clicks on an item, they will be navigated to its associated view automatically.

The hamburger icon is present on any view that has an associated item the navigation drawer.

An item can be a parent or a child. Parent items can be set to be collapsable, in which case a caret will display next to the item that will show and hide its children on toggle.

An icon can be specified for an item, and will be placed on the left of its text.

### Basic Example

![](https://2865107717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9TCHLR67eLHBOjPvHhud%2Fuploads%2Fgit-blob-65741b3321dd1257ba43064c39f21ce23d2a35d8%2Fnavigation-item-example.png?alt=media)

{% hint style="success" %}
**`item` best practices**

**Naming a view**

View names should be:

* short and concise
* devoid of action words (e.g. use "Sites" instead of "View sites")

**Collapsible items**

When deciding whether parent items with children should be collapsable or not - have a look at the amount of content in the navigation drawer as a whole. If it contains a lot of items, make parent items collapsable so that all/most parent items are visible without scrolling. If the drawer is fairly empty, and most children items are visible without scrolling, don’t make parent items collapsible.
{% endhint %}

## Standard Attributes

### **`label`**

`<item label="{$:getSiteName()}: Summary"/>`

Provide a label for a navigation item. Can be a static string, a [format string](https://docs.journeyapps.com/reference/app-features/xml-format-strings) or a [JS/TS function](https://docs.journeyapps.com/reference/app-features/calling-js-functions-from-xml).

```xml
<!-- XML -->
<navigation >
    <section >
        <item label="Sites"/>
        <!-- Other navigation items here -->
    </section>
</navigation>
```

{% hint style="info" %}
**`label` best practices**

View names should be:

* short and concise
* devoid of action words (e.g. use "Sites" instead of "View sites")
  {% endhint %}

### `validate`

`<item label="User details" view="user/details" validate="true">`

**Optional**

**Default**: `false`

Validation will check all required fields on the view and confirm that they contain values. In the case validation fails, the user cannot navigate to another item on the navigation drawer.

```xml
<item label="Home" view="main"/>
<item label="User details" view="user/details" validate="true"/>
```

{% hint style="info" %}
Validation on navigation items operate differently from validation on other view components. Validation is performed on the view that corresponds to the item that has `validate="true"` specified and not the target item being navigated to.
{% endhint %}

### `view`

`<item view="users">`

The `view` attribute ties a destination to a particular view path. The user will be navigated to that view.

```xml
<!-- XML -->
<item label="Site" view="site"/>
```

{% hint style="info" %}
The `navigation` drawer will only be visible on views which has corresponding view paths specified.
{% endhint %}

{% hint style="info" %}
In the case a parent `item` has no `view` specified and is `collapsable`, the `item` will behave in a similar way a collapsable `section` behaves.
{% endhint %}

## Advanced Attributes

### `args`

`<item args="$:buildItemArgs()">`

In the case where the view being navigated to expects view parameters, we pass arguments using `args=$:function()`. The `$:function()` should return an array of values that match the number and type of the expected view parameters.

**Example**: The view with view path `site` expects two parameters, the current user and the current site.

```xml
<!-- XML -->
<item label="Site" view="site" args="$:getSiteArgs()"/>
```

```javascript
// JS
function getSiteArgs() {
    return [ user, view.currentSite ];
}
```

{% hint style="info" %}
In the case where multiple items reference the same view path, please see the [`id`](#id) reference on how to manually set the active item.
{% endhint %}

### `collapsable`

`<item collapsable="true"/>`

**Default**: false

Specify that the parent item should be collapsable.

```xml
<!-- XML -->
<navigation >
    <section >
        <item label="Site" view="site" collapsable="true">
            <item label="Equipment" view="equipment" />
        </item>
    </section>
</navigation>
```

{% hint style="info" %}
`collapsable="true"` will only be applied to a parent item if it contains children items.
{% endhint %}

{% hint style="success" %}
**`collapsable` best practices**

When deciding whether parent items with children should be collapsable or not - have a look at the amount of content in the navigation drawer as a whole. If it contains a lot of items, make parent items collapsable so that all/most parent items are visible without scrolling. If the drawer is fairly empty, and most children items are visible without scrolling, don’t make parent items collapsible.
{% endhint %}

### `from-js`

`<item from-js="$:buildItemFromJS">`

Construct a navigation item from JavaScript/TypeScript, using a `$:function()` that returns an array of `component.navigationItem` objects.

```xml
<!-- XML -->
<item from-js="$:buildItemFromJS()"/>
```

```javascript
// JS
function buildItemFromJS() {
    // Your logic here
    return component.navigationItem({
        label: "Users",
        view: "users",
        id: "js-item",
        args: function() {
            return [ user ];
        },
        icon: "icons/man.png",
        collapsable: true,
        items: buildSubItems()
    });
}

function buildSubItems(){
    // Logic to construct an array of component.navigationItem objects
}
```

### `id`

`<item id="{$:getItemId()}"/>`

**Type**: Format string

**Default**: unset

The `id` attribute is used to target the `item` when triggering a component method. See [Component methods](https://docs.journeyapps.com/reference/build/ui-components/all-ui-components/navigation-navigation-drawer/..#component-methods) for more detail.

### `icon`

`<item icon="fa-bomb">`

Specify an icon for a navigation item. String representing a font-awesome, ionicon or png icon.

```xml
<!-- XML -->
<item label="Site" view="site" icon="$:getSiteIcon()"/>
```

```javascript
// JS
function getSiteIcon() {
    // Logic here
    return "icons/site.png";
}
```

### `icon-color`

Specify a color for an `item`'s [`icon`](#icon) using `icon-color="my-named-color"`. Supports named colors and #HEX values.

### `indicator`

See [`navigation` > `indicator`](https://docs.journeyapps.com/reference/build/ui-components/all-ui-components/navigation-navigation-drawer/..#indicator)

### `items`

`<item items="$:buildItems()"/>`

{% hint style="info" %}
In a `section`, the `item` element can be nested to a maximum depth of two.
{% endhint %}

Construct a parent item's items from JavaScript/TypeScript using a function that returns an object array of type `navigationItem`.

```xml
<!-- XML -->
<navigation >
    <section>
        <item label="Users" view="users" items="$:buildItems()" collapsable="true"/>
    </section>
</navigation>
```

```javascript
// JS
function buildItems() {
    var paths = ["operators", "admins"];

    return ["Operators", "Administrators"].map(function(item, idx) {
        return component.navigationItem({
            label: item,
            view: paths[idx]
        });
    });
}
```

### `show-if` and `hide-if`

{% content-ref url="../../../xml-fields/show-if" %}
[show-if](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/show-if)
{% endcontent-ref %}

{% content-ref url="../../../xml-fields/hide-if" %}
[hide-if](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/hide-if)
{% endcontent-ref %}
