|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { VariantProps } from 'class-variance-authority' |
| 3 | +import type { HTMLAttributes } from 'vue' |
| 4 | +import { cva } from 'class-variance-authority' |
| 5 | +import { computed, ref } from 'vue' |
| 6 | +import { cn } from '#utils' |
| 7 | +import { Badge } from './badge' |
| 8 | +
|
| 9 | +defineOptions({ |
| 10 | + name: 'BuiltInBadge', |
| 11 | +}) |
| 12 | +
|
| 13 | +const props = defineProps<{ |
| 14 | + variant?: BadgeVariant['variant'] |
| 15 | + value: string | number | boolean |
| 16 | + class?: HTMLAttributes['class'] |
| 17 | + badgeClass?: HTMLAttributes['class'] |
| 18 | +}>() |
| 19 | +
|
| 20 | +const badgeDotVariant = cva( |
| 21 | + 'absolute start-[100%] h-1.5 w-1.5 rounded-full px-0 ring-1 ring-background before:(absolute inset-0 bg-inherit block h-full w-full animate-ping rounded-full content-empty) -translate-x-[50%] -translate-y-[50%] rtl:(translate-x-[50%]) -indent-9999', |
| 22 | + { |
| 23 | + variants: { |
| 24 | + variant: { |
| 25 | + default: |
| 26 | + 'bg-primary hover:bg-primary/80', |
| 27 | + secondary: |
| 28 | + 'bg-secondary hover:bg-secondary/80', |
| 29 | + destructive: |
| 30 | + 'bg-destructive hover:bg-destructive/80', |
| 31 | + }, |
| 32 | + }, |
| 33 | + defaultVariants: { |
| 34 | + variant: 'default', |
| 35 | + }, |
| 36 | + }, |
| 37 | +) |
| 38 | +type BadgeVariant = VariantProps<typeof badgeDotVariant> |
| 39 | +
|
| 40 | +const show = computed(() => { |
| 41 | + switch (typeof props.value) { |
| 42 | + case 'string': |
| 43 | + return props.value.length > 0 |
| 44 | + case 'number': |
| 45 | + return props.value > 0 |
| 46 | + case 'boolean': |
| 47 | + return props.value |
| 48 | + default: |
| 49 | + return props.value !== undefined && props.value !== null |
| 50 | + } |
| 51 | +}) |
| 52 | +
|
| 53 | +const transitionClass = ref({ |
| 54 | + enterActiveClass: 'ease-in-out duration-500', |
| 55 | + enterFromClass: 'opacity-0', |
| 56 | + enterToClass: 'opacity-100', |
| 57 | + leaveActiveClass: 'ease-in-out duration-500', |
| 58 | + leaveFromClass: 'opacity-100', |
| 59 | + leaveToClass: 'opacity-0', |
| 60 | +}) |
| 61 | +</script> |
| 62 | + |
| 63 | +<template> |
| 64 | + <div :class="cn('relative inline-flex', props.class)"> |
| 65 | + <slot /> |
| 66 | + <Transition v-bind="transitionClass"> |
| 67 | + <div v-if="show" class="h-full w-full absolute"> |
| 68 | + <div v-if="value === true" :class="badgeDotVariant({ variant })" /> |
| 69 | + <Badge v-else :variant :class="cn('absolute start-[50%] top-0 z-20 whitespace-nowrap px-1.5 py-0 ring-1 ring-primary-foreground -translate-y-[50%] hover:bg-none!', props.badgeClass)"> |
| 70 | + {{ value }} |
| 71 | + </Badge> |
| 72 | + </div> |
| 73 | + </Transition> |
| 74 | + </div> |
| 75 | +</template> |
0 commit comments