2022-03-24 11:48:03 +00:00
|
|
|
import { customElement, property } from 'lit/decorators.js';
|
2022-03-15 21:42:59 +00:00
|
|
|
import { classMap } from 'lit/directives/class-map.js';
|
|
|
|
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
|
|
import { html } from 'lit/static-html.js';
|
2022-03-24 11:48:03 +00:00
|
|
|
import styles from '~/components/button/button.styles';
|
|
|
|
import RadioBase from '~/internal/radio';
|
|
|
|
import { HasSlotController } from '~/internal/slot';
|
2022-03-15 21:42:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @since 2.0
|
|
|
|
* @status stable
|
|
|
|
*
|
2022-03-16 14:42:01 +00:00
|
|
|
* @slot - The radio's label.
|
|
|
|
*
|
2022-03-15 21:42:59 +00:00
|
|
|
* @event sl-blur - Emitted when the button loses focus.
|
2022-03-16 14:42:01 +00:00
|
|
|
* @event sl-change - Emitted when the button's checked state changes.
|
2022-03-15 21:42:59 +00:00
|
|
|
* @event sl-focus - Emitted when the button gains focus.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @csspart base - The component's internal wrapper.
|
|
|
|
* @csspart prefix - The prefix slot's container.
|
|
|
|
* @csspart label - The button's label.
|
|
|
|
* @csspart suffix - The suffix slot's container.
|
|
|
|
*/
|
|
|
|
@customElement('sl-radio-button')
|
2022-03-24 11:48:03 +00:00
|
|
|
export default class SlRadioButton extends RadioBase {
|
2022-03-15 21:42:59 +00:00
|
|
|
static styles = styles;
|
|
|
|
|
|
|
|
private readonly hasSlotController = new HasSlotController(this, '[default]', 'prefix', 'suffix');
|
|
|
|
|
|
|
|
/** The button's variant. */
|
|
|
|
@property({ reflect: true }) variant: 'default' | 'primary' | 'success' | 'neutral' | 'warning' | 'danger' =
|
|
|
|
'default';
|
|
|
|
|
|
|
|
/** The button's size. */
|
|
|
|
@property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium';
|
|
|
|
|
2022-03-24 11:48:03 +00:00
|
|
|
/**
|
|
|
|
* This will be true when the control is in an invalid state. Validity in radio buttons is determined by the message
|
|
|
|
* provided by the `setCustomValidity` method.
|
|
|
|
*/
|
|
|
|
@property({ type: Boolean, reflect: true }) invalid = false;
|
|
|
|
|
2022-03-15 21:42:59 +00:00
|
|
|
/** Draws a pill-style button with rounded edges. */
|
|
|
|
@property({ type: Boolean, reflect: true }) pill = false;
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return html`
|
2022-03-24 11:48:03 +00:00
|
|
|
<button
|
|
|
|
part="base"
|
|
|
|
class=${classMap({
|
|
|
|
button: true,
|
|
|
|
'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--small': this.size === 'small',
|
|
|
|
'button--medium': this.size === 'medium',
|
|
|
|
'button--large': this.size === 'large',
|
|
|
|
'button--checked': this.checked,
|
|
|
|
'button--disabled': this.disabled,
|
|
|
|
'button--focused': this.hasFocus,
|
|
|
|
'button--outline': true,
|
|
|
|
'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=${this.disabled}
|
|
|
|
type="button"
|
|
|
|
name=${ifDefined(this.name)}
|
|
|
|
value=${ifDefined(this.value)}
|
|
|
|
@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>
|
|
|
|
</button>
|
2022-03-15 21:42:59 +00:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-radio-button': SlRadioButton;
|
|
|
|
}
|
|
|
|
}
|