Skip to content

Commit e3ddcd0

Browse files
committed
[PR feedback] Create internal color util for badge colors
1 parent feb1bb6 commit e3ddcd0

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

src/components/badge/beta_badge/beta_badge.styles.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ import {
1414
euiTextTruncate,
1515
mathWithUnits,
1616
} from '../../../global_styling';
17-
import { UseEuiTheme, tint } from '../../../services';
17+
import { UseEuiTheme, tint, isColorDark, hexToRgb } from '../../../services';
1818

1919
export const euiBetaBadgeStyles = (euiThemeContext: UseEuiTheme) => {
2020
const { euiTheme, colorMode } = euiThemeContext;
2121

22-
const textColor =
23-
colorMode === 'DARK' ? euiTheme.colors.ghost : euiTheme.colors.ink;
24-
const invertedTextColor =
25-
colorMode === 'DARK' ? euiTheme.colors.ink : euiTheme.colors.ghost;
26-
2722
return {
2823
euiBetaBadge: css`
2924
display: inline-block;
@@ -39,22 +34,20 @@ export const euiBetaBadgeStyles = (euiThemeContext: UseEuiTheme) => {
3934
4035
&:focus {
4136
${euiFocusRing(euiThemeContext, 'outset', {
42-
color: textColor,
37+
color:
38+
colorMode === 'DARK' ? euiTheme.colors.ghost : euiTheme.colors.ink,
4339
})}
4440
}
4541
`,
4642
// Colors
4743
accent: css`
48-
background-color: ${euiTheme.colors.accentText};
49-
color: ${invertedTextColor};
44+
${getBadgeColors(euiTheme.colors.accentText, euiThemeContext)}
5045
`,
5146
subdued: css`
52-
background-color: ${tint(euiTheme.colors.lightShade, 0.3)};
53-
color: ${textColor};
47+
${getBadgeColors(tint(euiTheme.colors.lightShade, 0.3), euiThemeContext)}
5448
`,
5549
hollow: css`
56-
background-color: ${euiTheme.colors.emptyShade};
57-
color: ${textColor};
50+
${getBadgeColors(euiTheme.colors.emptyShade, euiThemeContext)}
5851
box-shadow: inset 0 0 0 ${euiTheme.border.width.thin}
5952
${euiTheme.border.color};
6053
`,
@@ -94,3 +87,18 @@ export const euiBetaBadgeStyles = (euiThemeContext: UseEuiTheme) => {
9487
`,
9588
};
9689
};
90+
91+
// Util for detecting text color based on badge bg color
92+
export const getBadgeColors = (
93+
backgroundColor: string,
94+
{ euiTheme }: UseEuiTheme
95+
) => {
96+
const textColor = isColorDark(...hexToRgb(backgroundColor))
97+
? euiTheme.colors.ghost
98+
: euiTheme.colors.ink;
99+
100+
return `
101+
background-color: ${backgroundColor};
102+
color: ${textColor};
103+
`;
104+
};

src/components/badge/notification_badge/badge_notification.styles.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import {
1616
} from '../../../global_styling';
1717
import { UseEuiTheme, tint } from '../../../services';
1818

19+
import { getBadgeColors } from '../beta_badge/beta_badge.styles';
20+
1921
export const euiNotificationBadgeStyles = (euiThemeContext: UseEuiTheme) => {
20-
const { euiTheme, colorMode } = euiThemeContext;
22+
const { euiTheme } = euiThemeContext;
2123

2224
return {
2325
euiNotificationBadge: css`
@@ -53,16 +55,10 @@ export const euiNotificationBadgeStyles = (euiThemeContext: UseEuiTheme) => {
5355
`,
5456
// Colors
5557
accent: css`
56-
background-color: ${euiTheme.colors.accentText};
57-
color: ${colorMode === 'DARK'
58-
? euiTheme.colors.ink
59-
: euiTheme.colors.ghost};
58+
${getBadgeColors(euiTheme.colors.accentText, euiThemeContext)}
6059
`,
6160
subdued: css`
62-
background-color: ${tint(euiTheme.colors.lightShade, 0.3)};
63-
color: ${colorMode === 'DARK'
64-
? euiTheme.colors.ghost
65-
: euiTheme.colors.ink};
61+
${getBadgeColors(tint(euiTheme.colors.lightShade, 0.3), euiThemeContext)}
6662
`,
6763
};
6864
};

0 commit comments

Comments
 (0)