Navigation Menu 문서
Navigation Menu는 웹사이트의 페이지를 오가며 탐색할 수 있게 해 주는 메뉴예요. 메뉴 바를 만들 때 필요한 트리거, 콘텐츠, 뷰포트, 인디케이터를 모두 조합할 수 있어서, 다단계 메뉴와 고급 애니메이션까지 손쉽게 구현할 수 있어요.
출처: 문서
본문
아래 예시는 Bits UI의 대표 메뉴 바를 보여 줘요. NavigationMenu.Root가 전체를 관리하고, List 안에 Item과 Trigger, Content를 배치하고, Viewport와 Indicator로 콘텐츠 표시를 애니메이션해요.
<script lang="ts">
import { NavigationMenu } from "bits-ui";
import CaretDown from "phosphor-svelte/lib/CaretDown";
import cn from "clsx";
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: "Link Preview",
href: "/docs/components/link-preview",
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 content—known as tab panels—that 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."
}
];
type ListItemProps = {
className?: string;
title: string;
href: string;
content: string;
};
</script>
{#snippet ListItem({ className, title, content, href }: ListItemProps)}
<li>
<NavigationMenu.Link
class={cn(
"hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground outline-hidden block select-none space-y-1 rounded-md p-3 leading-none no-underline transition-colors",
className
)}
{href}
>
<div class="text-sm font-medium leading-none">{title}</div>
<p class="text-muted-foreground line-clamp-2 text-sm leading-snug">
{content}
</p>
</NavigationMenu.Link>
</li>
{/snippet}
<NavigationMenu.Root class="relative z-10 flex w-full justify-center">
<NavigationMenu.List
class="group flex list-none items-center justify-center p-1"
>
<NavigationMenu.Item value="getting-started">
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus-visible:bg-muted focus-visible:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus-visible:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Getting started
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left absolute left-0 top-0 w-full sm:w-auto"
>
<ul
class="m-0 grid list-none gap-x-2.5 p-3 sm:w-[600px] sm:grid-flow-col sm:grid-rows-3 sm:p-[22px]"
>
<li class="row-span-3 mb-2 sm:mb-0">
<NavigationMenu.Link
href="/"
class="from-muted/50 to-muted bg-linear-to-b outline-hidden flex h-full w-full select-none flex-col justify-end rounded-md p-6 no-underline focus:shadow-md"
>
<!-- <Icons.logo class="h-6 w-6" /> -->
<div class="mb-2 mt-4 text-lg font-medium">Bits UI</div>
<p class="text-muted-foreground text-sm leading-tight">
The headless components for Svelte.
</p>
</NavigationMenu.Link>
</li>
{@render ListItem({
href: "/docs",
title: "Introduction",
content: "Headless components for Svelte and SvelteKit"
})}
{@render ListItem({
href: "/docs/getting-started",
title: "Getting Started",
content: "How to install and use Bits UI"
})}
{@render ListItem({
href: "/docs/styling",
title: "Styling",
content: "How to style Bits UI components"
})}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus-visible:bg-muted focus-visible:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus-visible:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Components
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left absolute left-0 top-0 w-full sm:w-auto"
>
<ul
class="grid gap-3 p-3 sm:w-[400px] sm:p-6 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.Link
class="hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
href="/docs"
>
<span class="hidden sm:inline"> Documentation </span>
<span class="inline sm:hidden"> Docs </span>
</NavigationMenu.Link>
</NavigationMenu.Item>
<NavigationMenu.Indicator
class="data-[state=hidden]:animate-fade-out data-[state=visible]:animate-fade-in top-full z-10 flex h-2.5 items-end justify-center overflow-hidden opacity-100 transition-[all,transform_250ms_ease] duration-200 data-[state=hidden]:opacity-0"
>
<div
class="bg-border relative top-[70%] size-2.5 rotate-[45deg] rounded-tl-[2px]"
></div>
</NavigationMenu.Indicator>
</NavigationMenu.List>
<div
class="perspective-[2000px] absolute left-0 top-full flex w-full justify-center"
>
<NavigationMenu.Viewport
class="text-popover-foreground bg-background data-[state=closed]:animate-scale-out data-[state=open]:animate-scale-in relative mt-2.5 h-[var(--bits-navigation-menu-viewport-height)] w-full origin-[top_center] overflow-hidden rounded-md border shadow-lg transition-[width,_height] duration-200 sm:w-[var(--bits-navigation-menu-viewport-width)] "
/>
</div>
</NavigationMenu.Root>
구조 (Structure)
<script lang="ts">
import { NavigationMenu } from "bits-ui";
</script>
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger />
<NavigationMenu.Content />
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger />
<NavigationMenu.Content>
<NavigationMenu.Link />
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Link />
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger />
<NavigationMenu.Content>
<NavigationMenu.Sub>
<NavigationMenu.List />
<NavigationMenu.Viewport />
</NavigationMenu.Sub>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Indicator />
</NavigationMenu.List>
<NavigationMenu.Viewport />
</NavigationMenu.Root>
사용법 (Usage)
세로형 (Vertical)
orientation prop을 사용하면 세로형 메뉴를 만들 수 있어요.
<NavigationMenu.Root orientation="vertical">
<!-- ... -->
</NavigationMenu.Root>
유연한 레이아웃 (Flexible Layouts)
Content가 렌더링될 위치를 더 세밀하게 제어하고 싶다면 Viewport 컴포넌트를 사용해요. DOM 구조를 조정해야 하는 디자인이거나 고급 애니메이션을 구현할 유연성이 필요할 때 특히 유용해요. 탭 포커스는 자동으로 관리돼요.
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item one</NavigationMenu.Trigger>
<NavigationMenu.Content>Item one content</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item two</NavigationMenu.Trigger>
<NavigationMenu.Content>Item two content</NavigationMenu.Content>
</NavigationMenu.Item>
</NavigationMenu.List>
<!-- NavigationMenu.Content will be rendered here when active -->
<NavigationMenu.Viewport />
</NavigationMenu.Root>
인디케이터와 함께 (With Indicator)
선택적 Indicator 컴포넌트로 현재 활성화된 Trigger를 강조할 수 있어요. Viewport와 함께 화살표나 하이라이트 같은 애니메이션 시각 신호를 보여 주고 싶을 때 유용하죠.
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item one</NavigationMenu.Trigger>
<NavigationMenu.Content>Item one content</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item two</NavigationMenu.Trigger>
<NavigationMenu.Content>Item two content</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Indicator />
</NavigationMenu.List>
<NavigationMenu.Viewport />
</NavigationMenu.Root>
하위 메뉴 (Submenus)
내비게이션 메뉴를 중첩하고 NavigationMenu.Root 대신 Navigation.Sub 컴포넌트를 사용하면 하위 메뉴를 만들 수 있어요.
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item one</NavigationMenu.Trigger>
<NavigationMenu.Content>
<NavigationMenu.Sub>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Subitem one</NavigationMenu.Trigger>
<NavigationMenu.Content
>Subitem one content</NavigationMenu.Content
>
</NavigationMenu.Item>
</NavigationMenu.List>
</NavigationMenu.Sub>
</NavigationMenu.Content>
</NavigationMenu.Item>
</NavigationMenu.List>
</NavigationMenu.Root>
뷰포트가 있는 하위 메뉴 (Submenus with Viewport)
NavigationMenu.Sub 안에 NavigationMenu.Viewport 컴포넌트를 넣으면 그 하위 메뉴 전용 뷰포트를 만들 수 있어요.
<NavigationMenu.Sub>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item one</NavigationMenu.Trigger>
<NavigationMenu.Content>
<NavigationMenu.Link>Item one content</NavigationMenu.Link>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item two</NavigationMenu.Trigger>
<NavigationMenu.Content>
<NavigationMenu.Link>Item two content</NavigationMenu.Link>
</NavigationMenu.Content>
</NavigationMenu.Item>
</NavigationMenu.List>
<NavigationMenu.Viewport />
</NavigationMenu.Sub>
뷰포트 없이 (No Viewport)
NavigationMenu.Viewport 컴포넌트는 NavigationMenu.Content 사이를 완전한 닫힘/열림 애니메이션 없이 전환할 수 있게 해 줘요. 다만 이건 완전히 선택 사항이라 꼭 써야 하는 건 아니에요.
코드 펼치기 (Expand Code)
<script lang="ts">
import { NavigationMenu } from "bits-ui";
import CaretDown from "phosphor-svelte/lib/CaretDown";
import cn from "clsx";
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: "Link Preview",
href: "/docs/components/link-preview",
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 content—known as tab panels—that 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."
}
];
type ListItemProps = {
className?: string;
title: string;
href: string;
content: string;
};
</script>
{#snippet ListItem({ className, title, content, href }: ListItemProps)}
<li>
<NavigationMenu.Link
class={cn(
"hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground outline-hidden block select-none space-y-1 rounded-md p-3 leading-none no-underline transition-colors",
className
)}
{href}
>
<div class="text-sm font-medium leading-none">{title}</div>
<p class="text-muted-foreground line-clamp-2 text-sm leading-snug">
{content}
</p>
</NavigationMenu.Link>
</li>
{/snippet}
<NavigationMenu.Root class="relative z-10 flex w-full justify-center">
<NavigationMenu.List
class="group flex list-none items-center justify-center p-1"
>
<NavigationMenu.Item value="getting-started">
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Getting started
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 bg-background absolute left-0 top-full mt-2 w-full rounded-md border shadow-lg sm:w-auto"
>
<ul
class="m-0 grid list-none gap-x-2.5 p-3 sm:w-[600px] sm:grid-flow-col sm:grid-rows-3 sm:p-[22px]"
>
<li class="row-span-3 mb-2 sm:mb-0">
<NavigationMenu.Link
href="/"
class="from-muted/50 to-muted bg-linear-to-b outline-hidden flex h-full w-full select-none flex-col justify-end rounded-md p-6 no-underline focus:shadow-md"
>
<!-- <Icons.logo class="h-6 w-6" /> -->
<div class="mb-2 mt-4 text-lg font-medium">Bits UI</div>
<p class="text-muted-foreground text-sm leading-tight">
The headless components for Svelte.
</p>
</NavigationMenu.Link>
</li>
{@render ListItem({
href: "/docs",
title: "Introduction",
content: "Headless components for Svelte and SvelteKit"
})}
{@render ListItem({
href: "/docs/getting-started",
title: "Getting Started",
content: "How to install and use Bits UI"
})}
{@render ListItem({
href: "/docs/styling",
title: "Styling",
content: "How to style Bits UI components"
})}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Components
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 bg-background absolute left-0 top-full mt-2 w-full rounded-md border shadow-lg sm:w-auto"
>
<ul
class="grid gap-3 p-3 sm:w-[400px] sm:p-6 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.Link
class="hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
href="/docs"
>
<span class="hidden sm:inline"> Documentation </span>
<span class="inline sm:hidden"> Docs </span>
</NavigationMenu.Link>
</NavigationMenu.Item>
</NavigationMenu.List>
</NavigationMenu.Root>
고급 애니메이션 (Advanced Animation)
진입/퇴장 방향에 따라 NavigationMenu.Viewport 크기와 NavigationMenu.Content 위치를 애니메이션할 수 있도록 --bits-navigation-menu-viewport-[width|height]와 data-motion['from-start'|'to-start'|'from-end'|'to-end']를 노출해요.
이 값들을 position: absolute;와 함께 쓰면 항목 사이를 이동할 때 부드럽게 겹치며 전환되는 애니메이션 효과를 만들 수 있어요.
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item one</NavigationMenu.Trigger>
<NavigationMenu.Content class="NavigationMenuContent">
Item one content
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger>Item two</NavigationMenu.Trigger>
<NavigationMenu.Content class="NavigationMenuContent">
Item two content
</NavigationMenu.Content>
</NavigationMenu.Item>
</NavigationMenu.List>
<NavigationMenu.Viewport class="NavigationMenuViewport" />
</NavigationMenu.Root>
/* app.css */
.NavigationMenuContent {
position: absolute;
top: 0;
left: 0;
animation-duration: 250ms;
animation-timing-function: ease;
}
.NavigationMenuContent[data-motion="from-start"] {
animation-name: enter-from-left;
}
.NavigationMenuContent[data-motion="from-end"] {
animation-name: enter-from-right;
}
.NavigationMenuContent[data-motion="to-start"] {
animation-name: exit-to-left;
}
.NavigationMenuContent[data-motion="to-end"] {
animation-name: exit-to-right;
}
.NavigationMenuViewport {
position: relative;
width: var(--bits-navigation-menu-viewport-width);
height: var(--bits-navigation-menu-viewport-height);
transition:
width,
height,
250ms ease;
}
@keyframes enter-from-right {
from {
opacity: 0;
transform: translateX(200px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes enter-from-left {
from {
opacity: 0;
transform: translateX(-200px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes exit-to-right {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(200px);
}
}
@keyframes exit-to-left {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(-200px);
}
}
강제 마운트 (Force Mounting)
메뉴가 열려 있든 닫혀 있든 내비게이션 메뉴의 링크가 DOM에 계속 남아 있길 원할 수 있어요. SEO 목적으로 특히 유용하죠. NavigationMenu.Content와 NavigationMenu.Viewport 컴포넌트에 forceMount prop을 사용하면 이렇게 만들 수 있어요.
경고 (Warning)
참고: forceMount를 사용하면 NavigationMenu.Content와 NavigationMenu.Viewport 컴포넌트의 data-state 속성으로 요소의 표시 여부를 직접 관리해야 해요.
<NavigationMenu.Content forceMount></NavigationMenu.Content>
<NavigationMenu.Viewport forceMount></NavigationMenu.Viewport>
코드 펼치기 (Expand Code)
<script lang="ts">
import { NavigationMenu } from "bits-ui";
import CaretDown from "phosphor-svelte/lib/CaretDown";
import cn from "clsx";
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: "Link Preview",
href: "/docs/components/link-preview",
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 content—known as tab panels—that 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."
}
];
type ListItemProps = {
className?: string;
title: string;
href: string;
content: string;
};
</script>
{#snippet ListItem({ className, title, content, href }: ListItemProps)}
<li>
<NavigationMenu.Link
class={cn(
"hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground outline-hidden block select-none space-y-1 rounded-md p-3 leading-none no-underline transition-colors",
className
)}
{href}
>
<div class="text-sm font-medium leading-none">{title}</div>
<p class="text-muted-foreground line-clamp-2 text-sm leading-snug">
{content}
</p>
</NavigationMenu.Link>
</li>
{/snippet}
<NavigationMenu.Root class="relative z-10 flex w-full justify-center">
<NavigationMenu.List
class="group flex list-none items-center justify-center p-1"
>
<NavigationMenu.Item value="getting-started">
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Getting started
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left absolute left-0 top-0 w-full data-[state=closed]:hidden sm:w-auto"
forceMount
>
<ul
class="m-0 grid list-none gap-x-2.5 p-3 sm:w-[600px] sm:grid-flow-col sm:grid-rows-3 sm:p-[22px]"
>
<li class="row-span-3 mb-2 sm:mb-0">
<NavigationMenu.Link
href="/"
class="from-muted/50 to-muted bg-linear-to-b outline-hidden flex h-full w-full select-none flex-col justify-end rounded-md p-6 no-underline focus:shadow-md"
>
<!-- <Icons.logo class="h-6 w-6" /> -->
<div class="mb-2 mt-4 text-lg font-medium">Bits UI</div>
<p class="text-muted-foreground text-sm leading-tight">
The headless components for Svelte.
</p>
</NavigationMenu.Link>
</li>
{@render ListItem({
href: "/docs",
title: "Introduction",
content: "Headless components for Svelte and SvelteKit"
})}
{@render ListItem({
href: "/docs/getting-started",
title: "Getting Started",
content: "How to install and use Bits UI"
})}
{@render ListItem({
href: "/docs/styling",
title: "Styling",
content: "How to style Bits UI components"
})}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item>
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Components
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
forceMount
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left absolute left-0 top-0 w-full data-[state=closed]:hidden sm:w-auto"
>
<ul
class="grid gap-3 p-3 sm:w-[400px] sm:p-6 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.Link
class="hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
href="/docs"
>
<span class="hidden sm:inline"> Documentation </span>
<span class="inline sm:hidden"> Docs </span>
</NavigationMenu.Link>
</NavigationMenu.Item>
<NavigationMenu.Indicator
class="data-[state=hidden]:animate-fade-out data-[state=visible]:animate-fade-in top-full z-10 flex h-2.5 items-end justify-center overflow-hidden opacity-100 transition-[all,transform_250ms_ease] duration-200 data-[state=hidden]:opacity-0"
>
<div
class="bg-border relative top-[70%] size-2.5 rotate-[45deg] rounded-tl-[2px]"
></div>
</NavigationMenu.Indicator>
</NavigationMenu.List>
<div
class="perspective-[2000px] absolute left-0 top-full flex w-full justify-center"
>
<NavigationMenu.Viewport
forceMount
class="text-popover-foreground bg-background data-[state=closed]:animate-scale-out data-[state=open]:animate-scale-in relative mt-2.5 h-[var(--bits-navigation-menu-viewport-height)] w-full origin-[top_center] overflow-hidden rounded-md border shadow-lg transition-[width,_height] duration-200 data-[state=closed]:hidden sm:w-[var(--bits-navigation-menu-viewport-width)]"
/>
</div>
</NavigationMenu.Root>
호버 시 열기 (Open on Hover)
기본적으로 NavigationMenu.Item은 NavigationMenu.Trigger 위에 마우스를 올리면 자신의 NavigationMenu.Content를 열어요. NavigationMenu.Item에 openOnHover={false}를 전달하면 이 동작을 끌 수 있어요.
참고 (Note)
기본 동작과 달리 openOnHover가 false면, 포인터가 NavigationMenu.Content 밖으로 나가도 메뉴가 닫히지 않아요. 대신 사용자가 메뉴 바깥과 상호작용하거나 escape 키를 눌러야 닫혀요.
<NavigationMenu.Item openOnHover={false}>
<NavigationMenu.Trigger>Item one</NavigationMenu.Trigger>
<NavigationMenu.Content>Item one content</NavigationMenu.Content>
</NavigationMenu.Item>
코드 펼치기 (Expand Code)
<script lang="ts">
import { NavigationMenu } from "bits-ui";
import CaretDown from "phosphor-svelte/lib/CaretDown";
import cn from "clsx";
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: "Link Preview",
href: "/docs/components/link-preview",
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 content—known as tab panels—that 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."
}
];
type ListItemProps = {
className?: string;
title: string;
href: string;
content: string;
};
</script>
{#snippet ListItem({ className, title, content, href }: ListItemProps)}
<li>
<NavigationMenu.Link
class={cn(
"hover:bg-muted hover:text-accent-foreground focus:bg-muted focus:text-accent-foreground outline-hidden block select-none space-y-1 rounded-md p-3 leading-none no-underline transition-colors",
className
)}
{href}
>
<div class="text-sm font-medium leading-none">{title}</div>
<p class="text-muted-foreground line-clamp-2 text-sm leading-snug">
{content}
</p>
</NavigationMenu.Link>
</li>
{/snippet}
<NavigationMenu.Root class="relative z-10 flex w-full justify-center">
<NavigationMenu.List
class="group flex list-none items-center justify-center p-1"
>
<NavigationMenu.Item value="getting-started" openOnHover={false}>
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus-visible:bg-muted focus-visible:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus-visible:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Getting started
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left absolute left-0 top-0 w-full sm:w-auto"
>
<ul
class="m-0 grid list-none gap-x-2.5 p-3 sm:w-[600px] sm:grid-flow-col sm:grid-rows-3 sm:p-[22px]"
>
<li class="row-span-3 mb-2 sm:mb-0">
<NavigationMenu.Link
href="/"
class="from-muted/50 to-muted bg-linear-to-b outline-hidden flex h-full w-full select-none flex-col justify-end rounded-md p-6 no-underline focus:shadow-md"
>
<!-- <Icons.logo class="h-6 w-6" /> -->
<div class="mb-2 mt-4 text-lg font-medium">Bits UI</div>
<p class="text-muted-foreground text-sm leading-tight">
The headless components for Svelte.
</p>
</NavigationMenu.Link>
</li>
{@render ListItem({
href: "/docs",
title: "Introduction",
content: "Headless components for Svelte and SvelteKit"
})}
{@render ListItem({
href: "/docs/getting-started",
title: "Getting Started",
content: "How to install and use Bits UI"
})}
{@render ListItem({
href: "/docs/styling",
title: "Styling",
content: "How to style Bits UI components"
})}
</ul>
</NavigationMenu.Content>
</NavigationMenu.Item>
<NavigationMenu.Item openOnHover={false}>
<NavigationMenu.Trigger
class="hover:text-accent-foreground focus-visible:bg-muted focus-visible:text-accent-foreground data-[state=open]:shadow-mini dark:hover:bg-muted dark:data-[state=open]:bg-muted focus-visible:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-white"
>
Components
<CaretDown
class="relative top-[1px] ml-1 size-3 transition-transform duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenu.Trigger>
<NavigationMenu.Content
class="data-[motion=from-end]:animate-enter-from-right data-[motion=from-start]:animate-enter-from-left data-[motion=to-end]:animate-exit-to-right data-[motion=to-start]:animate-exit-to-left absolute left-0 top-0 w-full sm:w-auto"
>
<ul
class="grid gap-3 p-3 sm:w-[400px] sm:p-6 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.Link
class="hover:text-accent-foreground focus-visible:bg-muted focus-visible:text-accent-foreground dark:hover:bg-muted focus-visible:outline-hidden group inline-flex h-8 w-max items-center justify-center rounded-[7px] bg-transparent px-4 py-2 text-sm font-medium transition-colors hover:bg-white disabled:pointer-events-none disabled:opacity-50"
href="/docs"
>
<span class="hidden sm:inline"> Documentation </span>
<span class="inline sm:hidden"> Docs </span>
</NavigationMenu.Link>
</NavigationMenu.Item>
<NavigationMenu.Indicator
class="data-[state=hidden]:animate-fade-out data-[state=visible]:animate-fade-in top-full z-10 flex h-2.5 items-end justify-center overflow-hidden opacity-100 transition-[all,transform_250ms_ease] duration-200 data-[state=hidden]:opacity-0"
>
<div
class="bg-border relative top-[70%] size-2.5 rotate-[45deg] rounded-tl-[2px]"
></div>
</NavigationMenu.Indicator>
</NavigationMenu.List>
<div
class="perspective-[2000px] absolute left-0 top-full flex w-full justify-center"
>
<NavigationMenu.Viewport
class="text-popover-foreground bg-background data-[state=closed]:animate-scale-out data-[state=open]:animate-scale-in relative mt-2.5 h-[var(--bits-navigation-menu-viewport-height)] w-full origin-[top_center] overflow-hidden rounded-md border shadow-lg transition-[width,_height] duration-200 sm:w-[var(--bits-navigation-menu-viewport-width)]"
/>
</div>
</NavigationMenu.Root>
API Reference
NavigationMenu.Root
The root navigation menu component which manages & scopes the state of the navigation menu.
| Property | Type | Description | Details |
|---|---|---|---|
value $bindable |
string |
The value of the currently active menu.Default: —— undefined |
|
onValueChange |
function - (value: string) => void |
A callback function called when the active menu value changes. Called with an empty string when the menu closes.Default: —— undefined |
|
dir |
enum - 'ltr' | 'rtl' |
The reading direction of the app.Default: 'ltr' |
|
skipDelayDuration |
number |
How much time a user has to enter another trigger without incurring a delay again.Default: 300 |
|
delayDuration |
number |
The duration from when the mouse enters a trigger until the content opens.Default: 200 |
|
orientation |
enum - 'horizontal' | 'vertical' |
The orientation of the menu.Default: 'horizontal' |
|
ref $bindable |
HTMLNavElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
NavigationMenu.Sub
A sub navigation menu component which manages & scopes the state of a submenu, inside the content of a Root menu.
| Property | Type | Description | Details |
|---|---|---|---|
value $bindable |
string |
The value of the currently active submenu.Default: —— undefined |
|
onValueChange |
function - (value: string) => void |
A callback function called when the active menu value changes.Default: —— undefined |
|
orientation |
enum - 'horizontal' | 'vertical' |
The orientation of the menu.Default: 'horizontal' |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
NavigationMenu.List
A menu within the menubar.
| Property | Type | Description | Details |
|---|---|---|---|
ref $bindable |
HTMLUListElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
NavigationMenu.Item
A list item within the navigation menu.
| Property | Type | Description | Details |
|---|---|---|---|
value |
string |
The value of the item.Default: —— undefined |
|
openOnHover |
boolean |
Whether or not the item should open its content when the trigger is hovered. Note: When false, the menu will not close when the pointer moves outside of the content and will instead require the user to interact outside of the menu or press escape to close it.Default: true |
|
ref $bindable |
HTMLLiElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
NavigationMenu.Trigger
The button element which toggles the dropdown menu.
| Property | Type | Description | Details |
|---|---|---|---|
disabled |
boolean |
Whether or not the trigger is disabled.Default: false |
|
ref $bindable |
HTMLButtonElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
NavigationMenu.Content
The content displayed when the dropdown menu is open.
| Property | Type | Description | Details |
|---|---|---|---|
onInteractOutside |
function - (event: PointerEvent) => void |
Callback fired when an outside interaction event occurs, which is a pointerdown event. You can call event.preventDefault() to prevent the default behavior of handling the outside interaction.Default: —— undefined |
|
onFocusOutside |
function - (event: FocusEvent) => void |
Callback fired when focus leaves the dismissible layer. You can call event.preventDefault() to prevent the default behavior on focus leaving the layer.Default: —— undefined |
|
interactOutsideBehavior |
enum - 'close' | 'ignore' | 'defer-otherwise-close' | 'defer-otherwise-ignore' |
The behavior to use when an interaction occurs outside of the floating content. 'close' will close the content immediately. 'ignore' will prevent the content from closing. 'defer-otherwise-close' will defer to the parent element if it exists, otherwise it will close the content. 'defer-otherwise-ignore' will defer to the parent element if it exists, otherwise it will ignore the interaction.Default: 'close' |
|
onEscapeKeydown |
function - (event: KeyboardEvent) => void |
Callback fired when an escape keydown event occurs in the floating content. You can call event.preventDefault() to prevent the default behavior of handling the escape keydown event.Default: —— undefined |
|
escapeKeydownBehavior |
enum - 'close' | 'ignore' | 'defer-otherwise-close' | 'defer-otherwise-ignore' |
The behavior to use when an escape keydown event occurs in the floating content. 'close' will close the content immediately. 'ignore' will prevent the content from closing. 'defer-otherwise-close' will defer to the parent element if it exists, otherwise it will close the content. 'defer-otherwise-ignore' will defer to the parent element if it exists, otherwise it will ignore the interaction.Default: 'close' |
|
forceMount |
boolean |
Whether or not to forcefully mount the content. This is useful if you want to use Svelte transitions or another animation library for the content.Default: false |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-starting-style |
'' |
Present during the initial open frame. Use this to define the starting styles for CSS transitions. | |
data-ending-style |
'' |
Present while closing before unmount. Use this to define the ending styles for CSS transitions. | |
data-navigation-menu-content |
'' |
Present on the content element. |
NavigationMenu.Link
A link within the navigation menu.
| Property | Type | Description | Details |
|---|---|---|---|
active |
boolean |
Whether or not the link is active.Default: false |
|
onSelect |
function - () => void |
A callback function called when the link is selected.Default: —— undefined |
|
ref $bindable |
HTMLAnchorElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
NavigationMenu.Viewport
An optional viewport element for the navigation menu, which renders the content of the menu items if it is present. The content is rendered in place without it.
| Property | Type | Description | Details |
|---|---|---|---|
forceMount |
boolean |
Whether or not to forcefully mount the content. This is useful if you want to use Svelte transitions or another animation library for the content.Default: false |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-starting-style |
'' |
Present during the initial open frame. Use this to define the starting styles for CSS transitions. | |
data-ending-style |
'' |
Present while closing before unmount. Use this to define the ending styles for CSS transitions. | |
data-navigation-menu-viewport |
'' |
Present on the viewport element. |
NavigationMenu.Indicator
The indicator element for the navigation menu, which is used to indicate the current active item.
| Property | Type | Description | Details |
|---|---|---|---|
forceMount |
boolean |
Whether or not to forcefully mount the content. This is useful if you want to use Svelte transitions or another animation library for the content.Default: false |
|
ref $bindable |
HTMLSpanElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-starting-style |
'' |
Present during the initial open frame. Use this to define the starting styles for CSS transitions. | |
data-ending-style |
'' |
Present while closing before unmount. Use this to define the ending styles for CSS transitions. | |
data-navigation-menu-indicator |
'' |
Present on the indicator element. |
더 알아보기 (Learn more)
Navigation Menu는 콘텐츠를 여러 단계로 나눠 탐색할 때 유용해요. 더 단순한 수직/수평 메뉴가 필요하다면 Menubar를, 계층이 없는 일반 드롭다운 메뉴는 Dropdown Menu를 함께 알아 두면 좋아요.