Skip to content

feat: add sidebar submenu with shadcn-vue components #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 21 additions & 1 deletion resources/js/components/AppSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ const mainNavItems: NavItem[] = [
href: '/dashboard',
icon: LayoutGrid,
},
// Example
// {
// title: 'User Management',
// href: '#',
// icon: Shield,
// children: [
// {
// title: 'Roles',
// href: '#',
// },
// {
// title: 'Permissions',
// href: '#',
// },
// {
// title: 'Users',
// href: '#',
// },
// ],
// },
];

const footerNavItems: NavItem[] = [
Expand All @@ -37,7 +57,7 @@ const footerNavItems: NavItem[] = [
<SidebarMenuItem>
<SidebarMenuButton size="lg" as-child>
<Link :href="route('dashboard')">
<AppLogo />
<AppLogo />
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
Expand Down
54 changes: 44 additions & 10 deletions resources/js/components/NavMain.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
<script setup lang="ts">
import { SidebarGroup, SidebarGroupLabel, SidebarMenu, SidebarMenuButton, SidebarMenuItem } from '@/components/ui/sidebar';
import { SidebarGroup, SidebarGroupLabel, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarMenuSub, SidebarMenuSubButton, SidebarMenuSubItem } from '@/components/ui/sidebar'
import { type NavItem, type SharedData } from '@/types';
import { Link, usePage } from '@inertiajs/vue3';
import { ChevronRight } from 'lucide-vue-next'
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from './ui/collapsible'

defineProps<{
items: NavItem[];
}>();

const page = usePage<SharedData>();

function isChildActive(item: { children: any[], href: string }) {
return item.children?.some(child => child.href === page.url) || item.href === page.url
}
</script>

<template>
<SidebarGroup class="px-2 py-0">
<SidebarGroupLabel>Platform</SidebarGroupLabel>
<SidebarMenu>
<SidebarMenuItem v-for="item in items" :key="item.title">
<SidebarMenuButton
as-child :is-active="item.href === page.url"
:tooltip="item.title"
>
<Link :href="item.href">
<template v-for="item in items" :key="item.title">
<!-- Regular items (no children) -->
<SidebarMenuItem v-if="!item.children">
<SidebarMenuButton as-child :is-active="item.href === page.url" :tooltip="item.title">
<Link :href="item.href">
<component :is="item.icon" />
<span>{{ item.title }}</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>

<!-- Collapsible items (with children) -->
<Collapsible v-else :default-open="isChildActive({ children: item.children || [], href: item.href })"
class="group/collapsible">
<SidebarMenuItem>
<CollapsibleTrigger as-child>
<SidebarMenuButton
:is-active="isChildActive({ children: item.children || [], href: item.href })"
:tooltip="item.title">
<component :is="item.icon" v-if="item.icon" />
<span>{{ item.title }}</span>
<ChevronRight
class="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" />
</SidebarMenuButton>
</CollapsibleTrigger>
<CollapsibleContent>
<SidebarMenuSub>
<SidebarMenuSubItem v-for="subItem in item.children" :key="subItem.title">
<SidebarMenuSubButton as-child :is-active="subItem.href === page.url">
<Link :href="subItem.href">
<span>{{ subItem.title }}</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
</SidebarMenuSub>
</CollapsibleContent>
</SidebarMenuItem>
</Collapsible>
</template>
</SidebarMenu>
</SidebarGroup>
</template>
1 change: 1 addition & 0 deletions resources/js/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface NavItem {
href: string;
icon?: LucideIcon;
isActive?: boolean;
children?: Array;
}

export interface SharedData extends PageProps {
Expand Down