# sidebar

This UI component displays a sidebar with items on the left side of the screen on desktop and tablets, to indicate to the user how far they are progressing with a process.

{% hint style="info" %}
**Sidebar hidden by default on small screens**

By default the sidebar is not shown if the app is used on devices with smaller screens (< 768px). This behavior can be changed in its configuration.
{% endhint %}

The text contained inside the `<sidebar>` tag can be a [formatted string](https://docs.journeyapps.com/reference/app-features/xml-format-strings), meaning that you can make the text dynamic.

```xml
<sidebar>
    <item state="normal" icon="ion-ios-clock-outline">Pending</item>
    <item state="normal" icon="ion-ios-redo-outline">Dispatched</item>
    <item state="active" icon="ion-ios-location-outline">Arrived</item>
    <item state="disabled" icon="ion-ios-gear-outline">Work Started</item>
    <item state="disabled" icon="ion-ios-checkmark-outline">Closed</item>
</sidebar>
```

{% hint style="info" %}
**Sidebar Priority**

The sidebar must be the first component listed in the View XML.
{% endhint %}

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

### Configuration

#### sidebar

| Option              | Required   | Details                                                                                                                                                                                                                                                                                            |
| ------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `position`          | *optional* | <p>Specifies the position of the sidebar. Can be <code>left</code> or <code>right</code>.<br>Can also be specified by returning a value from a function.<br>Defaults to <code>left</code>.<br><strong>Note</strong> that the position of the sidebar will only update after a view transition.</p> |
| `items`             | *optional* | <p>Function which returns an array of <code>component.sidebarItem</code> objects.<br><strong>Note</strong>: If <code>items=</code> is specified, the sidebar cannot contain any <code>\<item></code> children.</p>                                                                                 |
| `visible-on-mobile` | *optional* | <p>Boolean indicating whether or not to show the sidebar on mobile.<br>Can be either "true" or "false".<br>Defaults to "false"</p>                                                                                                                                                                 |

#### item

| Option          | Required   | Details                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| --------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `state`         | *optional* | <p>The way the sidebar item is visually rendered, to indicate to the user whether they have already completed the step indicated by the sidebar item in a process. It can be one of the following:<br></p><ul><li><code>normal</code>: The sidebar item is <strong>shown normally</strong> — should be used for steps in a process that the user has already completed.</li><li><code>active</code>: The sidebar item is <strong>highlighted</strong> — should be used to indicate that the user is currently at the step in the process indicated by this sidebar item.</li><li><code>disabled</code>: The sidebar item text and icon is <strong>greyed</strong> out — should be used for steps in a process that the user has not yet completed.</li></ul><p>In other words, exactly one field <em>should</em> be <code>active</code>. All fields before it <em>should</em> be <code>normal</code>, and all fields afterwards <em>should</em> be <code>disabled</code>.<br>Defaults to <code>normal</code></p> |
| `icon`          | *optional* | Path of the icon to display on the sidebar item, left of the text. Icons can be uploaded using the **Assets** workspace in OXIDE.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `icon-color`    | *optional* | <p>Sets the icon's color. Can be a named color, like <code>primary</code> or a hex value, like <code>#008000</code>.<br>Can also be specified by returning a value from a function.<br>Return <code>null</code> to use the icon's default color.<br>Was added in version 4.36.0 of the JourneyApps Container.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `show-if`       | *optional* | Controls whether the sidebar item is hidden or shown. The argument specified to `show-if` can either be a literal boolean value (`true` or `false`), or it can specify a variable, parameter or field that can be a string, number, object, etc. that **evaluates to false** or **evaluates to true** (see the reference documentation for [`show-if`](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/show-if) and [`hide-if`](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/hide-if) for further details)                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `hide-if`       | *optional* | The opposite of `show-if` (see above).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `on-press`      | *optional* | <p>Function to call when the item is pressed - either a JavaScript/TypeScript function or a navigation function.<br><strong>Note:</strong> Adding <code>on-press</code> to an <code>item</code> adds a right chevron icon to its right-hand-side.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `on-press-icon` | *optional* | <p>The Ionicon to display on the right-hand-side of the item, typically denoting the action.<br>Defaults to <code>ion-chevron-right</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `validate`      | *optional* | Whether to run field validation before calling the associated `on-press` (see above). Can be either `true`, `false`, or the result of a function, e.g. (`$:shouldBeValidated()`).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |

### Dynamically Generated Sidebars

{% hint style="info" %}
**Version compatibility**

The feature to generate sidebars dynamically was introduced in version **4.38.5** of the JourneyApps Container.
{% endhint %}

```xml
<sidebar items="$:getItems()">
<!-- Cannot have <item> here if items= is defined -->
</sidebar>
```

The function in `items=` must return an array of `component.sidebarItem` objects. `component.sidebarItem` has the following properties:

```javascript
{
    label: string;
    state: string;
    icon: string;
    iconColor: string;
    onPress: function;
    onPressIcon: string;
    validate: boolean;
}
```

{% hint style="info" %}
**Implicit `show-if` and `hide-if`**

Note that the items specified above do not have `showIf` or `hideIf` fields. All items added to the array will automatically be shown. In order to hide an item, omit it from the returned array.
{% endhint %}

### Basic Example

```xml
<sidebar items="$:getItems()" />
```

```javascript
function getItems() {
  var items = [];
  items.push(
    component.sidebarItem({
      label: 'Home',
      onPress: function() {
        link.home();
      }
    })
  );
  items.push(
    component.sidebarItem({
      label: 'Orders',
      state: 'active'
    })
  );

// You can also construct a sidebarItem like this:
  var thirdItem = component.sidebarItem();
  thirdItem.label = 'Staff';
  thirdItem.onPress = function() {
    link.staff();
  };
  thirdItem.icon = 'fa-user-astronaut';
  items.push(thirdItem);

  return items;
}
```

### Example 1 using SharedJS

```xml
<sidebar items="$:getSharedItems()" />
```

```javascript
// In your app's SharedJS
function getSharedItems() {
    return [
        component.sidebarItem({ label: 'Item 1' }),
        component.sidebarItem({ label: 'Item 2' })
    ];
}
```

### Example 2 using SharedJS

```xml
<sidebar items="$:getItems()" />
```

```javascript
// In your app's SharedJS
function getItemsInSharedJS() {
    return [
        component.sidebarItem({ label: 'Item 1' }),
        component.sidebarItem({ label: 'Item 2' })
    ];
}

// In your view's JS
function getItems() {
    return getItemsInSharedJS().concat([
        component.sidebarItem({
            label: 'Item 3'
        })
    ]);
}
```
