Skip to content

Commit 0538347

Browse files
committed
- Added target to menu items
1 parent 65a8973 commit 0538347

11 files changed

Lines changed: 63 additions & 4 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class AdminSidebar extends Menu implements IsMenu
5858
return $request->query('foo') == 'bars';
5959
})
6060
->badge(__('New'), 'text-bg-primary'),
61+
62+
MenuItem::make(__('External Link'))
63+
->url('https://google.com')
64+
->target('_blank'),
6165
];
6266
}
6367
}
@@ -91,6 +95,7 @@ You may use the following methods to further configure the menu item:
9195
- `children` - sets the children of the menu item. Note that the default views support only 2 levels of items. If you want more levels or infinite levels, you can supply your own view when rendering the menu.
9296
- `hideIfNoChildrenVisible` - hides the menu item if it has children but doesn't have any visible children. By default, this option is active for items with blank links.
9397
- `dontHideIfNoChildrenVisible` - don't hide the menu item even if it has no visible children
98+
- `target` - sets the `href` target of the menu item
9499

95100
### Displaying a Menu
96101

@@ -164,6 +169,8 @@ $item->getCssClass() // returns the custom css class, if any set for the item
164169
$item->getAggregatedCount($user) // get the count of the item + the count of all visible child items, will return 0 if the current user can't see the count
165170
$item->getVisibleCount($user) // get the count of the item, will return 0 if the current user can't see the count
166171
$item->getVisibleChildren($user) // returns an array of all visible child items
172+
$item->hasTarget() // checks if the item has a href target value defined
173+
$item->getTarget() // returns the item's href target attribute value
167174
```
168175

169176
If you want to customize the default views, you can publish the package views and customize them. To publish the view files to `resources/views/vendor/menu-builder`, run:

resources/views/bootstrap-5/child-menu-item.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<li class="nav-item{{ $item->hasCssClass() ? ' ' . $item->getCssClass() : '' }}">
22
<a href="{{ $item->getLink() }}"
3+
@if($item->hasTarget())
4+
target="{{ $item->getTarget() }}"
5+
@endif
36
@class([
47
'nav-link d-flex justify-content-between align-items-center',
58
'active' => $item->isActive()

resources/views/bootstrap-5/root-menu-item.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<li class="nav-item{{ $item->hasCssClass() ? ' ' . $item->getCssClass() : '' }}">
22
<a href="{{ $item->getLink() }}"
3+
@if($item->hasTarget())
4+
target="{{ $item->getTarget() }}"
5+
@endif
36
@class([
47
'nav-link d-flex justify-content-between align-items-center',
58
'active' => $item->isActive() || $item->hasActiveChild($user)

resources/views/material-admin-26/child-menu-item.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
])
1111
@endif
1212
>
13-
<a href="{{ $item->getLink() }}" class="d-flex justify-content-between align-items-center">
13+
<a href="{{ $item->getLink() }}"
14+
@if($item->hasTarget())
15+
target="{{ $item->getTarget() }}"
16+
@endif
17+
class="d-flex justify-content-between align-items-center">
1418
<span>{{ $item->getLabel() }}</span>
1519
@if($count = $item->getVisibleCount($user))
1620
<span class="badge badge-pill bg-primary align-self-start">{{ $count }}</span>

resources/views/material-admin-26/root-menu-item.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
])
1515
@endif
1616
>
17-
<a href="{{ $item->getLink() }}">
17+
<a href="{{ $item->getLink() }}"
18+
@if($item->hasTarget())
19+
target="{{ $item->getTarget() }}"
20+
@endif
21+
>
1822
@if($item->hasIcon())<i class="{{ $item->getIcon($icon_prefix) }}"></i> @endif{{ $item->getLabel() }}
1923
@if($count = $item->getAggregatedCount($user))
2024
<span class="ml-2 badge badge-pill">{{ $count }}</span>

src/Menu/MenuItem.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Javaabu\MenuBuilder\Traits\HasPermission;
1717
use Javaabu\MenuBuilder\Traits\HasRoute;
1818
use Javaabu\MenuBuilder\Traits\HasRoutePatterns;
19+
use Javaabu\MenuBuilder\Traits\HasTarget;
1920
use Javaabu\MenuBuilder\Traits\HasUrl;
2021

2122
class MenuItem
@@ -31,6 +32,7 @@ class MenuItem
3132
use HasCount;
3233
use HasBadge;
3334
use HasCssClass;
35+
use HasTarget;
3436
use CanHaveChildren;
3537
use Macroable;
3638

src/Traits/HasTarget.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Javaabu\MenuBuilder\Traits;
4+
5+
use Closure;
6+
7+
trait HasTarget
8+
{
9+
protected Closure | string | null $target = null;
10+
11+
public function target(Closure | string | null $badge): self
12+
{
13+
$this->target = $badge;
14+
15+
return $this;
16+
}
17+
18+
public function getTarget(): ?string
19+
{
20+
return $this->evaluate($this->target);
21+
}
22+
23+
public function hasTarget(): bool
24+
{
25+
return ! empty($this->getTarget());
26+
}
27+
}

tests/Menus/Sidebar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function menuItems(): array
4545

4646
MenuItem::make('Returns')
4747
->url('http://localhost/returns')
48+
->target('_blank')
4849
->icon('assignment-return')
4950
->badge(__('Error'), 'text-bg-danger'),
5051
];

tests/Unit/MenuItemTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public function it_can_set_the_menu_item_class(): void
3232
$this->assertEquals('new', $menu_item->getCssClass());
3333
}
3434

35+
/** @test */
36+
public function it_can_set_the_menu_item_target(): void
37+
{
38+
$menu_item = MenuItem::make('Dashboard')->target('_blank');
39+
40+
$this->assertEquals('_blank', $menu_item->getTarget());
41+
}
42+
3543
/** @test */
3644
public function it_can_set_the_menu_item_badge(): void
3745
{

tests/stubs/bootstrap-5.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</a>
3636
</li>
3737
<li class="nav-item">
38-
<a href="http://localhost/returns" class="nav-link d-flex justify-content-between align-items-center">
38+
<a href="http://localhost/returns" target="_blank" class="nav-link d-flex justify-content-between align-items-center">
3939
<span><i class="zmdi zmdi-assignment-return"></i> Returns</span>
4040
<span class="ms-2 badge text-bg-danger rounded-pill">Error</span>
4141
</a>

0 commit comments

Comments
 (0)