Skip to content

Commit 9c60156

Browse files
committed
fix(slider): update "value" default to match browser native range input
BREAKING CHANGE: When not supplied value will now be set to halfway between min and max.
1 parent 3f4fbc3 commit 9c60156

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

packages/slider/src/SliderHandle.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ export class SliderHandle extends Focusable {
8888

8989
_forcedUnit = '';
9090

91+
/**
92+
* By default, the value of a Slider Handle will be halfway between its
93+
* `min` and `max` values, or the `min` value when `max` is less than `min`.
94+
*/
9195
@property({ type: Number })
92-
value = 10;
96+
value!: number;
9397

9498
@property({ type: Boolean, reflect: true })
9599
public dragging = false;
@@ -134,6 +138,17 @@ export class SliderHandle extends Focusable {
134138
super.update(changes);
135139
}
136140

141+
protected firstUpdated(changedProperties: PropertyValues<this>): void {
142+
super.firstUpdated(changedProperties);
143+
this.dispatchEvent(new CustomEvent('sp-slider-handle-ready'));
144+
const { max, min } = this as { max: number; min: number };
145+
if (this.value == null) {
146+
if (!isNaN(max) && !isNaN(min)) {
147+
this.value = max < min ? min : min + (max - min) / 2;
148+
}
149+
}
150+
}
151+
137152
protected updated(changedProperties: PropertyValues<this>): void {
138153
if (changedProperties.has('value')) {
139154
const oldValue = changedProperties.get('value');
@@ -146,11 +161,6 @@ export class SliderHandle extends Focusable {
146161
super.updated(changedProperties);
147162
}
148163

149-
protected firstUpdated(changedProperties: PropertyValues<this>): void {
150-
super.firstUpdated(changedProperties);
151-
this.dispatchEvent(new CustomEvent('sp-slider-handle-ready'));
152-
}
153-
154164
@property({ attribute: false })
155165
public normalization: SliderNormalization = defaultNormalization;
156166

packages/slider/test/slider.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe('Slider', () => {
9999

100100
await elementUpdated(el);
101101

102-
expect(el.value).to.equal(10);
102+
expect(el.value).to.equal(46);
103103
expect(el.highlight).to.be.false;
104104

105105
el.focus();
@@ -108,14 +108,14 @@ describe('Slider', () => {
108108
});
109109
await elementUpdated(el);
110110

111-
expect(el.value).to.equal(9);
111+
expect(el.value).to.equal(45);
112112
expect(el.highlight).to.be.true;
113113
await sendKeys({
114114
press: 'ArrowUp',
115115
});
116116
await elementUpdated(el);
117117

118-
expect(el.value).to.equal(10);
118+
expect(el.value).to.equal(46);
119119
expect(el.highlight).to.be.true;
120120
});
121121
it('accepts pointer events', async () => {
@@ -214,7 +214,7 @@ describe('Slider', () => {
214214

215215
await elementUpdated(el);
216216

217-
expect(el.value).to.equal(10);
217+
expect(el.value).to.equal(35);
218218

219219
const controls = el.shadowRoot.querySelector(
220220
'#controls'
@@ -236,7 +236,7 @@ describe('Slider', () => {
236236
await elementUpdated(el);
237237

238238
expect(pointerId).to.equal(-1);
239-
expect(el.value).to.equal(10);
239+
expect(el.value).to.equal(35);
240240
expect(el.dragging, 'handle is not yet being dragged').to.be.false;
241241

242242
controls.dispatchEvent(
@@ -298,7 +298,7 @@ describe('Slider', () => {
298298

299299
expect(el.dragging).to.be.false;
300300
expect(pointerId).to.equal(-1);
301-
expect(el.value).to.equal(10);
301+
expect(el.value).to.equal(50);
302302

303303
const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
304304
handle.setPointerCapture = (id: number) => (pointerId = id);
@@ -330,7 +330,7 @@ describe('Slider', () => {
330330
await elementUpdated(el);
331331

332332
expect(pointerId).to.equal(-1);
333-
expect(el.value).to.equal(10);
333+
expect(el.value).to.equal(50);
334334
});
335335
it('accepts pointermove events', async () => {
336336
const el = await fixture<Slider>(
@@ -340,7 +340,7 @@ describe('Slider', () => {
340340
);
341341
await elementUpdated(el);
342342

343-
expect(el.value).to.equal(10);
343+
expect(el.value).to.equal(50);
344344

345345
const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
346346
await sendMouse({
@@ -380,7 +380,7 @@ describe('Slider', () => {
380380
);
381381
await elementUpdated(el);
382382

383-
expect(el.value, 'initial').to.equal(10);
383+
expect(el.value, 'initial').to.equal(50);
384384

385385
const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
386386
el.track.setPointerCapture = (id: number) => (pointerId = id);
@@ -561,7 +561,7 @@ describe('Slider', () => {
561561

562562
await elementUpdated(el);
563563

564-
expect(el.value).to.equal(10);
564+
expect(el.value).to.equal(50);
565565
expect(el.dragging).to.be.false;
566566

567567
const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
@@ -574,7 +574,7 @@ describe('Slider', () => {
574574
);
575575
await nextFrame();
576576

577-
expect(el.value).to.equal(10);
577+
expect(el.value).to.equal(50);
578578
});
579579
it('responds to input events on the <input/> element', async () => {
580580
const el = await fixture<Slider>(
@@ -585,7 +585,7 @@ describe('Slider', () => {
585585

586586
await elementUpdated(el);
587587

588-
expect(el.value).to.equal(10);
588+
expect(el.value).to.equal(50);
589589

590590
const input = el.shadowRoot.querySelector('.input') as HTMLInputElement;
591591

@@ -815,7 +815,7 @@ describe('Slider', () => {
815815

816816
await elementUpdated(el);
817817

818-
expect(el.value).to.equal(10);
818+
expect(el.value).to.equal(50);
819819

820820
el.min = 0;
821821
el.max = 200;

0 commit comments

Comments
 (0)