Skip to content
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

feat(badge): add badge component #7483

Merged
merged 20 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from 8 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
84 changes: 84 additions & 0 deletions src/demo-app/badge/badge-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<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


<h3>Text</h3>
<span [matBadge]="badgeContent" 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>

<input type="text" [(ngModel)]="badgeContent" />

<br />
<br />
<br />

<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="primary" 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>

<br />
Copy link
Member

Choose a reason for hiding this comment

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

Use a div with a margin instead of br-s?

<br />
<br />

<h3>Icons</h3>
<mat-icon color="primary" [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>

<br />
<br />
<br />

<h3>Font Icons</h3>
<span matIconBadge="home" matBadgeOverlap="false">Home</span> is where you live

<br />
<br />
<br />

<h3>SVG Icons</h3>
<span [matSvgIconBadge]="svgIcon" matBadgeOverlap="false">Home</span> is where I live

<button mat-button (click)="svgIcon = 'home'">Change Icon</button>

</div>
Empty file.
25 changes: 25 additions & 0 deletions src/demo-app/badge/badge-demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Component} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';
import {MatIconRegistry} from '@angular/material';

@Component({
moduleId: module.id,
selector: 'badge-demo',
templateUrl: 'badge-demo.html',
styleUrls: ['badge-demo.css'],
})
export class BadgeDemo {

badgeContent = '1';
svgIcon = 'cat';

constructor(_iconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
_iconRegistry.addSvgIcon('home',
sanitizer.bypassSecurityTrustResourceUrl(
'https://upload.wikimedia.org/wikipedia/commons/3/34/Home-icon.svg'));
Copy link
Member

Choose a reason for hiding this comment

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

You can use the home icon from the icon demo instead of fetching one remotely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that, however, it was not working. That demo page is broken too if you open it up.


_iconRegistry.addSvgIcon('cat',
sanitizer.bypassSecurityTrustResourceUrl(
'https://upload.wikimedia.org/wikipedia/commons/3/34/Home-icon.svg'));
}
}
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 @@ -43,6 +43,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: 'Button Toggle', route: '/button-toggle'},
{name: 'Button', route: '/button'},
{name: 'Card', route: '/card'},
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 @@ -47,6 +47,7 @@ import {TooltipDemo} from '../tooltip/tooltip-demo';
import {TypographyDemo} from '../typography/typography-demo';
import {DemoApp, Home} from './demo-app';
import {DEMO_APP_ROUTES} from './routes';
import {BadgeDemo} from '../badge/badge-demo';

