From cb747d9421516e37ece272e64e873230548f9aaf Mon Sep 17 00:00:00 2001 From: Alex Ghiu Date: Fri, 25 Oct 2019 17:30:30 +0200 Subject: [PATCH] fix(switch): cross-browser fixes --- packages/switch/src/LionButtonSwitch.js | 56 ++++++++++++------- packages/switch/stories/index.stories.js | 9 ++- .../switch/test/lion-button-switch.test.js | 6 -- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/packages/switch/src/LionButtonSwitch.js b/packages/switch/src/LionButtonSwitch.js index 00f6c2b45..6828324a2 100644 --- a/packages/switch/src/LionButtonSwitch.js +++ b/packages/switch/src/LionButtonSwitch.js @@ -1,9 +1,12 @@ -import { LionButton } from '@lion/button'; -import { html, css } from '@lion/core'; +import { html, css, LitElement, DisabledWithTabIndexMixin } from '@lion/core'; -export class LionButtonSwitch extends LionButton { +export class LionButtonSwitch extends DisabledWithTabIndexMixin(LitElement) { static get properties() { return { + role: { + type: String, + reflect: true, + }, checked: { type: Boolean, reflect: true, @@ -13,23 +16,24 @@ export class LionButtonSwitch extends LionButton { static get styles() { return [ - ...super.styles, css` :host { display: inline-block; position: relative; width: 36px; height: 16px; - /* Override "button" styles */ - min-height: auto; - padding: 0; + outline: 0; } .btn { + position: relative; height: 100%; - /* Override "button" styles */ - min-height: auto; - padding: 0; + outline: 0; + } + + :host(:focus) .btn { + /* if you extend, please overwrite */ + outline: 2px solid #bde4ff; } .button-switch__track { @@ -53,11 +57,12 @@ export class LionButtonSwitch extends LionButton { ]; } - // eslint-disable-next-line class-methods-use-this - _renderBefore() { + render() { return html` -
-
+
+
+
+
`; } @@ -66,6 +71,8 @@ export class LionButtonSwitch extends LionButton { this.role = 'switch'; this.checked = false; this.addEventListener('click', this.__handleToggleStateChange); + this.addEventListener('keydown', this.__handleKeydown); + this.addEventListener('keyup', this.__handleKeyup); } connectedCallback() { @@ -73,15 +80,12 @@ export class LionButtonSwitch extends LionButton { this.setAttribute('aria-checked', `${this.checked}`); } - firstUpdated(changedProperties) { - super.firstUpdated(changedProperties); - this.removeAttribute('type'); - } - __handleToggleStateChange() { if (this.disabled) { return; } + // Force IE11 to focus the component. + this.focus(); this.checked = !this.checked; this.dispatchEvent( 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. * diff --git a/packages/switch/stories/index.stories.js b/packages/switch/stories/index.stories.js index 431aaa719..83d365b75 100644 --- a/packages/switch/stories/index.stories.js +++ b/packages/switch/stories/index.stories.js @@ -4,6 +4,7 @@ import { LitElement } from '@lion/core'; import { LocalizeMixin } from '@lion/localize'; import '../lion-input-switch.js'; +import '../lion-button-switch.js'; import '@lion/form/lion-form.js'; storiesOf('Forms|Switch', module) @@ -83,4 +84,10 @@ storiesOf('Forms|Switch', module) return html` `; - }); + }) + .add( + 'Button', + () => html` + + `, + ); diff --git a/packages/switch/test/lion-button-switch.test.js b/packages/switch/test/lion-button-switch.test.js index 77a9fd02b..fd240532a 100644 --- a/packages/switch/test/lion-button-switch.test.js +++ b/packages/switch/test/lion-button-switch.test.js @@ -1,8 +1,6 @@ import { expect, fixture, html } from '@open-wc/testing'; import sinon from 'sinon'; -import { LionButton } from '@lion/button'; - import '../lion-button-switch.js'; 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', () => { expect(el.tabIndex).to.equal(0); expect(el.getAttribute('tabindex')).to.equal('0');