页眉
用法
头部组件渲染一个 <header>
元素。
使用 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>
标题
使用 title
属性更改头部的标题。默认为 Nuxt UI
。
<template>
<UHeader title="Nuxt UI" />
</template>
您也可以使用 title
插槽添加您自己的标志。
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
</UHeader>
</template>
至
使用 to
属性更改标题的链接。默认为 /
。
<template>
<UHeader to="/docs" />
</template>
您也可以使用 left
插槽完全覆盖链接。
<template>
<UHeader>
<template #left>
<NuxtLink to="/docs">
<Logo class="h-6 w-auto" />
</NuxtLink>
</template>
</UHeader>
</template>
模式
使用 mode
属性更改头部菜单的模式。默认为 modal
。
使用 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',
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
属性自定义在移动设备上显示的切换按钮。
您可以传递 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
属性更改切换按钮的侧边。默认为 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>
示例
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
属性
属性 | 默认值 | 类型 |
---|---|---|
as |
|
此组件应渲染为的元素或组件。 |
title |
|
|
过渡到 |
|
|
mode |
|
头部菜单的模式。 |
菜单 |
头部菜单组件的属性。
| |
toggle |
|
自定义用于打开头部菜单的切换按钮,该按钮在使用 |
toggleSide |
|
渲染切换按钮的侧边位置。 |
open |
|
|
ui |
|
插槽
插槽 | 类型 |
---|---|
title |
|
left |
|
default |
|
right |
|
toggle |
|
top |
|
bottom |
|
主体 |
|
内容 |
|
事件
事件 | 类型 |
---|---|
update:open |
|
主题
export default defineAppConfig({
ui: {
header: {
slots: {
root: 'bg-default/75 backdrop-blur 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'
}
}
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
header: {
slots: {
root: 'bg-default/75 backdrop-blur 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'
}
}
}
}
}
})
]
})
更新日志
5cb65
— 特性:导入 @nuxt/ui-pro
组件