Skip to content

Commit d809890

Browse files
author
Mikhail Bashkirov
committed
fix(button): indicate visually the active state on enter/space
1 parent 1f6b283 commit d809890

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

packages/button/src/LionButton.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export class LionButton extends DelegateMixin(SlotMixin(LionLitElement)) {
8787
fill: whitesmoke;
8888
}
8989
90+
:host(:active) .btn,
91+
.btn[active] {
92+
background: grey;
93+
}
94+
9095
:host([disabled]) {
9196
pointer-events: none;
9297
}
@@ -140,32 +145,42 @@ export class LionButton extends DelegateMixin(SlotMixin(LionLitElement)) {
140145

141146
connectedCallback() {
142147
super.connectedCallback();
143-
this.__setupKeydownDelegation();
148+
this.__setupDelegation();
144149
}
145150

146151
disconnectedCallback() {
147152
super.disconnectedCallback();
148-
this.__teardownKeydownDelegation();
153+
this.__teardownDelegation();
149154
}
150155

151156
__clickDelegationHandler(e) {
152157
e.stopPropagation(); // prevent click on the fake element and cause click on the native button
153158
this.$$slot('_button').click();
154159
}
155160

156-
__setupKeydownDelegation() {
161+
__setupDelegation() {
157162
this.addEventListener('keydown', this.__keydownDelegationHandler);
163+
this.addEventListener('keyup', this.__keyupDelegationHandler);
158164
}
159165

160-
__teardownKeydownDelegation() {
166+
__teardownDelegation() {
161167
this.removeEventListener('keydown', this.__keydownDelegationHandler);
168+
this.removeEventListener('keyup', this.__keyupDelegationHandler);
162169
}
163170

164171
__keydownDelegationHandler(e) {
172+
if (e.keyCode === 32 /* space */ || e.keyCode === 13 /* enter */) {
173+
e.preventDefault();
174+
this.shadowRoot.querySelector('.btn').setAttribute('active', '');
175+
}
176+
}
177+
178+
__keyupDelegationHandler(e) {
165179
// Makes the real button the trigger in forms (will submit form, as opposed to paper-button)
166180
// and make click handlers on button work on space and enter
167181
if (e.keyCode === 32 /* space */ || e.keyCode === 13 /* enter */) {
168182
e.preventDefault();
183+
this.shadowRoot.querySelector('.btn').removeAttribute('active');
169184
this.$$slot('_button').click();
170185
}
171186
}

packages/button/test/lion-button.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, fixture, html } from '@open-wc/testing';
1+
import { expect, fixture, html, aTimeout } from '@open-wc/testing';
22
import sinon from 'sinon';
33
import { pressEnter, pressSpace } from '@polymer/iron-test-helpers/mock-interactions.js';
44

@@ -117,6 +117,8 @@ describe('lion-button', () => {
117117
`);
118118

119119
pressSpace(form.querySelector('lion-button'));
120+
await aTimeout();
121+
await aTimeout();
120122

121123
expect(formSubmitSpy.called).to.be.true;
122124
});
@@ -130,6 +132,8 @@ describe('lion-button', () => {
130132
`);
131133

132134
pressEnter(form.querySelector('lion-button'));
135+
await aTimeout();
136+
await aTimeout();
133137

134138
expect(formSubmitSpy.called).to.be.true;
135139
});

0 commit comments

Comments
 (0)