Skip to content

Commit b78eee8

Browse files
dfreedmcopybara-github
authored andcommitted
chore(focus): Add StrongFocus support to Button
PiperOrigin-RevId: 403430582
1 parent 210cc7b commit b78eee8

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

components/button/lib/button.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {eventOptions, property, query, queryAssignedNodes, queryAsync, state} fr
1414
import {ClassInfo, classMap} from 'lit/directives/class-map';
1515
import {ifDefined} from 'lit/directives/if-defined';
1616

17+
import {pointerPress, shouldShowStrongFocus} from '../../focus/strong-focus';
1718
import {Ripple} from '../../ripple/mwc-ripple';
1819
import {RippleHandlers} from '../../ripple/ripple-handlers';
1920

@@ -43,6 +44,8 @@ export abstract class Button extends LitElement {
4344

4445
@queryAsync('md-ripple') ripple!: Promise<Ripple|null>;
4546

47+
@state() protected showFocusRing = false;
48+
4649
@queryAssignedNodes('icon', true, '*')
4750
protected iconElement!: HTMLElement[]|null;
4851

@@ -148,8 +151,8 @@ export abstract class Button extends LitElement {
148151

149152
/** @soyTemplate */
150153
protected renderFocusRing(): TemplateResult {
151-
// TODO(b/202994744): conditionally enable focus ring
152-
return html`<md-focus-ring .visible="${false}"></md-focus-ring>`;
154+
return html`<md-focus-ring .visible="${
155+
this.showFocusRing}"></md-focus-ring>`;
153156
}
154157

155158
/** @soyTemplate */
@@ -176,15 +179,13 @@ export abstract class Button extends LitElement {
176179
override focus() {
177180
const buttonElement = this.buttonElement;
178181
if (buttonElement) {
179-
this.rippleHandlers.startFocus();
180182
buttonElement.focus();
181183
}
182184
}
183185

184186
override blur() {
185187
const buttonElement = this.buttonElement;
186188
if (buttonElement) {
187-
this.rippleHandlers.endFocus();
188189
buttonElement.blur();
189190
}
190191
}
@@ -199,6 +200,7 @@ export abstract class Button extends LitElement {
199200

200201
window.addEventListener('mouseup', onUp);
201202
this.rippleHandlers.startPress(evt);
203+
pointerPress();
202204
}
203205

204206
protected handleRippleDeactivate() {
@@ -214,11 +216,11 @@ export abstract class Button extends LitElement {
214216
}
215217

216218
protected handleRippleFocus() {
217-
this.rippleHandlers.startFocus();
219+
this.showFocusRing = shouldShowStrongFocus();
218220
}
219221

220222
protected handleRippleBlur() {
221-
this.rippleHandlers.endFocus();
223+
this.showFocusRing = false;
222224
}
223225

224226
protected handleSlotChange() {

components/button/lib/link-button.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export abstract class LinkButton extends Button {
2828
// TODO(b/182405623): restore whitespace
2929
return html`
3030
<span class="md3-button__host"
31-
@focus="${this.handleRippleFocus}"
32-
@blur="${this.handleRippleBlur}"
31+
@focusin="${this.handleRippleFocus}"
32+
@focusout="${this.handleRippleBlur}"
3333
@mousedown="${this.handleRippleActivate}"
3434
@mouseenter="${this.handleRippleMouseEnter}"
3535
@mouseleave="${this.handleRippleMouseLeave}"

0 commit comments

Comments
 (0)