@@ -78,38 +78,42 @@ import { EventService } from "carbon-components-angular/utils";
7878 </label>
7979 <div
8080 class="cds--slider"
81+ (click)="onClick($event)"
8182 [ngClass]="{'cds--slider--disabled': disabled}">
8283 <ng-container *ngIf="!isRange()">
83- <div
84- #thumbs
85- role="slider"
86- [id]="id"
87- [attr.aria-labelledby]="labelId"
88- class="cds--slider__thumb"
89- [ngStyle]="{left: getFractionComplete(value) * 100 + '%'}"
90- tabindex="0"
91- (mousedown)="onMouseDown($event)"
92- (keydown)="onKeyDown($event)">
84+ <div class="cds--slider__thumb-wrapper"
85+ [ngStyle]="{insetInlineStart: getFractionComplete(value) * 100 + '%'}">
86+ <div
87+ #thumbs
88+ role="slider"
89+ [id]="id"
90+ [attr.aria-labelledby]="labelId"
91+ class="cds--slider__thumb"
92+ tabindex="0"
93+ (mousedown)="onMouseDown($event)"
94+ (keydown)="onKeyDown($event)">
95+ </div>
9396 </div>
9497 </ng-container>
9598 <ng-container *ngIf="isRange()">
96- <div
97- #thumbs
98- *ngFor="let thumb of value; let i = index; trackBy: trackThumbsBy"
99- role="slider"
100- [id]="id + (i > 0 ? '-' + i : '')"
101- [attr.aria-labelledby]="labelId"
102- class="cds--slider__thumb"
103- [ngStyle]="{left: getFractionComplete(thumb) * 100 + '%'}"
104- tabindex="0"
105- (mousedown)="onMouseDown($event, i)"
106- (keydown)="onKeyDown($event, i)">
99+ <div class="cds--slider__thumb-wrapper"
100+ [ngStyle]="{insetInlineStart: getFractionComplete(thumb) * 100 + '%'}"
101+ *ngFor="let thumb of value; let i = index; trackBy: trackThumbsBy">
102+ <div
103+ #thumbs
104+ role="slider"
105+ [id]="id + (i > 0 ? '-' + i : '')"
106+ [attr.aria-labelledby]="labelId"
107+ class="cds--slider__thumb"
108+ tabindex="0"
109+ (mousedown)="onMouseDown($event, i)"
110+ (keydown)="onKeyDown($event, i)">
111+ </div>
107112 </div>
108113 </ng-container>
109114 <div
110115 #track
111- class="cds--slider__track"
112- (click)="onClick($event)">
116+ class="cds--slider__track">
113117 </div>
114118 <div
115119 #filledTrack
@@ -446,11 +450,24 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
446450 this . value = this . value ;
447451 }
448452
449- /** Handles clicks on the range track, and setting the value to it's "real" equivalent */
453+ /**
454+ * Handles clicks on the slider, and setting the value to it's "real" equivalent.
455+ * Will assign the value to the closest thumb if in range mode.
456+ * */
450457 onClick ( event ) {
451458 if ( this . disabled ) { return ; }
452459 const trackLeft = this . track . nativeElement . getBoundingClientRect ( ) . left ;
453- this . _value [ 0 ] = this . convertToValue ( event . clientX - trackLeft ) ;
460+ const trackValue = this . convertToValue ( event . clientX - trackLeft ) ;
461+ if ( this . isRange ( ) ) {
462+ if ( Math . abs ( this . _value [ 0 ] - trackValue ) < Math . abs ( this . _value [ 1 ] - trackValue ) ) {
463+ this . _value [ 0 ] = trackValue ;
464+ } else {
465+ this . _value [ 1 ] = trackValue ;
466+ }
467+ } else {
468+ this . _value [ 0 ] = trackValue ;
469+ }
470+
454471 this . value = this . value ;
455472 }
456473
0 commit comments