Skip to content
6 changes: 6 additions & 0 deletions demo/e2e/screenshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const routes = [
...typesPath({ path: '/main/index/action-sheet', name: 'action-sheet' }, actionSheetTypes),
...typesPath({ path: '/main/index/alert', name: 'alert' }, alertTypes),
{ path: '/main/index/button', name: 'button' },
{ path: '/main/index/toolbar-badge', name: 'toolbar-badge' },
{ path: '/main/index/checkbox', name: 'checkbox' },
{ path: '/main/index/range', name: 'range' },
...typesPath({ path: '/main/index/toast', name: 'toast' }, toastTypes),
Expand Down Expand Up @@ -74,6 +75,11 @@ const prepareScreenShot = async (page: Page, routeName: string) => {
await expect(focused).toBeFocused();
await expect(focused).toHaveClass(/ion-focused/);
}
if (routeName === 'toolbar-badge') {
// Keep both directions and the unaffected-control examples in one image.
await page.setViewportSize({ width: 1200, height: 1000 });
return;
}
if (!routeName.includes(':')) {
const scrollHeight = await page.locator('ion-content[role="main"]:visible').evaluate(async (el: any) => {
const scrollEl = await el.getScrollElement();
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.
2 changes: 1 addition & 1 deletion demo/src/app/docs/docs-content.generated.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/src/app/docs/docs-page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ describe('DocsPage', () => {
expect(element.querySelector('article.docs-content h1')?.textContent).toContain('Special markup');
expect(element.querySelectorAll('.code-block-container pre.shiki code').length).toBeGreaterThan(0);
expect(element.querySelector('.code-block-container code span[style*="color"]')).not.toBeNull();
expect(element.querySelectorAll('figure.docs-example').length).toBe(6);
expect(element.querySelectorAll('figure.docs-example').length).toBe(8);
});
});
1 change: 1 addition & 0 deletions demo/src/app/index/index-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class IndexPageComponent {
{ name: 'toast', enable: true },
{ name: 'toggle', enable: true },
{ name: 'toolbar', enable: true },
{ name: 'toolbar-badge', enable: true },
]);
readonly enableComponents = computed(() => this.components().filter((c) => c.enable));
readonly disableComponents = computed(() => this.components().filter((c) => !c.enable));
Expand Down
4 changes: 4 additions & 0 deletions demo/src/app/index/index.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const routes: Routes = [
path: 'color-palette',
loadComponent: () => import('./pages/color-palette/color-palette.page').then((m) => m.ColorPalettePage),
},
{
path: 'toolbar-badge',
loadComponent: () => import('./pages/toolbar-badge/toolbar-badge.page').then((m) => m.ToolbarBadgePage),
},
{
path: 'button',
loadComponent: () => import('./pages/button/button.page').then((m) => m.ButtonPage),
Expand Down
78 changes: 78 additions & 0 deletions demo/src/app/index/pages/toolbar-badge/toolbar-badge.page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<ion-header>
<ion-toolbar
><ion-buttons slot="start"><ion-back-button defaultHref="/main/index"></ion-back-button></ion-buttons
><ion-title>Toolbar badges</ion-title></ion-toolbar
>
</ion-header>
<ion-content>
<div class="badge-examples">
@for (direction of directions; track direction) {
<section [attr.dir]="direction">
<h2>{{ direction.toUpperCase() }}</h2>
<p>Empty / 3 / 99+ / no badge</p>
<h3>Standalone buttons</h3>
<ion-toolbar>
@for (value of badges; track value) {
<ion-button slot="end" aria-label="Notifications"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge>{{ value }}</ion-badge></ion-button
>
}
<ion-button slot="end" aria-label="More"><ion-icon slot="icon-only" [icon]="more"></ion-icon></ion-button>
</ion-toolbar>
<h3>Grouped buttons</h3>
<ion-toolbar>
<ion-buttons slot="end" data-badge-group>
@for (value of badges; track value) {
<ion-button aria-label="Notifications"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge>{{ value }}</ion-badge></ion-button
>
}
<ion-button aria-label="More"><ion-icon slot="icon-only" [icon]="more"></ion-icon></ion-button>
</ion-buttons>
</ion-toolbar>
<h3>Single group / menu group</h3>
<ion-toolbar>
<ion-buttons slot="start"
><ion-button aria-label="Notifications"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge>3</ion-badge></ion-button
></ion-buttons
>
<ion-buttons slot="end"
><ion-menu-button [autoHide]="false"></ion-menu-button
><ion-button aria-label="Notifications"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge>3</ion-badge></ion-button
></ion-buttons
>
</ion-toolbar>
<h3>Custom color / hidden / CSS class / opt-out</h3>
<ion-toolbar>
<ion-buttons slot="start"
><ion-button aria-label="Notifications"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge color="success">3</ion-badge></ion-button
><ion-button aria-label="Notifications"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge hidden>3</ion-badge></ion-button
>
<ion-button aria-label="Notifications hidden by CSS"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge class="badge-off">3</ion-badge></ion-button
>
<ion-button aria-label="Unread indicator hidden by CSS"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge class="badge-off"></ion-badge
></ion-button>
</ion-buttons>
<ion-buttons slot="end" class="md3-disabled"
><ion-button aria-label="Notifications"
><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge>3</ion-badge></ion-button
></ion-buttons
>
</ion-toolbar>
<h3>Outside toolbar / nested badge</h3>
<ion-button aria-label="Notifications"><ion-icon slot="icon-only" [icon]="more"></ion-icon><ion-badge>3</ion-badge></ion-button>
<ion-toolbar
><ion-button
><span>Nested <ion-badge>3</ion-badge></span></ion-button
></ion-toolbar
>
</section>
}
</div>
</ion-content>
26 changes: 26 additions & 0 deletions demo/src/app/index/pages/toolbar-badge/toolbar-badge.page.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.badge-examples {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 24px;
padding: 24px;
}

h2 {
margin: 0 0 12px;
font-size: 20px;
}

h3 {
margin: 20px 0 8px;
font-size: 14px;
}

@media (max-width: 700px) {
.badge-examples {
grid-template-columns: 1fr;
}
}

.badge-off {
display: none;
}
28 changes: 28 additions & 0 deletions demo/src/app/index/pages/toolbar-badge/toolbar-badge.page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
IonBadge,
IonBackButton,
IonButton,
IonButtons,
IonContent,
IonHeader,
IonIcon,
IonMenuButton,
IonTitle,
IonToolbar,
} from '@demo/ionic';
import { ellipsisHorizontal } from 'ionicons/icons';

@Component({
selector: 'app-toolbar-badge',
standalone: true,
imports: [IonBadge, IonBackButton, IonButton, IonButtons, IonContent, IonHeader, IonIcon, IonMenuButton, IonTitle, IonToolbar],
templateUrl: './toolbar-badge.page.html',
styleUrls: ['./toolbar-badge.page.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ToolbarBadgePage {
readonly more = ellipsisHorizontal;
readonly directions = ['ltr', 'rtl'] as const;
readonly badges = ['', '3', '99+'];
}
48 changes: 48 additions & 0 deletions docs/special-markup.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,54 @@ same Android appearance without an additional effect or JavaScript helper.
Explicit Ionic colors, disabled state, and `fill="clear"` / `fill="outline"` keep their
normal meaning. The class does not force a different fill.

## Toolbar button badges

Place `ion-badge` directly inside an `ion-button` in an `ion-toolbar`. Both
standalone buttons and buttons inside `ion-buttons` support this markup.
An empty badge shows an unread indicator; text such as `3` or `99+` shows a count.
For an indicator, keep the contents completely empty (`<ion-badge></ion-badge>`),
without spaces or line breaks between the tags.

Standalone toolbar button:

```html preview
<ion-toolbar>
<ion-button slot="end" aria-label="Notifications">
<ion-icon name="ellipsis-horizontal" slot="icon-only"></ion-icon>
<ion-badge>3</ion-badge>
</ion-button>
</ion-toolbar>
```

Grouped toolbar buttons, with empty, numeric and wide badges:

```html preview
<ion-toolbar>
<ion-buttons slot="end">
<ion-button aria-label="Unread notifications">
<ion-icon name="ellipsis-horizontal" slot="icon-only"></ion-icon>
<ion-badge></ion-badge>
</ion-button>
<ion-button aria-label="3 notifications">
<ion-icon name="ellipsis-horizontal" slot="icon-only"></ion-icon>
<ion-badge>3</ion-badge>
</ion-button>
<ion-button aria-label="99 or more notifications">
<ion-icon name="ellipsis-horizontal" slot="icon-only"></ion-icon>
<ion-badge>99+</ion-badge>
</ion-button>
</ion-buttons>
</ion-toolbar>
```

Use the badge's `color` property to choose an Ionic color. Setting `hidden` or
`display: none`, or removing the badge, hides it without changing the action's
layout. Include the notification state in the action's accessible label.

The treatment is limited to direct-child badges in toolbar buttons. Badges in
tabs, outside toolbars, or nested in another child keep their existing styling.
Theme opt-outs on the toolbar, group, button or badge remain supported.

## Preferred overlay actions

For alerts and action sheets, use `role: 'preferred'` for the recommended action,
Expand Down
61 changes: 61 additions & 0 deletions src/styles/components/ion-button.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@use '../utils/colors' as md3;

@mixin theme-button() {
// Default is be round button
--box-shadow: none;
Expand Down Expand Up @@ -113,3 +115,62 @@ ion-back-button.md:not(.md3-disabled) {
--padding-start: 16px;
--padding-end: 16px;
}

// Badge layout belongs only to toolbar buttons with a direct badge child.
ion-toolbar.md:not(.md3-disabled) {
ion-button.md:has(> ion-badge:not(.md3-disabled)):not(.md3-disabled):not(:where(ion-buttons:is(.md3-disabled) *)) {
// Keep the action's layout and hit area; only the badge paints outside it.
overflow: visible;

&::part(native) {
overflow: visible;
contain: none;
}

> ion-badge:not(.md3-disabled) {
--background: #{md3.color(error)};
--color: #{md3.color(on-error)};
position: absolute;
box-sizing: border-box;
margin: 0;
pointer-events: none;
z-index: 1;
--padding-top: 0px;
--padding-bottom: 0px;
--padding-start: 4px;
--padding-end: 4px;
min-width: 16px;
height: 16px;
border-radius: 8px;
font-size: 11px;
font-weight: 500;
line-height: 16px;
letter-spacing: 0;
text-transform: none;
unicode-bidi: plaintext;
// Material's text badge grows from the icon center toward the trailing edge.
inset-block-start: calc(50% - 16px);
inset-inline-start: 50%;

&[hidden] {
display: none;
}

// Override Ionic's empty-badge default without outranking application classes.
:where(&:empty) {
display: inline-block;
}

&:empty {
--padding-start: 0px;
--padding-end: 0px;
min-width: 6px;
width: 6px;
height: 6px;
border-radius: 3px;
inset-block-start: calc(50% - 12px);
inset-inline-start: calc(50% + 6px);
}
}
}
}
Loading