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 *
0 commit comments