Skip to content

fix(button): update styling for badge #30414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions core/src/components/badge/badge.ionic.scss
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,12 @@
// Badge in Button
// --------------------------------------------------

:host([vertical]:not(.in-tab-button).in-button.badge-small) {
@include globals.position(null, calc(-1 * var(globals.$ion-space-050)));
}

:host([vertical]:not(.in-tab-button).in-button.badge-medium),
:host([vertical]:not(.in-tab-button).in-button.badge-large) {
@include globals.position(null, globals.$ion-space-050);
}

:host(:not(:empty).in-button) {
--padding-start: #{globals.$ion-scale-050};
--padding-end: #{globals.$ion-scale-050};

@include globals.typography(globals.$ion-body-action-xs);

min-width: globals.$ion-scale-400;
height: globals.$ion-scale-400;

Expand Down
3 changes: 2 additions & 1 deletion core/src/components/badge/badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Badge implements ComponentInterface {
const theme = getIonTheme(this);
const { size } = this;

// TODO(ROU-10747): Remove theme check when sizes are defined for all themes.
// TODO(FW-6355): Remove theme check when sizes are defined for all themes.
if (theme !== 'ionic') {
return undefined;
}
Expand Down Expand Up @@ -133,6 +133,7 @@ export class Badge implements ComponentInterface {
[`badge-vertical-${this.vertical}`]: this.vertical !== undefined,
'in-button': hostContext('ion-button', this.el),
'in-tab-button': hostContext('ion-tab-button', this.el),
'long-badge': (this.el.textContent?.trim().length ?? 0) > 2,
})}
>
<slot></slot>
Expand Down
8 changes: 8 additions & 0 deletions core/src/components/badge/test/hint/badge.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@ configs({ directions: ['ltr'], modes: ['md', 'ios', 'ionic-md'] }).forEach(({ co

await expect(container).toHaveScreenshot(screenshot(`badge-hint-button-bottom`));
});

test('should not have visual regressions for different button sizes', async ({ page }) => {
await page.goto('/src/components/badge/test/hint', config);

const container = page.locator('#button-size');

await expect(container).toHaveScreenshot(screenshot(`badge-hint-button-size`));
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions core/src/components/badge/test/hint/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,47 @@
</ion-button>
</div>
</ion-list>
<ion-list id="button-size">
<ion-list-header>
<ion-label> Inside Button - Button Size </ion-label>
</ion-list-header>
<div class="ion-display-flex ion-align-items-center ion-justify-content-around ion-margin">
<ion-button fill="clear">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="top">1</ion-badge>
</ion-button>
<ion-button fill="clear" size="small">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="top">1</ion-badge>
</ion-button>
<ion-button fill="clear" size="medium">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="top">1</ion-badge>
</ion-button>
<ion-button fill="clear" size="large">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="top">1</ion-badge>
</ion-button>
</div>
<div class="ion-display-flex ion-align-items-center ion-justify-content-around ion-margin">
<ion-button fill="clear">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="bottom">1</ion-badge>
</ion-button>
<ion-button fill="clear" size="small">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="bottom">1</ion-badge>
</ion-button>
<ion-button fill="clear" size="medium">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="bottom">1</ion-badge>
</ion-button>
<ion-button fill="clear" size="large">
<ion-icon slot="icon-only" name="add"></ion-icon>
<ion-badge color="danger" size="small" vertical="bottom">1</ion-badge>
</ion-button>
</div>
</ion-list>
</ion-content>
</ion-app>
</body>
Expand Down
8 changes: 1 addition & 7 deletions core/src/components/button/button.common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,7 @@ ion-ripple-effect {
@include border-radius(inherit);
}

// This rule works for Chrome.
:has(ion-badge) .button-native {
--overflow: visible;
}

// This rule works for the rest of the browsers.
:host(:has(ion-badge)) .button-native {
:host(.button-has-badge) .button-native {
--overflow: visible;
}

Expand Down
49 changes: 43 additions & 6 deletions core/src/components/button/button.ionic.scss
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,14 @@
::slotted(ion-icon[slot="start"]),
::slotted(ion-icon[slot="end"]),
::slotted(ion-icon[slot="icon-only"]) {
font-size: globals.$ion-font-size-400;
font-size: globals.$ion-font-size-500;
}

:host(.button-small),
:host(.button-large) {
::slotted(ion-icon[slot="start"]),
::slotted(ion-icon[slot="end"]),
::slotted(ion-icon[slot="icon-only"]) {
font-size: inherit;
font-size: globals.$ion-font-size-600;
}
}

Expand Down Expand Up @@ -264,8 +263,46 @@
@include globals.margin-horizontal(globals.$ion-space-300, null);
}

// Button Badge
// Button with Badge
// --------------------------------------------------
:host ::slotted(ion-badge[vertical]:not(:empty)) {
@include globals.padding(globals.$ion-space-050);

:host(.button-has-badge) {
--padding-top: #{globals.$ion-space-0};
--padding-bottom: #{globals.$ion-space-0};
}

:host(.button-small) ::slotted(ion-badge) {
@include globals.position(null, calc(-1 * globals.$ion-space-050), null, null);
}

:host(.button-medium) {
::slotted(ion-badge.long-badge.badge-vertical-top) {
@include globals.position($top: globals.$ion-space-100);
}
::slotted(ion-badge.long-badge.badge-vertical-bottom) {
@include globals.position($bottom: globals.$ion-space-100);
}

::slotted(ion-badge:not(.long-badge).badge-vertical-top) {
@include globals.position(globals.$ion-space-100, globals.$ion-space-150, null, null);
}
::slotted(ion-badge:not(.long-badge).badge-vertical-bottom) {
@include globals.position(null, globals.$ion-space-150, globals.$ion-space-100, null);
}
}

:host(.button-large) {
::slotted(ion-badge.long-badge.badge-vertical-top) {
@include globals.position($top: globals.$ion-space-200);
}
::slotted(ion-badge.long-badge.badge-vertical-bottom) {
@include globals.position($bottom: globals.$ion-space-200);
}

::slotted(ion-badge:not(.long-badge).badge-vertical-top) {
@include globals.position(globals.$ion-space-200, globals.$ion-space-200, null, null);
}
::slotted(ion-badge:not(.long-badge).badge-vertical-bottom) {
@include globals.position(null, globals.$ion-space-200, globals.$ion-space-200, null);
}
}
21 changes: 19 additions & 2 deletions core/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ export class Button implements ComponentInterface, AnchorInterface, ButtonInterf
return !!this.el.querySelector('[slot="icon-only"]');
}

private get hasBadge() {
return !!this.el.querySelector('ion-badge');
}

private get rippleType() {
const hasClearFill = this.fill === undefined || this.fill === 'clear';

Expand Down Expand Up @@ -345,8 +349,20 @@ export class Button implements ComponentInterface, AnchorInterface, ButtonInterf
};

render() {
const { buttonType, type, disabled, rel, target, href, color, expand, hasIconOnly, strong, inheritedAttributes } =
this;
const {
buttonType,
type,
disabled,
rel,
target,
href,
color,
expand,
hasIconOnly,
hasBadge,
strong,
inheritedAttributes,
} = this;

const theme = getIonTheme(this);
const mode = getIonMode(this);
Expand Down Expand Up @@ -398,6 +414,7 @@ export class Button implements ComponentInterface, AnchorInterface, ButtonInterf
'in-toolbar-color': hostContext('ion-toolbar[color]', this.el),
'in-buttons': hostContext('ion-buttons', this.el),
'button-has-icon-only': hasIconOnly,
'button-has-badge': hasBadge,
'button-disabled': disabled,
'ion-activatable': true,
'ion-focusable': true,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading