import { classMap } from 'lit/directives/class-map.js'; import { FormControlController, validValidityState } from '../../internal/form.js'; import { HasSlotController } from '../../internal/slot.js'; import { html, literal } from 'lit/static-html.js'; import { ifDefined } from 'lit/directives/if-defined.js'; import { LocalizeController } from '../../utilities/localize.js'; import { property, query, state } from 'lit/decorators.js'; import { watch } from '../../internal/watch.js'; import ShoelaceElement from '../../internal/shoelace-element.js'; import SlIcon from '../icon/icon.component.js'; import SlSpinner from '../spinner/spinner.component.js'; import styles from './button.styles.js'; import type { CSSResultGroup } from 'lit'; import type { ShoelaceFormControl } from '../../internal/shoelace-element.js'; /** * @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. * @event sl-invalid - Emitted when the form control has been checked for validity and its constraints aren't satisfied. * * @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. * @csspart spinner - The spinner that shows when the button is in the loading state. */ export default class SlButton extends ShoelaceElement implements ShoelaceFormControl { static styles: CSSResultGroup = styles; static dependencies = { 'sl-icon': SlIcon, 'sl-spinner': SlSpinner }; private readonly formControlController = new FormControlController(this, { form: input => { // Buttons support a form attribute that points to an arbitrary form, so if this attribute is 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'); }, assumeInteractionOn: ['click'] }); 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 * `