Skip to content

Commit 68a0c90

Browse files
devversionkara
authored andcommitted
feat(slide-toggle): add support for labelPosition (#2836)
Closes #2820
1 parent f899b5f commit 68a0c90

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/lib/slide-toggle/slide-toggle.scss

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,30 @@ md-slide-toggle {
6666
cursor: pointer;
6767
}
6868

69+
/* If the label should be placed before the thumb then we just change the orders. */
70+
.md-slide-toggle-label-before {
71+
.md-slide-toggle-label { order: 1; }
72+
.md-slide-toggle-container { order: 2; }
73+
}
74+
6975
// Container for the composition of the slide-toggle / switch indicator.
7076
.md-slide-toggle-container {
7177
cursor: grab;
7278
width: $md-slide-toggle-width;
7379
height: $md-slide-toggle-height;
7480

7581
position: relative;
82+
}
7683

84+
/* Apply the margin for slide-toggles and revert it for RTL toggles with labelPosition before. */
85+
[dir='rtl'] .md-slide-toggle-label-before .md-slide-toggle-container, .md-slide-toggle-container {
7786
margin-right: $md-slide-toggle-spacing;
87+
margin-left: 0;
88+
}
7889

79-
[dir='rtl'] & {
90+
/* Switch the margins in RTL mode and also switch it if the labelPosition is set to before. */
91+
[dir='rtl'], .md-slide-toggle-label-before {
92+
.md-slide-toggle-container {
8093
margin-left: $md-slide-toggle-spacing;
8194
margin-right: 0;
8295
}

src/lib/slide-toggle/slide-toggle.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ describe('MdSlideToggle', () => {
357357
expect(document.activeElement).toBe(inputElement);
358358
expect(slideToggleElement.classList).toContain('md-slide-toggle-focused');
359359
});
360+
361+
it('should set a element class if labelPosition is set to before', () => {
362+
expect(slideToggleElement.classList).not.toContain('md-slide-toggle-label-before');
363+
364+
testComponent.labelPosition = 'before';
365+
fixture.detectChanges();
366+
367+
expect(slideToggleElement.classList).toContain('md-slide-toggle-label-before');
368+
});
369+
360370
});
361371

362372
describe('custom template', () => {
@@ -584,6 +594,7 @@ function dispatchFocusChangeEvent(eventName: string, element: HTMLElement): void
584594
[aria-label]="slideLabel"
585595
[aria-labelledby]="slideLabelledBy"
586596
[tabIndex]="slideTabindex"
597+
[labelPosition]="labelPosition"
587598
(change)="onSlideChange($event)"
588599
(click)="onSlideClick($event)">
589600
@@ -603,6 +614,7 @@ class SlideToggleTestApp {
603614
slideLabelledBy: string;
604615
slideTabindex: number;
605616
lastEvent: MdSlideToggleChange;
617+
labelPosition: string;
606618

607619
onSlideClick(event: Event) {}
608620
onSlideChange(event: MdSlideToggleChange) {

src/lib/slide-toggle/slide-toggle.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ let nextId = 0;
5151
'[class.md-disabled]': 'disabled',
5252
// This md-slide-toggle prefix will change, once the temporary ripple is removed.
5353
'[class.md-slide-toggle-focused]': '_hasFocus',
54+
'[class.md-slide-toggle-label-before]': 'labelPosition == "before"',
5455
'(mousedown)': '_setMousedown()'
5556
},
5657
templateUrl: 'slide-toggle.html',
@@ -85,6 +86,9 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
8586
/** Used to specify the tabIndex value for the underlying input element. */
8687
@Input() tabIndex: number = 0;
8788

89+
/** Whether the label should appear after or before the slide-toggle. Defaults to 'after' */
90+
@Input() labelPosition: 'before' | 'after' = 'after';
91+
8892
/** Used to set the aria-label attribute on the underlying input element. */
8993
@Input('aria-label') ariaLabel: string = null;
9094

0 commit comments

Comments
 (0)