2021-03-06 17:01:39 +00:00
|
|
|
import { LitElement, customElement, html, property, query, unsafeCSS } from 'lit-element';
|
|
|
|
import { classMap } from 'lit-html/directives/class-map';
|
|
|
|
import { event, EventEmitter } from '../../internal/event';
|
2021-02-26 14:09:13 +00:00
|
|
|
import styles from 'sass:./tab.scss';
|
|
|
|
|
|
|
|
let id = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @since 2.0
|
|
|
|
* @status stable
|
|
|
|
*
|
|
|
|
* @dependency sl-icon-button
|
|
|
|
*
|
|
|
|
* @slot - The tab's label.
|
|
|
|
*
|
|
|
|
* @part base - The component's base wrapper.
|
|
|
|
* @part close-button - The close button, which is the icon button's base wrapper.
|
|
|
|
*/
|
2021-03-06 17:01:39 +00:00
|
|
|
@customElement('sl-tab')
|
|
|
|
export class SlTab extends LitElement {
|
|
|
|
static styles = unsafeCSS(styles);
|
|
|
|
|
|
|
|
@query('.tab') tab: HTMLElement;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
private componentId = `tab-${++id}`;
|
|
|
|
|
|
|
|
/** The name of the tab panel the tab will control. The panel must be located in the same tab group. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property() panel = '';
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** Draws the tab in an active state. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) active = false;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** Makes the tab closable and shows a close icon. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) closable = false;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** Draws the tab in a disabled state. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) disabled = false;
|
|
|
|
|
|
|
|
/** Emitted when the tab is closable and the close button is activated. */
|
|
|
|
@event('sl-close') slClose: EventEmitter<void>;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** Sets focus to the tab. */
|
|
|
|
setFocus(options?: FocusOptions) {
|
|
|
|
this.tab.focus(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Removes focus from the tab. */
|
|
|
|
removeFocus() {
|
|
|
|
this.tab.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCloseClick() {
|
2021-03-06 17:01:39 +00:00
|
|
|
this.slClose.emit();
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
// If the user didn't provide an ID, we'll set one so we can link tabs and tab panels with aria labels
|
|
|
|
this.id = this.id || this.componentId;
|
|
|
|
|
|
|
|
return html`
|
|
|
|
<div
|
|
|
|
part="base"
|
|
|
|
class=${classMap({
|
|
|
|
tab: true,
|
|
|
|
'tab--active': this.active,
|
|
|
|
'tab--closable': this.closable,
|
|
|
|
'tab--disabled': this.disabled
|
|
|
|
})}
|
|
|
|
role="tab"
|
|
|
|
aria-disabled=${this.disabled ? 'true' : 'false'}
|
|
|
|
aria-selected=${this.active ? 'true' : 'false'}
|
|
|
|
tabindex=${this.disabled || !this.active ? '-1' : '0'}
|
|
|
|
>
|
2021-03-06 17:01:39 +00:00
|
|
|
<slot></slot>
|
2021-02-26 14:09:13 +00:00
|
|
|
${this.closable
|
|
|
|
? html`
|
|
|
|
<sl-icon-button
|
|
|
|
name="x"
|
|
|
|
exportparts="base:close-button"
|
|
|
|
class="tab__close-button"
|
2021-03-06 17:01:39 +00:00
|
|
|
@click=${this.handleCloseClick}
|
2021-02-26 14:09:13 +00:00
|
|
|
tabindex="-1"
|
|
|
|
aria-hidden="true"
|
2021-03-06 17:01:39 +00:00
|
|
|
></sl-icon-button>
|
2021-02-26 14:09:13 +00:00
|
|
|
`
|
|
|
|
: ''}
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|