Skip to content

feat(badge): add badge component #7483

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 20 commits into from
Feb 13, 2018
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
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/src/lib/tabs/** @andrewseguin
/src/lib/toolbar/** @devversion
/src/lib/tooltip/** @andrewseguin
/src/lib/badge/** @amcdnl

# Angular Material core
/src/lib/core/* @jelbourn
Expand Down Expand Up @@ -129,6 +130,7 @@
/src/demo-app/toolbar/** @devversion
/src/demo-app/tooltip/** @andrewseguin
/src/demo-app/typography/** @crisbeto
/src/demo-app/badge/** @amcdnl

# E2E app
/e2e/* @jelbourn
Expand Down
90 changes: 90 additions & 0 deletions src/demo-app/badge/badge-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<div class="badge-demo">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if the demo had a button to toggle badge visibility to see the animation


<div class="badge-examples">
<h3>Text</h3>
<span [matBadge]="badgeContent" matBadgeOverlap="false" *ngIf="visible">
Hello
</span>

<span [matBadge]="11111" matBadgeOverlap="false">
Hello
</span>

<span matBadge="22" matBadgeOverlap="false" matBadgePosition="below after" matBadgeColor="accent">
Hello
</span>

<span matBadge="22" matBadgeOverlap="false" matBadgePosition="above before" matBadgeColor="warn">
Hello
</span>

<span matBadge="⚡️" matBadgeOverlap="false" matBadgePosition="below before">
Hello
</span>

<span [matBadge]="badgeContent" matBadgeDescription="I've got {{badgeContent}} problems">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it’s preferable to use property binding [matBadgeDescription]="..."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jelbourn ^^?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using interpolation is fine when it's mixed with static text

Aria
</span>

<span [matBadge]="badgeContent" matBadgeHidden="true">
Hidden
</span>

<input type="text" [(ngModel)]="badgeContent" />
<button (click)="visible = !visible">Toggle</button>
</div>

<div class="badge-examples">
<h3>Buttons</h3>
<button mat-raised-button [matBadge]="badgeContent">
<mat-icon color="primary">home</mat-icon>
</button>

<button mat-raised-button matBadge="22" matBadgePosition="below after" color="primary" matBadgeColor="accent">
<mat-icon color="accent">home</mat-icon>
</button>

<button mat-raised-button matBadge="22" matBadgePosition="above before">
<mat-icon color="primary">home</mat-icon>
</button>

<button mat-raised-button matBadge="22" matBadgePosition="below before">
<mat-icon color="primary">home</mat-icon>
</button>

<button mat-raised-button>
<mat-icon color="primary" matBadge="22" color="accent">home</mat-icon>
</button>
</div>

<div class="badge-examples">
<h3>Icons</h3>
<mat-icon [matBadge]="badgeContent">
home
</mat-icon>

<mat-icon color="primary" matBadge="22" matBadgePosition="below after" matBadgeColor="accent">
home
</mat-icon>

<mat-icon color="primary" matBadge="22" matBadgePosition="above before" matBadgeColor="warn">
home
</mat-icon>

<mat-icon color="primary" matBadge="22" matBadgePosition="below before">
home
</mat-icon>
</div>

<div class="badge-examples">
<h3>Size</h3>
<mat-icon [matBadge]="badgeContent" matBadgeSize="small">
home
</mat-icon>
<mat-icon [matBadge]="badgeContent" matBadgeSize="large">
home
</mat-icon>

</div>

</div>
3 changes: 3 additions & 0 deletions src/demo-app/badge/badge-demo.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.badge-examples {
margin-bottom: 25px;
}
20 changes: 20 additions & 0 deletions src/demo-app/badge/badge-demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Component} from '@angular/core';

@Component({
moduleId: module.id,
selector: 'badge-demo',
templateUrl: 'badge-demo.html',
styleUrls: ['badge-demo.css'],
})
export class BadgeDemo {
visible = true;
badgeContent = '0';
}
1 change: 1 addition & 0 deletions src/demo-app/demo-app/demo-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class DemoApp {
dark = false;
navItems = [
{name: 'Autocomplete', route: '/autocomplete'},
{name: 'Badge', route: '/badge'},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Routing to the badge in the demo app fails

Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'badge'

{name: 'Bottom sheet', route: '/bottom-sheet'},
{name: 'Button Toggle', route: '/button-toggle'},
{name: 'Button', route: '/button'},
Expand Down
2 changes: 2 additions & 0 deletions src/demo-app/demo-app/demo-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {TypographyDemo} from '../typography/typography-demo';
import {DemoApp, Home} from './demo-app';
import {DEMO_APP_ROUTES} from './routes';
import {TableDemoModule} from '../table/table-demo-module';
import {BadgeDemo} from 'badge/badge-demo';

@NgModule({
imports: [
Expand All @@ -78,6 +79,7 @@ import {TableDemoModule} from '../table/table-demo-module';
BaselineDemo,
ButtonDemo,
ButtonToggleDemo,
BadgeDemo,
CardDemo,
CheckboxDemo,
ChipsDemo,
Expand Down
2 changes: 2 additions & 0 deletions src/demo-app/demo-app/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ import {TypographyDemo} from '../typography/typography-demo';
import {DemoApp, Home} from './demo-app';
import {TableDemoPage} from '../table/table-demo-page';
import {TABLE_DEMO_ROUTES} from '../table/routes';
import {BadgeDemo} from 'badge/badge-demo';

export const DEMO_APP_ROUTES: Routes = [
{path: '', component: DemoApp, children: [
{path: '', component: Home},
{path: 'autocomplete', component: AutocompleteDemo},
{path: 'badge', component: BadgeDemo},
{path: 'bottom-sheet', component: BottomSheetDemo},
{path: 'baseline', component: BaselineDemo},
{path: 'button', component: ButtonDemo},
Expand Down
2 changes: 2 additions & 0 deletions src/demo-app/demo-material-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {CdkTableModule} from '@angular/cdk/table';
import {NgModule} from '@angular/core';
import {
MatAutocompleteModule,
MatBadgeModule,
MatBottomSheetModule,
MatButtonModule,
MatButtonToggleModule,
Expand Down Expand Up @@ -58,6 +59,7 @@ import {
@NgModule({
exports: [
MatAutocompleteModule,
MatBadgeModule,
MatBottomSheetModule,
MatButtonModule,
MatButtonToggleModule,
Expand Down
1 change: 1 addition & 0 deletions src/demo-app/system-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ System.config({
'@angular/material/tabs': 'dist/packages/material/tabs/index.js',
'@angular/material/toolbar': 'dist/packages/material/toolbar/index.js',
'@angular/material/tooltip': 'dist/packages/material/tooltip/index.js',
'@angular/material/badge': 'dist/packages/material/badge/index.js',
},
packages: {
// Thirdparty barrels.
Expand Down
167 changes: 167 additions & 0 deletions src/lib/badge/_badge-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// This contains all of the styles for the badge
// rather than just the color/theme because of
// no style sheet support for directives.
@import '../core/theming/palette';
@import '../core/theming/theming';
@import '../core/typography/typography-utils';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment here that explains why the badge theme contains all of the styles for the badge instead of just color and typography

$mat-badge-font-size: 12px;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be coming in through the typography configuration. @crisbeto thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be ideal, but I don't see any breakpoint that maps nicely. @amcdnl is this font size somewhere in the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crisbeto - There is no spec for badges.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work to just use the font-size from caption?

$mat-badge-font-weight: 600;
$mat-badge-default-size: 22px !default;
$mat-badge-small-size: $mat-badge-default-size - 6;
$mat-badge-large-size: $mat-badge-default-size + 6;

// Mixin for building offset given different sizes
@mixin _mat-badge-size($size) {
.mat-badge-content {
width: $size;
height: $size;
line-height: $size;
}

&.mat-badge-above {
.mat-badge-content {
top: -$size / 2;
}
}

&.mat-badge-below {
.mat-badge-content {
bottom: -$size / 2;
}
}

&.mat-badge-before {
margin-left: $size;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to account for RTL


&[dir='rtl'] {
margin-left: 0;
margin-right: $size;
}

.mat-badge-content {
left: -$size;
}
}

&.mat-badge-after {
margin-right: $size;

&[dir='rtl'] {
margin-right: 0;
margin-left: $size;
}

.mat-badge-content {
right: -$size;
}
}

&.mat-badge-overlap {
&.mat-badge-before {
margin-left: $size / 2;

&[dir='rtl'] {
margin-left: 0;
margin-right: $size / 2;
}

.mat-badge-content {
left: -$size / 2;
}
}

&.mat-badge-after {
margin-right: $size / 2;

&[dir='rtl'] {
margin-right: 0;
margin-left: $size;
}

.mat-badge-content {
right: -$size / 2;
}
}
}
}

@mixin mat-badge-theme($theme) {
$accent: map-get($theme, accent);
$warn: map-get($theme, warn);
$primary: map-get($theme, primary);

.mat-badge-content {
color: mat-color($primary, default-contrast);
background: mat-color($primary);
}

&.mat-badge-accent {
.mat-badge-content {
background: mat-color($accent);
color: mat-color($accent, default-contrast);
}
}

&.mat-badge-warn {
.mat-badge-content {
color: mat-color($warn, default-contrast);
background: mat-color($warn);
}
}
}

@mixin mat-badge-typography($config) {
.mat-badge-content {
font-weight: $mat-badge-font-weight;
font-size: $mat-badge-font-size;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size and weight should come from the typography config, no? cc @crisbeto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sizes different based on the small|med|large so needed to make them independent.

font-family: mat-font-family($config);
}

.mat-badge-small .mat-badge-content {
font-size: $mat-badge-font-size / 2;
}

.mat-badge-large .mat-badge-content {
font-size: $mat-badge-font-size * 2;
}
}

.mat-badge {
position: relative;
}

.mat-badge-hidden {
.mat-badge-content {
display: none;
}
}

.mat-badge-content {
position: absolute;
text-align: center;
display: inline-block;
border-radius: 50%;
transition: all 0.2 ease-in-out;
transform: scale(0.6);
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
pointer-events: none;
}

// The active class is added after the element is added
// so it can animate scale to default
.mat-badge-content.mat-badge-active {
transform: scale(1);
}

.mat-badge-small {
@include _mat-badge-size($mat-badge-small-size);
}
.mat-badge-medium {
@include _mat-badge-size($mat-badge-default-size);
}
.mat-badge-large {
@include _mat-badge-size($mat-badge-large-size);
}
27 changes: 27 additions & 0 deletions src/lib/badge/badge-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {NgModule} from '@angular/core';
import {MatCommonModule} from '@angular/material/core';
import {A11yModule} from '@angular/cdk/a11y';
import {MatBadge} from './badge';


@NgModule({
imports: [
MatCommonModule,
A11yModule,
],
exports: [
MatBadge,
],
declarations: [
MatBadge,
],
})
export class MatBadgeModule {}
Loading