Skip to content

Commit 98b0144

Browse files
milwad-devfreekmurzesebastiandedeyne
authored
Add id attribute for menu (#143)
* add `addId` function * add ids into toArray method * add `addId` into README * add `addId` function into HasHtmlAttributes * Update README.md * add ids into mergeWith * add parent id * Fix styling * Update README.md * Change signatures of id in src/Html/Attributes.php Co-authored-by: Sebastian De Deyne <sebastiandedeyne@gmail.com> * Change signatures of id in src/Html/Attributes.php Co-authored-by: Sebastian De Deyne <sebastiandedeyne@gmail.com> * Change signatures of id in src/Traits/HasHtmlAttributes.php Co-authored-by: Sebastian De Deyne <sebastiandedeyne@gmail.com> * Change signatures of id in src/Traits/HasParentAttributes.php Co-authored-by: Sebastian De Deyne <sebastiandedeyne@gmail.com> * change ids to id in Attributes * change addId to id * add `id` method into HasHtmlAttributes and HasParentAttributes * Update README.md --------- Co-authored-by: milwad-dev <milwad-dev@users.noreply.github.com> Co-authored-by: Freek Van der Herten <freek@spatie.be> Co-authored-by: Sebastian De Deyne <sebastiandedeyne@gmail.com>
1 parent 1ba9281 commit 98b0144

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ Menu::new()
9595
</div
9696
```
9797

98+
## Adding id to elements
99+
100+
You can add id, so you can easily target some of these elements with CSS or JS.
101+
102+
```php
103+
Menu::new()
104+
->id('navigation')
105+
->add(Link::to('/', 'Home')->id('home-link'))
106+
->add(Link::to('/about', 'About'))
107+
->add(Link::to('/contact', 'Contact'))
108+
```
109+
110+
```html
111+
<ul id="navigation">
112+
<li><a href="/" id="home-link">Home</a></li>
113+
<li><a href="/about">About</a></li>
114+
<li><a href="/contact">Contact</a></li>
115+
</ul>
116+
```
117+
98118
## Not Afraid of Depths
99119

100120
The menu supports submenus, which in turn can be nested infinitely.

src/HasHtmlAttributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public function setAttribute(string $attribute, string $value = ''): static;
99
public function setAttributes(array $attributes): static;
1010

1111
public function addClass(string $class): static;
12+
13+
public function id(?string $id): static;
1214
}

src/HasParentAttributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ public function setParentAttribute(string $attribute, string $value = ''): stati
1717
public function setParentAttributes(array $attributes): static;
1818

1919
public function addParentClass(string $class): static;
20+
21+
public function parentId(?string $id): static;
2022
}

src/Html/Attributes.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Attributes
88

99
protected array $classes = [];
1010

11+
protected ?string $id = null;
12+
1113
public function __construct(array $attributes = [])
1214
{
1315
$this->setAttributes($attributes);
@@ -59,10 +61,18 @@ public function addClass(string | array $class): self
5961
return $this;
6062
}
6163

64+
public function id(?string $id): self
65+
{
66+
$this->id = $id;
67+
68+
return $this;
69+
}
70+
6271
public function mergeWith(self $attributes): self
6372
{
6473
$this->attributes = array_merge($this->attributes, $attributes->attributes);
6574
$this->classes = array_merge($this->classes, $attributes->classes);
75+
$this->id = $this->id ?: $attributes->id;
6676

6777
return $this;
6878
}
@@ -74,11 +84,14 @@ public function isEmpty(): bool
7484

7585
public function toArray(): array
7686
{
77-
if (empty($this->classes)) {
87+
if (empty($this->classes) || empty($this->id)) {
7888
return $this->attributes;
7989
}
8090

81-
return array_merge($this->attributes, ['class' => implode(' ', $this->classes)]);
91+
return array_merge($this->attributes, [
92+
'class' => implode(' ', $this->classes),
93+
'id' => implode(' ', $this->id),
94+
]);
8295
}
8396

8497
public function toString(): string

src/Traits/HasHtmlAttributes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public function addClass(string $class): static
2929

3030
return $this;
3131
}
32+
33+
public function id(?string $id): static
34+
{
35+
$this->htmlAttributes->id($id);
36+
37+
return $this;
38+
}
3239
}

src/Traits/HasParentAttributes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ public function addParentClass(string $class): static
4040

4141
return $this;
4242
}
43+
44+
public function parentId(?string $id): static
45+
{
46+
$this->parentAttributes->id($id);
47+
48+
return $this;
49+
}
4350
}

0 commit comments

Comments
 (0)