@NgModule({
imports: [
Expand All @@ -59,6 +60,7 @@ import {DEMO_APP_ROUTES} from './routes';
],
declarations: [
AutocompleteDemo,
BadgeDemo,
BaselineDemo,
ButtonDemo,
ButtonToggleDemo,
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 @@ -41,11 +41,13 @@ import {ToolbarDemo} from '../toolbar/toolbar-demo';
import {TooltipDemo} from '../tooltip/tooltip-demo';
import {TypographyDemo} from '../typography/typography-demo';
import {DemoApp, Home} from './demo-app';
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: 'baseline', component: BaselineDemo},
{path: 'button', component: ButtonDemo},
{path: 'button-toggle', component: ButtonToggleDemo},
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
@@ -1,6 +1,7 @@
import {NgModule} from '@angular/core';
import {
MatAutocompleteModule,
MatBadgeModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
Expand Down Expand Up @@ -46,6 +47,7 @@ import {PortalModule} from '@angular/cdk/portal';
@NgModule({
exports: [
MatAutocompleteModule,
MatBadgeModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
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 @@ -76,6 +76,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
177 changes: 177 additions & 0 deletions src/lib/badge/_badge-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
@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-size: 22px !default;
Copy link
Member

Choose a reason for hiding this comment

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

$mat-badge-default-size ?


.mat-badge {
position: relative;
}

.mat-badge:after,
Copy link
Member

Choose a reason for hiding this comment

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

These :after instances should be ::after. I think we had a linting rule for it at some point.

.mat-badge-icon,
.mat-badge-text,
.mat-badge-svg-icon {
position: absolute;
Copy link
Member

Choose a reason for hiding this comment

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

All of these positioning and alignment styles should be in the badge.scss, not in the theme. The theme is only for color-related properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, however, since this is a directive there is no way to include styles w/ the component. Do you have a good solution for that?

Copy link
Member

Choose a reason for hiding this comment

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

You can make it a component that has a template of <ng-content>. Having these styles in the theme will cause them to be loaded multiple times for pages with multiple themes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya, thats what I had. You can't do multiple components on the same element though, example: icon w/ a badge.

Copy link
Member

Choose a reason for hiding this comment

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

You can't, but you can have an icon inside a badge.

text-align: center;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: center;
align-items: center;
}

.mat-badge:after {
width: $mat-badge-size;
height: $mat-badge-size;
border-radius: 50%;
content: '';
z-index: 1;
}

.mat-badge-text,
.mat-badge .mat-badge-icon {
z-index: 2;
font-size: 16px;
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 be in the typography mixin and the value, ideally, should come from the typography breakpoints.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I've moved it inside that mixin.

This size is independent from the font-size of the page since its specifically designed for this fixed width/height of the badge.

width: $mat-badge-size;
height: $mat-badge-size;
}

.mat-badge-svg-icon {
z-index: 2;
width: 12px;
height: 12px;
Copy link
Member

Choose a reason for hiding this comment

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

Move this into a variable?

z-index: 2;
transform: translate(-5px, 5px);
}

.mat-badge-above {
.mat-badge-svg-icon,
.mat-badge-icon,
.mat-badge-text,
&:after {
top: -$mat-badge-size / 2;
}
}

.mat-badge-below {
.mat-badge-svg-icon,
.mat-badge-icon,
.mat-badge-text,
&:after {
bottom: -$mat-badge-size / 2;
}
}

.mat-badge-before {
margin-left: $mat-badge-size;

.mat-badge-svg-icon,
.mat-badge-icon,
.mat-badge-text,
&:after {
left: -$mat-badge-size;
}
}

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

.mat-badge-svg-icon,
.mat-badge-icon,
.mat-badge-text,
&:after {
right: -$mat-badge-size;
}
}

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

.mat-badge-svg-icon,
.mat-badge-icon,
.mat-badge-text,
&:after {
left: -$mat-badge-size / 2;
}
}

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

.mat-badge-svg-icon,
.mat-badge-icon,
.mat-badge-text,
&:after {
right: -$mat-badge-size / 2;
}
}
}

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

.mat-badge {
.mat-badge-svg-icon {
fill: mat-color($primary, default-contrast);
}

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

&:after {
background: mat-color($primary);
}

&.mat-badge-accent {
.mat-badge-svg-icon {
fill: mat-color($accent, default-contrast);
}

.mat-badge-text,
.mat-badge-icon {
color: mat-color($accent, default-contrast);
}

&:after {
background: mat-color($accent);
}
}

&.mat-badge-warn {
.mat-badge-svg-icon {
fill: mat-color($warn, default-contrast);
}

.mat-badge-text,
.mat-badge-icon {
color: mat-color($warn, default-contrast);
}

&:after {
background: mat-color($warn);
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

There's an extra closing } here (masked by the extra indentation in the mixin block)


@mixin mat-badge-typography($config) {
.mat-badge-svg-icon,
.mat-badge-icon,
.mat-badge-text {
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.

}

.mat-badge-text {
font-family: mat-font-family($config);
}
}
28 changes: 28 additions & 0 deletions src/lib/badge/badge-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright Google Inc. 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 {MatBadge, MatSvgIconBadge, MatIconBadge} from './badge';


@NgModule({
imports: [MatCommonModule],
exports: [
MatBadge,
MatIconBadge,
MatSvgIconBadge,
MatCommonModule,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason to export MatCommonModule here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. thanks

],
declarations: [
MatBadge,
MatIconBadge,
MatSvgIconBadge,
],
})
export class MatBadgeModule {}
Loading