내비게이션 메뉴 (Navigation Menu)
웹사이트를 탐색하기 위한 링크들의 모음이에요. 상위 내비게이션 바에서 메뉴를 펼쳐 하위 링크를 보여 주는 용도로 많이 쓰여요.
출처: 문서
본문
<script lang="ts">
import CircleAlertIcon from "@lucide/svelte/icons/circle-alert";
import CircleCheckIcon from "@lucide/svelte/icons/circle-check";
import CircleDashedIcon from "@lucide/svelte/icons/circle-dashed";
import * as NavigationMenu from "$lib/components/ui/navigation-menu/index.js";
import { navigationMenuTriggerStyle } from "$lib/components/ui/navigation-menu/navigation-menu-trigger.svelte";
const components: { title: string; href: string; description: string }[] = [
{
title: "Alert Dialog",
href: "/docs/components/alert-dialog",
description:
"A modal dialog that interrupts the user with important content and expects a response."
},
{
title: "Hover Card",
href: "/docs/components/hover-card",
description:
"For sighted users to preview content available behind a link."
},
{
title: "Progress",
href: "/docs/components/progress",
description:
"Displays an indicator showing the completion progress of a task, typically displayed as a progress bar."
},
{
title: "Scroll-area",
href: "/docs/components/scroll-area",
description: "Visually or semantically separates content."
},
{
title: "Tabs",
href: "/docs/components/tabs",
description:
"A set of layered sections of contentknown as tab panelsthat are displayed one at a time."
},
{
title: "Tooltip",
href: "/docs/components/tooltip",
description:
"A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it."
}
];
</script>
{#snippet ListItem({
title,
content,
href
}: {
title: string;
content: string;
href: string;
})}
<li>
<NavigationMenu.Link {href}>
<div class="flex flex-col gap-1 text-sm">
<div class="leading-none font-medium">{title}</div>
<div class="line-clamp-2 text-muted-foreground">{content}</div>
</div>
</NavigationMenu.Link>
</li>
{/snippet}
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Getting started</NavigationMenu.Trigger>
<NavigationMenu.Content>
<ul class="w-96">
{@render ListItem({
href: "/docs",
title: "Introduction",
content: "Re-usable components built with Tailwind CSS."
})}
{@render ListItem({
href: "/docs/installation",
title: "Installation",
content: "How to install dependencies and structure your app."
})}
{@render ListItem({
href: "/docs/components/typography",
title: "Typography",
content: "Styles for headings, paragraphs, lists...etc"
})}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item class="hidden md:flex">
<NavigationMenu.Trigger>Components</NavigationMenu.Trigger>
<NavigationMenu.Content>
<ul
class="grid w-[400px] gap-2 md:w-[500px] md:grid-cols-2 lg:w-[600px]"
>
{#each components as component (component.title)}
{@render ListItem({
href: component.href,
title: component.title,
content: component.description
})}
{/each}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger>With Icon</NavigationMenu.Trigger>
<NavigationMenu.Content>
<ul class="grid w-[200px]">
<li>
<NavigationMenu.Link href="##" class="flex-row items-center gap-2">
<CircleAlertIcon />
Backlog
</NavigationMenu.Link>
<NavigationMenu.Link href="##" class="flex-row items-center gap-2">
<CircleDashedIcon />
To Do
</NavigationMenu.Link>
<NavigationMenu.Link href="##" class="flex-row items-center gap-2">
<CircleCheckIcon />
Done
</NavigationMenu.Link>
</li>
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Link href="/docs" class={navigationMenuTriggerStyle()}>
Docs
</NavigationMenu.Link>
</NavigationMenu.Item>
</NavigationMenu.List>
</NavigationMenu.Root>
View Code
설치
pnpm dlx shadcn-svelte@latest add navigation-menu
npx shadcn-svelte@latest add navigation-menu
bun x shadcn-svelte@latest add navigation-menu
사용법
<script lang="ts">
import * as NavigationMenu from "$lib/components/ui/navigation-menu/index.js";
</script>
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item One</NavigationMenu.Trigger>
<NavigationMenu.Content>
<NavigationMenu.Link>Link</NavigationMenu.Link>
</NavigationMenu.Content>
</NavigationMenu.Item>
</NavigationMenu.List>
</NavigationMenu.Root>
링크 컴포넌트
NavigationMenu.Link에 href prop을 사용하면 링크를 렌더링해요. navigationMenuTriggerStyle()을 적용하면 트리거처럼 스타일링할 수 있어요.
<script lang="ts">
import * as NavigationMenu from "$lib/components/ui/navigation-menu/index.js";
import { navigationMenuTriggerStyle } from "$lib/components/ui/navigation-menu/navigation-menu-trigger.svelte";
</script>
<NavigationMenu.Item>
<NavigationMenu.Link href="/docs" class={navigationMenuTriggerStyle()}>
Documentation
</NavigationMenu.Link>
</NavigationMenu.Item>
더 알아보기 (Learn more)
원문 문서에서는 bits-ui의 Navigation Menu 문서와 API 레퍼런스 링크를 통해 컴포넌트의 동작 원리와 전체 prop 목록을 더 자세히 살펴볼 수 있어요. 설치 방법이나 다른 컴포넌트와의 조합이 궁금하면 shadcn-svelte 공식 문서를 참고해 보세요.