Skip to content

Commit bd7ab02

Browse files
authored
Merge pull request #3448 from plotly/v4-styling
Slider and Range Slider styling updates
2 parents 6177cff + ff61bcd commit bd7ab02

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

components/dash-core-components/src/components/css/sliders.css

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
touch-action: none;
88
width: 100%;
99
height: 14px;
10-
padding: 8px 0 28px 0;
10+
padding: 18px 0 18px 0;
1111
box-sizing: border-box;
1212
/* Override Radix's default margin/padding behavior */
1313
--radix-slider-thumb-width: 16px;
1414
}
1515

16+
.dash-slider-root.has-marks {
17+
padding: 8px 0 28px 0;
18+
}
19+
1620
.dash-slider-root[data-orientation='vertical'].has-marks {
1721
padding-bottom: 0px;
1822
}
@@ -167,7 +171,7 @@
167171
.dash-slider-container {
168172
display: flex;
169173
align-items: center;
170-
gap: 12px;
174+
gap: 16px;
171175
width: 100%;
172176
}
173177

@@ -184,7 +188,7 @@
184188
}
185189

186190
.dash-range-slider-min-input {
187-
min-width: 64px;
191+
text-align: center;
188192
}
189193

190194
.dash-range-slider-max-input {
@@ -193,7 +197,7 @@
193197

194198
.dash-range-slider-input {
195199
width: 64px;
196-
margin-top: 8px;
200+
text-align: center;
197201
-webkit-appearance: textfield;
198202
-moz-appearance: textfield;
199203
appearance: textfield;

components/dash-core-components/src/fragments/RangeSlider.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,35 @@ export default function RangeSlider(props: RangeSliderProps) {
166166
});
167167
}, [min, max, processedMarks, step, sliderWidth]);
168168

169+
// Calculate dynamic input width based on digits needed and container size
170+
const inputWidth = useMemo(() => {
171+
if (!sliderWidth) {
172+
return '64px'; // fallback to current width
173+
}
174+
175+
// Count digits needed for min and max values
176+
const maxDigits = Math.max(
177+
String(Math.floor(Math.abs(minMaxValues.max_mark))).length,
178+
String(Math.floor(Math.abs(minMaxValues.min_mark))).length
179+
);
180+
181+
// Add 1 for minus sign if min is negative
182+
const totalChars = maxDigits + (minMaxValues.min_mark < 0 ? 1 : 0);
183+
184+
// Calculate width as percentage of container (5% min, 15% max)
185+
/* eslint-disable no-magic-numbers */
186+
const minWidth = sliderWidth * 0.05;
187+
const maxWidth = sliderWidth * 0.15;
188+
const charBasedWidth = totalChars * 12; // approx 12px per character
189+
/* eslint-enable no-magic-numbers */
190+
191+
const calculatedWidth = Math.max(
192+
minWidth,
193+
Math.min(maxWidth, charBasedWidth)
194+
);
195+
return `${calculatedWidth}px`;
196+
}, [sliderWidth, minMaxValues.min_mark, minMaxValues.max_mark]);
197+
169198
const handleValueChange = (newValue: number[]) => {
170199
let adjustedValue = newValue;
171200

@@ -205,6 +234,7 @@ export default function RangeSlider(props: RangeSliderProps) {
205234
<input
206235
type="number"
207236
className="dash-input-container dash-range-slider-input dash-range-slider-min-input"
237+
style={{width: inputWidth}}
208238
value={value[0] ?? ''}
209239
onChange={e => {
210240
const inputValue = e.target.value;
@@ -265,7 +295,8 @@ export default function RangeSlider(props: RangeSliderProps) {
265295
{showInputs && !vertical && (
266296
<input
267297
type="number"
268-
className="dash-input-container dash-range-slider-input dash-range-slider-max-input"
298+
className="dash-input-container dash-range-slider-input dash-range-slider-max-input"
299+
style={{width: inputWidth}}
269300
value={value[value.length - 1] ?? ''}
270301
onChange={e => {
271302
const inputValue = e.target.value;

components/dash-core-components/tests/integration/misc/test_persistence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def make_output(*args):
168168
["4️⃣", "6️⃣"],
169169
"yes maybe",
170170
"r",
171-
[5, 9],
171+
[5, 8],
172172
22,
173173
"C",
174174
"knock knock\nwho's there?",

components/dash-core-components/tests/integration/sliders/test_sliders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def update_output(value):
3030
dash_dcc.click_at_coord_fractions(slider, 0.5, 0.25)
3131
dash_dcc.wait_for_text_to_equal("#out", "You have selected 11")
3232
dash_dcc.click_at_coord_fractions(slider, 0.75, 0.25)
33-
dash_dcc.wait_for_text_to_equal("#out", "You have selected 17")
33+
dash_dcc.wait_for_text_to_equal("#out", "You have selected 16")
3434

3535
assert dash_dcc.get_logs() == []
3636

@@ -265,7 +265,7 @@ def update_output2(value):
265265

266266
dash_dcc.click_and_hold_at_coord_fractions(slider, 0.25, 0.25)
267267
dash_dcc.move_to_coord_fractions(slider, 0.75, 0.25)
268-
dash_dcc.wait_for_text_to_equal("#out-drag-value", "You have dragged 17")
268+
dash_dcc.wait_for_text_to_equal("#out-drag-value", "You have dragged 16")
269269
dash_dcc.move_to_coord_fractions(slider, 0.5, 0.25)
270270
dash_dcc.wait_for_text_to_equal("#out-drag-value", "You have dragged 11")
271271
dash_dcc.wait_for_text_to_equal("#out-value", "You have selected 5")

0 commit comments

Comments
 (0)