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

@@ -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'