Skip to content

Commit

Permalink
fix(split-button): follow visible tab order
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook Johnson authored and Westbrook committed Aug 29, 2020
1 parent ecfb6ab commit 966d3b6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/shared/src/focusable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class Focusable extends FocusVisiblePolyfillMixin(SpectrumElement) {
return;
}

this.focusElement.focus();
this.focus();
}

private handleDisabledChanged(
Expand Down
85 changes: 57 additions & 28 deletions packages/split-button/src/SplitButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
property,
html,
PropertyValues,
query,
} from 'lit-element';
import { ifDefined } from 'lit-html/directives/if-defined.js';

Expand All @@ -38,6 +39,9 @@ export class SplitButton extends DropdownBase {
return [buttonBaseStyles, buttonStyles, styles, ChevronDownMediumStyle];
}

@property({ type: Boolean, reflect: true })
public left = false;

/**
* The visual variant to apply to this button.
*/
Expand All @@ -51,9 +55,24 @@ export class SplitButton extends DropdownBase {
@property({ type: String })
public type: 'field' | 'more' = 'field';

@query('.trigger')
private trigger!: HTMLButtonElement;

protected listRole = 'menu';
protected itemRole = 'menuitem';

public focus(): void {
if (this.disabled) {
return;
}
if (this.left) {
this.trigger.focus();
return;
}

super.focus();
}

protected sizePopover(popover: HTMLElement): void {
popover.style.setProperty('min-width', `${this.offsetWidth}px`);
}
Expand All @@ -77,7 +96,7 @@ export class SplitButton extends DropdownBase {
const target = event.composedPath()[0];
if (
target &&
target === this.focusElement &&
target === (this.left ? this.trigger : this.focusElement) &&
!event.defaultPrevented &&
event.shiftKey &&
event.code === 'Tab'
Expand All @@ -103,34 +122,44 @@ export class SplitButton extends DropdownBase {
}

protected render(): TemplateResult {
return html`
<button
aria-haspopup="true"
aria-label=${ifDefined(this.label || undefined)}
id="button"
class="button ${this.variant}"
@click=${this.passClick}
?disabled=${this.disabled}
>
${this.buttonContent}
</button>
<button
class="button trigger ${this.variant}"
@blur=${this.onButtonBlur}
@click=${this.onButtonClick}
@focus=${this.onButtonFocus}
?disabled=${this.disabled}
>
<sp-icon
class="icon ${this.type === 'field'
? 'chevron-down-medium'
: 'more-medium'}"
const buttons: TemplateResult[] = [
html`
<button
aria-haspopup="true"
aria-label=${ifDefined(this.label || undefined)}
id="button"
class="button ${this.variant}"
@click=${this.passClick}
?disabled=${this.disabled}
>
${this.buttonContent}
</button>
`,
html`
<button
class="button trigger ${this.variant}"
@blur=${this.onButtonBlur}
@click=${this.onButtonClick}
@focus=${this.onButtonFocus}
?disabled=${this.disabled}
>
${this.type === 'field'
? ChevronDownMediumIcon({ hidden: true })
: MoreIcon({ hidden: true })}
</sp-icon>
</button>
<sp-icon
class="icon ${this.type === 'field'
? 'chevron-down-medium'
: 'more-medium'}"
>
${this.type === 'field'
? ChevronDownMediumIcon({ hidden: true })
: MoreIcon({ hidden: true })}
</sp-icon>
</button>
`,
];
if (this.left) {
buttons.reverse();
}
return html`
${buttons}
<sp-popover
open
id="popover"
Expand Down
4 changes: 0 additions & 4 deletions packages/split-button/src/split-button.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ governing permissions and limitations under the License.

@import './spectrum-split-button.css';

:host([left]) {
flex-direction: row-reverse;
}

sp-popover {
display: none;
}
Expand Down

0 comments on commit 966d3b6

Please sign in to comment.