Skip to content

Commit 71f108c

Browse files
committed
fix(badge): only apply subtle when badge is used by itself with text or an icon
1 parent 502fab6 commit 71f108c

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

core/src/components/badge/badge.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Badge implements ComponentInterface {
3232
* Set to `"bold"` for a badge with vibrant, bold colors or to `"subtle"` for
3333
* a badge with muted, subtle colors.
3434
*/
35-
@Prop() hue?: 'bold' | 'subtle' = 'subtle';
35+
@Prop() hue?: 'bold' | 'subtle';
3636

3737
/**
3838
* Set to `"rectangular"` for non-rounded corners.
@@ -93,8 +93,31 @@ export class Badge implements ComponentInterface {
9393
return size;
9494
}
9595

96-
render() {
96+
// The 'subtle' hue is the default for badges containing text or icons
97+
// The 'bold' hue is used when inside of a button, tab button, or when
98+
// the badge is empty (no text or icon)
99+
private getHue(): string | undefined {
97100
const { hue } = this;
101+
102+
if (hue !== undefined) {
103+
return hue;
104+
}
105+
106+
const inButton = hostContext('ion-button', this.el);
107+
const inTabButton = hostContext('ion-tab-button', this.el);
108+
const hasContent = this.el.textContent?.trim() !== '' || this.el.querySelector('ion-icon') !== null;
109+
110+
// Return 'bold' if the badge is inside a button, tab button, or has no content
111+
if (inButton || inTabButton || !hasContent) {
112+
return 'bold';
113+
}
114+
115+
// Return 'subtle' if the badge contains visible text or an icon
116+
return 'subtle';
117+
}
118+
119+
render() {
120+
const hue = this.getHue();
98121
const shape = this.getShape();
99122
const size = this.getSize();
100123
const theme = getIonTheme(this);

0 commit comments

Comments
 (0)