af126c7feat: tailwind theme and the first components25d | 1 | use topcoat::{ |
| 2 | Result, |
| 3 | view::{Attributes, View, class, component, view}, |
| 4 | }; |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 10 | #[allow(dead_code)] |
| 11 | pub enum ButtonVariant { |
| 12 | |
| 13 | #[default] |
| 14 | Primary, |
| 15 | |
| 16 | Secondary, |
| 17 | |
| 18 | Outline, |
| 19 | |
| 20 | Ghost, |
| 21 | |
| 22 | Destructive, |
| 23 | } |
| 24 | |
| 25 | impl ButtonVariant { |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | fn classes(self) -> &'static str { |
| 39 | match self { |
| 40 | Self::Primary => { |
| 41 | "border-transparent bg-primary text-primary-foreground shadow-xs \ |
| 42 | hover:bg-primary/90 active:bg-primary/80" |
| 43 | } |
| 44 | Self::Secondary => { |
| 45 | "border-transparent bg-foreground/5 text-foreground shadow-xs \ |
| 46 | hover:bg-foreground/10 active:bg-foreground/15" |
| 47 | } |
| 48 | Self::Outline => { |
| 49 | "border-border text-foreground shadow-xs hover:bg-foreground/5 \ |
| 50 | active:bg-foreground/10" |
| 51 | } |
| 52 | Self::Ghost => { |
| 53 | "border-transparent text-foreground hover:bg-foreground/5 active:bg-foreground/10" |
| 54 | } |
| 55 | Self::Destructive => { |
| 56 | "border-transparent bg-destructive text-destructive-foreground shadow-xs \ |
| 57 | hover:bg-destructive/90 active:bg-destructive/80" |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 67 | #[allow(dead_code)] |
| 68 | pub enum ButtonSize { |
| 69 | |
| 70 | Sm, |
| 71 | |
| 72 | #[default] |
| 73 | Md, |
| 74 | |
| 75 | Lg, |
| 76 | |
| 77 | Icon, |
| 78 | } |
| 79 | |
| 80 | impl ButtonSize { |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | fn classes(self) -> &'static str { |
| 86 | match self { |
| 87 | Self::Sm => "h-8 gap-1.5 rounded-md px-3 text-xs", |
| 88 | Self::Md => "h-9 gap-2 rounded-lg px-4 text-sm", |
| 89 | Self::Lg => "h-10 gap-2 rounded-lg px-5 text-base", |
| 90 | Self::Icon => "size-9 rounded-lg text-base", |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | const BASE: &str = "inline-flex shrink-0 items-center justify-center border \ |
| 100 | font-medium whitespace-nowrap transition-colors outline-none select-none \ |
| 101 | focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 \ |
| 102 | focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-50"; |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | #[must_use] |
| 117 | pub fn button_variants(variant: ButtonVariant, size: ButtonSize) -> String { |
| 118 | format!("{BASE} {} {}", variant.classes(), size.classes()) |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[component] |
| 142 | pub async fn button( |
| 143 | #[default] variant: ButtonVariant, |
| 144 | #[default] size: ButtonSize, |
| 145 | #[default] mut attrs: Attributes, |
| 146 | #[default] child: View, |
| 147 | ) -> Result { |
| 148 | view! { |
| 149 | <button |
| 150 | class=(class!( |
| 151 | BASE, |
| 152 | variant.classes(), |
| 153 | size.classes(), |
| 154 | attrs.remove("class"), |
| 155 | )) |
| 156 | (attrs) |
| 157 | > |
| 158 | (child) |
| 159 | </button> |
| 160 | } |
| 161 | } |