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

85 wiersze
2.5 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, unsafeCSS } from 'lit';
2021-05-27 21:00:43 +00:00
import { customElement, property, query } from 'lit/decorators.js';
2021-02-26 14:09:13 +00:00
import styles from 'sass:./button-group.scss';
2020-08-06 13:07:24 +00:00
/**
* @since 2.0
* @status stable
*
2021-06-25 20:25:46 +00:00
* @slot - One or more `<sl-button>` elements to display in the button group.
2020-08-06 13:07:24 +00:00
*
2021-06-25 20:25:46 +00:00
* @csspart base - The component's base wrapper.
2020-08-06 13:07:24 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-button-group')
2021-03-09 00:14:32 +00:00
export default class SlButtonGroup extends LitElement {
2021-03-06 17:01:39 +00:00
static styles = unsafeCSS(styles);
2020-08-06 13:07:24 +00:00
@query('slot') defaultSlot: HTMLSlotElement;
2020-09-04 21:11:25 +00:00
/** A label to use for the button group's `aria-label` attribute. */
2021-07-01 00:04:46 +00:00
@property() label = '';
2020-08-06 13:07:24 +00:00
handleFocus(event: CustomEvent) {
const button = findButton(event.target as HTMLElement);
button?.classList.add('sl-button-group__button--focus');
2020-08-06 13:07:24 +00:00
}
handleBlur(event: CustomEvent) {
const button = findButton(event.target as HTMLElement);
button?.classList.remove('sl-button-group__button--focus');
}
handleMouseOver(event: CustomEvent) {
const button = findButton(event.target as HTMLElement);
button?.classList.add('sl-button-group__button--hover');
}
handleMouseOut(event: CustomEvent) {
const button = findButton(event.target as HTMLElement);
button?.classList.remove('sl-button-group__button--hover');
}
handleSlotChange() {
const slottedElements = [...this.defaultSlot.assignedElements({ flatten: true })] as HTMLElement[];
slottedElements.map(el => {
const index = slottedElements.indexOf(el);
const button = findButton(el);
if (button) {
button.classList.add('sl-button-group__button');
button.classList.toggle('sl-button-group__button--first', index === 0);
button.classList.toggle('sl-button-group__button--inner', index > 0 && index < slottedElements.length - 1);
button.classList.toggle('sl-button-group__button--last', index === slottedElements.length - 1);
}
});
2020-08-06 13:07:24 +00:00
}
render() {
2021-02-26 14:09:13 +00:00
return html`
<div
part="base"
class="button-group"
2021-04-01 11:50:27 +00:00
role="group"
2021-02-26 14:09:13 +00:00
aria-label=${this.label}
2021-03-06 17:01:39 +00:00
@focusout=${this.handleBlur}
@focusin=${this.handleFocus}
@mouseover=${this.handleMouseOver}
@mouseout=${this.handleMouseOut}
2021-02-26 14:09:13 +00:00
>
<slot @slotchange=${this.handleSlotChange}></slot>
2020-08-06 13:07:24 +00:00
</div>
2021-02-26 14:09:13 +00:00
`;
2020-08-06 13:07:24 +00:00
}
}
function findButton(el: HTMLElement) {
return el.tagName.toLowerCase() === 'sl-button' ? el : el.querySelector('sl-button');
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-button-group': SlButtonGroup;
}
}