Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion labs/gb/components/splitbutton/split-button-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export class SplitButtonElement extends LitElement {
}
.split-btn {
flex: 1;
/* Contains the slots' touch targets (::before, absolute). The leading
slot is display: contents, so without this its touch target sizes
against the viewport and covers the page. */
position: relative;
}
/* The menu icon is the trailing slot's ::after, painted over the
slotted button: clicks pass through to the button. */
[name='trailing']::after {
pointer-events: none;
}
[name='leading'] {
display: contents;
Expand Down Expand Up @@ -90,20 +99,38 @@ export class SplitButtonElement extends LitElement {
part="leading-btn"
class="${button(buttonConfig)}"
@focusin=${this.updateSlotFocusVisible}
@focusout=${this.updateSlotFocusVisible}>
@focusout=${this.updateSlotFocusVisible}
@click=${this.forwardSlotClick}>
</slot>
<slot
name="trailing"
part="trailing-btn"
class="${button(buttonConfig)}"
@focusin=${this.updateSlotFocusVisible}
@focusout=${this.updateSlotFocusVisible}
@click=${this.forwardSlotClick}
@slotchange=${this.handleTrailingSlotchange}>
</slot>
<slot></slot>
</div>`;
}

/**
* A click on a slot's own box (its 48dp touch target, outside the slotted
* button) activates that slot's button, as a tap on the touch target of a
* button does.
*/
private forwardSlotClick(event: MouseEvent) {
const slot = event.currentTarget as HTMLSlotElement;
if (event.target !== slot) return;
const target = slot
.assignedElements()
.find((el): el is HTMLButtonElement => el.matches('button'));
if (!target || target.disabled) return;
event.stopPropagation();
target.click();
}

private updateSlotFocusVisible(event: Event) {
const slot = event.currentTarget as HTMLSlotElement;
const hasFocusVisible = slot
Expand Down
Loading