shoelace/src/components/button/button.ts

242 wiersze
8.3 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement } from 'lit';
2021-05-27 21:00:43 +00:00
import { customElement, property, query, state } from 'lit/decorators.js';
2021-09-29 12:40:26 +00:00
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { html, literal } from 'lit/static-html.js';
2022-03-24 11:48:03 +00:00
import '~/components/spinner/spinner';
import { emit } from '~/internal/event';
import { FormSubmitController } from '~/internal/form';
import { HasSlotController } from '~/internal/slot';
2021-07-10 00:45:44 +00:00
import styles from './button.styles';
2021-07-12 14:36:06 +00:00
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.
*
2022-03-09 20:54:18 +00:00
* @csspart base - The component's internal wrapper.
2022-03-15 21:42:59 +00:00
* @csspart prefix - The prefix slot's container.
2021-06-25 20:25:46 +00:00
* @csspart label - The button's label.
2022-03-15 21:42:59 +00:00
* @csspart suffix - The suffix slot's container.
2021-06-25 20:25:46 +00:00
* @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-07-10 00:45:44 +00:00
static styles = styles;
2021-03-06 17:01:39 +00:00
2021-03-15 17:03:01 +00:00
@query('.button') button: HTMLButtonElement | HTMLLinkElement;
2021-03-06 17:01:39 +00:00
2022-02-28 14:59:32 +00:00
private readonly formSubmitController = new FormSubmitController(this, {
form: (input: HTMLInputElement) => {
// Buttons support a form attribute that points to an arbitrary form, so if this attribute it set we need to query
// the form from the same root using its id
if (input.hasAttribute('form')) {
const doc = input.getRootNode() as Document | ShadowRoot;
const formId = input.getAttribute('form')!;
return doc.getElementById(formId) as HTMLFormElement;
}
// Fall back to the closest containing form
return input.closest('form');
}
});
private readonly hasSlotController = new HasSlotController(this, '[default]', 'prefix', 'suffix');
@state() private hasFocus = false;
2021-02-26 14:09:13 +00:00
2021-12-13 22:38:40 +00:00
/** The button's variant. */
@property({ reflect: true }) variant: 'default' | 'primary' | 'success' | 'neutral' | 'warning' | 'danger' | 'text' =
2021-03-06 17:01:39 +00:00
'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-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) caret = false;
2021-02-26 14:09:13 +00:00
/** Disables the button. */
2021-07-01 00:04:46 +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-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) loading = false;
2021-02-26 14:09:13 +00:00
2021-09-24 20:40:03 +00:00
/** Draws an outlined button. */
@property({ type: Boolean, reflect: true }) outline = false;
2021-02-26 14:09:13 +00:00
/** Draws a pill-style button with rounded edges. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) pill = false;
2021-02-26 14:09:13 +00:00
/** Draws a circle button. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) circle = false;
2021-02-26 14:09:13 +00:00
/**
* The type of button. When the type is `submit`, the button will submit the surrounding form. Note that the default
* value is `button` instead of `submit`, which is opposite of how native `<button>` elements behave.
*/
@property() type: 'button' | 'submit' = 'button';
2021-02-26 14:09:13 +00:00
/** An optional name for the button. Ignored when `href` is set. */
@property() name?: string;
2021-02-26 14:09:13 +00:00
/** An optional value for the button. Ignored when `href` is set. */
@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>`. */
@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. */
@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. */
@property() download?: string;
2021-03-06 17:01:39 +00:00
2022-02-28 14:59:32 +00:00
/**
* The "form owner" to associate the button with. If omitted, the closest containing form will be used instead. The
* value of this attribute must be an id of a form in the same document or shadow root as the button.
*/
@property() form: string;
/** Used to override the form owner's `action` attribute. */
@property({ attribute: 'formaction' }) formAction: string;
/** Used to override the form owner's `method` attribute. */
@property({ attribute: 'formmethod' }) formMethod: 'post' | 'get';
/** Used to override the form owner's `novalidate` attribute. */
@property({ attribute: 'formnovalidate', type: Boolean }) formNoValidate: boolean;
/** Used to override the form owner's `target` attribute. */
@property({ attribute: 'formtarget' }) formTarget: '_self' | '_blank' | '_parent' | '_top' | string;
/** 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();
}
2021-06-26 00:07:22 +00:00
handleBlur() {
2021-02-26 14:09:13 +00:00
this.hasFocus = false;
emit(this, 'sl-blur');
2021-02-26 14:09:13 +00:00
}
2021-06-26 00:07:22 +00:00
handleFocus() {
2021-02-26 14:09:13 +00:00
this.hasFocus = true;
emit(this, 'sl-focus');
2021-02-26 14:09:13 +00:00
}
2021-06-26 00:07:22 +00:00
handleClick(event: MouseEvent) {
2021-02-26 14:09:13 +00:00
if (this.disabled || this.loading) {
event.preventDefault();
event.stopPropagation();
return;
}
if (this.type === 'submit') {
2022-02-28 14:59:32 +00:00
this.formSubmitController.submit(this);
2021-02-26 14:09:13 +00:00
}
}
render() {
2022-01-19 14:37:07 +00:00
const isLink = this.href ? true : false;
const tag = isLink ? literal`a` : literal`button`;
/* eslint-disable lit/binding-positions, lit/no-invalid-html */
return html`
<${tag}
part="base"
class=${classMap({
button: true,
2021-12-13 22:38:40 +00:00
'button--default': this.variant === 'default',
'button--primary': this.variant === 'primary',
'button--success': this.variant === 'success',
'button--neutral': this.variant === 'neutral',
'button--warning': this.variant === 'warning',
'button--danger': this.variant === 'danger',
'button--text': this.variant === '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--standard': !this.outline,
'button--outline': this.outline,
'button--pill': this.pill,
'button--has-label': this.hasSlotController.test('[default]'),
'button--has-prefix': this.hasSlotController.test('prefix'),
'button--has-suffix': this.hasSlotController.test('suffix')
})}
?disabled=${ifDefined(isLink ? undefined : this.disabled)}
type=${this.type}
name=${ifDefined(isLink ? undefined : this.name)}
value=${ifDefined(isLink ? undefined : this.value)}
href=${ifDefined(this.href)}
target=${ifDefined(this.target)}
download=${ifDefined(this.download)}
2022-01-19 14:37:07 +00:00
rel=${ifDefined(this.target ? 'noreferrer noopener' : undefined)}
role="button"
aria-disabled=${this.disabled ? 'true' : 'false'}
tabindex=${this.disabled ? '-1' : '0'}
@blur=${this.handleBlur}
@focus=${this.handleFocus}
@click=${this.handleClick}
>
<span part="prefix" class="button__prefix">
<slot name="prefix"></slot>
</span>
<span part="label" class="button__label">
<slot></slot>
</span>
<span part="suffix" class="button__suffix">
<slot name="suffix"></slot>
</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>
`
: ''
}
${this.loading ? html`<sl-spinner></sl-spinner>` : ''}
</${tag}>
2021-02-26 14:09:13 +00:00
`;
/* eslint-enable lit/binding-positions, lit/no-invalid-html */
2021-02-26 14:09:13 +00:00
}
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-button': SlButton;
}
}