Skip to content

Commit

Permalink
feat(chips): add icon support
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 518397757
  • Loading branch information
asyncLiz authored and copybara-github committed Mar 21, 2023
1 parent eed1abd commit 7e02a15
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 23 deletions.
41 changes: 39 additions & 2 deletions chips/lib/_shared.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
}

.container {
align-items: center;
align-items: baseline;
appearance: none;
background: none;
border: none;
Expand All @@ -51,7 +51,7 @@
gap: 8px;
height: 100%;
outline: none;
padding: 0 16px;
padding: 0 8px;
position: relative;
text-decoration: none;
width: 100%;
Expand Down Expand Up @@ -132,8 +132,11 @@
}

.label {
align-items: center;
color: var(--_label-text-color);
display: flex;
font: var(--_label-text-type);
height: 100%;
text-overflow: ellipsis;
user-select: none;
white-space: nowrap;
Expand All @@ -143,4 +146,38 @@
color: var(--_disabled-label-text-color);
opacity: var(--_disabled-label-text-opacity);
}

.icon {
align-self: center;
}

.icon ::slotted(*) {
font-size: inherit;
height: 100%;
width: 100%;
}

.icon.leading {
color: var(--_leading-icon-color);
font-size: var(--_leading-icon-size);
max-height: var(--_leading-icon-size);
max-width: var(--_leading-icon-size);
}

:hover .icon.leading {
color: var(--_hover-leading-icon-color);
}

:focus .icon.leading {
color: var(--_focus-leading-icon-color);
}

:active .icon.leading {
color: var(--_pressed-leading-icon-color);
}

.disabled .icon.leading {
color: var(--_disabled-leading-icon-color);
opacity: var(--_disabled-leading-icon-opacity);
}
}
15 changes: 13 additions & 2 deletions chips/lib/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../../elevation/elevation.js';
import '../../focus/focus-ring.js';
import '../../ripple/ripple.js';

