2022-04-19 13:30:17 +00:00
|
|
|
import { LitElement } from 'lit';
|
|
|
|
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';
|
2022-04-19 13:30:17 +00:00
|
|
|
import { html, literal } from 'lit/static-html.js';
|
2022-03-24 12:01:09 +00:00
|
|
|
import '../../components/icon/icon';
|
2022-04-19 13:30:17 +00:00
|
|
|
import { emit } from '../../internal/event';
|
2021-07-10 00:45:44 +00:00
|
|
|
import styles from './icon-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-icon
|
|
|
|
*
|
2022-04-19 13:30:17 +00:00
|
|
|
* @event sl-blur - Emitted when the icon button loses focus.
|
|
|
|
* @event sl-focus - Emitted when the icon button gains focus.
|
|
|
|
*
|
2022-03-09 20:54:18 +00:00
|
|
|
* @csspart base - The component's internal wrapper.
|
2021-02-26 14:09:13 +00:00
|
|
|
*/
|
2021-03-18 13:04:23 +00:00
|
|
|
@customElement('sl-icon-button')
|
2021-03-09 00:14:32 +00:00
|
|
|
export default class SlIconButton extends LitElement {
|
2021-07-10 00:45:44 +00:00
|
|
|
static styles = styles;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
2022-04-19 13:30:17 +00:00
|
|
|
@state() private hasFocus = false;
|
|
|
|
|
2022-03-16 21:39:53 +00:00
|
|
|
@query('.icon-button') button: HTMLButtonElement | HTMLLinkElement;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** The name of the icon to draw. */
|
2022-01-16 05:47:14 +00:00
|
|
|
@property() name?: string;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** The name of a registered custom icon library. */
|
2022-01-16 05:47:14 +00:00
|
|
|
@property() library?: string;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** An external URL of an SVG file. */
|
2022-01-16 05:47:14 +00:00
|
|
|
@property() src?: string;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
2021-08-10 14:52:17 +00:00
|
|
|
/** When set, the underlying button will be rendered as an `<a>` with this `href` instead of a `<button>`. */
|
2022-01-16 05:47:14 +00:00
|
|
|
@property() href?: string;
|
2021-08-10 14:52:17 +00:00
|
|
|
|
|
|
|
/** Tells the browser where to open the link. Only used when `href` is set. */
|
2022-01-16 05:47:14 +00:00
|
|
|
@property() target?: '_blank' | '_parent' | '_self' | '_top';
|
2021-08-10 14:52:17 +00:00
|
|
|
|
|
|
|
/** Tells the browser to download the linked file as this filename. Only used when `href` is set. */
|
2022-01-16 05:47:14 +00:00
|
|
|
@property() download?: string;
|
2021-08-10 14:52:17 +00:00
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
/**
|
|
|
|
* A description that gets read by screen readers and other assistive devices. For optimal accessibility, you should
|
|
|
|
* always include a label that describes what the icon button does.
|
|
|
|
*/
|
2021-07-01 00:04:46 +00:00
|
|
|
@property() label = '';
|
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
|
|
|
|
2022-04-19 13:30:17 +00:00
|
|
|
/** Simulates a click on the icon button. */
|
|
|
|
click() {
|
|
|
|
this.button.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets focus on the icon button. */
|
|
|
|
focus(options?: FocusOptions) {
|
|
|
|
this.button.focus(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Removes focus from the icon button. */
|
|
|
|
blur() {
|
|
|
|
this.button.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBlur() {
|
|
|
|
this.hasFocus = false;
|
|
|
|
emit(this, 'sl-blur');
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFocus() {
|
|
|
|
this.hasFocus = true;
|
|
|
|
emit(this, 'sl-focus');
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(event: MouseEvent) {
|
|
|
|
if (this.disabled) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
render() {
|
2022-01-19 14:37:07 +00:00
|
|
|
const isLink = this.href ? true : false;
|
2022-04-19 13:30:17 +00:00
|
|
|
const tag = isLink ? literal`a` : literal`button`;
|
2021-08-10 14:52:17 +00:00
|
|
|
|
2022-04-19 13:30:17 +00:00
|
|
|
/* eslint-disable lit/binding-positions, lit/no-invalid-html */
|
|
|
|
return html`
|
|
|
|
<${tag}
|
|
|
|
part="base"
|
|
|
|
class=${classMap({
|
|
|
|
'icon-button': true,
|
|
|
|
'icon-button--disabled': !isLink && this.disabled,
|
|
|
|
'icon-button--focused': this.hasFocus
|
|
|
|
})}
|
|
|
|
?disabled=${ifDefined(isLink ? undefined : this.disabled)}
|
|
|
|
type=${ifDefined(isLink ? undefined : 'button')}
|
|
|
|
href=${ifDefined(isLink ? this.href : undefined)}
|
|
|
|
target=${ifDefined(isLink ? this.target : undefined)}
|
|
|
|
download=${ifDefined(isLink ? this.download : undefined)}
|
|
|
|
rel=${ifDefined(isLink && this.target ? 'noreferrer noopener' : undefined)}
|
|
|
|
role=${ifDefined(isLink ? undefined : 'button')}
|
|
|
|
aria-disabled=${this.disabled ? 'true' : 'false'}
|
|
|
|
aria-label="${this.label}"
|
|
|
|
tabindex=${this.disabled ? '-1' : '0'}
|
|
|
|
@blur=${this.handleBlur}
|
|
|
|
@focus=${this.handleFocus}
|
|
|
|
@click=${this.handleClick}
|
|
|
|
>
|
|
|
|
<sl-icon
|
2022-06-06 21:14:50 +00:00
|
|
|
class="icon-button__icon"
|
2022-04-19 13:30:17 +00:00
|
|
|
name=${ifDefined(this.name)}
|
|
|
|
library=${ifDefined(this.library)}
|
|
|
|
src=${ifDefined(this.src)}
|
|
|
|
aria-hidden="true"
|
|
|
|
></sl-icon>
|
|
|
|
</${tag}>
|
2021-08-10 14:52:17 +00:00
|
|
|
`;
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:07:38 +00:00
|
|
|
|
2021-03-12 14:09:08 +00:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-icon-button': SlIconButton;
|
|
|
|
}
|
|
|
|
}
|