Skip to content

Commit

Permalink
fix: custom contrast color affected by parents
Browse files Browse the repository at this point in the history
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
  • Loading branch information
SychO9 committed May 22, 2023
1 parent 253a3d2 commit 577890d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion framework/core/js/src/common/components/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class Badge<CustomAttrs extends IBadgeAttrs = IBadgeAttrs> extend
view() {
const { type, icon: iconName, label, color, style = {}, ...attrs } = this.attrs;

const className = classList('Badge', [type && `Badge--${type}`], attrs.className, color && textContrastClass(color));
const className = classList('Badge', [type && `Badge--${type}`], attrs.className, textContrastClass(color));

const iconChild = iconName ? icon(iconName, { className: 'Badge-icon' }) : m.trust('&nbsp;');

Expand Down
4 changes: 3 additions & 1 deletion framework/core/js/src/common/helpers/textContrastClass.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import isDark from '../utils/isDark';

export default function textContrastClass(hexcolor: string | null): string {
export default function textContrastClass(hexcolor: string | null | undefined): string {
if (!hexcolor) return 'text-contrast--unchanged';

return isDark(hexcolor) ? 'text-contrast--light' : 'text-contrast--dark';
}
6 changes: 6 additions & 0 deletions framework/core/less/common/scaffolding.less
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,10 @@ blockquote ol:last-child {
--contrast-color: var(--text-on-dark);
color: var(--contrast-color);
}

// This exists to prevent inheriting the contrast color from a parent element.
// Like when a badge is inside a tag hero.
&--unchanged {
--contrast-color: var(--unchanged-color);
}
}

0 comments on commit 577890d

Please # to comment.