@@ -78,38 +78,42 @@ import { EventService } from "carbon-components-angular/utils";
78
78
</label>
79
79
<div
80
80
class="cds--slider"
81
+ (click)="onClick($event)"
81
82
[ngClass]="{'cds--slider--disabled': disabled}">
82
83
<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>
93
96
</div>
94
97
</ng-container>
95
98
<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>
107
112
</div>
108
113
</ng-container>
109
114
<div
110
115
#track
111
- class="cds--slider__track"
112
- (click)="onClick($event)">
116
+ class="cds--slider__track">
113
117
</div>
114
118
<div
115
119
#filledTrack
@@ -446,11 +450,24 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
446
450
this . value = this . value ;
447
451
}
448
452
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
+ * */
450
457
onClick ( event ) {
451
458
if ( this . disabled ) { return ; }
452
459
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
+
454
471
this . value = this . value ;
455
472
}
456
473
0 commit comments