Skip to content

Commit 1d52a20

Browse files
denStrigoyggg
authored andcommitted
fix(toggle): animation in IE11 (#2062)
1 parent 147ec2e commit 1d52a20

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

src/framework/theme/components/toggle/_toggle.component.theme.scss

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@
1414
border-radius: nb-theme(toggle-border-radius);
1515
cursor: nb-theme(toggle-cursor);
1616

17+
/*
18+
We need to set initial positions as Angular animations won't work in IE11 if positions have no initial value.
19+
Setting it in SCSS as we don't have access to theme variables from TS.
20+
*/
21+
@include nb-ltr() {
22+
&.checked .toggle-switcher {
23+
left: calc(100% - #{nb-theme(toggle-switcher-size)} - #{nb-theme(toggle-border-width)});
24+
}
25+
26+
&:not(.checked) .toggle-switcher {
27+
right: 0;
28+
}
29+
}
30+
31+
@include nb-rtl() {
32+
&.checked .toggle-switcher {
33+
right: calc(100% - #{nb-theme(toggle-switcher-size)} - #{nb-theme(toggle-border-width)});
34+
}
35+
36+
&:not(.checked) .toggle-switcher {
37+
left: 0;
38+
}
39+
}
1740
}
1841

1942
.native-input:enabled:focus + .toggle {

src/framework/theme/components/toggle/toggle.component.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { trigger, state, style, animate, transition } from '@angular/animations'
2020
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
2121
import { Subject } from 'rxjs';
2222
import { takeUntil } from 'rxjs/operators';
23-
import { NbLayoutDirectionService, NbLayoutDirection } from '../../services/direction.service';
23+
import { NbLayoutDirectionService } from '../../services/direction.service';
2424
import { NbComponentStatus } from '../component-status';
2525

2626
import { convertToBoolProperty, emptyStatusWarning } from '../helpers';
@@ -253,11 +253,11 @@ import { convertToBoolProperty, emptyStatusWarning } from '../helpers';
253253
@Component({
254254
selector: 'nb-toggle',
255255
animations: [
256-
trigger('onOff', [
257-
state('ltrOn', style({ right: 0 })),
258-
state('rtlOn', style({ left: 0 })),
256+
trigger('position', [
257+
state('right', style({ right: 0, left: '*' })),
258+
state('left', style({ left: 0, right: '*' })),
259259
transition(':enter', [animate(0)]),
260-
transition('* <=> *', [animate('0.15s')]),
260+
transition('right <=> left', [animate('0.15s')]),
261261
]),
262262
],
263263
template: `
@@ -272,7 +272,7 @@ import { convertToBoolProperty, emptyStatusWarning } from '../helpers';
272272
(blur)="onTouched()"
273273
(click)="onInputClick($event)">
274274
<div class="toggle" [class.checked]="checked">
275-
<span [@onOff]="checkState()" class="toggle-switcher">
275+
<span [@position]="checkState()" class="toggle-switcher">
276276
<nb-icon *ngIf="checked" icon="checkmark-bold-outline" pack="nebular-essentials"></nb-icon>
277277
</span>
278278
</div>
@@ -423,12 +423,10 @@ export class NbToggleComponent implements OnInit, OnDestroy, ControlValueAccesso
423423

424424
checkState(): string {
425425
if (this.checked) {
426-
if (this.layoutDirection.getDirection() === NbLayoutDirection.LTR) {
427-
return 'ltrOn';
428-
} else {
429-
return 'rtlOn';
430-
}
426+
return this.layoutDirection.isLtr() ? 'right' : 'left';
431427
}
428+
429+
return this.layoutDirection.isLtr() ? 'left' : 'right';
432430
}
433431

434432
registerOnChange(fn: any) {

src/framework/theme/components/toggle/toggle.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Component, DebugElement } from '@angular/core';
55
import { By } from '@angular/platform-browser';
66
import { ReactiveFormsModule, FormControl } from '@angular/forms';
77
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
8-
import { NbLayoutDirectionService } from '../../services/direction.service';
8+
import { NbLayoutDirectionService, NbLayoutDirection } from '../../services/direction.service';
99

1010

1111
describe('Component: NbToggle', () => {
@@ -80,6 +80,39 @@ describe('Component: NbToggle', () => {
8080
fixture.detectChanges();
8181
expect(testContainerEl.classList.contains('status-info')).toBeTruthy();
8282
});
83+
84+
describe('state animation', () => {
85+
86+
beforeEach(() => {
87+
TestBed.get(NbLayoutDirectionService).setDirection(NbLayoutDirection.LTR);
88+
});
89+
90+
afterAll(() => {
91+
TestBed.get(NbLayoutDirectionService).setDirection(NbLayoutDirection.LTR);
92+
});
93+
94+
it(`should has 'left' position when unchecked in LTR`, () => {
95+
expect(toggle.checkState()).toEqual('left');
96+
});
97+
98+
it(`should has 'right' position when checked in LTR`, () => {
99+
toggle.checked = true;
100+
expect(toggle.checkState()).toEqual('right');
101+
});
102+
103+
it(`should has 'right' position when unchecked in RTL`, () => {
104+
const directionService: NbLayoutDirectionService = TestBed.get(NbLayoutDirectionService);
105+
directionService.setDirection(NbLayoutDirection.RTL);
106+
expect(toggle.checkState()).toEqual('right');
107+
});
108+
109+
it(`should has 'left' position when checked in RTL`, () => {
110+
const directionService: NbLayoutDirectionService = TestBed.get(NbLayoutDirectionService);
111+
directionService.setDirection(NbLayoutDirection.RTL);
112+
toggle.checked = true;
113+
expect(toggle.checkState()).toEqual('left');
114+
});
115+
});
83116
});
84117

85118
// Test component with reactive forms

0 commit comments

Comments
 (0)