Skip to content

Commit 95316e9

Browse files
committed
feat(chips): Add remove functionality/styling.
Add events, styling and keyboard controls to allow removable chips. - Add basic styling for a user-provided remove icon. - Add keyboard controls for backspace/delete. - Add `(remove)` event which is emitted when the remove icon or one of the delete keys is pressed. - Add `md-chip-remove` directive which can be applied to `<md-icon>` (or others) to inform the chip of the remove request. Additionally, fix some issues with dark theme and add styling/support for usage inside the `md-input-container` and add styling for focused chips. BREAKING CHANGE - The `selectable` property of the `md-chip-list` has now been moved to `md-chip` to maintain consistency with the new `removable` option. If you used the following code, ```html <md-chip-list [selectable]="selectable"> <md-chip>My Chip</md-chip> </md-chip-list> ``` you should switch it to ```html <md-chip-list> <md-chip [selectable]="selectable">My Chip</md-chip> </md-chip-list> ``` References #120. Add basic focus support.
1 parent dccbe41 commit 95316e9

File tree

12 files changed

+904
-268
lines changed

12 files changed

+904
-268
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ <h4>Advanced</h4>
2323

2424
<md-chip-list selectable="false">
2525
<md-chip color="accent" selected="true">Selected/Colored</md-chip>
26+
2627
<md-chip color="warn" selected="true" *ngIf="visible"
27-
(destroy)="alert('chip destroyed')" (click)="toggleVisible()">
28+
(destroy)="alert('chip destroyed')" (remove)="toggleVisible()">
29+
<md-icon md-chip-remove>cancel</md-icon>
2830
With Events
2931
</md-chip>
3032
</md-chip-list>
@@ -37,16 +39,28 @@ <h4>Advanced</h4>
3739
<md-card-content>
3840
<h4>Input Container</h4>
3941

40-
<md-chip-list>
41-
<md-chip *ngFor="let person of people" [color]="color">
42-
{{person.name}}
43-
</md-chip>
44-
</md-chip-list>
42+
<p>
43+
You can easily put the the <code>&lt;md-chip-list&gt;</code> inside of an
44+
<code>&lt;md-input-container&gt;</code>.
45+
</p>
4546

4647
<md-input-container>
47-
<input md-input #input (keyup.enter)="add(input)" placeholder="New Contributor..."/>
48+
<md-chip-list>
49+
<md-chip *ngFor="let person of people" [color]="color" [selectable]="selectable"
50+
[removable]="removable" (remove)="remove(person)">
51+
{{person.name}}
52+
<md-icon md-chip-remove>cancel</md-icon>
53+
</md-chip>
54+
55+
<input md-input #input (keyup.enter)="add(input)" placeholder="Material contributors..." />
56+
</md-chip-list>
4857
</md-input-container>
4958

59+
<p>
60+
<md-checkbox name="selectable" [(ngModel)]="selectable">Selectable</md-checkbox>
61+
<md-checkbox name="removable" [(ngModel)]="removable">Removable</md-checkbox>
62+
</p>
63+
5064
<h4>Stacked Chips</h4>
5165

5266
<p>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
.md-basic-chip {
2121
margin: auto 10px;
2222
}
23+
24+
md-chip-list input {
25+
width: 150px;
26+
}
2327
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface DemoColor {
1919
export class ChipsDemo {
2020
visible: boolean = true;
2121
color: string = '';
22+
selectable: boolean = true;
23+
removable: boolean = true;
2224

2325
people: Person[] = [
2426
{ name: 'Kara' },
@@ -47,6 +49,14 @@ export class ChipsDemo {
4749
}
4850
}
4951

52+
remove(person: Person): void {
53+
let index = this.people.indexOf(person);
54+
55+
if (index >= 0) {
56+
this.people.splice(index, 1);
57+
}
58+
}
59+
5060
toggleVisible(): void {
5161
this.visible = false;
5262
}

src/lib/chips/_chips-theme.scss

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// The spec only provides guidance for light-themed chips. When inside of a dark theme, fall back
1818
// to standard background and foreground colors.
19-
$unselected-background: if($is-dark-theme, md-color($background, card), #e0e0e0);
19+
$unselected-background: if($is-dark-theme, #656565, #e0e0e0);
2020
$unselected-foreground: if($is-dark-theme, md-color($foreground, text), $light-foreground);
2121

2222
$selected-background: if($is-dark-theme, md-color($background, app-bar), #808080);
@@ -25,25 +25,104 @@
2525
.md-chip:not(.md-basic-chip) {
2626
background-color: $unselected-background;
2727
color: $unselected-foreground;
28+
29+
.md-chip-focus-border {
30+
pointer-events: none;
31+
}
32+
33+
&:focus {
34+
outline: none;
35+
36+
.md-chip-focus-border {
37+
border-color: $unselected-foreground;
38+
}
39+
40+
&.md-primary {
41+
.md-chip-focus-border {
42+
border-color: md-color($primary, 500);
43+
}
44+
}
45+
46+
&.md-accent {
47+
.md-chip-focus-border {
48+
border-color: md-color($accent, 500);
49+
}
50+
}
51+
52+
&.md-warn {
53+
.md-chip-focus-border {
54+
border-color: md-color($warn, 500);
55+
}
56+
}
57+
}
58+
59+
.md-chip-remove {
60+
color: $unselected-foreground;
61+
opacity: 0.3;
62+
63+
&:hover {
64+
opacity: 0.54;
65+
}
66+
}
2867
}
2968

3069
.md-chip.md-chip-selected:not(.md-basic-chip) {
3170
background-color: $selected-background;
3271
color: $selected-foreground;
3372

73+
&:focus .md-chip-focus-border {
74+
border-color: $selected-foreground;
75+
}
76+
77+
.md-chip-remove {
78+
color: $selected-foreground;
79+
opacity: 0.4;
80+
81+
&:hover {
82+
opacity: 0.54;
83+
}
84+
}
85+
3486
&.md-primary {
3587
background-color: md-color($primary, 500);
3688
color: md-contrast($primary, 500);
89+
90+
.md-chip-remove {
91+
color: md-contrast($primary, 500);
92+
opacity: 0.4;
93+
94+
&:hover {
95+
opacity: 0.54;
96+
}
97+
}
3798
}
3899

39100
&.md-accent {
40101
background-color: md-color($accent, 500);
41102
color: md-contrast($accent, 500);
103+
104+
.md-chip-remove {
105+
color: md-contrast($accent, 500);
106+
opacity: 0.4;
107+
108+
&:hover {
109+
opacity: 0.54;
110+
}
111+
}
42112
}
43113

44114
&.md-warn {
45115
background-color: md-color($warn, 500);
46116
color: md-contrast($warn, 500);
117+
118+
.md-chip-remove {
119+
color: md-contrast($warn, 500);
120+
opacity: 0.4;
121+
122+
&:hover {
123+
opacity: 0.54;
124+
}
125+
}
47126
}
48127
}
49128
}

0 commit comments

Comments
 (0)