# HasData

> Provides an immutable API for data-* attributes.

[API reference](/docs/api/index.md) › [HTML Attribute](/docs/extensions/html-attribute/reference/index.md) › [Global](/docs/extensions/html-attribute/reference/global/index.md) › HasData

| trait | `UIAwesome\Html\Attribute\Global\HasData` |
| --- | --- |
| Source | [`src/Global/HasData.php`](https://github.com/ui-awesome/html-attribute/blob/main/src/Global/HasData.php#L20) |

Used by 4 types:

- [`UIAwesome\Html\Core\Element\BaseBlock`](/docs/extensions/html-core/reference/element/baseblock/index.md)
- [`UIAwesome\Html\Core\Element\BaseInline`](/docs/extensions/html-core/reference/element/baseinline/index.md)
- [`UIAwesome\Html\Core\Element\BaseInput`](/docs/extensions/html-core/reference/element/baseinput/index.md)
- [`UIAwesome\Html\Core\Element\BaseVoid`](/docs/extensions/html-core/reference/element/basevoid/index.md)

Provides an immutable API for `data-*` attributes.

See also: <https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*>.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`addDataAttribute()`](#method-addDataAttribute) | Sets a `data-*` attribute. | `HasData` |
| [`dataAttributes()`](#method-dataAttributes) | Sets multiple `data-*` attributes. | `HasData` |
| [`removeDataAttribute()`](#method-removeDataAttribute) | Removes a `data-*` attribute. | `HasData` |

## Method details

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

```php
public function addDataAttribute(string|UnitEnum $key, bool|Closure(): mixed|float|int|string|Stringable|UnitEnum|null $value): static
```

Sets a `data-*` attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Data attribute key without the `data-` prefix. |
| `$value` | `bool\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`(): mixed\|float\|int\|string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php)`\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php)`\|null` | Data attribute value, or `null` to remove the attribute. |

**Returns**

`static`: New instance with the updated `data-*` attribute.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if one or more arguments are invalid, of incorrect type or format.

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

```php
public function dataAttributes(mixed[] $values): static
```

Sets multiple `data-*` attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$values` | `mixed[]` | Associative array of data keys and values. Values may be scalar, Stringable, UnitEnum, Closure, or `null` to remove the attribute. |

**Returns**

`static`: New instance with the updated `data-*` attributes.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if one or more arguments are invalid, of incorrect type or format.

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

```php
public function removeDataAttribute(string|UnitEnum $key): static
```

Removes a `data-*` attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Data attribute key without the `data-` prefix. |

**Returns**

`static`: New instance with the specified `data-*` attribute removed.