feat(Alert): new component

Resolves #23
This commit is contained in:
Benjamin Canac
2024-04-12 19:00:55 +02:00
parent 514d17048f
commit 1535313596
8 changed files with 473 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ const appConfig = useAppConfig()
const components = [
'accordion',
'alert',
'avatar',
'badge',
'button',

View File

@@ -0,0 +1,35 @@
<script setup lang="ts">
import theme from '#build/ui/alert'
const variants = Object.keys(theme.variants.variant)
const actions = [{
label: 'Action',
click() {
console.log('Action clicked')
}
}]
const data = {
title: 'Heads up!',
description: 'You can add components to your app using the cli.',
icon: 'i-heroicons-command-line',
actions,
close: true
}
</script>
<template>
<div class="flex flex-col gap-2 flex-1">
<UAlert :title="data.title" />
<UAlert :title="data.title" :icon="data.icon" />
<UAlert :title="data.title" :icon="data.icon" :close="data.close" />
<UAlert :title="data.title" :icon="data.icon" :close="data.close" :actions="data.actions" />
<UAlert :title="data.title" :icon="data.icon" :close="data.close" :description="data.description" />
<UAlert :title="data.title" :avatar="{ src: 'https://avatars.githubusercontent.com/u/739984?v=4' }" :close="data.close" :description="data.description" />
<UAlert v-bind="data" color="white" />
<UAlert v-bind="data" color="gray" />
<UAlert v-bind="data" color="black" />
<UAlert v-for="variant in variants" :key="variant" v-bind="data" color="primary" :variant="(variant as any)" />
</div>
</template>

View File

@@ -0,0 +1,106 @@
<script lang="ts">
import { isVNode, type VNode } from 'vue'
import { tv, type VariantProps } 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/alert'
import type { AvatarProps, ButtonProps, IconProps } from '#ui/types'
const appConfig = _appConfig as AppConfig & { ui: { alert: Partial<typeof theme> } }
const alert = tv({ extend: tv(theme), ...(appConfig.ui?.alert || {}) })
type AlertVariants = VariantProps<typeof alert>
export interface AlertProps extends Omit<PrimitiveProps, 'asChild'> {
title?: string
description?: string | VNode | (() => VNode)
icon?: IconProps['name']
avatar?: AvatarProps
color?: AlertVariants['color']
variant?: AlertVariants['variant']
actions?: ButtonProps[]
close?: ButtonProps | boolean
class?: any
ui?: Partial<typeof alert.slots>
}
export interface AlertEmits {
(e: 'close'): void
}
export interface AlertSlots {
leading(): any
title(): any
description(): any
close(): any
}
</script>
<script setup lang="ts">
import { computed } from 'vue'
import { Primitive } from 'radix-vue'
import { useAppConfig } from '#imports'
import { UIcon, UAvatar } from '#components'
const props = withDefaults(defineProps<AlertProps>(), { as: 'div' })
const emits = defineEmits<AlertEmits>()
defineSlots<AlertSlots>()
const appConfig = useAppConfig()
const multiline = computed(() => !!props.title && !!props.description)
const ui = computed(() => tv({ extend: alert, slots: props.ui })({
color: props.color,
variant: props.variant
}))
</script>
<template>
<Primitive :as="as" :class="ui.root({ class: props.class, multiline })">
<slot name="leading">
<UAvatar v-if="avatar" size="2xl" v-bind="avatar" :class="ui.avatar()" />
<UIcon v-else-if="icon" :name="icon" :class="ui.icon()" />
</slot>
<div :class="ui.wrapper()">
<div v-if="title || $slots.title" :class="ui.title()">
<slot name="title">
{{ title }}
</slot>
</div>
<template v-if="description || $slots.description">
<component :is="description" v-if="description && isVNode(description)" />
<div v-else :class="ui.description()">
<slot name="description">
{{ description }}
</slot>
</div>
</template>
<div v-if="multiline && actions?.length" :class="ui.actions({ multiline: true })">
<UButton v-for="(action, index) in actions" :key="index" size="xs" v-bind="action" />
</div>
</div>
<div v-if="(!multiline && actions?.length) || close" :class="ui.actions({ multiline: false })">
<template v-if="!multiline">
<UButton v-for="(action, index) in actions" :key="index" size="xs" v-bind="action" />
</template>
<UButton
v-if="close"
:icon="appConfig.ui.icons.close"
size="sm"
color="gray"
variant="link"
aria-label="Close"
v-bind="typeof close === 'object' ? close : {}"
:class="ui.close()"
@click="emits('close')"
/>
</div>
</Primitive>
</template>

View File

@@ -1,5 +1,6 @@
export * from '../components/App.vue'
export * from '../components/Accordion.vue'
export * from '../components/Alert.vue'
export * from '../components/Avatar.vue'
export * from '../components/Badge.vue'
export * from '../components/Button.vue'

83
src/theme/alert.ts Normal file
View File

@@ -0,0 +1,83 @@
export default (config: { colors: string[] }) => ({
slots: {
root: 'relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5',
wrapper: 'min-w-0 flex-1 flex flex-col gap-1',
title: 'text-sm font-medium',
description: 'text-sm',
icon: 'shrink-0 size-5',
avatar: 'shrink-0',
actions: 'flex gap-1.5 shrink-0',
close: 'p-0'
},
variants: {
color: {
...Object.fromEntries(config.colors.map((color: string) => [color, ''])),
white: '',
gray: '',
black: ''
},
variant: {
solid: '',
outline: '',
soft: '',
subtle: ''
},
multiline: {
true: {
root: 'items-start',
actions: 'items-start mt-1'
},
false: {
root: 'items-center',
actions: 'items-center'
}
}
},
compoundVariants: [...config.colors.map((color: string) => ({
color,
variant: 'solid',
class: {
root: `bg-${color}-500 dark:bg-${color}-400 text-white dark:text-gray-900`
}
})), ...config.colors.map((color: string) => ({
color,
variant: 'outline',
class: {
root: `text-${color}-500 dark:text-${color}-400 ring ring-inset ring-${color}-500 dark:ring-${color}-400`
}
})), ...config.colors.map((color: string) => ({
color,
variant: 'soft',
class: {
root: `bg-${color}-50 dark:bg-${color}-400/10 text-${color}-500 dark:text-${color}-400`
}
})), ...config.colors.map((color: string) => ({
color,
variant: 'subtle',
class: {
root: `bg-${color}-50 dark:bg-${color}-400/10 text-${color}-500 dark:text-${color}-400 ring ring-inset ring-${color}-500/25 dark:ring-${color}-400/25`
}
})), {
color: 'white',
variant: 'solid',
class: {
root: 'ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900'
}
}, {
color: 'gray',
variant: 'solid',
class: {
root: 'ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-700 dark:text-gray-200 bg-gray-50 dark:bg-gray-800'
}
}, {
color: 'black',
variant: 'solid',
class: {
root: 'text-white dark:text-gray-900 bg-gray-900 dark:bg-white'
}
}],
defaultVariants: {
color: 'white',
variant: 'solid'
}
})

View File

@@ -1,4 +1,5 @@
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 button } from './button'

