헤더
헤더 (Header)
헤더(Header)는 사이트 내비게이션을 위한 반응형 헤더 컴포넌트예요. 사이트 상단에 고정되고, 화면 크기에 맞춰 메뉴가 모달로 접히는 형태로 동작해요. 헤더 높이는 CSS 변수로 조절할 수 있어요.
출처: Nuxt UI
본문
사이트 내비게이션을 위한 반응형 헤더예요.
사용법 (Usage)
Header 컴포넌트는 <header> 요소를 렌더링해요.
[!TIP] See: /docs/getting-started/theme/css-variables#header
헤더의 높이는
--ui-header-heightCSS 변수로 정의돼요.
left, default, right 슬롯으로 헤더를, body 또는 content 슬롯으로 헤더 메뉴를 꾸밀 수 있어요.
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
</UHeader>
</template>
[!NOTE]
이 예시에서는 NavigationMenu 컴포넌트를 사용해 헤더 링크를 가운데에 배치했어요.
Title (제목)
title prop으로 헤더의 제목을 바꿀 수 있어요. 기본값은 Nuxt UI예요.
<template>
<UHeader title="Nuxt UI" />
</template>
title 슬롯으로 나만의 로고를 넣을 수도 있어요.
[!TIP] See: #props
링크의 기본
aria-label을 바꾸려면titleprop도 함께 넣어 주세요.
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template></UHeader>
</template>
To
to prop으로 제목의 링크를 바꿀 수 있어요. 기본값은 /예요.
<template>
<UHeader to="/docs" />
</template>
left 슬롯으로 링크를 완전히 재정의할 수도 있어요.
<template>
<UHeader>
<template #left>
<NuxtLink to="/docs">
<Logo class="h-6 w-auto" />
</NuxtLink>
</template></UHeader>
</template>
Mode (모드)
mode prop으로 헤더 메뉴의 모드를 바꿀 수 있어요. 기본값은 modal이에요.
body 슬롯으로 메뉴 본문(헤더 아래)을, content 슬롯으로 메뉴 전체를 채울 수 있어요.
[!TIP] See: #props
menuprop으로 헤더 메뉴를 꾸밀 수 있는데, 선택한 모드에 따라 자동으로 맞춰져요.
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
icon: 'i-lucide-book-open',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
icon: 'i-lucide-box',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
icon: 'i-simple-icons-figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
icon: 'i-lucide-rocket',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
</template>
Toggle (토글)
toggle prop으로 모바일에서 표시되는 토글 버튼을 꾸밀 수 있어요.
Button 컴포넌트의 속성을 아무거나 전달해 커스텀할 수 있어요.
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
icon: 'i-lucide-book-open',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
icon: 'i-lucide-box',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
icon: 'i-simple-icons-figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
icon: 'i-lucide-rocket',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UHeader
:toggle="{
color: 'primary',
variant: 'subtle',
class: 'rounded-full'
}"
>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
</template>
Toggle Side (토글 위치)
toggle-side prop으로 토글 버튼의 위치를 바꿀 수 있어요. 기본값은 right예요.
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
icon: 'i-lucide-book-open',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
icon: 'i-lucide-box',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
icon: 'i-simple-icons-figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
icon: 'i-lucide-rocket',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UHeader toggle-side="left">
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
</template>
예시 (Examples)
애니메이션 토글 사용하기
#toggle 슬롯으로 기본 토글 버튼을 Motion Vue로 만든 커스텀 햄버거 아이콘으로 바꿀 수 있어요.
<script setup lang="ts">
import { motion } from 'motion-v'
import type { VariantType } from 'motion-v'
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
icon: 'i-lucide-book-open',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
icon: 'i-lucide-box',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
icon: 'i-simple-icons-figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
icon: 'i-lucide-rocket',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
const variants: { [k: string]: VariantType | ((custom: unknown) => VariantType) } = {
normal: {
rotate: 0,
y: 0,
opacity: 1
},
close: (custom: unknown) => {
const c = custom as number
return {
rotate: c === 1 ? 45 : c === 3 ? -45 : 0,
y: c === 1 ? 6 : c === 3 ? -6 : 0,
opacity: c === 2 ? 0 : 1,
transition: {
type: 'spring',
stiffness: 260,
damping: 20
}
}
}
}
</script>
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
<template #toggle="{ open, toggle, ui }">
<UButton
size="sm"
variant="ghost"
color="neutral"
square
:class="ui.toggle({ toggleSide: 'right' })"
@click="toggle"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="size-5"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<motion.line
x1="4"
y1="6"
x2="20"
y2="6"
:variants="variants"
:animate="open ? 'close' : 'normal'"
:custom="1"
tabindex="-1"
/>
<motion.line
x1="4"
y1="12"
x2="20"
y2="12"
:variants="variants"
:animate="open ? 'close' : 'normal'"
:custom="2"
tabindex="-1"
/>
<motion.line
x1="4"
y1="18"
x2="20"
y2="18"
:variants="variants"
:animate="open ? 'close' : 'normal'"
:custom="3"
tabindex="-1"
/>
</svg>
</UButton>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
</template>
app.vue 안에서 사용하기
app.vue 또는 레이아웃에서 Header 컴포넌트를 사용해요:
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UApp>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
<UMain>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</UMain>
<UFooter />
</UApp>
</template>
API
Props
/**
* Props for the Header component
*/
interface HeaderProps {
/**
* The element or component this component should render as.
* @default 'header'
*/
as?: any;
/**
* @default 'Nuxt UI'
*/
title?: string | undefined;
/**
* @default '/'
*/
to?: string | undefined;
/**
* The mode of the header menu.
* @default 'modal'
*/
mode?: T | undefined;
/**
* The props for the header menu component.
*/
menu?: HeaderMenu<T> | undefined;
/**
* Customize the toggle button to open the header menu displayed when the `content` slot is used.
* `{ color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
* @default true
*/
toggle?: boolean | Omit<ButtonProps, LinkPropsKeys> | undefined;
/**
* The side to render the toggle button on.
* @default 'right'
*/
toggleSide?: "left" | "right" | undefined;
/**
* Automatically close when route changes.
* @default true
*/
autoClose?: boolean | undefined;
ui?: { root?: SlotClass; container?: SlotClass; left?: SlotClass; center?: SlotClass; right?: SlotClass; title?: SlotClass; toggle?: SlotClass; content?: SlotClass; overlay?: SlotClass; header?: SlotClass; body?: SlotClass; } | undefined;
/**
* @default false
*/
open?: boolean | undefined;
}
Slots
/**
* Slots for the Header component
*/
interface HeaderSlots {
title(): any;
left(): any;
default(): any;
right(): any;
toggle(): any;
top(): any;
bottom(): any;
body(): any;
content(): any;
}
Emits
/**
* Emitted events for the Header component
*/
interface HeaderEmits {
update:open: (payload: [value: boolean]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
header: {
slots: {
root: 'bg-default/75 backdrop-blur-sm border-b border-default h-(--ui-header-height) sticky top-0 z-50',
container: 'flex items-center justify-between gap-3 h-full',
left: 'lg:flex-1 flex items-center gap-1.5',
center: 'hidden lg:flex',
right: 'flex items-center justify-end lg:flex-1 gap-1.5',
title: 'shrink-0 font-bold text-xl text-highlighted flex items-end gap-1.5',
toggle: 'lg:hidden',
content: 'lg:hidden',
overlay: 'lg:hidden',
header: 'px-4 sm:px-6 h-(--ui-header-height) shrink-0 flex items-center justify-between gap-3',
body: 'p-4 sm:p-6 overflow-y-auto'
},
variants: {
toggleSide: {
left: {
toggle: '-ms-1.5'
},
right: {
toggle: '-me-1.5'
}
}
}
}
}
})
변경 기록 (Changelog)
더 알아보기 (Learn more)
- GitHub 소스
- NavigationMenu — 헤더 링크를 만드는 데 쓰는 컴포넌트
- 전체 사이트맵