import {html, LitElement, nothing} from 'lit';
import {html, LitElement, nothing, TemplateResult} from 'lit';
import {property, queryAsync, state} from 'lit/decorators.js';
import {classMap} from 'lit/directives/class-map.js';
import {when} from 'lit/directives/when.js';
Expand Down Expand Up @@ -52,11 +52,22 @@ export class Chip extends LitElement {
<md-elevation shadow=${this.elevated} surface></md-elevation>
${when(this.showRipple, this.renderRipple)}
<md-focus-ring .visible=${this.showFocusRing}></md-focus-ring>
<div class="label">${this.label}</div>
<span class="icon leading">
<slot name="leading-icon"></slot>
</span>
<span class="label">${this.label}</span>
<span class="icon trailing">
${this.renderTrailingIcon?.() || nothing}
</span>
</${button}>
`;
}

// Not all chip variants have a trailing icon. We still render a wrapper
// <span class="icon trailing"> to compute the correct padding + gap of the
// button.
protected renderTrailingIcon?: () => TemplateResult;

private readonly getRipple = () => { // bind to this
this.showRipple = true;
return this.ripple;
Expand Down
39 changes: 29 additions & 10 deletions tokens/_md-comp-assist-chip.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
//
//

// go/keep-sorted start
@use 'sass:map';
Expand All @@ -16,26 +16,45 @@
@use './v0_161/md-comp-assist-chip';
// go/keep-sorted end

$_default: (
'md-sys-color': md-sys-color.values-light(),
'md-sys-elevation': md-sys-elevation.values(),
'md-sys-shape': md-sys-shape.values(),
'md-sys-state': md-sys-state.values(),
'md-sys-typescale': md-sys-typescale.values(),
);

$_unsupported-tokens: (
// go/keep-sorted start
'dragged-leading-icon-color',
'dragged-state-layer-color',
'dragged-state-layer-opacity',
'label-text-font',
'label-text-line-height',
'label-text-size',
'label-text-tracking',
'label-text-weight'
'label-text-weight' // go/keep-sorted end
);

$_renamed-tokens: (
// go/keep-sorted start
'with-icon-disabled-icon-color': 'disabled-leading-icon-color',
'with-icon-disabled-icon-opacity': 'disabled-leading-icon-opacity',
'with-icon-dragged-icon-color': 'dragged-leading-icon-color',
'with-icon-focus-icon-color': 'focus-leading-icon-color',
'with-icon-hover-icon-color': 'hover-leading-icon-color',
'with-icon-icon-color': 'leading-icon-color',
'with-icon-icon-size': 'leading-icon-size',
'with-icon-pressed-icon-color': 'pressed-leading-icon-color',
// go/keep-sorted end
);

$_default: (
'md-sys-color': md-sys-color.values-light(),
'md-sys-elevation': md-sys-elevation.values(),
'md-sys-shape': md-sys-shape.values(),
'md-sys-state': md-sys-state.values(),
'md-sys-typescale': md-sys-typescale.values(),
);

@function values($deps: $_default, $exclude-hardcoded-values: false) {
$tokens: md-comp-assist-chip.values($deps, $exclude-hardcoded-values);
@each $old-token, $new-token in $_renamed-tokens {
$tokens: map.set($tokens, $new-token, map.get($tokens, $old-token));
$tokens: map.remove($tokens, $old-token);
}
$tokens: map.remove($tokens, $_unsupported-tokens...);
@each $token, $value in $tokens {
$tokens: map.set(
Expand Down
41 changes: 32 additions & 9 deletions tokens/_md-comp-suggestion-chip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,49 @@
@use './v0_161/md-comp-suggestion-chip';
// go/keep-sorted end

$_default: (
'md-sys-color': md-sys-color.values-light(),
'md-sys-elevation': md-sys-elevation.values(),
'md-sys-shape': md-sys-shape.values(),
'md-sys-state': md-sys-state.values(),
'md-sys-typescale': md-sys-typescale.values(),
);

$_unsupported-tokens: (
// go/keep-sorted start
'dragged-leading-icon-color',
'dragged-state-layer-color',
'dragged-state-layer-opacity',
'label-text-font',
'label-text-line-height',
'label-text-size',
'label-text-tracking',
'label-text-weight'
'label-text-weight',
// go/keep-sorted end
);

$_renamed-tokens: (
// go/keep-sorted start
'with-leading-icon-disabled-leading-icon-color': 'disabled-leading-icon-color',
'with-leading-icon-disabled-leading-icon-opacity':
'disabled-leading-icon-opacity',
'with-leading-icon-dragged-leading-icon-color': 'dragged-leading-icon-color',
'with-leading-icon-focus-leading-icon-color': 'focus-leading-icon-color',
'with-leading-icon-hover-leading-icon-color': 'hover-leading-icon-color',
'with-leading-icon-leading-icon-color': 'leading-icon-color',
'with-leading-icon-leading-icon-size': 'leading-icon-size',
'with-leading-icon-pressed-leading-icon-color': 'pressed-leading-icon-color',
// go/keep-sorted end
);

$_default: (
// go/keep-sorted start
'md-sys-color': md-sys-color.values-light(),
'md-sys-elevation': md-sys-elevation.values(),
'md-sys-shape': md-sys-shape.values(),
'md-sys-state': md-sys-state.values(),
'md-sys-typescale': md-sys-typescale.values(),
// go/keep-sorted end
);

@function values($deps: $_default, $exclude-hardcoded-values: false) {
$tokens: md-comp-suggestion-chip.values($deps, $exclude-hardcoded-values);
@each $old-token, $new-token in $_renamed-tokens {
$tokens: map.set($tokens, $new-token, map.get($tokens, $old-token));
$tokens: map.remove($tokens, $old-token);
}
$tokens: map.remove($tokens, $_unsupported-tokens...);
@each $token, $value in $tokens {
$tokens: map.set(
Expand Down

0 comments on commit 7e02a15

Please sign in to comment.