Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/guide/building-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ class MySharpMenu extends Code16\Sharp\Utils\Menu\SharpMenu
}
```

You can open the link in a new tab using the `openInNewTab` parameter:

```php
class MySharpMenu extends Code16\Sharp\Utils\Menu\SharpMenu
{
public function build(): self
{
return $this->addExternalLink('https://google.com', 'Some external link', openInNewTab: true);
}
}
```

### Define icons

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.
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Layouts/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
<DropdownMenuSeparator />
</template>
<template v-else>
<DropdownMenuItem :as="item.isExternalLink ? 'a' : Link" :href="item.url">
<DropdownMenuItem :as="item.isExternalLink ? 'a' : Link" :href="item.url" :target="item.openInNewTab ? '_blank' : '_self'">
<template v-if="item.icon">
<Icon class="size-4" :icon="item.icon" />
</template>
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/MenuItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<Icon :icon="item.icon" class="size-4" />
</template>
<span class="flex-1">
<component :is="item.isExternalLink ? 'a' : Link" :href="item.url">
<component :is="item.isExternalLink ? 'a' : Link" :href="item.url" :target="item.openInNewTab ? '_blank' : '_self'">
<span class="absolute inset-0 z-1"></span>
{{ item.label }}
</component>
Expand Down
1 change: 1 addition & 0 deletions resources/js/types/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ export type MenuItemData = {
current: boolean;
children: Array<MenuItemData> | null;
isCollapsible: boolean;
openInNewTab: boolean;
};
export type NotificationData = {
title: string;
Expand Down
2 changes: 2 additions & 0 deletions src/Data/MenuItemData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
/** @var MenuItemData[]|null */
public ?array $children = null,
public bool $isCollapsible = false,
public bool $openInNewTab = false,
) {}

public static function from(SharpMenuItem $item)
Expand Down Expand Up @@ -63,6 +64,7 @@ public static function from(SharpMenuItem $item)
isExternalLink: $item->isExternalLink(),
entityKey: $item->isEntity() ? $item->getEntityKey() : null,
current: $item->isEntity() && $item->isCurrent(),
openInNewTab: $item->isOpenInNewTab(),
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Utils/Menu/HasSharpMenuItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public function addExternalLink(
?Closure $badge = null,
?string $badgeTooltip = null,
string|SharpLinkTo|null $badgeLink = null,
bool $openInNewTab = false,
): self {
$this->items[] = (new SharpMenuItemLink($label, $icon, $badge, $badgeTooltip, $badgeLink))
$this->items[] = (new SharpMenuItemLink($label, $icon, $badge, $badgeTooltip, $badgeLink, $openInNewTab))
->setUrl($url);

return $this;
Expand Down
6 changes: 6 additions & 0 deletions src/Utils/Menu/SharpMenuItemLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
protected ?Closure $badge,
protected ?string $badgeTooltip = null,
protected string|SharpLinkTo|null $badgeLink = null,
protected bool $openInNewTab = false,
) {}

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

return $this->isEntity() && $rootEntityKey === $this->getEntityKey();
}

public function isOpenInNewTab(): bool
{
return $this->openInNewTab;
}
}
Loading