Skip to content

Commit f287b5b

Browse files
committed
refactor(CBadge, CCard): improve background and text color handling
1 parent 0c32d1c commit f287b5b

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

packages/coreui-react/src/components/badge/CBadge.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export interface CBadgeProps extends HTMLAttributes<HTMLDivElement | HTMLSpanEle
3535
* Size the component small.
3636
*/
3737
size?: 'sm'
38+
/**
39+
* Sets the component's color scheme to one of CoreUI's themed colors, ensuring the text color contrast adheres to the WCAG 4.5:1 contrast ratio standard for accessibility.
40+
*
41+
* @type 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string
42+
* @since 5.0.0-rc.3
43+
*/
44+
textBgColor?: Colors
3845
/**
3946
* Sets the text color of the component to one of CoreUI’s themed colors.
4047
*
@@ -55,6 +62,7 @@ export const CBadge: PolymorphicRefForwardingComponent<'span', CBadgeProps> = fo
5562
position,
5663
shape,
5764
size,
65+
textBgColor,
5866
textColor,
5967
...rest
6068
},
@@ -65,14 +73,15 @@ export const CBadge: PolymorphicRefForwardingComponent<'span', CBadgeProps> = fo
6573
className={classNames(
6674
'badge',
6775
{
68-
[`text-bg-${color}`]: color,
76+
[`bg-${color}`]: color,
6977
'position-absolute translate-middle': position,
7078
'top-0': position?.includes('top'),
7179
'top-100': position?.includes('bottom'),
7280
'start-100': position?.includes('end'),
7381
'start-0': position?.includes('start'),
7482
[`badge-${size}`]: size,
7583
[`text-${textColor}`]: textColor,
84+
[`text-bg-${textBgColor}`]: textBgColor,
7685
},
7786
shape,
7887
className,
@@ -94,6 +103,7 @@ CBadge.propTypes = {
94103
position: PropTypes.oneOf(['top-start', 'top-end', 'bottom-end', 'bottom-start']),
95104
shape: shapePropType,
96105
size: PropTypes.oneOf(['sm']),
106+
textBgColor: colorPropType,
97107
textColor: textColorsPropType,
98108
}
99109

packages/coreui-react/src/components/card/CCard.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ export interface CCardProps extends HTMLAttributes<HTMLDivElement> {
2222
* @type 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | 'primary-emphasis' | 'secondary-emphasis' | 'success-emphasis' | 'danger-emphasis' | 'warning-emphasis' | 'info-emphasis' | 'light-emphasis' | 'body' | 'body-emphasis' | 'body-secondary' | 'body-tertiary' | 'black' | 'black-50' | 'white' | 'white-50' | string
2323
*/
2424
textColor?: string
25+
/**
26+
* Sets the component's color scheme to one of CoreUI's themed colors, ensuring the text color contrast adheres to the WCAG 4.5:1 contrast ratio standard for accessibility.
27+
*
28+
* @type 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string
29+
* @since 5.0.0-rc.3
30+
*/
31+
textBgColor?: Colors
2532
}
2633

