|
| 1 | +--- |
| 2 | +created: 2024-12-03 |
| 3 | +authors: |
| 4 | + - Nishant |
| 5 | +--- |
| 6 | +This layer contains the the components and styles used for IC Hack 25. At this point, [Class Variance Authority (CVA)](https://cva.style/docs) is unstable and experimental but it seems we don't need alot of functionality from them. You may need **tailwind-merge**. In case of failure, here's how you would convert the following CVA code to use custom props (untested). |
| 7 | + |
| 8 | +```html |
| 9 | +<script setup lang="ts"> |
| 10 | +import { cva, type VariantProps } from 'cva'; |
| 11 | +
|
| 12 | +const button = cva('px-1 py-2', { |
| 13 | + variants: { |
| 14 | + intent: { |
| 15 | + primary: 'bg-blue-500 text-white', |
| 16 | + secondary: 'bg-gray-500 text-white', |
| 17 | + success: 'bg-green-500 text-white', |
| 18 | + danger: 'bg-red-500 text-white' |
| 19 | + }, |
| 20 | + size: { |
| 21 | + sm: 'text-sm', |
| 22 | + md: 'text-md', |
| 23 | + lg: 'text-lg' |
| 24 | + } |
| 25 | + } |
| 26 | +}); |
| 27 | +type ButtonVariant = VariantProps<typeof button>; |
| 28 | +
|
| 29 | +const { intent = 'primary', size = 'sm' } = defineProps<ButtonVariant>(); |
| 30 | +</script> |
| 31 | + |
| 32 | +<template> |
| 33 | + <button :class="button({ intent, size })"><slot></slot></button> |
| 34 | +</template> |
| 35 | +``` |
| 36 | + |
| 37 | +This code becomes: |
| 38 | + |
| 39 | +```html |
| 40 | +<script setup lang="ts"> |
| 41 | +import { twMerge } from 'tailwind-merge'; // Something like this |
| 42 | +
|
| 43 | +type Variant = { |
| 44 | + intent: 'primary' | 'secondary' | 'success' | 'danger' |
| 45 | + size: 'sm' | 'md' | 'lg' |
| 46 | +} |
| 47 | +
|
| 48 | +const button = (variant: Variant) => { |
| 49 | + const base = 'px-1 py-2'; |
| 50 | + const intentClasses = { |
| 51 | + primary: 'bg-blue-500 text-white', |
| 52 | + secondary: 'bg-gray-500 text-white', |
| 53 | + success: 'bg-green-500 text-white', |
| 54 | + danger: 'bg-red-500 text-white' |
| 55 | + }; |
| 56 | + const sizeClasses = { |
| 57 | + sm: 'text-sm', |
| 58 | + md: 'text-md', |
| 59 | + lg: 'text-lg' |
| 60 | + }; |
| 61 | +
|
| 62 | + return twMerge(`${base} ${intentClasses[variant.intent]} ${sizeClasses[variant.size]}`) |
| 63 | +} |
| 64 | +
|
| 65 | +const { intent = 'primary', size = 'sm' } = defineProps<Variant>(); |
| 66 | +</script> |
| 67 | + |
| 68 | +<template> |
| 69 | + <button :class="button({ intent, size })"><slot></slot></button> |
| 70 | +</template> |
| 71 | +``` |
0 commit comments