mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-21 23:40:39 +01:00
feat(HorizontalNavigation): new component (#1279)
This commit is contained in:
109
src/runtime/components/navigation/HorizontalNavigation.vue
Normal file
109
src/runtime/components/navigation/HorizontalNavigation.vue
Normal file
@@ -0,0 +1,109 @@
|
||||
<template>
|
||||
<nav :class="ui.wrapper" v-bind="attrs">
|
||||
<ul v-for="(section, sectionIndex) of sections" :key="`section${sectionIndex}`" :class="ui.container">
|
||||
<li v-for="(link, index) of section" :key="`section${sectionIndex}-${index}`">
|
||||
<ULink
|
||||
v-slot="{ isActive }"
|
||||
v-bind="getULinkProps(link)"
|
||||
:class="[ui.base, ui.before, ui.after]"
|
||||
:active-class="ui.active"
|
||||
:inactive-class="ui.inactive"
|
||||
@click="link.click"
|
||||
@keyup.enter="$event.target.blur()"
|
||||
>
|
||||
<slot name="avatar" :link="link" :is-active="isActive">
|
||||
<UAvatar
|
||||
v-if="link.avatar"
|
||||
v-bind="{ size: ui.avatar.size, ...link.avatar }"
|
||||
:class="[ui.avatar.base]"
|
||||
/>
|
||||
</slot>
|
||||
<slot name="icon" :link="link" :is-active="isActive">
|
||||
<UIcon
|
||||
v-if="link.icon"
|
||||
:name="link.icon"
|
||||
:class="twMerge(twJoin(ui.icon.base, isActive ? ui.icon.active : ui.icon.inactive), link.iconClass)"
|
||||
/>
|
||||
</slot>
|
||||
<slot :link="link" :is-active="isActive">
|
||||
<span v-if="link.label" :class="twMerge(ui.label, link.labelClass)">
|
||||
<span v-if="isActive" class="sr-only">
|
||||
Current page:
|
||||
</span>
|
||||
{{ link.label }}
|
||||
</span>
|
||||
</slot>
|
||||
<slot name="badge" :link="link" :is-active="isActive">
|
||||
<UBadge
|
||||
v-if="link.badge"
|
||||
v-bind="{
|
||||
size: ui.badge.size,
|
||||
color: ui.badge.color,
|
||||
variant: ui.badge.variant,
|
||||
...((typeof link.badge === 'string' || typeof link.badge === 'number') ? { label: link.badge } : link.badge)
|
||||
}"
|
||||
:class="ui.badge.base"
|
||||
/>
|
||||
</slot>
|
||||
</ULink>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { toRef, defineComponent, computed } from 'vue'
|
||||
import type { PropType } from 'vue'
|
||||
import { twMerge, twJoin } from 'tailwind-merge'
|
||||
import UIcon from '../elements/Icon.vue'
|
||||
import UAvatar from '../elements/Avatar.vue'
|
||||
import UBadge from '../elements/Badge.vue'
|
||||
import ULink from '../elements/Link.vue'
|
||||
import { useUI } from '../../composables/useUI'
|
||||
import { mergeConfig, getULinkProps } from '../../utils'
|
||||
import type { HorizontalNavigationLink, Strategy } from '../../types'
|
||||
// @ts-expect-error
|
||||
import appConfig from '#build/app.config'
|
||||
import { horizontalNavigation } from '#ui/ui.config'
|
||||
|
||||
const config = mergeConfig<typeof horizontalNavigation>(appConfig.ui.strategy, appConfig.ui.horizontalNavigation, horizontalNavigation)
|
||||
|
||||
export default defineComponent({
|
||||
components: {
|
||||
UIcon,
|
||||
UAvatar,
|
||||
UBadge,
|
||||
ULink
|
||||
},
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
links: {
|
||||
type: Array as PropType<HorizontalNavigationLink[][] | HorizontalNavigationLink[]>,
|
||||
default: () => []
|
||||
},
|
||||
class: {
|
||||
type: [String, Object, Array] as PropType<any>,
|
||||
default: () => ''
|
||||
},
|
||||
ui: {
|
||||
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
|
||||
default: () => ({})
|
||||
}
|
||||
},
|
||||
setup (props) {
|
||||
const { ui, attrs } = useUI('horizontalNavigation', toRef(props, 'ui'), config, toRef(props, 'class'))
|
||||
|
||||
const sections = computed(() => (Array.isArray(props.links[0]) ? props.links : [props.links]) as HorizontalNavigationLink[][])
|
||||
|
||||
return {
|
||||
// eslint-disable-next-line vue/no-dupe-keys
|
||||
ui,
|
||||
attrs,
|
||||
sections,
|
||||
getULinkProps,
|
||||
twMerge,
|
||||
twJoin
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
13
src/runtime/types/horizontal-navigation.d.ts
vendored
Normal file
13
src/runtime/types/horizontal-navigation.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { Link } from './link'
|
||||
import type { Avatar } from './avatar'
|
||||
import type { Badge } from './badge'
|
||||
|
||||
export interface HorizontalNavigationLink extends Link {
|
||||
label: string
|
||||
labelClass?: string
|
||||
icon?: string
|
||||
iconClass?: string
|
||||
avatar?: Avatar
|
||||
click?: Function
|
||||
badge?: string | number | Badge
|
||||
}
|
||||
1
src/runtime/types/index.d.ts
vendored
1
src/runtime/types/index.d.ts
vendored
@@ -10,6 +10,7 @@ export * from './command-palette'
|
||||
export * from './dropdown'
|
||||
export * from './form-group'
|
||||
export * from './form'
|
||||
export * from './horizontal-navigation'
|
||||
export * from './input'
|
||||
export * from './kbd'
|
||||
export * from './link'
|
||||
|
||||
@@ -38,6 +38,7 @@ export { default as divider } from './layout/divider'
|
||||
|
||||
// Navigation
|
||||
export { default as verticalNavigation } from './navigation/verticalNavigation'
|
||||
export { default as horizontalNavigation } from './navigation/horizontalNavigation'
|
||||
export { default as commandPalette } from './navigation/commandPalette'
|
||||
export { default as pagination } from './navigation/pagination'
|
||||
export { default as tabs } from './navigation/tabs'
|
||||
|
||||
25
src/runtime/ui.config/navigation/horizontalNavigation.ts
Normal file
25
src/runtime/ui.config/navigation/horizontalNavigation.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export default {
|
||||
wrapper: 'relative w-full flex items-center justify-between',
|
||||
container: 'flex items-center',
|
||||
base: 'group relative w-full flex items-center gap-1.5 px-2.5 py-3.5 rounded-md font-medium text-sm focus:outline-none focus-visible:outline-none dark:focus-visible:outline-none focus-visible:ring-inset focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400 disabled:cursor-not-allowed disabled:opacity-75',
|
||||
before: 'before:absolute before:inset-x-0 before:inset-y-2 before:inset-px before:rounded-md hover:before:bg-gray-50 dark:hover:before:bg-gray-800/50',
|
||||
after: 'after:absolute after:bottom-0 after:inset-x-2.5 after:block after:h-[2px] after:mt-2',
|
||||
active: 'text-gray-900 dark:text-white after:bg-primary-500 dark:after:bg-primary-400 after:rounded-full',
|
||||
inactive: 'text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white',
|
||||
label: 'truncate relative',
|
||||
icon: {
|
||||
base: 'flex-shrink-0 w-5 h-5',
|
||||
active: 'text-gray-700 dark:text-gray-200',
|
||||
inactive: 'text-gray-400 dark:text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-200'
|
||||
},
|
||||
avatar: {
|
||||
base: 'flex-shrink-0',
|
||||
size: '2xs' as const
|
||||
},
|
||||
badge: {
|
||||
base: 'flex-shrink-0 ml-auto relative rounded',
|
||||
color: 'gray' as const,
|
||||
variant: 'solid' as const,
|
||||
size: 'xs' as const
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user