@@ -8,42 +8,120 @@ import { CCloseButton } from '../close-button/CCloseButton'
8
8
import { useForkedRef } from '../../hooks'
9
9
import { colorPropType } from '../../props'
10
10
import type { Colors } from '../../types'
11
+ import { mergeClassNames } from '../../utils'
11
12
12
13
export interface CAlertProps extends HTMLAttributes < HTMLDivElement > {
13
14
/**
14
- * A string of all className you want applied to the component.
15
+ * Apply a CSS fade transition to the alert.
16
+ *
17
+ * @since 5.5.0
18
+ *
19
+ * @example
20
+ * <CAlert animation={false} color="success">No animation alert</CAlert>
21
+ */
22
+ animation ?: boolean
23
+
24
+ /**
25
+ * A string of additional CSS class names to apply to the alert component.
26
+ *
27
+ * @example
28
+ * <CAlert className="my-custom-class" color="danger">Custom Class Alert</CAlert>
15
29
*/
16
30
className ?: string
31
+
17
32
/**
18
33
* Sets the color context of the component to one of CoreUI’s themed colors.
19
34
*
20
35
* @type 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string
36
+ *
37
+ * @example
38
+ * <CAlert color="warning">Warning Alert</CAlert>
21
39
*/
22
40
color : Colors
41
+
23
42
/**
24
- * Optionally add a close button to alert and allow it to self dismiss.
43
+ * Custom class names to override or extend the default CSS classes used within the `CAlert` component.
44
+ * Each key corresponds to a specific part of the Alert component, allowing for granular styling control.
45
+ *
46
+ * @since v5.5.0
47
+ *
48
+ * @example
49
+ * const customClasses = {
50
+ * ALERT: 'my-custom-alert',
51
+ * ALERT_DISMISSIBLE: 'my-custom-dismissible',
52
+ * }
53
+ *
54
+ * <CAlert customClassNames={customClasses} />
55
+ */
56
+ customClassNames ?: Partial < typeof ALERT_CLASS_NAMES >
57
+
58
+ /**
59
+ * Optionally add a close button to the alert and allow it to self-dismiss.
60
+ *
61
+ * @example
62
+ * <CAlert dismissible color="success">
63
+ * Dismissible Alert
64
+ * </CAlert>
25
65
*/
26
66
dismissible ?: boolean
67
+
27
68
/**
28
- * Callback fired when the component requests to be closed.
69
+ * Callback fired when the alert requests to be closed. This occurs when the close button is clicked.
70
+ *
71
+ * @example
72
+ * const handleClose = () => {
73
+ * console.log('Alert closed')
74
+ * }
75
+ *
76
+ * <CAlert dismissible color="danger" onClose={handleClose}>
77
+ * Dismissible Alert with Callback
78
+ * </CAlert>
29
79
*/
30
80
onClose ?: ( ) => void
81
+
31
82
/**
32
- * Set the alert variant to a solid.
83
+ * Set the alert variant to a solid background. This changes the alert's appearance to have a solid color.
84
+ *
85
+ * @example
86
+ * <CAlert variant="solid" color="primary">
87
+ * Solid Variant Alert
88
+ * </CAlert>
33
89
*/
34
90
variant ?: 'solid' | string
91
+
35
92
/**
36
- * Toggle the visibility of component.
93
+ * Toggle the visibility of the alert component. When set to `false`, the alert will be hidden.
94
+ *
95
+ * @example
96
+ * const [visible, setVisible] = useState(true)
97
+ *
98
+ * <CAlert visible={visible} onClose={() => setVisible(false)} color="info">
99
+ * Toggleable Alert
100
+ * </CAlert>
37
101
*/
38
102
visible ?: boolean
39
103
}
40
104
105
+ export const ALERT_CLASS_NAMES = {
106
+ /**
107
+ * Base class for the alert container.
108
+ */
109
+ ALERT : 'alert' ,
110
+
111
+ /**
112
+ * Applied when the `dismissible` prop is enabled.
113
+ */
114
+ ALERT_DISMISSIBLE : 'alert-dismissible' ,
115
+ }
116
+
41
117
export const CAlert = forwardRef < HTMLDivElement , CAlertProps > (
42
118
(
43
119
{
44
120
children,
121
+ animation = true ,
45
122
className,
46
123
color = 'primary' ,
124
+ customClassNames,
47
125
dismissible,
48
126
variant,
49
127
visible = true ,
@@ -60,6 +138,11 @@ export const CAlert = forwardRef<HTMLDivElement, CAlertProps>(
60
138
setVisible ( visible )
61
139
} , [ visible ] )
62
140
141
+ const mergedClassNames = mergeClassNames < typeof ALERT_CLASS_NAMES > (
142
+ ALERT_CLASS_NAMES ,
143
+ customClassNames ,
144
+ )
145
+
63
146
return (
64
147
< Transition
65
148
in = { _visible }
@@ -72,10 +155,11 @@ export const CAlert = forwardRef<HTMLDivElement, CAlertProps>(
72
155
{ ( state ) => (
73
156
< div
74
157
className = { classNames (
75
- 'alert' ,
158
+ mergedClassNames . ALERT ,
76
159
variant === 'solid' ? `bg-${ color } text-white` : `alert-${ color } ` ,
77
160
{
78
- 'alert-dismissible fade' : dismissible ,
161
+ [ mergedClassNames . ALERT_DISMISSIBLE ] : dismissible ,
162
+ fade : animation ,
79
163
show : state === 'entered' ,
80
164
} ,
81
165
className ,
@@ -94,9 +178,11 @@ export const CAlert = forwardRef<HTMLDivElement, CAlertProps>(
94
178
)
95
179
96
180
CAlert . propTypes = {
181
+ animation : PropTypes . bool ,
97
182
children : PropTypes . node ,
98
183
className : PropTypes . string ,
99
184
color : colorPropType . isRequired ,
185
+ customClassNames : PropTypes . object ,
100
186
dismissible : PropTypes . bool ,
101
187
onClose : PropTypes . func ,
102
188
variant : PropTypes . string ,
0 commit comments