shoelace/src/components/button/button.ts

241 wiersze
7.6 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, unsafeCSS } from 'lit';
2021-05-27 21:00:43 +00:00
import { customElement, property, query, state } from 'lit/decorators.js';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
2021-03-15 11:56:15 +00:00
import { ifDefined } from 'lit-html/directives/if-defined';
import { emit } from '../../internal/event';
2021-02-26 14:09:13 +00:00
import { hasSlot } from '../../internal/slot';
2021-05-03 19:08:17 +00:00
import styles from 'sass:./button.scss';
2021-02-26 14:09:13 +00:00
/**
* @since 2.0
* @status stable
*
* @dependency sl-spinner
*
2021-06-25 20:25:46 +00:00
* @event sl-blur - Emitted when the button loses focus.
* @event sl-focus - Emitted when the button gains focus.
2021-02-26 14:09:13 +00:00
*
2021-06-25 20:25:46 +00:00
* @slot - The button's label.
* @slot prefix - Used to prepend an icon or similar element to the button.
* @slot suffix - Used to append an icon or similar element to the button.
*
2021-06-25 20:25:46 +00:00
* @csspart base - The component's base wrapper.
* @csspart prefix - The prefix container.
* @csspart label - The button's label.
* @csspart suffix - The suffix container.
* @csspart caret - The button's caret.
2021-02-26 14:09:13 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-button')
2021-03-09 00:14:32 +00:00
export default class SlButton extends LitElement {
2021-03-06 17:01:39 +00:00
static styles = unsafeCSS(styles);
2021-03-15 17:03:01 +00:00
@query('.button') button: HTMLButtonElement | HTMLLinkElement;
2021-03-06 17:01:39 +00:00
@state() private hasFocus = false;
@state() private hasLabel = false;
@state() private hasPrefix = false;
@state() private hasSuffix = false;
2021-02-26 14:09:13 +00:00
/** The button's type. */
2021-03-06 17:01:39 +00:00
@property({ reflect: true }) type: 'default' | 'primary' | 'success' | 'info' | 'warning' | 'danger' | 'text' =
'default';
2021-02-26 14:09:13 +00:00
/** The button'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 the button with a caret for use with dropdowns, popovers, etc. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) caret = false;
2021-02-26 14:09:13 +00:00
/** Disables the button. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2021-02-26 14:09:13 +00:00
/** Draws the button in a loading state. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) loading = false;
2021-02-26 14:09:13 +00:00
/** Draws a pill-style button 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
/** Draws a circle button. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) circle = false;
2021-02-26 14:09:13 +00:00
/** Indicates if activating the button should submit the form. Ignored when `href` is set. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean, reflect: true }) submit = false;
2021-02-26 14:09:13 +00:00
/** An optional name for the button. Ignored when `href` is set. */
2021-04-02 11:47:25 +00:00
@property() name: string;
2021-02-26 14:09:13 +00:00
/** An optional value for the button. Ignored when `href` is set. */
2021-04-02 11:47:25 +00:00
@property() value: string;
2021-02-26 14:09:13 +00:00
/** When set, the underlying button will be rendered as an `<a>` with this `href` instead of a `<button>`. */
2021-04-02 11:47:25 +00:00
@property() href: string;
2021-02-26 14:09:13 +00:00
/** Tells the browser where to open the link. Only used when `href` is set. */
2021-03-06 17:01:39 +00:00
@property() target: '_blank' | '_parent' | '_self' | '_top';
2021-02-26 14:09:13 +00:00
/** Tells the browser to download the linked file as this filename. Only used when `href` is set. */
2021-04-02 11:47:25 +00:00
@property() download: string;
2021-03-06 17:01:39 +00:00
connectedCallback() {
super.connectedCallback();
2021-02-26 14:09:13 +00:00
this.handleSlotChange();
}
/** Simulates a click on the button. */
click() {
this.button.click();
}
/** Sets focus on the button. */
focus(options?: FocusOptions) {
2021-02-26 14:09:13 +00:00
this.button.focus(options);
}
/** Removes focus from the button. */
blur() {
2021-02-26 14:09:13 +00:00
this.button.blur();
}
private handleSlotChange() {
2021-02-26 14:09:13 +00:00
this.hasLabel = hasSlot(this);
this.hasPrefix = hasSlot(this, 'prefix');
this.hasSuffix = hasSlot(this, 'suffix');
}
private handleBlur() {
2021-02-26 14:09:13 +00:00
this.hasFocus = false;
emit(this, 'sl-blur');
2021-02-26 14:09:13 +00:00
}
private handleFocus() {
2021-02-26 14:09:13 +00:00
this.hasFocus = true;
emit(this, 'sl-focus');
2021-02-26 14:09:13 +00:00
}
private handleClick(event: MouseEvent) {
2021-02-26 14:09:13 +00:00
if (this.disabled || this.loading) {
event.preventDefault();
event.stopPropagation();
}
}
render() {
const isLink = this.href ? true : false;
const interior = html`
<span part="prefix" class="button__prefix">
2021-03-06 17:01:39 +00:00
<slot @slotchange=${this.handleSlotChange} name="prefix"></slot>
2021-02-26 14:09:13 +00:00
</span>
<span part="label" class="button__label">
2021-03-06 17:01:39 +00:00
<slot @slotchange=${this.handleSlotChange}></slot>
2021-02-26 14:09:13 +00:00
</span>
<span part="suffix" class="button__suffix">
2021-03-06 17:01:39 +00:00
<slot @slotchange=${this.handleSlotChange} name="suffix"></slot>
2021-02-26 14:09:13 +00:00
</span>
${this.caret
? html`
<span part="caret" class="button__caret">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</span>
`
: ''}
2021-03-06 17:01:39 +00:00
${this.loading ? html`<sl-spinner></sl-spinner>` : ''}
2021-02-26 14:09:13 +00:00
`;
const button = html`
<button
part="base"
class=${classMap({
button: true,
'button--default': this.type === 'default',
'button--primary': this.type === 'primary',
'button--success': this.type === 'success',
'button--info': this.type === 'info',
'button--warning': this.type === 'warning',
'button--danger': this.type === 'danger',
'button--text': this.type === 'text',
'button--small': this.size === 'small',
'button--medium': this.size === 'medium',
'button--large': this.size === 'large',
'button--caret': this.caret,
'button--circle': this.circle,
'button--disabled': this.disabled,
'button--focused': this.hasFocus,
'button--loading': this.loading,
'button--pill': this.pill,
'button--has-label': this.hasLabel,
'button--has-prefix': this.hasPrefix,
'button--has-suffix': this.hasSuffix
})}
2021-03-06 17:01:39 +00:00
?disabled=${this.disabled}
2021-02-26 14:09:13 +00:00
type=${this.submit ? 'submit' : 'button'}
2021-04-02 11:47:25 +00:00
name=${ifDefined(this.name)}
value=${ifDefined(this.value)}
2021-03-06 17:01:39 +00:00
@blur=${this.handleBlur}
@focus=${this.handleFocus}
@click=${this.handleClick}
2021-02-26 14:09:13 +00:00
>
${interior}
</button>
`;
const link = html`
<a
ref=${(el: HTMLLinkElement) => (this.button = el)}
part="base"
class=${classMap({
button: true,
'button--default': this.type === 'default',
'button--primary': this.type === 'primary',
'button--success': this.type === 'success',
'button--info': this.type === 'info',
'button--warning': this.type === 'warning',
'button--danger': this.type === 'danger',
'button--text': this.type === 'text',
'button--small': this.size === 'small',
'button--medium': this.size === 'medium',
'button--large': this.size === 'large',
'button--caret': this.caret,
'button--circle': this.circle,
'button--disabled': this.disabled,
'button--focused': this.hasFocus,
'button--loading': this.loading,
'button--pill': this.pill,
'button--has-label': this.hasLabel,
'button--has-prefix': this.hasPrefix,
'button--has-suffix': this.hasSuffix
})}
2021-04-02 11:47:25 +00:00
href=${ifDefined(this.href)}
2021-03-15 11:56:15 +00:00
target=${ifDefined(this.target)}
download=${ifDefined(this.download)}
rel=${ifDefined(this.target ? 'noreferrer noopener' : undefined)}
2021-03-31 16:03:20 +00:00
role="button"
aria-disabled=${this.disabled ? 'true' : 'false'}
tabindex=${this.disabled ? '-1' : '0'}
2021-03-24 15:33:44 +00:00
@blur=${this.handleBlur}
@focus=${this.handleFocus}
@click=${this.handleClick}
2021-02-26 14:09:13 +00:00
>
${interior}
</a>
`;
return isLink ? link : button;
}
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-button': SlButton;
}
}