Skip to content

Commit f45c315

Browse files
topherfangiojelbourn
authored andcommitted
feat(chips): add (select), [color] and dark theme to chips. (#2242)
Add new functionality/options to chips for managing selection/color. *MdChipList Options* - `[selectable]` - Programmatically control whether or not the chips in the list are capable of being selected. - The SPACE key will automatically select the currently focused chip. *MdChip Options* - `[color]` - Programmatically control the selected color of the chip. - `[selected]` - Programmatically control whether or not the chip is selected. - `(select)` - Event emitted when the chip is selected. - `(deselect)` - Event emitted when the chip is deselected. Additionally, adds basic support for dark themeed chips using existing colors from other components in the spec and cleanup demos by using cards and toolbars like other demos. References #120.
1 parent e4d2bef commit f45c315

File tree

9 files changed

+392
-127
lines changed

9 files changed

+392
-127
lines changed

src/demo-app/chips/chips-demo.html

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
11
<div class="chips-demo">
2-
<section>
3-
<h3>Static Chips</h3>
4-
5-
<h5>Simple</h5>
6-
7-
<md-chip-list>
8-
<md-chip>Chip 1</md-chip>
9-
<md-chip>Chip 2</md-chip>
10-
<md-chip>Chip 3</md-chip>
11-
</md-chip-list>
12-
13-
<h5>Advanced</h5>
14-
15-
<md-chip-list>
16-
<md-chip class="md-accent selected">Selected/Colored</md-chip>
17-
<md-chip class="md-warn" *ngIf="visible"
18-
(destroy)="alert('chip destroyed')" (click)="toggleVisible()">
19-
With Events
20-
</md-chip>
21-
</md-chip-list>
22-
23-
<h5>Unstyled</h5>
24-
25-
<md-chip-list>
26-
<md-basic-chip>Basic Chip 1</md-basic-chip>
27-
<md-basic-chip>Basic Chip 2</md-basic-chip>
28-
<md-basic-chip>Basic Chip 3</md-basic-chip>
29-
</md-chip-list>
30-
31-
<h3>Material Contributors</h3>
32-
33-
<md-chip-list>
34-
<md-chip *ngFor="let person of people; let even = even" [ngClass]="[color, even ? 'selected' : '' ]">
35-
{{person.name}}
36-
</md-chip>
37-
</md-chip-list>
38-
39-
<br />
40-
41-
<md-input #input (keyup.enter)="add(input)" (blur)="add(input)" placeholder="New Contributor...">
42-
</md-input>
43-
44-
<h3>Stacked Chips</h3>
45-
46-
<p>
47-
You can also stack the chips if you want them on top of each other.
48-
</p>
49-
50-
<md-chip-list class="md-chip-list-stacked">
51-
<md-chip (focus)="color = ''" class="selected">
52-
None
53-
</md-chip>
54-
55-
<md-chip (focus)="color = 'md-primary'" class="selected md-primary">
56-
Primary
57-
</md-chip>
58-
59-
<md-chip (focus)="color = 'md-accent'" class="selected md-accent">
60-
Accent
61-
</md-chip>
62-
63-
<md-chip (focus)="color = 'md-warn'" class="selected md-warn">
64-
Warn
65-
</md-chip>
66-
</md-chip-list>
67-
</section>
2+
<md-card>
3+
<md-toolbar color="primary">Static Chips</md-toolbar>
4+
5+
<md-card-content>
6+
<h4>Simple</h4>
7+
8+
<md-chip-list>
9+
<md-chip>Chip 1</md-chip>
10+
<md-chip>Chip 2</md-chip>
11+
<md-chip>Chip 3</md-chip>
12+
</md-chip-list>
13+
14+
<h4>Unstyled</h4>
15+
16+
<md-chip-list>
17+
<md-basic-chip>Basic Chip 1</md-basic-chip>
18+
<md-basic-chip>Basic Chip 2</md-basic-chip>
19+
<md-basic-chip>Basic Chip 3</md-basic-chip>
20+
</md-chip-list>
21+
22+
<h4>Advanced</h4>
23+
24+
<md-chip-list selectable="false">
25+
<md-chip color="accent" selected="true">Selected/Colored</md-chip>
26+
<md-chip color="warn" selected="true" *ngIf="visible"
27+
(destroy)="alert('chip destroyed')" (click)="toggleVisible()">
28+
With Events
29+
</md-chip>
30+
</md-chip-list>
31+
</md-card-content>
32+
</md-card>
33+
34+
<md-card>
35+
<md-toolbar color="primary">Dynamic Chips</md-toolbar>
36+
37+
<md-card-content>
38+
<h4>Input Container</h4>
39+
40+
<md-chip-list>
41+
<md-chip *ngFor="let person of people" [color]="color">
42+
{{person.name}}
43+
</md-chip>
44+
</md-chip-list>
45+
46+
<md-input-container>
47+
<input md-input #input (keyup.enter)="add(input)" placeholder="New Contributor..."/>
48+
</md-input-container>
49+
50+
<h4>Stacked Chips</h4>
51+
52+
<p>
53+
You can also stack the chips if you want them on top of each other and/or use the
54+
<code>(focus)</code> event to run custom code.
55+
</p>
56+
57+
<md-chip-list class="md-chip-list-stacked">
58+
<md-chip *ngFor="let aColor of availableColors"
59+
(focus)="color = aColor.color" color="{{aColor.color}}" selected="true">
60+
{{aColor.name}}
61+
</md-chip>
62+
</md-chip-list>
63+
</md-card-content>
64+
</md-card>
6865
</div>

src/demo-app/chips/chips-demo.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
max-width: 200px;
55
}
66

