shoelace/src/components/tag/tag.ts

91 wiersze
2.3 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, unsafeCSS } from 'lit';
2021-05-27 21:00:43 +00:00
import { customElement, property } from 'lit/decorators.js';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
import { emit } from '../../internal/event';
2021-02-26 14:09:13 +00:00
import styles from 'sass:./tag.scss';
/**
* @since 2.0
* @status stable
*
* @dependency sl-icon-button
*
* @slot default The tag's content.
2021-02-26 14:09:13 +00:00
*
* @event sl-clear Emitted when the clear button is activated.
*
* @csspart base The component's base wrapper.
* @csspart content The tag content.
* @csspart clear-button The clear button.
2021-02-26 14:09:13 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-tag')
2021-03-09 00:14:32 +00:00
export default class SlTag extends LitElement {
2021-03-06 17:01:39 +00:00
static styles = unsafeCSS(styles);
2021-02-26 14:09:13 +00:00
/** The tag's type. */
2021-03-06 17:01:39 +00:00
@property({ reflect: true }) type: 'primary' | 'success' | 'info' | 'warning' | 'danger' | 'text' = 'primary';
2021-02-26 14:09:13 +00:00
/** The tag's size. */
2021-03-06 17:01:39 +00:00
@property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium';
2021-02-26 14:09:13 +00:00
/** Draws a pill-style tag with rounded edges. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) pill = false;
2021-02-26 14:09:13 +00:00
/** Makes the tag clearable. */
2021-03-06 20:34:33 +00:00
@property({ type: Boolean }) clearable = false;
2021-03-06 17:01:39 +00:00
2021-02-26 14:09:13 +00:00
handleClearClick() {
emit(this, 'sl-clear');
2021-02-26 14:09:13 +00:00
}
render() {
return html`
<span
part="base"
class=${classMap({
tag: true,
// Types
'tag--primary': this.type === 'primary',
'tag--success': this.type === 'success',
'tag--info': this.type === 'info',
'tag--warning': this.type === 'warning',
'tag--danger': this.type === 'danger',
'tag--text': this.type === 'text',
// Sizes
'tag--small': this.size === 'small',
'tag--medium': this.size === 'medium',
'tag--large': this.size === 'large',
// Modifers
'tag--pill': this.pill,
'tag--clearable': this.clearable
})}
>
<span part="content" class="tag__content">
2021-03-06 17:01:39 +00:00
<slot></slot>
2021-02-26 14:09:13 +00:00
</span>
${this.clearable
? html`
<sl-icon-button
exportparts="base:clear-button"
name="x"
library="system"
2021-02-26 14:09:13 +00:00
class="tag__clear"
2021-03-06 17:01:39 +00:00
@click=${this.handleClearClick}
></sl-icon-button>
2021-02-26 14:09:13 +00:00
`
: ''}
</span>
`;
}
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-tag': SlTag;
}
}