The Coline design system: accessible React components for Coline Apps, themed live by the host workspace, and for Coline Sites in light and dark. Tailwind v4 stylesheet included. MIT.
| 6 | import { Label } from "./label" |
| 7 | import { Separator } from "./separator" |
| 8 |
| 9 | function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) { |
| 10 | return ( |
| 11 | <fieldset |
| 12 | data-slot="field-set" |
| 13 | className={cn( |
| 14 | "flex flex-col gap-6 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3", |
| 15 | className |
| 16 | )} |
| 17 | {...props} |
| 18 | /> |
| 19 | ) |
| 20 | } |
| 21 |
| 22 | function FieldLegend({ |
| 23 | className, |
| 24 | variant = "legend", |
| 25 | ...props |
| 26 | }: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) { |
| 27 | return ( |
| 28 | <legend |
| 29 | data-slot="field-legend" |
| 30 | data-variant={variant} |
| 31 | className={cn( |
| 32 | "mb-3 font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base", |
| 33 | className |
| 34 | )} |
| 35 | {...props} |
| 36 | /> |
| 37 | ) |
| 38 | } |
| 39 |
| 40 | function FieldGroup({ className, ...props }: React.ComponentProps<"div">) { |
| 41 | return ( |
| 42 | <div |
| 43 | data-slot="field-group" |
| 44 | className={cn( |
| 45 | "group/field-group @container/field-group flex w-full flex-col gap-7 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4", |
| 46 | className |
| 47 | )} |
| 48 | {...props} |
| 49 | /> |
| 50 | ) |
| 51 | } |
| 52 |
| 53 | const fieldVariants = cva( |
| 54 | "group/field flex w-full gap-3 data-[invalid=true]:text-destructive", |
| 55 | { |
| 56 | variants: { |
| 57 | orientation: { |
| 58 | vertical: "flex-col *:w-full [&>.sr-only]:w-auto", |
| 59 | horizontal: |
| 60 | "flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px", |
| 61 | responsive: |
| 62 | "flex-col *:w-full @md/field-group:flex-row @md/field-group:items-center @md/field-group:*:w-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px", |
| 63 | }, |
| 64 | }, |
| 65 | defaultVariants: { |
| 66 | orientation: "vertical", |
| 67 | }, |
| 68 | } |
| 69 | ) |
| 70 |
| 71 | function Field({ |
| 72 | className, |
| 73 | orientation = "vertical", |
| 74 | ...props |
| 75 | }: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) { |
| 76 | return ( |
| 77 | <div |
| 78 | role="group" |
| 79 | data-slot="field" |
| 80 | data-orientation={orientation} |
| 81 | className={cn(fieldVariants({ orientation }), className)} |
| 82 | {...props} |
| 83 | /> |
| 84 | ) |
| 85 | } |
| 86 |
| 87 | function FieldContent({ className, ...props }: React.ComponentProps<"div">) { |
| 88 | return ( |
| 89 | <div |
| 90 | data-slot="field-content" |
| 91 | className={cn( |
| 92 | "group/field-content flex flex-1 flex-col gap-1 leading-snug", |
| 93 | className |
| 94 | )} |
| 95 | {...props} |
| 96 | /> |
| 97 | ) |
| 98 | } |
| 99 |
| 100 | function FieldLabel({ |
| 101 | className, |
| 102 | ...props |
| 103 | }: React.ComponentProps<typeof Label>) { |
| 104 | return ( |
| 105 | <Label |
| 106 | data-slot="field-label" |
| 107 | className={cn( |
| 108 | "group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:opacity-50 has-data-[state=checked]:border-primary/30 has-data-[state=checked]:bg-primary/5 has-[>[data-slot=field]]:rounded-xl has-[>[data-slot=field]]:border *:data-[slot=field]:p-4 dark:has-data-[state=checked]:border-primary/20 dark:has-data-[state=checked]:bg-primary/10", |
| 109 | "has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col", |
| 110 | className |
| 111 | )} |
| 112 | {...props} |
| 113 | /> |
| 114 | ) |
| 115 | } |
| 116 |
| 117 | function FieldTitle({ className, ...props }: React.ComponentProps<"div">) { |
| 118 | return ( |
| 119 | <div |
| 120 | data-slot="field-label" |
| 121 | className={cn( |
| 122 | "flex w-fit items-center gap-2 text-sm leading-snug font-medium group-data-[disabled=true]/field:opacity-50", |
| 123 | className |
| 124 | )} |
| 125 | {...props} |
| 126 | /> |
| 127 | ) |
| 128 | } |
| 129 |
| 130 | function FieldDescription({ className, ...props }: React.ComponentProps<"p">) { |
| 131 | return ( |
| 132 | <p |
| 133 | data-slot="field-description" |
| 134 | className={cn( |
| 135 | "text-left text-sm leading-normal font-normal text-muted-foreground group-has-data-[orientation=horizontal]/field:text-balance [[data-variant=legend]+&]:-mt-1.5", |
| 136 | "last:mt-0 nth-last-2:-mt-1", |
| 137 | "[&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary", |
| 138 | className |
| 139 | )} |
| 140 | {...props} |
| 141 | /> |
| 142 | ) |
| 143 | } |
| 144 |
| 145 | function FieldSeparator({ |
| 146 | children, |
| 147 | className, |
| 148 | ...props |
| 149 | }: React.ComponentProps<"div"> & { |
| 150 | children?: React.ReactNode |
| 151 | }) { |
| 152 | return ( |
| 153 | <div |
| 154 | data-slot="field-separator" |
| 155 | data-content={!!children} |
| 156 | className={cn( |
| 157 | "relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2", |
| 158 | className |
| 159 | )} |
| 160 | {...props} |
| 161 | > |
| 162 | <Separator className="absolute inset-0 top-1/2" /> |
| 163 | {children && ( |
| 164 | <span |
| 165 | className="relative mx-auto block w-fit bg-background px-2 text-muted-foreground" |
| 166 | data-slot="field-separator-content" |
| 167 | > |
| 168 | {children} |
| 169 | </span> |
| 170 | )} |
| 171 | </div> |
| 172 | ) |
| 173 | } |
| 174 |
| 175 | function FieldError({ |
| 176 | className, |
| 177 | children, |
| 178 | errors, |
| 179 | ...props |
| 180 | }: React.ComponentProps<"div"> & { |
| 181 | errors?: Array<{ message?: string } | undefined> |
| 182 | }) { |
| 183 | const uniqueErrors = errors?.length |
| 184 | ? [...new Map(errors.map((error) => [error?.message, error])).values()] |
| 185 | : [] |
| 186 |
| 187 | const content = children |
| 188 | ?? (uniqueErrors.length === 1 |
| 189 | ? uniqueErrors[0]?.message |
| 190 | : uniqueErrors.length > 1 |
| 191 | ? ( |
| 192 | <ul className="ml-4 flex list-disc flex-col gap-1"> |
| 193 | {uniqueErrors.map( |
| 194 | (error, index) => |
| 195 | error?.message && <li key={index}>{error.message}</li> |
| 196 | )} |
| 197 | </ul> |
| 198 | ) |
| 199 | : null) |
| 200 |
| 201 | if (!content) { |
| 202 | return null |
| 203 | } |
| 204 |
| 205 | return ( |
| 206 | <div |
| 207 | role="alert" |
| 208 | data-slot="field-error" |
| 209 | className={cn("text-sm font-normal text-destructive", className)} |
| 210 | {...props} |
| 211 | > |
| 212 | {content} |
| 213 | </div> |
| 214 | ) |
| 215 | } |
| 216 |
| 217 | export { |
| 218 | Field, |
| 219 | FieldLabel, |
| 220 | FieldDescription, |
| 221 | FieldError, |
| 222 | FieldGroup, |
| 223 | FieldLegend, |
| 224 | FieldSeparator, |
| 225 | FieldSet, |
| 226 | FieldContent, |
| 227 | FieldTitle, |
| 228 | } |
| 229 |