Skip to content

Commit e76a319

Browse files
committed
refactor(Toggle Switch): incorporates feedback from UX and a11y
1 parent 286f0e3 commit e76a319

File tree

7 files changed

+86
-84
lines changed

7 files changed

+86
-84
lines changed

packages/core-components/src/components.d.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -991,13 +991,17 @@ export namespace Components {
991991
*/
992992
"disabled": boolean;
993993
/**
994-
* The label of the toggle button. This is required
994+
* An optional label for the toggle switch.
995995
*/
996-
"label": string;
996+
"label"?: string;
997997
/**
998998
* The alignment of the toggle switch label.
999999
*/
10001000
"labelPosition"?: 'left' | 'right';
1001+
/**
1002+
* The toggle name. Use this if the toggle switch is used in a form group.
1003+
*/
1004+
"name"?: string;
10011005
/**
10021006
* Whether or not the toggle button is currently on or off. Per default it is off.
10031007
*/
@@ -3022,13 +3026,17 @@ declare namespace LocalJSX {
30223026
*/
30233027
"disabled"?: boolean;
30243028
/**
3025-
* The label of the toggle button. This is required
3029+
* An optional label for the toggle switch.
30263030
*/
3027-
"label": string;
3031+
"label"?: string;
30283032
/**
30293033
* The alignment of the toggle switch label.
30303034
*/
30313035
"labelPosition"?: 'left' | 'right';
3036+
/**
3037+
* The toggle name. Use this if the toggle switch is used in a form group.
3038+
*/
3039+
"name"?: string;
30323040
/**
30333041
* Emits the toggle switch value when it's state changes.
30343042
*/

packages/core-components/src/components/toggle-switch/readme.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
## Properties
99

10-
| Property | Attribute | Description | Type | Default |
11-
| -------------------- | ---------------- | -------------------------------------------------------------------------------- | ------------------- | ----------- |
12-
| `disabled` | `disabled` | Whether or not the toggle button is currently disabled. Per default it is false. | `boolean` | `false` |
13-
| `label` _(required)_ | `label` | The label of the toggle button. This is required | `string` | `undefined` |
14-
| `labelPosition` | `label-position` | The alignment of the toggle switch label. | `"left" \| "right"` | `'left'` |
15-
| `state` | `state` | Whether or not the toggle button is currently on or off. Per default it is off. | `boolean` | `false` |
10+
| Property | Attribute | Description | Type | Default |
11+
| --------------- | ---------------- | -------------------------------------------------------------------------------- | ------------------- | ----------- |
12+
| `disabled` | `disabled` | Whether or not the toggle button is currently disabled. Per default it is false. | `boolean` | `false` |
13+
| `label` | `label` | An optional label for the toggle switch. | `string` | `undefined` |
14+
| `labelPosition` | `label-position` | The alignment of the toggle switch label. | `"left" \| "right"` | `'left'` |
15+
| `state` | `state` | Whether or not the toggle button is currently on or off. Per default it is off. | `boolean` | `false` |
1616

1717

1818
## Events

packages/core-components/src/components/toggle-switch/toggle-switch.e2e.tsx

+2-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ describe('B2B-InputList', () => {
2424
state="true"></b2b-toggle-switch>
2525
`);
2626

27-
const label = await page.find(
28-
'b2b-toggle-switch >>> .b2b-toggle__text-left',
29-
);
27+
const label = await page.find('b2b-toggle-switch >>> .b2b-toggle__text');
3028
expect(label.textContent).toBe('IT-test-label');
3129
});
3230

@@ -40,9 +38,7 @@ describe('B2B-InputList', () => {
4038
state="true"></b2b-toggle-switch>
4139
`);
4240

43-
const label = await page.find(
44-
'b2b-toggle-switch >>> .b2b-toggle__text-right',
45-
);
41+
const label = await page.find('b2b-toggle-switch >>> .b2b-toggle__text');
4642
expect(label.textContent).toBe('IT-test-label-right');
4743
});
4844

packages/core-components/src/components/toggle-switch/toggle-switch.scss

+35-42
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,65 @@ $transition-size: var(--b2b-size-50);
99
$special-grey: #b1b1b1;
1010

1111
.b2b-toggle {
12-
display: flex;
12+
display: inline-flex;
13+
position: relative;
1314
font-family: var(--b2b-font-family-default);
1415

1516
&__icon {
1617
position: absolute;
1718
fill: var(--b2b-color-success-100);
1819
display: inline-block;
19-
margin-top: var(--b2b-size-space-25);
20-
margin-left: var(--b2b-size-space-150);
20+
left: var(--b2b-size-60);
21+
top: var(--b2b-size-10);
2122
width: var(--b2b-size-30);
2223
height: var(--b2b-size-30);
2324
z-index: 1000;
2425
opacity: 0;
25-
cursor: pointer;
2626

2727
&--show {
28-
.b2b-toggle__icon {
29-
transition: opacity 1s;
30-
transition: opacity 1s ease;
31-
opacity: 1;
32-
}
28+
transition: opacity 1s;
29+
transition: opacity 1s ease;
30+
opacity: 1;
3331
}
3432
}
3533

3634
&__left {
37-
display: flex;
38-
flex-direction: row-reverse;
39-
}
40-
41-
&__right {
42-
display: flex;
43-
flex-direction: row;
44-
}
35+
.b2b-toggle__label {
36+
flex-direction: row-reverse;
37+
}
4538

46-
&__input {
47-
display: none;
39+
.b2b-toggle__text {
40+
margin-right: var(--b2b-size-30);
41+
}
4842

49-
&:checked + .b2b-toggle__label .b2b-toggle__switch {
50-
&::before {
51-
background-color: var(--b2b-color-success-100);
52-
}
43+
.b2b-toggle__icon {
44+
left: var(--b2b-size-60);
45+
}
46+
}
5347

54-
&::after {
55-
background-color: var(--b2b-color-white-100);
56-
transform: translateX($transition-size);
57-
}
48+
&__right {
49+
.b2b-toggle__text {
50+
margin-left: var(--b2b-size-30);
5851
}
5952
}
6053

6154
&__label {
55+
display: inline-flex;
6256
align-items: center;
63-
cursor: pointer;
6457
}
6558

6659
&__switch {
6760
position: relative;
6861
display: flex;
6962
width: $switch-width;
7063
height: $switch-height;
64+
cursor: pointer;
7165

7266
&::before,
7367
&::after {
7468
position: absolute;
7569
content: '';
70+
cursor: pointer;
7671
}
7772

7873
&::before {
@@ -95,34 +90,32 @@ $special-grey: #b1b1b1;
9590
}
9691
}
9792

98-
&--disabled {
93+
&--checked {
94+
.b2b-toggle__switch::before {
95+
background-color: var(--b2b-color-success-100);
96+
}
97+
9998
.b2b-toggle__switch::after {
100-
background-color: var(--b2b-color-grey-100);
99+
background-color: var(--b2b-color-white-100);
100+
transform: translateX($transition-size);
101101
}
102+
}
102103

104+
&--disabled {
103105
.b2b-toggle__icon {
104106
fill: $special-grey;
105107
}
106108

107-
.b2b-toggle__input:checked + .b2b-toggle__label .b2b-toggle__switch {
109+
.b2b-toggle__switch {
110+
cursor: default;
111+
108112
&::before {
109113
background-color: $special-grey; // special color from figma
110114
}
111115

112116
&::after {
113117
background-color: var(--b2b-color-grey-100);
114-
transform: translateX($transition-size);
115118
}
116119
}
117120
}
118-
119-
&__text-left {
120-
margin-top: var(--b2b-size-5);
121-
margin-right: var(--b2b-size-space-50);
122-
}
123-
124-
&__text-right {
125-
margin-top: var(--b2b-size-5);
126-
margin-left: var(--b2b-size-space-50);
127-
}
128121
}

packages/core-components/src/components/toggle-switch/toggle-switch.tsx

+29-24
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ import { ToggleSwitchEventDetail } from '../../utils/interfaces/interaction.inte
1717
export class B2bToggleSwitchComponent {
1818
@Element() host: HTMLB2bToggleSwitchElement;
1919

20-
/** The label of the toggle button. This is required */
21-
@Prop() label!: string;
20+
/** An optional label for the toggle switch. */
21+
@Prop() label?: string;
22+
23+
/**The toggle name. Use this if the toggle switch is used in a form group. */
24+
@Prop() name?: string;
2225

2326
/** The alignment of the toggle switch label. */
2427
@Prop() labelPosition?: 'left' | 'right' = 'left';
@@ -27,13 +30,14 @@ export class B2bToggleSwitchComponent {
2730
@Prop() disabled = false;
2831

2932
/** Whether or not the toggle button is currently on or off. Per default it is off. */
30-
@Prop() state = false;
33+
@Prop({ mutable: true }) state = false;
3134

3235
/** Emits the toggle switch value when it's state changes. */
3336
@Event({ eventName: 'b2b-change' })
3437
b2bChange: EventEmitter<ToggleSwitchEventDetail>;
3538

3639
private emitDetail = () => {
40+
console.log(this.state);
3741
this.b2bChange.emit({
3842
value: this.state,
3943
});
@@ -53,43 +57,44 @@ export class B2bToggleSwitchComponent {
5357
this.emitDetail();
5458
};
5559

60+
private onKeyDown = (ev: KeyboardEvent) => {
61+
if (ev.key != 'Enter' || this.disabled) {
62+
return;
63+
} else {
64+
this.state = !this.state;
65+
}
66+
this.emitDetail();
67+
};
68+
5669
render() {
5770
return (
5871
<Host>
5972
<div
6073
class={{
6174
'b2b-toggle': true,
75+
[`b2b-toggle__${this.labelPosition}`]: true,
6276
'b2b-toggle--disabled': this.disabled,
77+
'b2b-toggle--checked': this.state,
6378
}}>
64-
<div
65-
class={{
66-
[`b2b-toggle__${this.labelPosition}`]: true,
67-
'b2b-toggle__icon--show': this.state,
68-
}}>
69-
<div onClick={this.onClick}>
79+
<label class="b2b-toggle__label" htmlFor="toggle">
80+
<span
81+
class="b2b-toggle__switch"
82+
role="switch"
83+
tabIndex={0}
84+
onKeyDown={this.onKeyDown}
85+
onClick={this.onClick}>
7086
<svg
7187
class={{
7288
'b2b-toggle__icon': true,
89+
'b2b-toggle__icon--show': this.state,
7390
}}
7491
xmlns="http://www.w3.org/2000/svg"
7592
viewBox="0 0 30 30">
7693
<path d="M11.798 25.082c-.341 0-.681-.13-.942-.389l-7.132-7.115a1.334 1.334 0 0 1 1.884-1.888l6.19 6.175L26.391 7.307a1.334 1.334 0 0 1 1.884 1.888L12.74 24.693c-.26.259-.601.389-.942.389z" />
7794
</svg>
78-
<input
79-
class="b2b-toggle__input"
80-
id="toggle"
81-
type="checkbox"
82-
checked={this.state}
83-
disabled={this.disabled}
84-
/>
85-
<label class="b2b-toggle__label" htmlFor="toggle">
86-
<span class="b2b-toggle__switch" />
87-
</label>
88-
</div>
89-
<div class={{ [`b2b-toggle__text-${this.labelPosition}`]: true }}>
90-
{this.label}
91-
</div>
92-
</div>
95+
</span>
96+
{this.label && <span class="b2b-toggle__text">{this.label}</span>}
97+
</label>
9398
</div>
9499
</Host>
95100
);

packages/core-components/src/docs/config/components-args.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3022,7 +3022,7 @@
30223022
},
30233023
"defaultValue": {}
30243024
},
3025-
"description": "The label of the toggle button. This is required"
3025+
"description": "An optional label for the toggle switch."
30263026
},
30273027
"labelPosition": {
30283028
"options": [

packages/core-components/src/html/interaction.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@
488488
>Enabled Toggle Switch Component - On</b2b-headline
489489
>
490490
<b2b-toggle-switch
491-
label="Label"
491+
label="Long Label for the fun of it"
492492
disabled="false"
493493
state="true"></b2b-toggle-switch>
494494

0 commit comments

Comments
 (0)