@@ -2,25 +2,78 @@ import React, { createContext, forwardRef, HTMLAttributes, useState } from 'reac
2
2
import PropTypes from 'prop-types'
3
3
import classNames from 'classnames'
4
4
5
+ import { mergeClassNames } from '../../utils'
6
+
5
7
export interface CAccordionProps extends HTMLAttributes < HTMLDivElement > {
6
8
/**
7
- * The active item key.
9
+ * Determines which accordion item is currently active (expanded) by default.
10
+ * Accepts a number or string corresponding to the `itemKey` of the desired accordion item.
11
+ *
12
+ * @example
13
+ * <CAccordion activeItemKey="1">...</CAccordion>
8
14
*/
9
15
activeItemKey ?: number | string
16
+
10
17
/**
11
- * Make accordion items stay open when another item is opened
18
+ * When set to `true`, multiple accordion items within the React Accordion can be open simultaneously.
19
+ * This is ideal for scenarios where users need to view multiple sections at once without collapsing others.
20
+ *
21
+ * @default false
22
+ *
23
+ * @example
24
+ * <CAccordion alwaysOpen>...</CAccordion>
12
25
*/
13
26
alwaysOpen ?: boolean
27
+
14
28
/**
15
- * A string of all className you want applied to the base component.
29
+ * Allows you to apply custom CSS classes to the React Accordion for enhanced styling and theming.
30
+ *
31
+ * @example
32
+ * <CAccordion className="my-custom-accordion">...</CAccordion>
16
33
*/
17
34
className ?: string
35
+
18
36
/**
19
- * Removes the default background-color, some borders, and some rounded corners to render accordions edge-to-edge with their parent container.
37
+ * Allows overriding or extending the default CSS class names used in the component.
38
+ *
39
+ * - `ACCORDION`: Base class for the accordion component.
40
+ * - `ACCORDION_FLUSH`: Class applied when the `flush` prop is set to true, ensuring an edge-to-edge layout.
41
+ *
42
+ * Use this prop to customize the styles of specific parts of the accordion.
43
+ *
44
+ * @example
45
+ * const customClasses = {
46
+ * ACCORDION: 'custom-accordion',
47
+ * ACCORDION_FLUSH: 'custom-accordion-flush'
48
+ * }
49
+ * <CAccordion customClassNames={customClasses}>...</CAccordion>
50
+ */
51
+ customClassNames ?: Partial < typeof ACCORDION_CLASS_NAMES >
52
+
53
+ /**
54
+ * When `flush` is set to `true`, the React Accordion renders edge-to-edge with its parent container,
55
+ * creating a seamless and modern look ideal for minimalist designs.
56
+ *
57
+ * @default false
58
+ *
59
+ * @example
60
+ * <CAccordion flush>...</CAccordion>
20
61
*/
21
62
flush ?: boolean
22
63
}
23
64
65
+ export const ACCORDION_CLASS_NAMES = {
66
+ /**
67
+ * Base class for the accordion container.
68
+ */
69
+ ACCORDION : 'accordion' ,
70
+
71
+ /**
72
+ * Applied when the `flush` prop is enabled.
73
+ */
74
+ ACCORDION_FLUSH : 'accordion-flush' ,
75
+ }
76
+
24
77
export interface CAccordionContextProps {
25
78
_activeItemKey ?: number | string
26
79
alwaysOpen ?: boolean
@@ -30,12 +83,24 @@ export interface CAccordionContextProps {
30
83
export const CAccordionContext = createContext ( { } as CAccordionContextProps )
31
84
32
85
export const CAccordion = forwardRef < HTMLDivElement , CAccordionProps > (
33
- ( { children, activeItemKey, alwaysOpen = false , className, flush, ...rest } , ref ) => {
86
+ (
87
+ { children, activeItemKey, alwaysOpen = false , className, customClassNames, flush, ...rest } ,
88
+ ref ,
89
+ ) => {
34
90
const [ _activeItemKey , setActiveKey ] = useState ( activeItemKey )
35
91
92
+ const _classNames = mergeClassNames < typeof ACCORDION_CLASS_NAMES > (
93
+ ACCORDION_CLASS_NAMES ,
94
+ customClassNames ,
95
+ )
96
+
36
97
return (
37
98
< div
38
- className = { classNames ( 'accordion' , { 'accordion-flush' : flush } , className ) }
99
+ className = { classNames (
100
+ _classNames . ACCORDION ,
101
+ { [ _classNames . ACCORDION_FLUSH ] : flush } ,
102
+ className ,
103
+ ) }
39
104
{ ...rest }
40
105
ref = { ref }
41
106
>
0 commit comments