shoelace/src/components/button/button.ts

235 wiersze
7.5 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, internalProperty, property, unsafeCSS } from 'lit-element';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
import { event, EventEmitter, tag } from '../../internal/decorators';
2021-02-26 14:09:13 +00:00
import styles from 'sass:./button.scss';
import { hasSlot } from '../../internal/slot';
/**
* @tag sl-button
* @since 2.0
* @status stable
*
* @dependency sl-spinner
*
* @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.
*
* @part base - The component's base wrapper.
* @part prefix - The prefix container.
* @part label - The button's label.
* @part suffix - The suffix container.
* @part caret - The button's caret.
*/
@tag('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);
button: HTMLButtonElement | HTMLLinkElement;
@internalProperty() private hasFocus = false;
@internalProperty() private hasLabel = false;
@internalProperty() private hasPrefix = false;
@internalProperty() 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-03-06 17:01:39 +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-03-06 17:01:39 +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-03-06 17:01:39 +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-03-06 17:01:39 +00:00
@property() download: string;
/** Emitted when the button loses focus. */
@event('sl-blur') slBlur: EventEmitter<void>;
/** Emitted when the button gains focus. */
@event('sl-focus') slFocus: EventEmitter<void>;
2021-02-26 14:09:13 +00:00
2021-03-06 17:01:39 +00:00
connectedCallback() {
super.connectedCallback();
2021-02-26 14:09:13 +00:00
this.handleSlotChange();
}
/** setFocus - Sets focus on the button. */
setFocus(options?: FocusOptions) {
this.button.focus(options);
}
/** removeFocus - Removes focus from the button. */
removeFocus() {
this.button.blur();
}
handleSlotChange() {
this.hasLabel = hasSlot(this);
this.hasPrefix = hasSlot(this, 'prefix');
this.hasSuffix = hasSlot(this, 'suffix');
}
handleBlur() {
this.hasFocus = false;
2021-03-06 17:01:39 +00:00
this.slBlur.emit();
2021-02-26 14:09:13 +00:00
}
handleFocus() {
this.hasFocus = true;
2021-03-06 17:01:39 +00:00
this.slFocus.emit();
2021-02-26 14:09:13 +00:00
}
handleClick(event: MouseEvent) {
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
ref=${(el: HTMLButtonElement) => (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-03-06 17:01:39 +00:00
?disabled=${this.disabled}
2021-02-26 14:09:13 +00:00
type=${this.submit ? 'submit' : 'button'}
name=${this.name}
2021-03-06 17:01:39 +00:00
.value=${this.value}
@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
})}
href=${this.href}
target=${this.target ? this.target : null}
download=${this.download ? this.download : null}
rel=${this.target ? 'noreferrer noopener' : null}
2021-03-06 17:01:39 +00:00
onblur=${this.handleBlur}
onfocus=${this.handleFocus}
onclick=${this.handleClick}
2021-02-26 14:09:13 +00:00
>
${interior}
</a>
`;
return isLink ? link : button;
}
}
declare global {
interface HTMLElementTagNameMap {
'sl-button': SlButton;
}
}