1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
---
name: create-component
description: Create reusable components using TailwindCSS utilities with proper patterns and best practices
tools: Write, Edit, Read, Grep, Glob
---
# Create TailwindCSS Component
This command helps create well-structured, reusable components using TailwindCSS utilities following best practices and design system patterns.
## What This Command Does
1. **Component Architecture**
- Creates component files with proper TailwindCSS utility composition
- Implements responsive design patterns
- Sets up proper TypeScript/PropTypes definitions
- Follows accessibility best practices
2. **Utility Composition**
- Uses semantic utility class combinations
- Implements proper state management (hover, focus, active)
- Creates responsive variants using breakpoint prefixes
- Follows mobile-first methodology
3. **Design System Integration**
- Uses design tokens from TailwindCSS configuration
- Implements consistent spacing and typography scales
- Applies proper color palette and semantic colors
- Follows component variant patterns
4. **Performance Optimization**
- Uses efficient utility combinations
- Optimizes for CSS purging
- Implements proper class composition strategies
- Avoids unnecessary custom CSS
## Component Templates
### Button Component
```jsx
// components/Button.jsx
import React from 'react'
import { cva } from 'class-variance-authority'
import { cn } from '@/lib/utils'
const buttonVariants = cva(
// Base styles
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline: "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link'
size?: 'default' | 'sm' | 'lg' | 'icon'
loading?: boolean
leftIcon?: React.ReactNode
rightIcon?: React.ReactNode
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, loading, leftIcon, rightIcon, children, ...props }, ref) => {
return (
<button
className={cn(buttonVariants({ variant, size }), className)}
ref={ref}
disabled={loading || props.disabled}
{...props}
>
{loading ? (
<svg className="mr-2 h-4 w-4 animate-spin" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" className="opacity-25" />
<path fill="currentColor" className="opacity-75" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
</svg>
) : leftIcon ? (
<span className="mr-2">{leftIcon}</span>
) : null}
{children}
{rightIcon && !loading && (
<span className="ml-2">{rightIcon}</span>
)}
</button>
)
}
)
Button.displayName = "Button"
export { Button, buttonVariants }
```
### Card Component
```jsx
// components/Card.jsx
import React from 'react'
import { cn } from '@/lib/utils'
const Card = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement> & {
hover?: boolean
padding?: 'none' | 'sm' | 'md' | 'lg'
}
>(({ className, hover = false, padding = 'md', children, ...props }, ref) => {
const paddingMap = {
none: '',
sm: 'p-4',
md: 'p-6',
lg: 'p-8'
}
return (
<div
ref={ref}
className={cn(
"rounded-lg border bg-card text-card-foreground shadow-sm",
hover && "transition-shadow hover:shadow-md",
paddingMap[padding],
className
)}
{...props}
>
{children}
</div>
)
})
const CardHeader = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex flex-col space-y-1.5 p-6", className)}
{...props}
/>
))
const CardTitle = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
<h3
ref={ref}
className={cn("text-2xl font-semibold leading-none tracking-tight", className)}
{...props}
/>
))
const CardDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<p
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
const CardContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("p-6 pt-0", className)} {...props} />
))
const CardFooter = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex items-center p-6 pt-0", className)}
{...props}
/>
))
Card.displayName = "Card"
CardHeader.displayName = "CardHeader"
CardTitle.displayName = "CardTitle"
CardDescription.displayName = "CardDescription"
CardContent.displayName = "CardContent"
CardFooter.displayName = "CardFooter"
export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }
```
### Input Component
```jsx
// components/Input.jsx
import React from 'react'
import { cva } from 'class-variance-authority'
import { cn } from '@/lib/utils'
const inputVariants = cva(
"flex w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
{
variants: {
size: {
sm: "h-8 px-2 text-xs",
default: "h-10 px-3",
lg: "h-12 px-4 text-base",
},
state: {
default: "",
error: "border-destructive focus-visible:ring-destructive",
success: "border-green-500 focus-visible:ring-green-500",
},
},
defaultVariants: {
size: "default",
state: "default",
},
}
)
export interface InputProps
extends React.InputHTMLAttributes<HTMLInputElement> {
size?: 'sm' | 'default' | 'lg'
state?: 'default' | 'error' | 'success'
label?: string
helperText?: string
error?: string
leftIcon?: React.ReactNode
rightIcon?: React.ReactNode
}
const Input = React.forwardRef<HTMLInputElement, InputProps>(
({
className,
type,
size,
state,
label,
helperText,
error,
leftIcon,
rightIcon,
...props
}, ref) => {
const inputState = error ? 'error' : state
return (
<div className="space-y-1">
{label && (
<label className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
{label}
</label>
)}
<div className="relative">
{leftIcon && (
<div className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground">
{leftIcon}
</div>
)}
<input
type={type}
className={cn(
inputVariants({ size, state: inputState }),
leftIcon && "pl-9",
rightIcon && "pr-9",
className
)}
ref={ref}
{...props}
/>
{rightIcon && (
<div className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground">
{rightIcon}
</div>
)}
</div>
{(helperText || error) && (
<p className={cn(
"text-xs",
error ? "text-destructive" : "text-muted-foreground"
)}>
{error || helperText}
</p>
)}
</div>
)
}
)
Input.displayName = "Input"
export { Input, inputVariants }
```
### Badge Component
```jsx
// components/Badge.jsx
import React from 'react'
import { cva } from 'class-variance-authority'
import { cn } from '@/lib/utils'
const badgeVariants = cva(
"inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
{
variants: {
variant: {
default: "border-transparent bg-primary text-primary-foreground hover:bg-primary/80",
secondary: "border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
destructive: "border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80",
success: "border-transparent bg-green-500 text-white hover:bg-green-600",
warning: "border-transparent bg-yellow-500 text-white hover:bg-yellow-600",
outline: "text-foreground",
},
size: {
sm: "px-2 py-0.5 text-xs",
default: "px-2.5 py-0.5 text-xs",
lg: "px-3 py-1 text-sm",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
export interface BadgeProps
extends React.HTMLAttributes<HTMLDivElement> {
variant?: 'default' | 'secondary' | 'destructive' | 'success' | 'warning' | 'outline'
size?: 'sm' | 'default' | 'lg'
removable?: boolean
onRemove?: () => void
}
const Badge = React.forwardRef<HTMLDivElement, BadgeProps>(
({ className, variant, size, removable, onRemove, children, ...props }, ref) => {
return (
<div
className={cn(badgeVariants({ variant, size }), className)}
ref={ref}
{...props}
>
{children}
{removable && (
<button
onClick={onRemove}
className="ml-1 -mr-1 rounded-full p-0.5 hover:bg-black/10 focus:outline-none"
aria-label="Remove badge"
>
<svg className="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M18 6L6 18M6 6l12 12" />
</svg>
</button>
)}
</div>
)
}
)
Badge.displayName = "Badge"
export { Badge, badgeVariants }
```
### Alert Component
```jsx
// components/Alert.jsx
import React from 'react'
import { cva } from 'class-variance-authority'
import { cn } from '@/lib/utils'
const alertVariants = cva(
"relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground",
{
variants: {
variant: {
default: "bg-background text-foreground",
destructive: "border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",
success: "border-green-500/50 text-green-700 dark:text-green-400 [&>svg]:text-green-600",
warning: "border-yellow-500/50 text-yellow-700 dark:text-yellow-400 [&>svg]:text-yellow-600",
info: "border-blue-500/50 text-blue-700 dark:text-blue-400 [&>svg]:text-blue-600",
},
},
defaultVariants: {
variant: "default",
},
}
)
const Alert = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement> & {
variant?: 'default' | 'destructive' | 'success' | 'warning' | 'info'
dismissible?: boolean
onDismiss?: () => void
}
>(({ className, variant, dismissible, onDismiss, children, ...props }, ref) => (
<div
ref={ref}
role="alert"
className={cn(alertVariants({ variant }), className)}
{...props}
>
{children}
{dismissible && (
<button
onClick={onDismiss}
className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2"
>
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M18 6L6 18M6 6l12 12" />
</svg>
<span className="sr-only">Close</span>
</button>
)}
</div>
))
const AlertTitle = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
<h5
ref={ref}
className={cn("mb-1 font-medium leading-none tracking-tight", className)}
{...props}
/>
))
const AlertDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("text-sm [&_p]:leading-relaxed", className)}
{...props}
/>
))
Alert.displayName = "Alert"
AlertTitle.displayName = "AlertTitle"
AlertDescription.displayName = "AlertDescription"
export { Alert, AlertTitle, AlertDescription }
```
## Layout Components
### Container Component
```jsx
// components/Container.jsx
import React from 'react'
import { cn } from '@/lib/utils'
export interface ContainerProps extends React.HTMLAttributes<HTMLDivElement> {
size?: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'full'
padding?: boolean
}
const Container = React.forwardRef<HTMLDivElement, ContainerProps>(
({ className, size = 'lg', padding = true, ...props }, ref) => {
const sizeClasses = {
sm: 'max-w-2xl',
md: 'max-w-4xl',
lg: 'max-w-6xl',
xl: 'max-w-7xl',
'2xl': 'max-w-8xl',
full: 'max-w-full'
}
return (
<div
ref={ref}
className={cn(
'mx-auto',
sizeClasses[size],
padding && 'px-4 sm:px-6 lg:px-8',
className
)}
{...props}
/>
)
}
)
Container.displayName = 'Container'
export { Container }
```
### Grid Component
```jsx
// components/Grid.jsx
import React from 'react'
import { cn } from '@/lib/utils'
export interface GridProps extends React.HTMLAttributes<HTMLDivElement> {
cols?: 1 | 2 | 3 | 4 | 5 | 6 | 12
gap?: 'none' | 'sm' | 'md' | 'lg' | 'xl'
responsive?: boolean
}
const Grid = React.forwardRef<HTMLDivElement, GridProps>(
({ className, cols = 1, gap = 'md', responsive = true, ...props }, ref) => {
const gapClasses = {
none: 'gap-0',
sm: 'gap-2',
md: 'gap-4',
lg: 'gap-6',
xl: 'gap-8'
}
const getResponsiveCols = (cols: number) => {
if (!responsive) return `grid-cols-${cols}`
switch (cols) {
case 1: return 'grid-cols-1'
case 2: return 'grid-cols-1 md:grid-cols-2'
case 3: return 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3'
case 4: return 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4'
case 5: return 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-5'
case 6: return 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6'
case 12: return 'grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 2xl:grid-cols-12'
default: return `grid-cols-${cols}`
}
}
return (
<div
ref={ref}
className={cn(
'grid',
getResponsiveCols(cols),
gapClasses[gap],
className
)}
{...props}
/>
)
}
)
Grid.displayName = 'Grid'
export { Grid }
```
## Utility Functions
### Class Name Utility
```typescript
// lib/utils.ts
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
// Responsive utility
export function responsive(
base: string,
sm?: string,
md?: string,
lg?: string,
xl?: string,
xxl?: string
) {
return cn(
base,
sm && `sm:${sm}`,
md && `md:${md}`,
lg && `lg:${lg}`,
xl && `xl:${xl}`,
xxl && `2xl:${xxl}`
)
}
// Focus ring utility
export function focusRing(color: string = 'ring-primary') {
return `focus:outline-none focus:ring-2 ${color} focus:ring-offset-2`
}
```
## Component Generation Script
### Auto-generate Component
```javascript
// scripts/create-component.js
const fs = require('fs')
const path = require('path')
function createComponent(name, type = 'basic') {
const componentName = name.charAt(0).toUpperCase() + name.slice(1)
const fileName = `${componentName}.tsx`
const componentDir = `./components/${componentName}`
// Create component directory
if (!fs.existsSync(componentDir)) {
fs.mkdirSync(componentDir, { recursive: true })
}
const templates = {
basic: basicComponentTemplate,
form: formComponentTemplate,
layout: layoutComponentTemplate,
interactive: interactiveComponentTemplate
}
const template = templates[type] || templates.basic
const componentCode = template(componentName, name)
// Write component file
fs.writeFileSync(path.join(componentDir, fileName), componentCode)
// Create index file
const indexContent = `export { ${componentName} } from './${componentName}'\nexport type { ${componentName}Props } from './${componentName}'`
fs.writeFileSync(path.join(componentDir, 'index.ts'), indexContent)
console.log(`✅ Component ${componentName} created successfully!`)
console.log(`📁 Location: ${componentDir}`)
console.log(`📝 Files created:`)
console.log(` - ${fileName}`)
console.log(` - index.ts`)
}
function basicComponentTemplate(componentName, kebabName) {
return `import React from 'react'
import { cn } from '@/lib/utils'
export interface ${componentName}Props extends React.HTMLAttributes<HTMLDivElement> {
variant?: 'default' | 'secondary'
size?: 'sm' | 'md' | 'lg'
}
const ${componentName} = React.forwardRef<HTMLDivElement, ${componentName}Props>(
({ className, variant = 'default', size = 'md', children, ...props }, ref) => {
const variants = {
default: 'bg-background text-foreground',
secondary: 'bg-secondary text-secondary-foreground'
}
const sizes = {
sm: 'p-2 text-sm',
md: 'p-4 text-base',
lg: 'p-6 text-lg'
}
return (
<div
ref={ref}
className={cn(
'rounded-lg border transition-colors',
variants[variant],
sizes[size],
className
)}
{...props}
>
{children}
</div>
)
}
)
${componentName}.displayName = '${componentName}'
export { ${componentName} }
`
}
// Usage: node scripts/create-component.js MyComponent basic
const [,, name, type] = process.argv
if (!name) {
console.error('Please provide a component name')
process.exit(1)
}
createComponent(name, type)
```
Remember: **Focus on utility composition, responsive design, accessibility, and performance optimization when creating TailwindCSS components!**
|