shoelace/src/components/icon-button/icon-button.ts

71 wiersze
1.8 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, property, query, unsafeCSS } from 'lit-element';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
import { tag } from '../../internal/decorators';
2021-03-06 17:01:39 +00:00
import { ifDefined } from 'lit-html/directives/if-defined';
2021-02-26 14:09:13 +00:00
import styles from 'sass:./icon-button.scss';
import { focusVisible } from '../../internal/focus-visible';
/**
* @since 2.0
* @status stable
*
* @dependency sl-icon
*
* @part base - The component's base wrapper.
*/
@tag('sl-icon-button')
2021-03-09 00:14:32 +00:00
export default class SlIconButton extends LitElement {
2021-03-06 17:01:39 +00:00
static styles = unsafeCSS(styles);
2021-02-26 14:09:13 +00:00
2021-03-06 17:01:39 +00:00
@query('button') button: HTMLButtonElement;
2021-02-26 14:09:13 +00:00
/** The name of the icon to draw. */
2021-03-06 17:01:39 +00:00
@property() name: string;
2021-02-26 14:09:13 +00:00
/** The name of a registered custom icon library. */
2021-03-06 17:01:39 +00:00
@property() library: string;
2021-02-26 14:09:13 +00:00
/** An external URL of an SVG file. */
2021-03-06 17:01:39 +00:00
@property() src: string;
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-03-06 17:01:39 +00:00
@property() label: string;
2021-02-26 14:09:13 +00:00
/** Disables the button. */
2021-03-06 20:34:33 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2021-02-26 14:09:13 +00:00
2021-03-06 17:01:39 +00:00
firstUpdated() {
2021-02-26 14:09:13 +00:00
focusVisible.observe(this.button);
}
disconnectedCallback() {
2021-03-06 17:01:39 +00:00
super.disconnectedCallback();
2021-02-26 14:09:13 +00:00
focusVisible.unobserve(this.button);
}
render() {
return html`
<button
part="base"
class=${classMap({
'icon-button': true,
'icon-button--disabled': this.disabled
})}
2021-03-06 17:01:39 +00:00
?disabled=${this.disabled}
2021-02-26 14:09:13 +00:00
type="button"
aria-label=${this.label}
>
2021-03-06 17:01:39 +00:00
<sl-icon
name=${ifDefined(this.name)}
library=${ifDefined(this.library)}
src=${ifDefined(this.src)}
aria-hidden="true"
></sl-icon>
2021-02-26 14:09:13 +00:00
</button>
`;
}
}