Skip to content

Commit

Permalink
fix: button clicking stop propagation of original event
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Mar 13, 2024
1 parent fd38267 commit d342e61
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
12 changes: 1 addition & 11 deletions src/button/Button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,11 @@ export const Template: StoryObj = {
},
},
render: (args) => {
let buttonRendered = false;
const handleClick = () => {
// Function required as the click event is triggered once on render
if (buttonRendered) {
alert("Button Clicked");
return;
}
buttonRendered = true;
};

return html`
<hot-button
intent=${args.intent}
?disabled=${args.disabled}
@click=${handleClick}
@click=${() => {alert("Button Clicked")}}
>
Button
</hot-button>
Expand Down
6 changes: 5 additions & 1 deletion src/button/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ export class button extends LitElement {
disabled: this.disabled,
})}
?disabled=${this.disabled}
@click=${(e: MouseEvent) => {this._handleClick(e)}}
>
<slot></slot>
</button>`;
}

updated(changedProperties) {
private _handleClick(e: MouseEvent) {
// As the original event is also named 'click'
// stop propagation of the original event
e.stopPropagation();
this.dispatchEvent(new Event("click"));
}
}

0 comments on commit d342e61

Please sign in to comment.