diff --git a/docs/general-usage/element-methods.md b/docs/general-usage/element-methods.md index 3f3ca74..d6dd59c 100644 --- a/docs/general-usage/element-methods.md +++ b/docs/general-usage/element-methods.md @@ -14,6 +14,7 @@ All `Spatie\Html\Elements` have some methods that make working with elements eas - [`class()`](#class) - [`id()`](#id) - [`data()`](#data) +- [`aria()`](#aria) - [`child()` and `children()`](#child-and-children) - [`prependChild()` and `prependChildren()`](#prependchild-and-prependchildren) - [`text()`](#text) @@ -103,6 +104,14 @@ echo Div::data('btn', 123); // "
" ``` +## `aria()` + +Add a aria- attribute: +```php +echo Div::aria('describedby', 'bar'); +// "" +``` + ## `child()` and `children()` Adds one or more child elements to the element: diff --git a/src/BaseElement.php b/src/BaseElement.php index b50163e..d10d0d5 100644 --- a/src/BaseElement.php +++ b/src/BaseElement.php @@ -169,6 +169,17 @@ public function data($name, $value = null) return $this->attribute("data-{$name}", $value); } + /** + * @param string $attribute + * @param string|null $value + * + * @return static + */ + public function aria($attribute, $value = null) + { + return $this->attribute("aria-{$attribute}", $value); + } + /** * @param \Spatie\Html\HtmlElement|string|iterable|int|float|null $children * @param callable|null $mapper diff --git a/tests/BaseElementTest.php b/tests/BaseElementTest.php index e23e060..9482fc8 100644 --- a/tests/BaseElementTest.php +++ b/tests/BaseElementTest.php @@ -303,3 +303,9 @@ class Div extends BaseElement '', Div::create()->data('foo', 'bar')->render() ); + +it('can set a aria attribute') + ->assertHtmlStringEqualsHtmlString( + '', + Div::create()->aria('describedby', 'bar')->render() + );