2734
export const CCard = forwardRef<HTMLDivElement, CCardProps>(
28-
({ children, className, color, textColor, ...rest }, ref) => {
35+
({ children, className, color, textBgColor, textColor, ...rest }, ref) => {
2936
return (
3037
<div
3138
className={classNames(
3239
'card',
3340
{
3441
[`bg-${color}`]: color,
3542
[`text-${textColor}`]: textColor,
43+
[`text-bg-${textBgColor}`]: textBgColor,
3644
},
3745
className,
3846
)}
@@ -49,6 +57,7 @@ CCard.propTypes = {
4957
children: PropTypes.node,
5058
className: PropTypes.string,
5159
color: colorPropType,
60+
textBgColor: colorPropType,
5261
textColor: PropTypes.string,
5362
}
5463

packages/docs/content/components/badge.mdx

+14-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,22 @@ Add any of the below-mentioned `color` props to modify the presentation of a rea
9999
<CBadge color="danger">danger</CBadge>
100100
<CBadge color="warning">warning</CBadge>
101101
<CBadge color="info">info</CBadge>
102-
<CBadge color="light">light</CBadge>
102+
<CBadge textBgColor="light">light</CBadge>
103103
<CBadge color="dark">dark</CBadge>
104104
```
105105

106+
You can also apply contextual variations with the `textBgColor` property, which automatically sets the text color to ensure compliance with the WCAG 4.5:1 contrast ratio standard for enhanced accessibility.
107+
108+
```jsx preview
109+
<CBadge textBgColor="primary">primary</CBadge>
110+
<CBadge textBgColor="success">success</CBadge>
111+
<CBadge textBgColor="danger">danger</CBadge>
112+
<CBadge textBgColor="warning">warning</CBadge>
113+
<CBadge textBgColor="info">info</CBadge>
114+
<CBadge textBgColor="light">light</CBadge>
115+
<CBadge textBgColor="dark">dark</CBadge>
116+
```
117+
106118
## Pill badges
107119

108120
Apply the `shape="rounded-pill"` prop to make badges rounded.
@@ -113,7 +125,7 @@ Apply the `shape="rounded-pill"` prop to make badges rounded.
113125
<CBadge color="danger" shape="rounded-pill">danger</CBadge>
114126
<CBadge color="warning" shape="rounded-pill">warning</CBadge>
115127
<CBadge color="info" shape="rounded-pill">info</CBadge>
116-
<CBadge color="light" shape="rounded-pill">light</CBadge>
128+
<CBadge textBgColor="light" shape="rounded-pill">light</CBadge>
117129
<CBadge color="dark" shape="rounded-pill">dark</CBadge>
118130
```
119131

packages/docs/content/components/card.mdx

+61
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,67 @@ Cards include various options for customizing their backgrounds, borders, and co
446446

447447
Use `color` property to change the appearance of a card.
448448

449+
<Example>
450+
<>
451+
{[
452+
{ color: 'primary' },
453+
{ color: 'secondary' },
454+
{ color: 'success' },
455+
{ color: 'danger' },
456+
{ color: 'warning' },
457+
{ color: 'info' },
458+
{ color: 'light' },
459+
{ color: 'dark' },
460+
].map((item, index) => (
461+
<CCard
462+
textBgColor={item.color}
463+
className="mb-3"
464+
style={{ maxWidth: '18rem' }}
465+
key={index}
466+
>
467+
<CCardHeader>Header</CCardHeader>
468+
<CCardBody>
469+
<CCardTitle>{item.color} card title</CCardTitle>
470+
<CCardText> Some quick example text to build on the card title and make up the bulk of the card's content.</CCardText>
471+
</CCardBody>
472+
</CCard>
473+
))}
474+
</>
475+
</Example>
476+
477+
You can also apply contextual variations with the `textBgColor` property, which automatically sets the text color to ensure compliance with the WCAG 4.5:1 contrast ratio standard for enhanced accessibility.
478+
479+
480+
```jsx
481+
<>
482+
{[
483+
{ color: 'primary' },
484+
{ color: 'secondary' },
485+
{ color: 'success' },
486+
{ color: 'danger' },
487+
{ color: 'warning' },
488+
{ color: 'info' },
489+
{ color: 'light' },
490+
{ color: 'dark' },
491+
].map((item, index) => (
492+
<CCard
493+
textBgColor={item.color}
494+
className="mb-3"
495+
style={{ maxWidth: '18rem' }}
496+
key={index}
497+
>
498+
<CCardHeader>Header</CCardHeader>
499+
<CCardBody>
500+
<CCardTitle>{item.color} card title</CCardTitle>
501+
<CCardText>
502+
Some quick example text to build on the card title and make up the bulk of the card's
503+
content.
504+
</CCardText>
505+
</CCardBody>
506+
</CCard>
507+
))}
508+
</>
509+
```
449510
<Example>
450511
<>
451512
{[

packages/docs/src/styles/_component-examples.scss

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
margin-left: .5rem;
7474
}
7575

76+
// Badges
77+
> .badge + .badge {
78+
margin-left: .25rem;
79+
}
80+
7681
// Buttons
7782
> .btn,
7883
> .btn-group {

0 commit comments

Comments
 (0)