Skip to content

Commit

Permalink
add disabled focusable click handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdholt committed Mar 30, 2023
1 parent 8d7afc2 commit 01d20ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/web-components/src/button/button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const storyTemplate = html<ButtonStoryArgs>`
shape="${x => x.shape}"
size="${x => x.size}"
?disabled="${x => x.disabled}"
?disabledFocusable="${x => x.disabledFocusable}"
?disabled-focusable="${x => x.disabledFocusable}"
?icon-only="${x => x.iconOnly}"
?icon="${x => x.icon}
?icon="${x => x.icon}"
>
${x => x.content}
</fluent-button>
Expand Down
30 changes: 28 additions & 2 deletions packages/web-components/src/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,36 @@ export class Button extends FASTButton {
@attr({ attribute: 'disabled-focusable', mode: 'boolean' })
public disabledFocusable?: boolean = false;
protected disabledFocusableChanged(prev: boolean, next: boolean): void {
if (!this.$fastController.isConnected) {
return;
}

if (this.disabledFocusable) {
((this as unknown) as HTMLElement).setAttribute('aria-disabled', 'true');
this.ariaDisabled = true;
} else {
((this as unknown) as HTMLElement).removeAttribute('aria-disabled');
this.ariaDisabled = false;
}
}

/**
* Prevents disabledFocusable click events
*/
private handleDisabledFocusableClick = (e: MouseEvent): void => {
if (e && this.disabledFocusable) {
e.stopImmediatePropagation();
return;
}
};

public connectedCallback(): void {
super.connectedCallback();

((this as unknown) as HTMLElement).addEventListener('click', this.handleDisabledFocusableClick);
}

public disconnectedCallback(): void {
super.disconnectedCallback();

((this as unknown) as HTMLElement).removeEventListener('click', this.handleDisabledFocusableClick);
}
}

0 comments on commit 01d20ab

Please sign in to comment.