7+
md-card {
8+
padding: 0;
9+
margin: 16px;
10+
11+
& md-toolbar {
12+
margin: 0;
13+
}
14+
15+
& md-card-content {
16+
padding: 24px;
17+
}
18+
}
19+
720
md-basic-chip {
821
margin: auto 10px;
922
}

src/demo-app/chips/chips-demo.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ export interface Person {
55
name: string;
66
}
77

8+
export interface DemoColor {
9+
name: string;
10+
color: string;
11+
}
12+
813
@Component({
914
moduleId: module.id,
1015
selector: 'chips-demo',
@@ -24,6 +29,13 @@ export class ChipsDemo {
2429
{ name: 'Paul' }
2530
];
2631

32+
availableColors: DemoColor[] = [
33+
{ name: 'none', color: '' },
34+
{ name: 'Primary', color: 'primary' },
35+
{ name: 'Accent', color: 'accent' },
36+
{ name: 'Warn', color: 'warn' }
37+
];
38+
2739
alert(message: string): void {
2840
alert(message);
2941
}

src/lib/chips/_chips-theme.scss

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import '../core/theming/palette';
12
@import '../core/theming/theming';
23

34
@mixin md-chips-theme($theme) {
@@ -6,27 +7,40 @@
67
$accent: map-get($theme, accent);
78
$warn: map-get($theme, warn);
89
$background: map-get($theme, background);
10+
$foreground: map-get($theme, foreground);
11+
12+
// Use spec-recommended color for regular foreground, and utilise contrast color for a grey very
13+
// close to the selected spec since no guidance is provided and to ensure palette consistency.
14+
$light-foreground: rgba(0, 0, 0, 0.87);
15+
$light-selected-foreground: md-contrast($md-grey, 600);
16+
17+
// The spec only provides guidance for light-themed chips. When inside of a dark theme, fall back
18+
// to standard background and foreground colors.
19+
$unselected-background: if($is-dark-theme, md-color($background, card), #e0e0e0);
20+
$unselected-foreground: if($is-dark-theme, md-color($foreground, text), $light-foreground);
21+
22+
$selected-background: if($is-dark-theme, md-color($background, app-bar), #808080);
23+
$selected-foreground: if($is-dark-theme, md-color($foreground, text), $light-selected-foreground);
924

1025
.md-chip {
11-
background-color: #e0e0e0;
12-
color: rgba(0, 0, 0, 0.87);
26+
background-color: $unselected-background;
27+
color: $unselected-foreground;
1328
}
1429

15-
.md-chip.selected {
16-
// There is no dark theme in the current spec, so this applies to both
17-
background-color: #808080;
18-
19-
// Use a contrast color for a grey very close to the background color
20-
color: md-contrast($md-grey, 600);
30+
.md-chip.md-chip-selected {
31+
background-color: $selected-background;
32+
color: $selected-foreground;
2133

2234
&.md-primary {
2335
background-color: md-color($primary, 500);
2436
color: md-contrast($primary, 500);
2537
}
38+
2639
&.md-accent {
2740
background-color: md-color($accent, 500);
2841
color: md-contrast($accent, 500);
2942
}
43+
3044
&.md-warn {
3145
background-color: md-color($warn, 500);
3246
color: md-contrast($warn, 500);

0 commit comments

Comments
 (0)