View File

@@ -0,0 +1,29 @@
import { describe, it, expect } from 'vitest'
import Alert, { type AlertProps } from '../../src/runtime/components/Alert.vue'
import ComponentRender from '../component-render'
describe('Alert', () => {
it.each([
['with title', { props: { title: 'Title' } }],
['with description', { props: { title: 'Title', description: 'Description' } }],
['with icon', { props: { title: 'Title', icon: 'i-heroicons-light-bulb' } }],
['with avatar', { props: { title: 'Title', avatar: { src: 'https://avatars.githubusercontent.com/u/739984?v=4' } } }],
['with as', { props: { title: 'Title', as: 'article' } }],
['with color green', { props: { title: 'Title', color: 'green' as const } }],
['with color white', { props: { title: 'Title', color: 'white' as const } }],
['with color gray', { props: { title: 'Title', color: 'gray' as const } }],
['with color black', { props: { title: 'Title', color: 'black' as const } }],
['with variant outline', { props: { title: 'Title', variant: 'outline' as const } }],
['with variant soft', { props: { title: 'Title', variant: 'soft' as const } }],
['with variant link', { props: { title: 'Title', variant: 'subtle' as const } }],
['with class', { props: { class: 'w-48' } }],
['with ui', { props: { ui: { title: 'font-bold' } } }],
['with leading slot', { slots: { title: () => 'Leading slot' } }],
['with title slot', { slots: { title: () => 'Title slot' } }],
['with description slot', { slots: { description: () => 'Description slot' } }],
['with close slot', { slots: { close: () => 'Close slot' } }]
])('renders %s correctly', async (nameOrHtml: string, options: { props?: AlertProps, slots?: any }) => {
const html = await ComponentRender(nameOrHtml, options, Alert)
expect(html).toMatchSnapshot()
})
})

View File

@@ -0,0 +1,217 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Alert > renders with as correctly 1`] = `
"<article class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</article>"
`;
exports[`Alert > renders with avatar correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900"><span class="inline-flex items-center justify-center select-none overflow-hidden rounded-full align-middle bg-gray-100 dark:bg-gray-800 size-11 text-[22px] 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>
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with class correctly 1`] = `
"<div class="relative overflow-hidden rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900 w-48">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<!--v-if-->
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with close slot correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<!--v-if-->
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with color black correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center text-white dark:text-gray-900 bg-gray-900 dark:bg-white">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with color gray correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-700 dark:text-gray-200 bg-gray-50 dark:bg-gray-800">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with color green correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center bg-green-500 dark:bg-green-400 text-white dark:text-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with color white correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with description correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-start ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<div class="text-sm">Description</div>
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with description slot correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<!--v-if-->
<div class="text-sm">Description slot</div>
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with icon correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900"><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="M12 18v-5.25m0 0a6.01 6.01 0 0 0 1.5-.189m-1.5.189a6.01 6.01 0 0 1-1.5-.189m3.75 7.478a12.06 12.06 0 0 1-4.5 0m3.75 2.383a14.406 14.406 0 0 1-3 0M14.25 18v-.192c0-.983.658-1.823 1.508-2.316a7.5 7.5 0 1 0-7.517 0c.85.493 1.509 1.333 1.509 2.316V18"></path>
</svg>
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with leading slot correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Leading slot</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with title correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with title slot correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title slot</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with ui correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center ring ring-inset ring-gray-300 dark:ring-gray-700 text-gray-900 dark:text-white bg-white dark:bg-gray-900">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<!--v-if-->
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with variant link correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with variant outline correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;
exports[`Alert > renders with variant soft correctly 1`] = `
"<div class="relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5 items-center">
<!--v-if-->
<div class="min-w-0 flex-1 flex flex-col gap-1">
<div class="text-sm font-medium">Title</div>
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>"
`;