Skip to content

Commit c9e8de0

Browse files
asynclizcopybara-github
authored andcommitted
feat(chips): add tokens to customize padding
Added chip tokens for `leading-space`, `trailing-space`, `icon-label-space`, `with-leading-icon-leading-space`, and `with-trailing-icon-trailing-space`. PiperOrigin-RevId: 588942324
1 parent 6a0d1b8 commit c9e8de0

File tree

8 files changed

+80
-5
lines changed

8 files changed

+80
-5
lines changed

chips/internal/_shared.scss

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,19 @@
8989
border: none;
9090
border-radius: inherit;
9191
display: flex;
92-
gap: 8px;
9392
outline: none;
9493
padding: 0;
9594
position: relative;
9695
text-decoration: none;
9796
}
9897

9998
.primary.action {
100-
padding-inline-start: 8px;
101-
padding-inline-end: 16px;
99+
padding-inline-start: var(--_leading-space);
100+
padding-inline-end: var(--_trailing-space);
101+
}
102+
103+
.has-icon .primary.action {
104+
padding-inline-start: var(--_with-leading-icon-leading-space);
102105
}
103106

104107
.touch {
@@ -190,6 +193,11 @@
190193
color: var(--_leading-icon-color);
191194
}
192195

196+
.leading.icon ::slotted(*),
197+
.leading.icon svg {
198+
margin-inline-end: var(--_icon-label-space);
199+
}
200+
193201
:where(:hover) .leading.icon {
194202
color: var(--_hover-leading-icon-color);
195203
}

chips/internal/_trailing-icon.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
.trailing.action {
1212
align-items: center;
1313
justify-content: center;
14-
padding: 0 8px;
14+
padding-inline-start: var(--_icon-label-space);
15+
padding-inline-end: var(--_with-trailing-icon-trailing-space);
1516
}
1617

