Skip to content

Commit

Permalink
feat(slide-toggle): add support for labelPosition
Browse files Browse the repository at this point in the history
* Adds an option to change the position of the slide-toggle's label.

Closes angular#2820
  • Loading branch information
devversion committed Jan 27, 2017
1 parent 55b9224 commit b12d940
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/lib/slide-toggle/slide-toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,30 @@ md-slide-toggle {
cursor: pointer;
}

/* If the label should be placed before the thumb then we just change the orders. */
.md-slide-toggle-label-before {
.md-slide-toggle-label { order: 1; }
.md-slide-toggle-container { order: 2; }
}

// Container for the composition of the slide-toggle / switch indicator.
.md-slide-toggle-container {
cursor: grab;
width: $md-slide-toggle-width;
height: $md-slide-toggle-height;

position: relative;
}

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

[dir='rtl'] & {
/* Switch the margins in RTL mode and also switch it if the labelPosition is set to before. */
[dir='rtl'], .md-slide-toggle-label-before {
.md-slide-toggle-container {
margin-left: $md-slide-toggle-spacing;
margin-right: 0;
}
Expand Down
12 changes: 12 additions & 0 deletions src/lib/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,16 @@ describe('MdSlideToggle', () => {
expect(document.activeElement).toBe(inputElement);
expect(slideToggleElement.classList).toContain('md-slide-toggle-focused');
});

it('should set a element class if labelPosition is set to before', () => {
expect(slideToggleElement.classList).not.toContain('md-slide-toggle-label-before');

testComponent.labelPosition = 'before';
fixture.detectChanges();

expect(slideToggleElement.classList).toContain('md-slide-toggle-label-before');
});

});

describe('custom template', () => {
Expand Down Expand Up @@ -584,6 +594,7 @@ function dispatchFocusChangeEvent(eventName: string, element: HTMLElement): void
[aria-label]="slideLabel"
[aria-labelledby]="slideLabelledBy"
[tabIndex]="slideTabindex"
[labelPosition]="labelPosition"
(change)="onSlideChange($event)"
(click)="onSlideClick($event)">
Expand All @@ -603,6 +614,7 @@ class SlideToggleTestApp {
slideLabelledBy: string;
slideTabindex: number;
lastEvent: MdSlideToggleChange;
labelPosition: string;

onSlideClick(event: Event) {}
onSlideChange(event: MdSlideToggleChange) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ let nextId = 0;
'[class.md-disabled]': 'disabled',
// This md-slide-toggle prefix will change, once the temporary ripple is removed.
'[class.md-slide-toggle-focused]': '_hasFocus',
'[class.md-slide-toggle-label-before]': 'labelPosition == "before"',
'(mousedown)': '_setMousedown()'
},
templateUrl: 'slide-toggle.html',
Expand Down Expand Up @@ -85,6 +86,9 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
/** Used to specify the tabIndex value for the underlying input element. */
@Input() tabIndex: number = 0;

/** Whether the label should appear after or before the checkbox. Defaults to 'after' */
@Input() labelPosition: 'before' | 'after' = 'after';

/** Used to set the aria-label attribute on the underlying input element. */
@Input('aria-label') ariaLabel: string = null;

Expand Down

0 comments on commit b12d940

Please sign in to comment.