fix(switch): cross-browser fixes

This commit is contained in:
Alex Ghiu 2019-10-25 17:30:30 +02:00 committed by CubLion
parent 91ebd2f405
commit cb747d9421
3 changed files with 45 additions and 26 deletions

View file

@ -1,9 +1,12 @@
import { LionButton } from '@lion/button'; import { html, css, LitElement, DisabledWithTabIndexMixin } from '@lion/core';
import { html, css } from '@lion/core';
export class LionButtonSwitch extends LionButton { export class LionButtonSwitch extends DisabledWithTabIndexMixin(LitElement) {
static get properties() { static get properties() {
return { return {
role: {
type: String,
reflect: true,
},
checked: { checked: {
type: Boolean, type: Boolean,
reflect: true, reflect: true,
@ -13,23 +16,24 @@ export class LionButtonSwitch extends LionButton {
static get styles() { static get styles() {
return [ return [
...super.styles,
css` css`
:host { :host {
display: inline-block; display: inline-block;
position: relative; position: relative;
width: 36px; width: 36px;
height: 16px; height: 16px;
/* Override "button" styles */ outline: 0;
min-height: auto;
padding: 0;
} }
.btn { .btn {
position: relative;
height: 100%; height: 100%;
/* Override "button" styles */ outline: 0;
min-height: auto; }
padding: 0;
:host(:focus) .btn {
/* if you extend, please overwrite */
outline: 2px solid #bde4ff;
} }
.button-switch__track { .button-switch__track {
@ -53,11 +57,12 @@ export class LionButtonSwitch extends LionButton {
]; ];
} }
// eslint-disable-next-line class-methods-use-this render() {
_renderBefore() {
return html` return html`
<div class="btn">
<div class="button-switch__track"></div> <div class="button-switch__track"></div>
<div class="button-switch__thumb"></div> <div class="button-switch__thumb"></div>
</div>
`; `;
} }
@ -66,6 +71,8 @@ export class LionButtonSwitch extends LionButton {
this.role = 'switch'; this.role = 'switch';
this.checked = false; this.checked = false;
this.addEventListener('click', this.__handleToggleStateChange); this.addEventListener('click', this.__handleToggleStateChange);
this.addEventListener('keydown', this.__handleKeydown);
this.addEventListener('keyup', this.__handleKeyup);
} }
connectedCallback() { connectedCallback() {
@ -73,15 +80,12 @@ export class LionButtonSwitch extends LionButton {
this.setAttribute('aria-checked', `${this.checked}`); this.setAttribute('aria-checked', `${this.checked}`);
} }
firstUpdated(changedProperties) {
super.firstUpdated(changedProperties);
this.removeAttribute('type');
}
__handleToggleStateChange() { __handleToggleStateChange() {
if (this.disabled) { if (this.disabled) {
return; return;
} }
// Force IE11 to focus the component.
this.focus();
this.checked = !this.checked; this.checked = !this.checked;
this.dispatchEvent( this.dispatchEvent(
new Event('checked-changed', { new Event('checked-changed', {
@ -91,6 +95,20 @@ export class LionButtonSwitch extends LionButton {
); );
} }
// eslint-disable-next-line class-methods-use-this
__handleKeydown(e) {
// prevent "space" scrolling on "macOS"
if (e.keyCode === 32) {
e.preventDefault();
}
}
__handleKeyup(e) {
if ([32 /* space */, 13 /* enter */].indexOf(e.keyCode) !== -1) {
this.__handleToggleStateChange();
}
}
/** /**
* We synchronously update aria-checked to support voice over on safari. * We synchronously update aria-checked to support voice over on safari.
* *

View file

@ -4,6 +4,7 @@ import { LitElement } from '@lion/core';
import { LocalizeMixin } from '@lion/localize'; import { LocalizeMixin } from '@lion/localize';
import '../lion-input-switch.js'; import '../lion-input-switch.js';
import '../lion-button-switch.js';
import '@lion/form/lion-form.js'; import '@lion/form/lion-form.js';
storiesOf('Forms|Switch', module) storiesOf('Forms|Switch', module)
@ -83,4 +84,10 @@ storiesOf('Forms|Switch', module)
return html` return html`
<lion-switch-validation-demo></lion-switch-validation-demo> <lion-switch-validation-demo></lion-switch-validation-demo>
`; `;
}); })
.add(
'Button',
() => html`
<lion-button-switch></lion-button-switch>
`,
);

View file

@ -1,8 +1,6 @@
import { expect, fixture, html } from '@open-wc/testing'; import { expect, fixture, html } from '@open-wc/testing';
import sinon from 'sinon'; import sinon from 'sinon';
import { LionButton } from '@lion/button';
import '../lion-button-switch.js'; import '../lion-button-switch.js';
describe('lion-button-switch', () => { describe('lion-button-switch', () => {
@ -13,10 +11,6 @@ describe('lion-button-switch', () => {
`); `);
}); });
it('should behave like a button', () => {
expect(el instanceof LionButton).to.be.true;
});
it('should be focusable', () => { it('should be focusable', () => {
expect(el.tabIndex).to.equal(0); expect(el.tabIndex).to.equal(0);
expect(el.getAttribute('tabindex')).to.equal('0'); expect(el.getAttribute('tabindex')).to.equal('0');