# NavItem

> Represents a Nav item: a link, or a toggle that opens a Menu.

[API reference](/docs/api/index.md) › [Bootstrap 6](/docs/extensions/bootstrap6/reference/index.md) › [item](/docs/extensions/bootstrap6/reference/item/index.md) › NavItem

| final readonly class | `yii2\extensions\bootstrap6\item\NavItem` |
| --- | --- |
| Uses traits | [`ItemTrait`](/docs/extensions/bootstrap6/reference/item/itemtrait/index.md) |
| Source | [`src/item/NavItem.php`](https://github.com/yii2-extensions/bootstrap6/blob/main/src/item/NavItem.php#L18) |

Represents a `Nav` item: a link, or a toggle that opens a `Menu`.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-active"></span>`$active` readonly constructor | `bool\|null` | Whether the item is active, or `null` to detect it from the [`$url`](/docs/extensions/bootstrap6/reference/item/navitem/index.md#property-url) route. Default: `null`. | `NavItem` |
| <span id="property-disabled"></span>`$disabled` readonly constructor | `bool` | Whether the item is disabled; it takes precedence over [`$active`](/docs/extensions/bootstrap6/reference/item/navitem/index.md#property-active). Default: `false`. | `NavItem` |
| <span id="property-encode"></span>`$encode` readonly constructor | `bool\|null` | Whether to HTML-encode [`$label`](/docs/extensions/bootstrap6/reference/item/navitem/index.md#property-label), or `null` to use `Nav::$encodeLabels`. Default: `null`. | `NavItem` |
| <span id="property-items"></span>`$items` readonly constructor | `array<array-key,` [`MenuItem`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md)`\|string>\|string` | Items of the menu toggled by the item, or the menu HTML; `[]` renders a plain link. Default: `[]`. | `NavItem` |
| <span id="property-label"></span>`$label` readonly constructor | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Label of the `.nav-link` element. | `NavItem` |
| <span id="property-linkOptions"></span>`$linkOptions` readonly constructor | `array<array-key, mixed>` | HTML attributes of the `.nav-link` element. Default: `[]`. | `NavItem` |
| <span id="property-menuOptions"></span>`$menuOptions` readonly constructor | `array<array-key, mixed>` | HTML attributes of the `.menu` panel. Default: `[]`. | `NavItem` |
| <span id="property-options"></span>`$options` readonly constructor | `array<array-key, mixed>` | HTML attributes of the `.nav-item` element. Default: `[]`. | `NavItem` |
| <span id="property-url"></span>`$url` readonly constructor | `array<array-key, mixed>\|string\|null` | URL processed by [`Url::to()`](/docs/api/yii/helpers/baseurl/index.md#method-to), or `null` for no `href`; a route array also drives the active state detection. Default: `'#'`. | `NavItem` |
| <span id="property-visible"></span>`$visible` readonly constructor | `bool` | Whether to render the item. Default: `true`. | `NavItem` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`fromArray()`](#method-fromArray) static | Creates an item from the array configuration documented in `Nav::$items`. | `NavItem` |
| [`menu()`](#method-menu) static | Creates a toggle that opens a `Menu`. | `NavItem` |
| [`to()`](#method-to) static | Creates a link item. | `NavItem` |
| [`withActive()`](#method-withActive) | Returns a copy with the given active state. | `NavItem` |
| [`withDisabled()`](#method-withDisabled) | Returns a copy with the given disabled state. | `NavItem` |
| [`withEncode()`](#method-withEncode) | Returns a copy with the given encoding flag. | `NavItem` |
| [`withItems()`](#method-withItems) | Returns a copy with the given menu items or menu HTML. | `NavItem` |
| [`withLabel()`](#method-withLabel) | Returns a copy with the given label. | `NavItem` |
| [`withLinkOptions()`](#method-withLinkOptions) | Returns a copy with the given `.nav-link` attributes. | `NavItem` |
| [`withMenuOptions()`](#method-withMenuOptions) | Returns a copy with the given `.menu` panel attributes. | `NavItem` |
| [`withOptions()`](#method-withOptions) | Returns a copy with the given `.nav-item` attributes. | `NavItem` |
| [`withUrl()`](#method-withUrl) | Returns a copy with the given URL. | `NavItem` |
| [`withVisible()`](#method-withVisible) | Returns a copy with the given visibility. | `NavItem` |

## Method details

### fromArray() <span id="method-fromArray"></span>

```php
public static function fromArray(array<array-key, mixed> $config): self
```

Creates an item from the array configuration documented in `Nav::$items`.

A missing `url` defaults to `#`; an explicit `null` renders no `href`. Nested `items` arrays are converted
through [`MenuItem::fromArray()`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#method-fromArray).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$config` | `array<array-key, mixed>` | Item configuration. |

**Returns**

`self`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the item, or one of its menu items, has no `string|Stringable` `label`.

### menu() <span id="method-menu"></span>

```php
public static function menu(string|Stringable $label, array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string>|string $items): self
```

Creates a toggle that opens a `Menu`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Toggle label. |
| `$items` | `array<array-key, array<string, mixed>\|`[`MenuItem`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md)`\|string>\|string` | Menu items, or the menu HTML; arrays are converted through [`MenuItem::fromArray()`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#method-fromArray). |

**Returns**

`self`

### to() <span id="method-to"></span>

```php
public static function to(string|Stringable $label, array<array-key, mixed>|string|null $url = '#'): self
```

Creates a link item.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Link label. |
| `$url` | `array<array-key, mixed>\|string\|null` | URL processed by [`Url::to()`](/docs/api/yii/helpers/baseurl/index.md#method-to); `null` renders no `href`. |

**Returns**

`self`

### withActive() <span id="method-withActive"></span>

```php
public function withActive(bool|null $active): self
```

Returns a copy with the given active state.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$active` | `bool\|null` |  |

**Returns**

`self`

### withDisabled() <span id="method-withDisabled"></span>

```php
public function withDisabled(bool $disabled): self
```

Returns a copy with the given disabled state.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$disabled` | `bool` |  |

**Returns**

`self`

### withEncode() <span id="method-withEncode"></span>

```php
public function withEncode(bool|null $encode): self
```

Returns a copy with the given encoding flag.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$encode` | `bool\|null` |  |

**Returns**

`self`

### withItems() <span id="method-withItems"></span>

```php
public function withItems(array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string>|string $items): self
```

Returns a copy with the given menu items or menu HTML.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$items` | `array<array-key, array<string, mixed>\|`[`MenuItem`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md)`\|string>\|string` | Menu items, or the menu HTML; arrays are converted through [`MenuItem::fromArray()`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#method-fromArray). |

**Returns**

`self`

### withLabel() <span id="method-withLabel"></span>

```php
public function withLabel(string|Stringable $label): self
```

Returns a copy with the given label.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) |  |

**Returns**

`self`

### withLinkOptions() <span id="method-withLinkOptions"></span>

```php
public function withLinkOptions(array<array-key, mixed> $linkOptions): self
```

Returns a copy with the given `.nav-link` attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$linkOptions` | `array<array-key, mixed>` | HTML attributes of the `.nav-link` element. |

**Returns**

`self`

### withMenuOptions() <span id="method-withMenuOptions"></span>

```php
public function withMenuOptions(array<array-key, mixed> $menuOptions): self
```

Returns a copy with the given `.menu` panel attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$menuOptions` | `array<array-key, mixed>` | HTML attributes of the `.menu` panel. |

**Returns**

`self`

### withOptions() <span id="method-withOptions"></span>

```php
public function withOptions(array<array-key, mixed> $options): self
```

Returns a copy with the given `.nav-item` attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$options` | `array<array-key, mixed>` | HTML attributes of the `.nav-item` element. |

**Returns**

`self`

### withUrl() <span id="method-withUrl"></span>

```php
public function withUrl(array<array-key, mixed>|string|null $url): self
```

Returns a copy with the given URL.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$url` | `array<array-key, mixed>\|string\|null` | URL processed by [`Url::to()`](/docs/api/yii/helpers/baseurl/index.md#method-to). |

**Returns**

`self`

### withVisible() <span id="method-withVisible"></span>

```php
public function withVisible(bool $visible): self
```

Returns a copy with the given visibility.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$visible` | `bool` |  |

**Returns**

`self`