Skip to content

Commit

Permalink
[refactor] Clean up disabled styles
Browse files Browse the repository at this point in the history
+ fix low contrast selection color on disabled badges

- skip inline style custom colors completely if disabled, no need to calculate or apply them. this allows us to remove `!important` CSS

- skip other named colors as well, also no need to apply them. and clickable styles, that definitely makes no sense
  • Loading branch information
cee-chen committed May 13, 2024
1 parent 139f20f commit b73304e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`EuiBadge is disabled 1`] = `
<span
aria-label="aria-label"
class="euiBadge testClass1 testClass2 emotion-euiBadge-default-disabled-euiTestCss"
class="euiBadge testClass1 testClass2 emotion-euiBadge-disabled-euiTestCss"
data-test-subj="test subject string"
title="Content"
>
Expand Down
2 changes: 1 addition & 1 deletion packages/eui/src/components/badge/badge.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const Playground: Story = {
export const CustomColors: Story = {
parameters: {
controls: {
include: ['color', 'children'],
include: ['color', 'children', 'isDisabled'],
},
},
args: {
Expand Down
13 changes: 5 additions & 8 deletions packages/eui/src/components/badge/badge.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,12 @@ export const euiBadgeStyles = (euiThemeContext: UseEuiTheme) => {
danger: css(setBadgeColorVars(badgeColors.danger)),
success: css(setBadgeColorVars(badgeColors.success)),
disabled: css`
/* stylelint-disable declaration-no-important */
${setBadgeColorVars(badgeColors.disabled)}
/* Using !important to override inline styles */
--euiBadgeTextColor: ${badgeColors.disabled.color};
--euiBadgeBackgroundColor: ${badgeColors.disabled.backgroundColor};
color: ${badgeColors.disabled.color} !important;
background-color: ${badgeColors.disabled.backgroundColor} !important;
/* stylelint-enable declaration-no-important */
/* Override selection color, since disabled badges have rgba backgrounds with opacity */
.euiBadge__text::selection {
color: ${euiTheme.colors.emptyShade};
}
`,

// Content wrapper
Expand Down
13 changes: 9 additions & 4 deletions packages/eui/src/components/badge/badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export const EuiBadge: FunctionComponent<EuiBadgeProps> = ({

const euiTheme = useEuiTheme();
const customColorStyles = useMemo(() => {
// Disabled badges should not have custom colors
if (isDisabled) return style;
// Named colors set their styles via Emotion CSS and not inline styles
if (isNamedColor) return style;

Expand Down Expand Up @@ -164,14 +166,17 @@ export const EuiBadge: FunctionComponent<EuiBadgeProps> = ({
);
}
}
}, [color, isNamedColor, style, euiTheme]);
}, [color, isNamedColor, isDisabled, style, euiTheme]);

const styles = useEuiMemoizedStyles(euiBadgeStyles);
const cssStyles = [
styles.euiBadge,
isNamedColor && styles[color as BadgeColor],
(onClick || href) && !iconOnClick && styles.clickable,
isDisabled && styles.disabled,
...(isDisabled
? [styles.disabled]
: [
isNamedColor && styles[color as BadgeColor],
!iconOnClick && (onClick || href) && styles.clickable,
]),
];
const textCssStyles = [
styles.text.euiBadge__text,
Expand Down

0 comments on commit b73304e

Please sign in to comment.