badge.svelte 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <script lang="ts" module>
  2. import { type VariantProps, tv } from "tailwind-variants";
  3. export const badgeVariants = tv({
  4. base: "focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive inline-flex w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-full border px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-[color,box-shadow] focus-visible:ring-[3px] [&>svg]:pointer-events-none [&>svg]:size-3",
  5. variants: {
  6. variant: {
  7. default:
  8. "bg-primary text-primary-foreground [a&]:hover:bg-primary/90 border-transparent",
  9. secondary:
  10. "bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90 border-transparent",
  11. destructive:
  12. "bg-destructive [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/70 border-transparent text-white",
  13. outline: "text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground",
  14. },
  15. },
  16. defaultVariants: {
  17. variant: "default",
  18. },
  19. });
  20. export type BadgeVariant = VariantProps<typeof badgeVariants>["variant"];
  21. </script>
  22. <script lang="ts">
  23. import type { HTMLAnchorAttributes } from "svelte/elements";
  24. import { cn, type WithElementRef } from "$lib/utils.js";
  25. let {
  26. ref = $bindable(null),
  27. href,
  28. class: className,
  29. variant = "default",
  30. children,
  31. ...restProps
  32. }: WithElementRef<HTMLAnchorAttributes> & {
  33. variant?: BadgeVariant;
  34. } = $props();
  35. </script>
  36. <svelte:element
  37. this={href ? "a" : "span"}
  38. bind:this={ref}
  39. data-slot="badge"
  40. {href}
  41. class={cn(badgeVariants({ variant }), className)}
  42. {...restProps}
  43. >
  44. {@render children?.()}
  45. </svelte:element>