Skip to content

Commit 94ee2ef

Browse files
authored
Merge pull request #692 from code16/allow-menu-open-new-tab
Allow opening external link to a new tab.
2 parents c91c70d + 25b66fe commit 94ee2ef

7 files changed

Lines changed: 25 additions & 3 deletions

File tree

docs/guide/building-menu.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ class MySharpMenu extends Code16\Sharp\Utils\Menu\SharpMenu
7272
}
7373
```
7474

75+
You can open the link in a new tab using the `openInNewTab` parameter:
76+
77+
```php
78+
class MySharpMenu extends Code16\Sharp\Utils\Menu\SharpMenu
79+
{
80+
public function build(): self
81+
{
82+
return $this->addExternalLink('https://google.com', 'Some external link', openInNewTab: true);
83+
}
84+
}
85+
```
86+
7587
### Define icons
7688

7789
Yon can specify a [blade-icons](https://blade-ui-kit.com/blade-icons) name for each link. It can be an icon set coming from a [package](https://github.com/blade-ui-kit/blade-icons?tab=readme-ov-file#icon-packages) or defined in the project config.

resources/js/Layouts/Layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
<DropdownMenuSeparator />
206206
</template>
207207
<template v-else>
208-
<DropdownMenuItem :as="item.isExternalLink ? 'a' : Link" :href="item.url">
208+
<DropdownMenuItem :as="item.isExternalLink ? 'a' : Link" :href="item.url" :target="item.openInNewTab ? '_blank' : '_self'">
209209
<template v-if="item.icon">
210210
<Icon class="size-4" :icon="item.icon" />
211211
</template>

resources/js/components/MenuItem.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<Icon :icon="item.icon" class="size-4" />
2727
</template>
2828
<span class="flex-1">
29-
<component :is="item.isExternalLink ? 'a' : Link" :href="item.url">
29+
<component :is="item.isExternalLink ? 'a' : Link" :href="item.url" :target="item.openInNewTab ? '_blank' : '_self'">
3030
<span class="absolute inset-0 z-1"></span>
3131
{{ item.label }}
3232
</component>

resources/js/types/generated.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ export type MenuItemData = {
746746
current: boolean;
747747
children: Array<MenuItemData> | null;
748748
isCollapsible: boolean;
749+
openInNewTab: boolean;
749750
};
750751
export type NotificationData = {
751752
title: string;

src/Data/MenuItemData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function __construct(
2828
/** @var MenuItemData[]|null */
2929
public ?array $children = null,
3030
public bool $isCollapsible = false,
31+
public bool $openInNewTab = false,
3132
) {}
3233

3334
public static function from(SharpMenuItem $item)
@@ -63,6 +64,7 @@ public static function from(SharpMenuItem $item)
6364
isExternalLink: $item->isExternalLink(),
6465
entityKey: $item->isEntity() ? $item->getEntityKey() : null,
6566
current: $item->isEntity() && $item->isCurrent(),
67+
openInNewTab: $item->isOpenInNewTab(),
6668
);
6769
}
6870

src/Utils/Menu/HasSharpMenuItems.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public function addExternalLink(
3636
?Closure $badge = null,
3737
?string $badgeTooltip = null,
3838
string|SharpLinkTo|null $badgeLink = null,
39+
bool $openInNewTab = false,
3940
): self {
40-
$this->items[] = (new SharpMenuItemLink($label, $icon, $badge, $badgeTooltip, $badgeLink))
41+
$this->items[] = (new SharpMenuItemLink($label, $icon, $badge, $badgeTooltip, $badgeLink, $openInNewTab))
4142
->setUrl($url);
4243

4344
return $this;

src/Utils/Menu/SharpMenuItemLink.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __construct(
2121
protected ?Closure $badge,
2222
protected ?string $badgeTooltip = null,
2323
protected string|SharpLinkTo|null $badgeLink = null,
24+
protected bool $openInNewTab = false,
2425
) {}
2526

2627
public function setEntity(string $entityKey): self
@@ -121,4 +122,9 @@ public function isCurrent(): bool
121122

122123
return $this->isEntity() && $rootEntityKey === $this->getEntityKey();
123124
}
125+
126+
public function isOpenInNewTab(): bool
127+
{
128+
return $this->openInNewTab;
129+
}
124130
}

0 commit comments

Comments
 (0)