@@ -6,101 +6,122 @@ import { ReactNode } from 'react';
6
6
* The variants control the badge's appearance and color.
7
7
*/
8
8
export enum BadgeVariant {
9
- default = " default" ,
10
- error = " error" ,
11
- success = " success" ,
12
- warning = " warning" ,
13
- reference = " reference" ,
9
+ default = ' default' ,
10
+ error = ' error' ,
11
+ success = ' success' ,
12
+ warning = ' warning' ,
13
+ reference = ' reference' ,
14
14
}
15
15
16
16
/**
17
17
* Props interface for the Badge component.
18
18
* Defines the expected properties that can be passed into the component.
19
19
*/
20
20
interface BadgeProps {
21
+ /**
22
+ * The content to be displayed inside the badge.
23
+ * It can be text, icons, or other React components.
24
+ */
25
+ children : ReactNode ;
21
26
22
- /**
23
- * The content to be displayed inside the badge.
24
- * It can be text, icons, or other React components .
25
- */
26
- children : ReactNode ;
27
+ /**
28
+ * Defines the style variant for the badge.
29
+ * Controls the background and text color (default: 'default') .
30
+ */
31
+ variant ?: BadgeVariant ;
27
32
28
- /**
29
- * Defines the style variant for the badge.
30
- * Controls the background and text color (default: 'default') .
31
- */
32
- variant ?: BadgeVariant ;
33
+ /**
34
+ * Custom background color for the badge.
35
+ * Can be a Tailwind CSS color class or custom class .
36
+ */
37
+ background ?: string ;
33
38
34
- /**
35
- * Custom background color for the badge.
36
- * Can be a Tailwind CSS color class or custom class.
37
- */
38
- background ?: string ;
39
+ /**
40
+ * Custom text color for the badge.
41
+ * Can be a Tailwind CSS color class or custom class.
42
+ */
43
+ textColor ?: string ;
39
44
40
- /**
41
- * Custom text color for the badge.
42
- * Can be a Tailwind CSS color class or custom class.
43
- */
44
- textColor ?: string ;
45
-
46
- /**
47
- * Additional custom CSS classes to apply to the badge element.
48
- * This is useful for applying further customization beyond the predefined styles.
49
- */
50
- className ?: string ;
45
+ /**
46
+ * Additional custom CSS classes to apply to the badge element.
47
+ * This is useful for applying further customization beyond the predefined styles.
48
+ */
49
+ className ?: string ;
51
50
}
52
51
53
52
/**
54
53
* Badge component that renders a styled badge with text or other content.
55
54
* The badge's appearance is controlled via variants, background, and text color.
56
- *
55
+ *
57
56
* @param {BadgeProps } props - The props for the Badge component.
58
57
* @returns {JSX.Element } The rendered Badge component.
59
58
*/
60
59
const Badge : React . FC < BadgeProps > = ( {
61
- children,
62
- variant = BadgeVariant . default ,
63
- background = 'bg-gray-200' ,
64
- textColor = 'text-gray-800' ,
65
- className = '' ,
60
+ children,
61
+ variant = BadgeVariant . default ,
62
+ background = 'bg-gray-200' ,
63
+ textColor = 'text-gray-800' ,
64
+ className = '' ,
66
65
} ) => {
66
+ /**
67
+ * A mapping of badge variants to their corresponding background and text color styles.
68
+ * This is used to determine the appearance of the badge based on the `variant` prop.
69
+ */
70
+ const variantStyles : Record <
71
+ BadgeVariant ,
72
+ { background : string ; textColor : string }
73
+ > = {
74
+ [ BadgeVariant . default ] : {
75
+ background : 'bg-shark-800' ,
76
+ textColor : 'text-shark-400' ,
77
+ } ,
78
+ [ BadgeVariant . error ] : {
79
+ background : 'bg-tamarind-800' ,
80
+ textColor : 'text-tamarind-400' ,
81
+ } ,
82
+ [ BadgeVariant . success ] : {
83
+ background : 'bg-bush-800' ,
84
+ textColor : 'text-bush-400' ,
85
+ } ,
86
+ [ BadgeVariant . warning ] : {
87
+ background : 'bg-clinker-800' ,
88
+ textColor : 'text-clinker-400' ,
89
+ } ,
90
+ [ BadgeVariant . reference ] : {
91
+ background : 'bg-deep-teal-800' ,
92
+ textColor : 'text-deep-teal-400' ,
93
+ } ,
94
+ } ;
67
95
68
- /**
69
- * A mapping of badge variants to their corresponding background and text color styles.
70
- * This is used to determine the appearance of the badge based on the `variant` prop.
71
- */
72
- const variantStyles : Record <
73
- BadgeVariant ,
74
- { background : string ; textColor : string }
75
- > = {
76
- [ BadgeVariant . default ] : { background : 'bg-shark-800' , textColor : 'text-shark-400' } ,
77
- [ BadgeVariant . error ] : { background : 'bg-tamarind-800' , textColor : 'text-tamarind-400' } ,
78
- [ BadgeVariant . success ] : { background : 'bg-bush-800' , textColor : 'text-bush-400' } ,
79
- [ BadgeVariant . warning ] : { background : 'bg-clinker-800' , textColor : 'text-clinker-400' } ,
80
- [ BadgeVariant . reference ] : { background : 'bg-deep-teal-800' , textColor : 'text-deep-teal-400' } ,
81
- } ;
82
-
83
- /**
84
- * Determine the resolved background color for the badge.
85
- * If a custom background is provided, it overrides the default variant background.
86
- */
87
- const resolvedBackground = background !== 'bg-gray-200' ? background : variantStyles [ variant ] . background ;
96
+ /**
97
+ * Determine the resolved background color for the badge.
98
+ * If a custom background is provided, it overrides the default variant background.
99
+ */
100
+ const resolvedBackground =
101
+ background !== 'bg-gray-200'
102
+ ? background
103
+ : variantStyles [ variant ] . background ;
88
104
89
- /**
90
- * Determine the resolved text color for the badge.
91
- * If a custom text color is provided, it overrides the default variant text color.
92
- */
93
- const resolvedTextColor = textColor !== 'text-gray-800' ? textColor : variantStyles [ variant ] . textColor ;
105
+ /**
106
+ * Determine the resolved text color for the badge.
107
+ * If a custom text color is provided, it overrides the default variant text color.
108
+ */
109
+ const resolvedTextColor =
110
+ textColor !== 'text-gray-800'
111
+ ? textColor
112
+ : variantStyles [ variant ] . textColor ;
94
113
95
- /**
96
- * Renders the badge as a span element with dynamic classes for styling.
97
- * The classes include padding, rounded corners, font size, and the background and text colors determined above.
98
- */
99
- return (
100
- < span className = { `inline-block px-3 py-1 rounded-full text-sm font-semibold ${ resolvedBackground } ${ resolvedTextColor } ${ className } ` } >
101
- { children }
102
- </ span >
103
- ) ;
114
+ /**
115
+ * Renders the badge as a span element with dynamic classes for styling.
116
+ * The classes include padding, rounded corners, font size, and the background and text colors determined above.
117
+ */
118
+ return (
119
+ < span
120
+ className = { `inline-block px-3 py-1 rounded-full text-sm font-semibold ${ resolvedBackground } ${ resolvedTextColor } ${ className } ` }
121
+ >
122
+ { children }
123
+ </ span >
124
+ ) ;
104
125
} ;
105
126
106
- export { Badge } ;
127
+ export { Badge } ;
0 commit comments