Skip to content

Commit

Permalink
Merge pull request #226 from bskl/add-aria-method
Browse files Browse the repository at this point in the history
Add aria helper method
  • Loading branch information
freekmurze authored Apr 25, 2024
2 parents 211efc0 + 1d9a520 commit 35802bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/general-usage/element-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -103,6 +104,14 @@ echo Div::data('btn', 123);
// "<div data-btn="123"></div>"
```

## `aria()`

Add a aria- attribute:
```php
echo Div::aria('describedby', 'bar');
// "<div aria-describedby="bar"></div>"
```

## `child()` and `children()`

Adds one or more child elements to the element:
Expand Down
11 changes: 11 additions & 0 deletions src/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,9 @@ class Div extends BaseElement
'<div data-foo="bar"></div>',
Div::create()->data('foo', 'bar')->render()
);

it('can set a aria attribute')
->assertHtmlStringEqualsHtmlString(
'<div aria-describedby="bar"></div>',
Div::create()->aria('describedby', 'bar')->render()
);

0 comments on commit 35802bd

Please sign in to comment.