/** * @license * Copyright 2023 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import {html, nothing} from 'lit'; import {property, query} from 'lit/decorators.js'; import {ARIAMixinStrict} from '../../internal/aria/aria.js'; import {MultiActionChip} from './multi-action-chip.js'; import {renderRemoveButton} from './trailing-icons.js'; /** * An input chip component. * * @fires remove {Event} Dispatched when the remove button is clicked. */ export class InputChip extends MultiActionChip { @property({type: Boolean}) avatar = false; @property() href = ''; @property() target: '_blank' | '_parent' | '_self' | '_top' | '' = ''; @property({type: Boolean, attribute: 'remove-only'}) removeOnly = false; @property({type: Boolean, reflect: true}) selected = false; protected get primaryId() { if (this.href) { return 'link'; } if (this.removeOnly) { return ''; } return 'button'; } protected override get rippleDisabled() { // Link chips cannot be disabled return !this.href && (this.disabled || this.softDisabled); } protected get primaryAction() { // Don't use @query() since a remove-only input chip still has a span that // has "primary action" classes. if (this.removeOnly) { return null; } return this.renderRoot.querySelector('.primary.action'); } @query('.trailing.action') protected readonly trailingAction!: HTMLElement | null; protected override getContainerClasses() { return { ...super.getContainerClasses(), avatar: this.avatar, // Link chips cannot be disabled disabled: !this.href && (this.disabled || this.softDisabled), link: !!this.href, selected: this.selected, 'has-trailing': true, }; } protected override renderPrimaryAction(content: unknown) { const {ariaLabel} = this as ARIAMixinStrict; if (this.href) { return html` ${content} `; } if (this.removeOnly) { return html` ${content} `; } return html` `; } protected override renderTrailingAction(focusListener: EventListener) { return renderRemoveButton({ focusListener, ariaLabel: this.ariaLabelRemove, disabled: !this.href && (this.disabled || this.softDisabled), tabbable: this.removeOnly, }); } }