Skip to content

Commit bef030f

Browse files
JasonElkiniOvergaard
authored andcommitted
feat(uui-slider): Hide label property + fix error for floating point numbers (#813)
* Add ability to hide the value label * Match decimal places displayed to decimal places in step * Fix lag * Fix thumb inner positioning on high DPI displays
1 parent ce437fa commit bef030f

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/uui-slider/lib/uui-slider.element.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
6161
*/
6262
static readonly formAssociated = true;
6363

64+
#stepDecimalPlaces = 0;
65+
6466
/**
6567
* Hides the numbers representing the value of each steps. Dots will still be visible
6668
* @type {boolean}
@@ -70,6 +72,15 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
7072
@property({ type: Boolean, attribute: 'hide-step-values' })
7173
hideStepValues = false;
7274

75+
/**
76+
* Hides the value label on the thumb.
77+
* @type {boolean}
78+
* @attr 'hide-value-label'
79+
* @default false
80+
*/
81+
@property({ type: Boolean, attribute: 'hide-value-label' })
82+
hideValueLabel = false;
83+
7384
/**
7485
* This is a minimum value of the input.
7586
* @type {number}
@@ -95,7 +106,16 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
95106
* @default 1
96107
*/
97108
@property({ type: Number })
98-
step = 1;
109+
public get step() {
110+
return this.#step;
111+
}
112+
113+
public set step(value) {
114+
this.#step = value;
115+
this.#stepDecimalPlaces = (value.toString().split('.')[1] || []).length;
116+
}
117+
118+
#step = 1;
99119

100120
/**
101121
* This is a value property of the uui-slider.
@@ -117,11 +137,13 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
117137

118138
let correctedValue = newVal ? parseFloat(newVal as string) : 0;
119139
correctedValue = Math.min(Math.max(correctedValue, this.min), this.max);
140+
120141
if (this.step > 0) {
121142
correctedValue = Math.round(correctedValue / this.step) * this.step;
122143
}
123144

124-
super.value = correctedValue.toString();
145+
super.value = correctedValue.toFixed(this.#stepDecimalPlaces).toString();
146+
125147
this._calculateSliderPosition();
126148
this.requestUpdate('value', oldVal);
127149
}
@@ -285,7 +307,9 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
285307
286308
<div id="track-inner" aria-hidden="true">
287309
<div id="thumb" style=${styleMap({ left: this._sliderPosition })}>
288-
<div id="thumb-label">${this.value}</div>
310+
${this.hideValueLabel
311+
? null
312+
: html`<div id="thumb-label">${this.value}</div>`}
289313
</div>
290314
</div>
291315
</div>
@@ -350,6 +374,9 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
350374
351375
#thumb {
352376
position: absolute;
377+
display: flex;
378+
align-items: center;
379+
justify-content: center;
353380
top: 2px;
354381
bottom: 0;
355382
left: 0;
@@ -362,8 +389,6 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
362389
363390
background-color: var(--uui-color-surface);
364391
border: 2px solid var(--uui-color-selected);
365-
366-
transition: 120ms left ease;
367392
}
368393
:host([disabled]) #thumb {
369394
background-color: var(--uui-color-disabled);
@@ -372,9 +397,6 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
372397
373398
#thumb:after {
374399
content: '';
375-
position: absolute;
376-
top: 2px;
377-
left: 2px;
378400
height: 9px;
379401
width: 9px;
380402
border-radius: 50%;

packages/uui-slider/lib/uui-slider.story.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default {
1414
step: 1,
1515
label: 'Slider label',
1616
hideStepValues: false,
17+
hideValueLabel: false,
1718
value: 0,
1819
disabled: false,
1920
},
@@ -38,7 +39,8 @@ const Template: Story = props => html`
3839
max=${props.max}
3940
?disabled=${props.disabled}
4041
.value=${props.value}
41-
.hideStepValues=${props.hideStepValues}></uui-slider>
42+
.hideStepValues=${props.hideStepValues}
43+
.hideValueLabel=${props.hideValueLabel}></uui-slider>
4244
`;
4345

4446
export const AAAOverview = Template.bind({});

0 commit comments

Comments
 (0)