Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iconbutton): handle activation click #5533

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
25 changes: 23 additions & 2 deletions iconbutton/internal/icon-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import '../../focus/md-focus-ring.js';
import '../../ripple/ripple.js';

import {html, LitElement, nothing} from 'lit';
import {property, state} from 'lit/decorators.js';
import {html, isServer, LitElement, nothing} from 'lit';
import {property, query, state} from 'lit/decorators.js';
import {classMap} from 'lit/directives/class-map.js';
import {literal, html as staticHtml} from 'lit/static-html.js';

Expand All @@ -19,6 +19,10 @@ import {
setupFormSubmitter,
type FormSubmitterType,
} from '../../internal/controller/form-submitter.js';
import {
dispatchActivationClick,
isActivationClick
} from '../../internal/events/form-label-activation.js';
import {isRtl} from '../../internal/controller/is-rtl.js';
import {
internals,
Expand Down Expand Up @@ -126,6 +130,15 @@ export class IconButton extends iconButtonBaseClass implements FormSubmitter {

@state() private flipIcon = isRtl(this, this.flipIconInRtl);

@query('.link') private readonly linkElement!: HTMLElement | null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this to capture both the button and anchor element that could be rendered. Both can have clicks forwarded to them.

@query('.icon-button') private readonly buttonElement!: HTMLElement | null;


constructor() {
super();
if (!isServer) {
this.addEventListener('click', this.handleActivationClick);
}
}

/**
* Link buttons cannot be disabled.
*/
Expand Down Expand Up @@ -235,4 +248,12 @@ export class IconButton extends iconButtonBaseClass implements FormSubmitter {
// Additionally, native change event is not an InputEvent.
this.dispatchEvent(new Event('change', {bubbles: true}));
}

private readonly handleActivationClick = (event: MouseEvent) => {
if (!isActivationClick(event) || !this.linkElement) {
return;
}
this.focus();
dispatchActivationClick(this.linkElement);
};
}
Loading