Skip to content

Commit cf102eb

Browse files
authored
[fix]: Improve slider directional css and make direction change obser… (#33834)
1 parent e242da3 commit cf102eb

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "makes direction change observable for slider",
4+
"packageName": "@fluentui/web-components",
5+
"email": "jes@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/web-components/docs/web-components.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,6 +3504,8 @@ export class Slider extends FASTElement implements SliderConfiguration {
35043504
decrement(): void;
35053505
// @internal (undocumented)
35063506
direction: Direction;
3507+
// (undocumented)
3508+
directionChanged(): void;
35073509
disabled: boolean;
35083510
// (undocumented)
35093511
protected disabledChanged(): void;

packages/web-components/src/slider/slider.styles.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ export const styles = css`
140140
width: var(--slider-progress);
141141
}
142142
143+
:host(:dir(rtl)) .track::before {
144+
width: calc(100% - var(--slider-progress));
145+
}
146+
143147
:host(${verticalState}) .track::before {
144148
width: 100%;
145149
bottom: 0;

packages/web-components/src/slider/slider.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class Slider extends FASTElement implements SliderConfiguration {
6868
switch (propertyName) {
6969
case 'min':
7070
case 'max':
71-
this.setSliderPosition(this.direction);
71+
this.setSliderPosition();
7272
case 'step':
7373
this.handleStepStyles();
7474
break;
@@ -225,7 +225,7 @@ export class Slider extends FASTElement implements SliderConfiguration {
225225
*/
226226
public get value(): string {
227227
Observable.track(this, 'value');
228-
return this._value.toString();
228+
return this._value?.toString() ?? '';
229229
}
230230

231231
public set value(value: string) {
@@ -245,7 +245,7 @@ export class Slider extends FASTElement implements SliderConfiguration {
245245
this._value = value.toString();
246246
this.elementInternals.ariaValueNow = this._value;
247247
this.elementInternals.ariaValueText = this.valueTextFormatter(this._value);
248-
this.setSliderPosition(this.direction);
248+
this.setSliderPosition();
249249
this.$emit('change');
250250
this.setFormValue(value);
251251
Observable.notify(this, 'value');
@@ -303,6 +303,9 @@ export class Slider extends FASTElement implements SliderConfiguration {
303303
*/
304304
@observable
305305
public direction: Direction = Direction.ltr;
306+
public directionChanged(): void {
307+
this.setSliderPosition();
308+
}
306309

307310
/**
308311
* @internal
@@ -502,7 +505,7 @@ export class Slider extends FASTElement implements SliderConfiguration {
502505
swapStates(this.elementInternals, prev, next, Orientation);
503506

504507
if (this.$fastController.isConnected) {
505-
this.setSliderPosition(this.direction);
508+
this.setSliderPosition();
506509
}
507510
}
508511

@@ -536,7 +539,7 @@ export class Slider extends FASTElement implements SliderConfiguration {
536539
this.setupTrackConstraints();
537540
this.setupListeners();
538541
this.setupDefaultValue();
539-
this.setSliderPosition(this.direction);
542+
this.setSliderPosition();
540543

541544
Observable.getNotifier(this).subscribe(this, 'max');
542545
Observable.getNotifier(this).subscribe(this, 'min');
@@ -629,18 +632,16 @@ export class Slider extends FASTElement implements SliderConfiguration {
629632

630633
/**
631634
* Places the thumb based on the current value
632-
*
633-
* @param direction - writing mode
634-
*/
635-
private setSliderPosition(direction: Direction): void {
636-
const newPct: number = convertPixelToPercent(parseFloat(this.value), this.minAsNumber, this.maxAsNumber, direction);
637-
const percentage: number = (1 - newPct) * 100;
638-
const thumbPosition = `calc(100% - ${percentage}%)`;
639-
const trackProgress =
640-
!(this.orientation === Orientation.vertical) && direction === Direction.rtl
641-
? `${percentage}%`
642-
: `calc(100% - ${percentage}%)`;
643-
this.position = `--slider-thumb: ${thumbPosition}; --slider-progress: ${trackProgress}`;
635+
*/
636+
private setSliderPosition(): void {
637+
const newPct: number = convertPixelToPercent(
638+
parseFloat(this.value),
639+
this.minAsNumber,
640+
this.maxAsNumber,
641+
this.direction,
642+
);
643+
const percentage: number = newPct * 100;
644+
this.position = `--slider-thumb: ${percentage}%; --slider-progress: ${percentage}%`;
644645
}
645646

646647
/**

0 commit comments

Comments
 (0)