import '../icon/icon'; import '../spinner/spinner'; import { classMap } from 'lit/directives/class-map.js'; import { customElement, property, query, state } from 'lit/decorators.js'; import { FormControlController } from '../../internal/form'; import { HasSlotController } from '../../internal/slot'; import { html, literal } from 'lit/static-html.js'; import { ifDefined } from 'lit/directives/if-defined.js'; import { LocalizeController } from '../../utilities/localize'; import { watch } from '../../internal/watch'; import ShoelaceElement from '../../internal/shoelace-element'; import styles from './button.styles'; import type { CSSResultGroup } from 'lit'; import type { ShoelaceFormControl } from '../../internal/shoelace-element'; /** * @summary Buttons represent actions that are available to the user. * @documentation https://shoelace.style/components/button * @status stable * @since 2.0 * * @dependency sl-icon * @dependency sl-spinner * * @event sl-blur - Emitted when the button loses focus. * @event sl-focus - Emitted when the button gains focus. * * @slot - The button's label. * @slot prefix - A presentational prefix icon or similar element. * @slot suffix - A presentational suffix icon or similar element. * * @csspart base - The component's base wrapper. * @csspart prefix - The container that wraps the prefix. * @csspart label - The button's label. * @csspart suffix - The container that wraps the suffix. * @csspart caret - The button's caret icon, an `` element. */ @customElement('sl-button') export default class SlButton extends ShoelaceElement implements ShoelaceFormControl { static styles: CSSResultGroup = styles; private readonly formControlController = new FormControlController(this, { form: input => { // 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'); private readonly localize = new LocalizeController(this); @query('.button') button: HTMLButtonElement | HTMLLinkElement; @state() private hasFocus = false; @state() invalid = false; @property() title = ''; // make reactive to pass through /** The button's theme variant. */ @property({ reflect: true }) variant: 'default' | 'primary' | 'success' | 'neutral' | 'warning' | 'danger' | 'text' = 'default'; /** The button's size. */ @property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium'; /** Draws the button with a caret. Used to indicate that the button triggers a dropdown menu or similar behavior. */ @property({ type: Boolean, reflect: true }) caret = false; /** Disables the button. */ @property({ type: Boolean, reflect: true }) disabled = false; /** Draws the button in a loading state. */ @property({ type: Boolean, reflect: true }) loading = false; /** Draws an outlined button. */ @property({ type: Boolean, reflect: true }) outline = false; /** Draws a pill-style button with rounded edges. */ @property({ type: Boolean, reflect: true }) pill = false; /** * Draws a circular icon button. When this attribute is present, the button expects a single `` in the * default slot. */ @property({ type: Boolean, reflect: true }) circle = false; /** * The type of button. Note that the default value is `button` instead of `submit`, which is opposite of how native * `