feat(Breadcrumb): new component

Resolves #22
This commit is contained in:
Benjamin Canac
2024-04-16 17:23:41 +02:00
parent 298ac68447
commit 53a2bc0264
8 changed files with 237 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ const components = [
'alert',
'avatar',
'badge',
'breadcrumb',
'button',
'card',
'checkbox',

View File

@@ -0,0 +1,20 @@
<script setup lang="ts">
const links = [{
label: 'Home',
avatar: {
src: 'https://avatars.githubusercontent.com/u/739984?v=4'
},
to: '/'
}, {
label: 'Navigation',
icon: 'i-heroicons-square-3-stack-3d',
disabled: true
}, {
label: 'Breadcrumb',
icon: 'i-heroicons-link'
}]
</script>
<template>
<UBreadcrumb :links="links" />
</template>

View File

@@ -0,0 +1,79 @@
<script lang="ts">
import { tv } from 'tailwind-variants'
import type { PrimitiveProps } from 'radix-vue'
import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/breadcrumb'
import type { AvatarProps, IconProps, LinkProps } from '#ui/types'
const appConfig = _appConfig as AppConfig & { ui: { breadcrumb: Partial<typeof theme> } }
const breadcrumb = tv({ extend: tv(theme), ...(appConfig.ui?.breadcrumb || {}) })
export interface BreadcrumbLink extends LinkProps {
label: string
icon?: IconProps['name']
avatar?: AvatarProps
}
export interface BreadcrumbProps<T> extends Omit<PrimitiveProps, 'asChild'> {
links?: T[]
separatorIcon?: IconProps['name']
class?: any
ui?: Partial<typeof breadcrumb.slots>
}
type SlotProps<T> = (props: { link: T, active: boolean, index: number }) => any
export interface BreadcrumbSlots<T> {
leading: SlotProps<T>
default: SlotProps<T>
trailing: SlotProps<T>
separator(): any
}
</script>
<script setup lang="ts" generic="T extends BreadcrumbLink">
import { computed } from 'vue'
import { Primitive } from 'radix-vue'
import { useAppConfig } from '#imports'
import { omit } from '#ui/utils'
const props = defineProps<BreadcrumbProps<T>>()
defineSlots<BreadcrumbSlots<T>>()
const appConfig = useAppConfig()
const ui = computed(() => tv({ extend: breadcrumb, slots: props.ui })())
</script>
<template>
<Primitive :as="as" aria-label="breadcrumb" :class="ui.root({ class: props.class })">
<ol :class="ui.list()">
<template v-for="(link, index) in links" :key="index">
<li :class="ui.item()">
<ULink as="span" v-bind="omit(link, ['label', 'icon', 'avatar'])" :aria-current="index === links!.length - 1 ? 'page' : undefined" :class="ui.link({ active: index === links!.length - 1, disabled: link.disabled })" raw>
<slot name="leading" :link="link" :active="index === links!.length - 1" :index="index">
<UAvatar v-if="link.avatar" size="2xs" v-bind="link.avatar" :class="ui.linkLeadingAvatar({ active: index === links!.length - 1 })" />
<UIcon v-else-if="link.icon" :name="link.icon" :class="ui.linkLeadingIcon({ active: index === links!.length - 1 })" />
</slot>
<span v-if="link.label || $slots.default" :class="ui.linkLabel()">
<slot :link="link" :active="index === links!.length - 1" :index="index">
{{ link.label }}
</slot>
</span>
<slot name="trailing" :link="link" :active="index === links!.length - 1" :index="index" />
</ULink>
</li>
<li v-if="index < links!.length - 1" role="presentation" :class="ui.separator()">
<slot name="separator">
<UIcon :name="separatorIcon || appConfig.ui.icons.chevronRight" :class="ui.separatorIcon()" />
</slot>
</li>
</template>
</ol>
</Primitive>
</template>

View File

@@ -3,6 +3,7 @@ export * from '../components/Accordion.vue'
export * from '../components/Alert.vue'
export * from '../components/Avatar.vue'
export * from '../components/Badge.vue'
export * from '../components/Breadcrumb.vue'
export * from '../components/Button.vue'
export * from '../components/Card.vue'
export * from '../components/Checkbox.vue'

28
src/theme/breadcrumb.ts Normal file
View File

@@ -0,0 +1,28 @@
export default {
slots: {
root: 'relative min-w-0',
list: 'flex items-center gap-1.5',
item: 'flex min-w-0',
link: 'group relative flex items-center gap-1.5 font-medium text-sm min-w-0',
linkLeadingIcon: 'shrink-0 size-5',
linkLeadingAvatar: 'shrink-0',
linkLabel: 'truncate',
separator: 'flex',
separatorIcon: 'shrink-0 size-5 text-gray-500 dark:text-gray-400'
},
variants: {
active: {
true: {
link: 'text-primary-500 dark:text-primary-400'
},
false: {
link: 'text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white'
}
},
disabled: {
true: {
link: 'cursor-not-allowed opacity-75'
}
}
}
}

View File

@@ -2,6 +2,7 @@ export { default as accordion } from './accordion'
export { default as alert } from './alert'
export { default as avatar } from './avatar'
export { default as badge } from './badge'
export { default as breadcrumb } from './breadcrumb'
export { default as button } from './button'
export { default as card } from './card'
export { default as checkbox } from './checkbox'

View File

@@ -0,0 +1,36 @@
import { describe, it, expect } from 'vitest'
import Breadcrumb, { type BreadcrumbProps } from '../../src/runtime/components/Breadcrumb.vue'
import ComponentRender from '../component-render'
describe('Breadcrumb', () => {
const links = [{
label: 'Home',
avatar: {
src: 'https://avatars.githubusercontent.com/u/739984?v=4'
},
to: '/'
}, {
label: 'Navigation',
icon: 'i-heroicons-square-3-stack-3d',
disabled: true
}, {
label: 'Breadcrumb',
icon: 'i-heroicons-link'
}]
it.each([
// Props
['with links', { props: { links } }],
['with separatorIcon', { props: { links, separatorIcon: 'i-heroicons-minus' } }],
['with class', { props: { class: 'w-48' } }],
['with ui', { props: { ui: { link: 'font-bold' } } }],
// Slots
['with default slot', { slots: { default: () => 'Default slot' } }],
['with leading slot', { slots: { leading: () => 'Leading slot' } }],
['with trailing slot', { slots: { trailing: () => 'Trailing slot' } }],
['with separator slot', { slots: { separator: () => '/' } }]
])('renders %s correctly', async (nameOrHtml: string, options: { props?: BreadcrumbProps<typeof links[number]>, slots?: any }) => {
const html = await ComponentRender(nameOrHtml, options, Breadcrumb)
expect(html).toMatchSnapshot()
})
})

View File

@@ -0,0 +1,71 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Breadcrumb > renders with class correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0 w-48">
<ol class="flex items-center gap-1.5"></ol>
</div>"
`;
exports[`Breadcrumb > renders with default slot correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0">
<ol class="flex items-center gap-1.5"></ol>
</div>"
`;
exports[`Breadcrumb > renders with leading slot correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0">
<ol class="flex items-center gap-1.5"></ol>
</div>"
`;
exports[`Breadcrumb > renders with links correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0">
<ol class="flex items-center gap-1.5">
<li class="flex min-w-0"><a href="/" class="group relative flex items-center gap-1.5 font-medium text-sm min-w-0 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white"><span class="inline-flex items-center justify-center select-none overflow-hidden rounded-full align-middle bg-gray-100 dark:bg-gray-800 size-5 text-[10px] shrink-0"><img role="img" src="https://avatars.githubusercontent.com/u/739984?v=4" class="h-full w-full rounded-[inherit] object-cover" style="display: none;"><span class="font-medium leading-none text-gray-500 dark:text-gray-400 truncate"></span></span><span class="truncate">Home</span></a></li>
<li role="presentation" class="flex"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5 text-gray-500 dark:text-gray-400" width="1em" height="1em" viewBox="0 0 20 20">
<path fill="currentColor" fill-rule="evenodd" d="M8.22 5.22a.75.75 0 0 1 1.06 0l4.25 4.25a.75.75 0 0 1 0 1.06l-4.25 4.25a.75.75 0 0 1-1.06-1.06L11.94 10L8.22 6.28a.75.75 0 0 1 0-1.06" clip-rule="evenodd"></path>
</svg></li>
<li class="flex min-w-0"><span class="group relative flex items-center gap-1.5 font-medium text-sm min-w-0 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white cursor-not-allowed opacity-75"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5" width="1em" height="1em" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6.429 9.75L2.25 12l4.179 2.25m0-4.5l5.571 3l5.571-3m-11.142 0L2.25 7.5L12 2.25l9.75 5.25l-4.179 2.25m0 0L21.75 12l-4.179 2.25m0 0l4.179 2.25L12 21.75L2.25 16.5l4.179-2.25m11.142 0l-5.571 3l-5.571-3"></path></svg><span class="truncate">Navigation</span></span></li>
<li role="presentation" class="flex"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5 text-gray-500 dark:text-gray-400" width="1em" height="1em" viewBox="0 0 20 20">
<path fill="currentColor" fill-rule="evenodd" d="M8.22 5.22a.75.75 0 0 1 1.06 0l4.25 4.25a.75.75 0 0 1 0 1.06l-4.25 4.25a.75.75 0 0 1-1.06-1.06L11.94 10L8.22 6.28a.75.75 0 0 1 0-1.06" clip-rule="evenodd"></path>
</svg></li>
<li class="flex min-w-0"><span aria-current="page" class="group relative flex items-center gap-1.5 font-medium text-sm min-w-0 text-primary-500 dark:text-primary-400"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5" width="1em" height="1em" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M13.19 8.688a4.5 4.5 0 0 1 1.242 7.244l-4.5 4.5a4.5 4.5 0 0 1-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 0 0-6.364-6.364l-4.5 4.5a4.5 4.5 0 0 0 1.242 7.244"></path></svg><span class="truncate">Breadcrumb</span></span></li>
<!--v-if-->
</ol>
</div>"
`;
exports[`Breadcrumb > renders with separator slot correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0">
<ol class="flex items-center gap-1.5"></ol>
</div>"
`;
exports[`Breadcrumb > renders with separatorIcon correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0">
<ol class="flex items-center gap-1.5">
<li class="flex min-w-0"><a href="/" class="group relative flex items-center gap-1.5 font-medium text-sm min-w-0 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white"><span class="inline-flex items-center justify-center select-none overflow-hidden rounded-full align-middle bg-gray-100 dark:bg-gray-800 size-5 text-[10px] shrink-0"><img role="img" src="https://avatars.githubusercontent.com/u/739984?v=4" class="h-full w-full rounded-[inherit] object-cover" style="display: none;"><span class="font-medium leading-none text-gray-500 dark:text-gray-400 truncate"></span></span><span class="truncate">Home</span></a></li>
<li role="presentation" class="flex"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5 text-gray-500 dark:text-gray-400" width="1em" height="1em" viewBox="0 0 24 24">
<path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M5 12h14"></path>
</svg></li>
<li class="flex min-w-0"><span class="group relative flex items-center gap-1.5 font-medium text-sm min-w-0 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white cursor-not-allowed opacity-75"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5" width="1em" height="1em" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6.429 9.75L2.25 12l4.179 2.25m0-4.5l5.571 3l5.571-3m-11.142 0L2.25 7.5L12 2.25l9.75 5.25l-4.179 2.25m0 0L21.75 12l-4.179 2.25m0 0l4.179 2.25L12 21.75L2.25 16.5l4.179-2.25m11.142 0l-5.571 3l-5.571-3"></path></svg><span class="truncate">Navigation</span></span></li>
<li role="presentation" class="flex"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5 text-gray-500 dark:text-gray-400" width="1em" height="1em" viewBox="0 0 24 24">
<path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M5 12h14"></path>
</svg></li>
<li class="flex min-w-0"><span aria-current="page" class="group relative flex items-center gap-1.5 font-medium text-sm min-w-0 text-primary-500 dark:text-primary-400"><svg data-v-9533427c="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon shrink-0 size-5" width="1em" height="1em" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M13.19 8.688a4.5 4.5 0 0 1 1.242 7.244l-4.5 4.5a4.5 4.5 0 0 1-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 0 0-6.364-6.364l-4.5 4.5a4.5 4.5 0 0 0 1.242 7.244"></path></svg><span class="truncate">Breadcrumb</span></span></li>
<!--v-if-->
</ol>
</div>"
`;
exports[`Breadcrumb > renders with trailing slot correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0">
<ol class="flex items-center gap-1.5"></ol>
</div>"
`;
exports[`Breadcrumb > renders with ui correctly 1`] = `
"<div aria-label="breadcrumb" class="relative min-w-0">
<ol class="flex items-center gap-1.5"></ol>
</div>"
`;