# TogglerAttributesTrait

> Provides fluent methods that modify the HTML attributes of the toggler rendered by the widget.

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

| trait | `yii2\extensions\bootstrap6\TogglerAttributesTrait` |
| --- | --- |
| Source | [`src/TogglerAttributesTrait.php`](https://github.com/yii2-extensions/bootstrap6/blob/main/src/TogglerAttributesTrait.php#L16) |

Used by 4 types:

- [`Collapse`](/docs/extensions/bootstrap6/reference/collapse/index.md)
- [`Dialog`](/docs/extensions/bootstrap6/reference/dialog/index.md)
- [`Drawer`](/docs/extensions/bootstrap6/reference/drawer/index.md)
- [`NavBar`](/docs/extensions/bootstrap6/reference/navbar/index.md)

Provides fluent methods that modify the HTML attributes of the toggler rendered by the widget.

The using class implements [`togglerOptions()`](/docs/extensions/bootstrap6/reference/togglerattributestrait/index.md#method-togglerOptions), which returns its toggler attribute array by reference; every
method modifies that array in place and returns the same instance. The attributes the toggler requires (for example
`data-bs-toggle` or `aria-controls`) are added while rendering.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`addTogglerAttribute()`](#method-addTogglerAttribute) | Sets an HTML attribute value of the toggler. | `TogglerAttributesTrait` |
| [`addTogglerClass()`](#method-addTogglerClass) | Adds one or more CSS classes to the existing toggler classes; duplicated classes are collapsed. | `TogglerAttributesTrait` |
| [`addTogglerCssStyle()`](#method-addTogglerCssStyle) | Adds CSS style properties to the `style` attribute of the toggler. | `TogglerAttributesTrait` |
| [`togglerOptions()`](#method-togglerOptions) protected abstract | Returns the HTML attributes of the toggler by reference, enabling the toggler when it is disabled. | `TogglerAttributesTrait` |

## Method details

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

```php
public function addTogglerAttribute(string $name, mixed $value): static
```

Sets an HTML attribute value of the toggler.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Attribute name. |
| `$value` | `mixed` | Attribute value, or `null` to omit the attribute when rendering. |

**Returns**

`static`

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

```php
public function addTogglerClass(BackedEnum|string|null ...$class): static
```

Adds one or more CSS classes to the existing toggler classes; duplicated classes are collapsed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `...$class` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string\|null` | CSS class names to add; an enum case adds its value, and `null` is ignored. |

**Returns**

`static`

**See also**

- <https://html.spec.whatwg.org/#classes>

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

```php
public function addTogglerCssStyle(array<string, string>|string $style, bool $overwrite = true): static
```

Adds CSS style properties to the `style` attribute of the toggler.

Follows the merge rules of [`AttributesTrait::addCssStyle()`](/docs/extensions/bootstrap6/reference/attributestrait/index.md#method-addCssStyle).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$style` | `array<string, string>\|string` | CSS style, either an array such as `['color' => 'red', 'font-weight' => 'bold']` or a string such as `color: red; font-weight: bold;`. |
| `$overwrite` | `bool` | Whether to replace the value of properties already set; when `false`, their existing value is kept. |

**Returns**

`static`

**See also**

- <https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/style>

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

```php
abstract protected function togglerOptions(): mixed[]
```

Returns the HTML attributes of the toggler by reference, enabling the toggler when it is disabled.

**Returns**

`mixed[]`