1718
.trailing.action :is(md-ripple, md-focus-ring) {

chips/internal/chip.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ export abstract class Chip extends LitElement {
5151
*/
5252
@property() label = '';
5353

54+
/**
55+
* Only needed for SSR.
56+
*
57+
* Add this attribute when a chip has a `slot="icon"` to avoid a Flash Of
58+
* Unstyled Content.
59+
*/
60+
@property({type: Boolean, reflect: true, attribute: 'has-icon'}) hasIcon =
61+
false;
62+
5463
/**
5564
* The `id` of the action the primary focus ring and ripple are for.
5665
* TODO(b/310046938): use the same id for both elements
@@ -90,6 +99,7 @@ export abstract class Chip extends LitElement {
9099
protected getContainerClasses(): ClassInfo {
91100
return {
92101
'disabled': this.disabled,
102+
'has-icon': this.hasIcon,
93103
};
94104
}
95105

@@ -109,7 +119,7 @@ export abstract class Chip extends LitElement {
109119
}
110120

111121
protected renderLeadingIcon(): TemplateResult {
112-
return html`<slot name="icon"></slot>`;
122+
return html`<slot name="icon" @slotchange=${this.handleIconChange}></slot>`;
113123
}
114124

115125
protected abstract renderPrimaryAction(content: unknown): unknown;
@@ -123,4 +133,9 @@ export abstract class Chip extends LitElement {
123133
<span class="touch"></span>
124134
`;
125135
}
136+
137+
private handleIconChange(event: Event) {
138+
const slot = event.target as HTMLSlotElement;
139+
this.hasIcon = slot.assignedElements({flatten: true}).length > 0;
140+
}
126141
}

chips/internal/filter-chip.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class FilterChip extends MultiActionChip {
4040
elevated: this.elevated,
4141
selected: this.selected,
4242
'has-trailing': this.removable,
43+
'has-icon': this.hasIcon || this.selected,
4344
};
4445
}
4546

tokens/_md-comp-assist-chip.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@ $supported-tokens: (
4242
'hover-leading-icon-color',
4343
'hover-state-layer-color',
4444
'hover-state-layer-opacity',
45+
'icon-label-space',
4546
'icon-size',
4647
'label-text-color',
4748
'label-text-font',
4849
'label-text-line-height',
4950
'label-text-size',
5051
'label-text-weight',
5152
'leading-icon-color',
53+
'leading-space',
5254
'outline-color',
5355
'outline-width',
5456
'pressed-label-text-color',
5557
'pressed-leading-icon-color',
5658
'pressed-state-layer-color',
5759
'pressed-state-layer-opacity',
60+
'trailing-space',
61+
'with-leading-icon-leading-space',
5862
// go/keep-sorted end
5963
);
6064

@@ -86,6 +90,13 @@ $_default: (
8690
md-comp-assist-chip.values($deps, $exclude-hardcoded-values),
8791
$supported-tokens: $supported-tokens,
8892
$unsupported-tokens: $unsupported-tokens,
93+
$new-tokens: (
94+
'leading-space': if($exclude-hardcoded-values, null, 16px),
95+
'trailing-space': if($exclude-hardcoded-values, null, 16px),
96+
'icon-label-space': if($exclude-hardcoded-values, null, 8px),
97+
'with-leading-icon-leading-space':
98+
if($exclude-hardcoded-values, null, 8px),
99+
),
89100
$renamed-tokens: (
90101
// Remove "flat-*" prefix (b/275577569)
91102
'flat-container-elevation': 'container-elevation',

tokens/_md-comp-filter-chip.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ $supported-tokens: (
4949
'hover-state-layer-color',
5050
'hover-state-layer-opacity',
5151
'hover-trailing-icon-color',
52+
'icon-label-space',
5253
'icon-size',
5354
'label-text-color',
5455
'label-text-font',
5556
'label-text-line-height',
5657
'label-text-size',
5758
'label-text-weight',
5859
'leading-icon-color',
60+
'leading-space',
5961
'outline-color',
6062
'outline-width',
6163
'pressed-label-text-color',
@@ -82,6 +84,9 @@ $supported-tokens: (
8284
'selected-pressed-trailing-icon-color',
8385
'selected-trailing-icon-color',
8486
'trailing-icon-color',
87+
'trailing-space',
88+
'with-leading-icon-leading-space',
89+
'with-trailing-icon-trailing-space',
8590
// go/keep-sorted end
8691
);
8792

@@ -127,6 +132,15 @@ $_default: (
127132
md-comp-filter-chip.values($deps, $exclude-hardcoded-values),
128133
$supported-tokens: $supported-tokens,
129134
$unsupported-tokens: $unsupported-tokens,
135+
$new-tokens: (
136+
'leading-space': if($exclude-hardcoded-values, null, 16px),
137+
'trailing-space': if($exclude-hardcoded-values, null, 16px),
138+
'icon-label-space': if($exclude-hardcoded-values, null, 8px),
139+
'with-leading-icon-leading-space':
140+
if($exclude-hardcoded-values, null, 8px),
141+
'with-trailing-icon-trailing-space':
142+
if($exclude-hardcoded-values, null, 8px),
143+
),
130144
$renamed-tokens: (
131145
// Remove "unselected-*" and "flat-*" prefix (b/275577569)
132146
'elevated-unselected-container-color': 'elevated-container-color',

tokens/_md-comp-input-chip.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ $supported-tokens: (
4242
'hover-state-layer-color',
4343
'hover-state-layer-opacity',
4444
'hover-trailing-icon-color',
45+
'icon-label-space',
4546
'icon-size',
4647
'label-text-color',
4748
'label-text-font',
4849
'label-text-line-height',
4950
'label-text-size',
5051
'label-text-weight',
5152
'leading-icon-color',
53+
'leading-space',
5254
'outline-color',
5355
'outline-width',
5456
'pressed-label-text-color',
@@ -75,6 +77,9 @@ $supported-tokens: (
7577
'selected-pressed-trailing-icon-color',
7678
'selected-trailing-icon-color',
7779
'trailing-icon-color',
80+
'trailing-space',
81+
'with-leading-icon-leading-space',
82+
'with-trailing-icon-trailing-space',
7883
// go/keep-sorted end
7984
);
8085

@@ -115,6 +120,15 @@ $_default: (
115120
md-comp-input-chip.values($deps, $exclude-hardcoded-values),
116121
$supported-tokens: $supported-tokens,
117122
$unsupported-tokens: $unsupported-tokens,
123+
$new-tokens: (
124+
'leading-space': if($exclude-hardcoded-values, null, 16px),
125+
'trailing-space': if($exclude-hardcoded-values, null, 16px),
126+
'icon-label-space': if($exclude-hardcoded-values, null, 8px),
127+
'with-leading-icon-leading-space':
128+
if($exclude-hardcoded-values, null, 8px),
129+
'with-trailing-icon-trailing-space':
130+
if($exclude-hardcoded-values, null, 8px),
131+
),
118132
$renamed-tokens: (
119133
// Remove "unselected-*" prefix (b/275577569)
120134
'disabled-unselected-outline-color': 'disabled-outline-color',

tokens/_md-comp-suggestion-chip.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@ $supported-tokens: (
4242
'hover-leading-icon-color',
4343
'hover-state-layer-color',
4444
'hover-state-layer-opacity',
45+
'icon-label-space',
4546
'icon-size',
4647
'label-text-color',
4748
'label-text-font',
4849
'label-text-line-height',
4950
'label-text-size',
5051
'label-text-weight',
5152
'leading-icon-color',
53+
'leading-space',
5254
'outline-color',
5355
'outline-width',
5456
'pressed-label-text-color',
5557
'pressed-leading-icon-color',
5658
'pressed-state-layer-color',
5759
'pressed-state-layer-opacity',
60+
'trailing-space',
61+
'with-leading-icon-leading-space',
5862
// go/keep-sorted end
5963
);
6064

@@ -86,6 +90,13 @@ $_default: (
8690
md-comp-suggestion-chip.values($deps, $exclude-hardcoded-values),
8791
$supported-tokens: $supported-tokens,
8892
$unsupported-tokens: $unsupported-tokens,
93+
$new-tokens: (
94+
'leading-space': if($exclude-hardcoded-values, null, 16px),
95+
'trailing-space': if($exclude-hardcoded-values, null, 16px),
96+
'icon-label-space': if($exclude-hardcoded-values, null, 8px),
97+
'with-leading-icon-leading-space':
98+
if($exclude-hardcoded-values, null, 8px),
99+
),
89100
$renamed-tokens: (
90101
// Remove "flat-*" prefix (b/275577569)
91102
'flat-container-elevation': 'container-elevation',

0 commit comments

Comments
 (0)