Skip to content

fix(slide-toggle): invert the thumb and slide gesture in rtl #12284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/slide-toggle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ng_module(
deps = [
"//src/lib/core",
"//src/cdk/a11y",
"//src/cdk/bidi",
"//src/cdk/coercion",
"//src/cdk/observers",
"//src/cdk/platform",
Expand Down
18 changes: 17 additions & 1 deletion src/lib/slide-toggle/slide-toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ $mat-slide-toggle-bar-track-width: $mat-slide-toggle-bar-width - $mat-slide-togg
&.mat-checked {
.mat-slide-toggle-thumb-container {
transform: translate3d($mat-slide-toggle-bar-track-width, 0, 0);

[dir='rtl'] & {
transform: translate3d(-$mat-slide-toggle-bar-track-width, 0, 0);
}
}
}

Expand Down Expand Up @@ -115,6 +119,11 @@ $mat-slide-toggle-bar-track-width: $mat-slide-toggle-bar-width - $mat-slide-togg
._mat-animation-noopable & {
transition: none;
}

[dir='rtl'] & {
left: auto;
right: 0;
}
}

// The visual thumb element that moves inside of the thumb bar.
Expand Down Expand Up @@ -147,8 +156,15 @@ $mat-slide-toggle-bar-track-width: $mat-slide-toggle-bar-width - $mat-slide-togg
.mat-slide-toggle-input {
// Move the input to the bottom and in the middle of the thumb.
// Visual improvement to properly show browser popups when being required.
$horizontal-offset: $mat-slide-toggle-thumb-size / 2;

bottom: 0;
left: $mat-slide-toggle-thumb-size / 2;
left: $horizontal-offset;

[dir='rtl'] & {
left: auto;
right: $horizontal-offset;
}
}

.mat-slide-toggle-bar,
Expand Down
52 changes: 50 additions & 2 deletions src/lib/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ComponentFixture, fakeAsync, flushMicrotasks, TestBed, tick} from '@angu
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
import {defaultRippleAnimationConfig} from '@angular/material/core';
import {By, HAMMER_GESTURE_CONFIG} from '@angular/platform-browser';
import {BidiModule, Direction} from '@angular/cdk/bidi';
import {TestGestureConfig} from '../slider/test-gesture-config';
import {MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS} from './slide-toggle-config';
import {MatSlideToggle, MatSlideToggleChange, MatSlideToggleModule} from './index';
Expand All @@ -18,7 +19,7 @@ describe('MatSlideToggle without forms', () => {
mutationObserverCallbacks = [];

TestBed.configureTestingModule({
imports: [MatSlideToggleModule],
imports: [MatSlideToggleModule, BidiModule],
declarations: [
SlideToggleBasic,
SlideToggleWithTabindexAttr,
Expand Down Expand Up @@ -493,6 +494,29 @@ describe('MatSlideToggle without forms', () => {
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));

it('should drag from start to end in RTL', fakeAsync(() => {
testComponent.direction = 'rtl';
fixture.detectChanges();

expect(slideToggle.checked).toBe(false);

gestureConfig.emitEventForElement('slidestart', slideThumbContainer);

expect(slideThumbContainer.classList).toContain('mat-dragging');

gestureConfig.emitEventForElement('slide', slideThumbContainer, {
deltaX: -200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
});

gestureConfig.emitEventForElement('slideend', slideThumbContainer);

// Flush the timeout for the slide ending.
tick();

expect(slideToggle.checked).toBe(true);
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));

it('should drag from end to start', fakeAsync(() => {
slideToggle.checked = true;

Expand All @@ -513,6 +537,29 @@ describe('MatSlideToggle without forms', () => {
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));

it('should drag from end to start in RTL', fakeAsync(() => {
testComponent.direction = 'rtl';
fixture.detectChanges();

slideToggle.checked = true;

gestureConfig.emitEventForElement('slidestart', slideThumbContainer);

expect(slideThumbContainer.classList).toContain('mat-dragging');

gestureConfig.emitEventForElement('slide', slideThumbContainer, {
deltaX: 200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
});

gestureConfig.emitEventForElement('slideend', slideThumbContainer);

// Flush the timeout for the slide ending.
tick();

expect(slideToggle.checked).toBe(false);
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));

it('should not drag when disabled', fakeAsync(() => {
slideToggle.disabled = true;

Expand Down Expand Up @@ -943,7 +990,7 @@ describe('MatSlideToggle with forms', () => {

@Component({
template: `
<mat-slide-toggle [required]="isRequired"
<mat-slide-toggle [dir]="direction" [required]="isRequired"
[disabled]="isDisabled"
[color]="slideColor"
[id]="slideId"
Expand Down Expand Up @@ -976,6 +1023,7 @@ class SlideToggleBasic {
labelPosition: string;
toggleTriggered: number = 0;
dragTriggered: number = 0;
direction: Direction = 'ltr';

onSlideClick: (event?: Event) => void = () => {};
onSlideChange = (event: MatSlideToggleChange) => this.lastEvent = event;
Expand Down
9 changes: 6 additions & 3 deletions src/lib/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {Platform} from '@angular/cdk/platform';
import {
Expand Down Expand Up @@ -193,7 +194,8 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
private _ngZone: NgZone,
@Inject(MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS)
public defaults: MatSlideToggleDefaultOptions,
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
@Optional() private _dir?: Directionality) {
super(elementRef);
this.tabIndex = parseInt(tabIndex) || 0;
}
Expand Down Expand Up @@ -330,9 +332,10 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro

_onDrag(event: HammerInput) {
if (this._dragging) {
this._dragPercentage = this._getDragPercentage(event.deltaX);
const direction = this._dir && this._dir.value === 'rtl' ? -1 : 1;
this._dragPercentage = this._getDragPercentage(event.deltaX * direction);
// Calculate the moved distance based on the thumb bar width.
const dragX = (this._dragPercentage / 100) * this._thumbBarWidth;
const dragX = (this._dragPercentage / 100) * this._thumbBarWidth * direction;
this._thumbEl.nativeElement.style.transform = `translate3d(${dragX}px, 0, 0)`;
}
}
Expand Down