Skip to content

Commit cb747d9

Browse files
committed
fix(switch): cross-browser fixes
1 parent 91ebd2f commit cb747d9

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

packages/switch/src/LionButtonSwitch.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { LionButton } from '@lion/button';
2-
import { html, css } from '@lion/core';
1+
import { html, css, LitElement, DisabledWithTabIndexMixin } from '@lion/core';
32

4-
export class LionButtonSwitch extends LionButton {
3+
export class LionButtonSwitch extends DisabledWithTabIndexMixin(LitElement) {
54
static get properties() {
65
return {
6+
role: {
7+
type: String,
8+
reflect: true,
9+
},
710
checked: {
811
type: Boolean,
912
reflect: true,
@@ -13,23 +16,24 @@ export class LionButtonSwitch extends LionButton {
1316

1417
static get styles() {
1518
return [
16-
...super.styles,
1719
css`
1820
:host {
1921
display: inline-block;
2022
position: relative;
2123
width: 36px;
2224
height: 16px;
23-
/* Override "button" styles */
24-
min-height: auto;
25-
padding: 0;
25+
outline: 0;
2626
}
2727
2828
.btn {
29+
position: relative;
2930
height: 100%;
30-
/* Override "button" styles */
31-
min-height: auto;
32-
padding: 0;
31+
outline: 0;
32+
}
33+
34+
:host(:focus) .btn {
35+
/* if you extend, please overwrite */
36+
outline: 2px solid #bde4ff;
3337
}
3438
3539
.button-switch__track {
@@ -53,11 +57,12 @@ export class LionButtonSwitch extends LionButton {
5357
];
5458
}
5559

56-
// eslint-disable-next-line class-methods-use-this
57-
_renderBefore() {
60+
render() {
5861
return html`
59-
<div class="button-switch__track"></div>
60-
<div class="button-switch__thumb"></div>
62+
<div class="btn">
63+
<div class="button-switch__track"></div>
64+
<div class="button-switch__thumb"></div>
65+
</div>
6166
`;
6267
}
6368

@@ -66,22 +71,21 @@ export class LionButtonSwitch extends LionButton {
6671
this.role = 'switch';
6772
this.checked = false;
6873
this.addEventListener('click', this.__handleToggleStateChange);
74+
this.addEventListener('keydown', this.__handleKeydown);
75+
this.addEventListener('keyup', this.__handleKeyup);
6976
}
7077

7178
connectedCallback() {
7279
super.connectedCallback();
7380
this.setAttribute('aria-checked', `${this.checked}`);
7481
}
7582

76-
firstUpdated(changedProperties) {
77-
super.firstUpdated(changedProperties);
78-
this.removeAttribute('type');
79-
}
80-
8183
__handleToggleStateChange() {
8284
if (this.disabled) {
8385
return;
8486
}
87+
// Force IE11 to focus the component.
88+
this.focus();
8589
this.checked = !this.checked;
8690
this.dispatchEvent(
8791
new Event('checked-changed', {
@@ -91,6 +95,20 @@ export class LionButtonSwitch extends LionButton {
9195
);
9296
}
9397

98+
// eslint-disable-next-line class-methods-use-this
99+
__handleKeydown(e) {
100+
// prevent "space" scrolling on "macOS"
101+
if (e.keyCode === 32) {
102+
e.preventDefault();
103+
}
104+
}
105+
106+
__handleKeyup(e) {
107+
if ([32 /* space */, 13 /* enter */].indexOf(e.keyCode) !== -1) {
108+
this.__handleToggleStateChange();
109+
}
110+
}
111+
94112
/**
95113
* We synchronously update aria-checked to support voice over on safari.
96114
*

packages/switch/stories/index.stories.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LitElement } from '@lion/core';
44
import { LocalizeMixin } from '@lion/localize';
55

66
import '../lion-input-switch.js';
7+
import '../lion-button-switch.js';
78
import '@lion/form/lion-form.js';
89

910
storiesOf('Forms|Switch', module)
@@ -83,4 +84,10 @@ storiesOf('Forms|Switch', module)
8384
return html`
8485
<lion-switch-validation-demo></lion-switch-validation-demo>
8586
`;
86-
});
87+
})
88+
.add(
89+
'Button',
90+
() => html`
91+
<lion-button-switch></lion-button-switch>
92+
`,
93+
);

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { expect, fixture, html } from '@open-wc/testing';
22
import sinon from 'sinon';
33

4-
import { LionButton } from '@lion/button';
5-
64
import '../lion-button-switch.js';
75

86
describe('lion-button-switch', () => {
@@ -13,10 +11,6 @@ describe('lion-button-switch', () => {
1311
`);
1412
});
1513

16-
it('should behave like a button', () => {
17-
expect(el instanceof LionButton).to.be.true;
18-
});
19-
2014
it('should be focusable', () => {
2115
expect(el.tabIndex).to.equal(0);
2216
expect(el.getAttribute('tabindex')).to.equal('0');

0 commit comments

Comments
 (0)