Skip to content

Truly independent web browser

LibWeb: Give range inputs a thin rounded track and a round thumb

Andreas Klingb7da7bb45fc7
diff --git a/Libraries/LibWeb/CSS/Default.css b/Libraries/LibWeb/CSS/Default.css
index a46b21ba8a6..387785b6590 100644
--- a/Libraries/LibWeb/CSS/Default.css
+++ b/Libraries/LibWeb/CSS/Default.css
@@ -102,35 +102,40 @@ option {
/* Custom <input type="range"> styles */
input[type=range] {
display: inline-block;
+ width: 129px;
height: 16px;
+ margin: 2px;
&::slider-track {
display: block;
position: relative;
- height: 6px;
+ height: 4px;
width: 100%;
- box-sizing: border-box;
- background-color: AccentColorText;
- border: 1px solid rgba(0, 0, 0, 0.5);
+ background-color: color-mix(in srgb, CanvasText 20%, transparent);
}
&::slider-fill {
display: block;
position: absolute;
height: 100%;
+ border-radius: inherit;
}
+ /* NB: The thumb's position assumes this width, see HTMLInputElement::update_slider_shadow_tree_elements(). */
&::slider-thumb {
display: block;
+ position: relative;
+ z-index: 1;
margin-top: -6px;
width: 16px;
height: 16px;
- transform: translateX(-50%);
border-radius: 50%;
- outline: 1px solid rgba(0, 0, 0, 0.5);
- z-index: 1;
background-color: AccentColor;
}
+
+ &:disabled::slider-thumb {
+ background-color: GrayText;
+ }
}
/* Custom <meter> styles */
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index 0b298fbb90b..c3f5a8b29a3 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -1602,14 +1602,32 @@ void HTMLInputElement::update_slider_shadow_tree_elements()
double maximum = *max();
double position = (value - minimum) / (maximum - minimum) * 100;
+ // NB: The thumb travels between the ends of the track without overhanging them, and the fill reaches its center.
+ // The fill assumes the 16px thumb width from the UA style sheet.
+ static constexpr double thumb_width = 16;
+ auto thumb_offset = thumb_width * position / 100;
+
+ // NB: Only the native slider rounds its track and shows how far the value is along it; a slider with
+ // `appearance: none` leaves both for the author to style.
+ auto style = computed_style();
+ bool const is_native = !style || style->appearance() != CSS::Appearance::None;
+
+ if (m_slider_runnable_track)
+ MUST(m_slider_runnable_track->style()->set_property(CSS::PropertyID::BorderRadius, is_native ? u"2px"sv : u"0"sv));
+
if (m_slider_progress_element) {
- auto width = Utf16String::formatted("{}%", position);
+ auto width = Utf16String::formatted("calc({}% + {}px)", position, thumb_width / 2 - thumb_offset);
MUST(m_slider_progress_element->style()->set_property(CSS::PropertyID::Width, width.utf16_view()));
+ auto fill_color = is_native ? (enabled() ? u"AccentColor"sv : u"GrayText"sv) : u"transparent"sv;
+ MUST(m_slider_progress_element->style()->set_property(CSS::PropertyID::BackgroundColor, fill_color));
}
if (m_slider_thumb) {
+ // NB: The thumb moves back by the same fraction of its own width, whatever width an author gives it.
auto margin_left = Utf16String::formatted("{}%", position);
MUST(m_slider_thumb->style()->set_property(CSS::PropertyID::MarginLeft, margin_left.utf16_view()));
+ auto translate = Utf16String::formatted("{}%", -position);
+ MUST(m_slider_thumb->style()->set_property(CSS::PropertyID::Translate, translate.utf16_view()));
}
}
diff --git a/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt b/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt
index d4702a0f813..c66ee3eec35 100644
--- a/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt
+++ b/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt
@@ -6,14 +6,14 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from TextNode start: 0, length: 5, rect: [8,8 53.3125x20] baseline: 16
"Label"
TextNode <#text> (not painted)
- RangeInputBox <input> at [61.3125,8] [0+0+0 346.6875 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
- Box <div> at [61.3125,8] flex-container(row) [0+0+0 346.6875 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [62.3125,14] positioned flex-item [0+1+0 344.6875 0+1+0] [0+1+0 4 0+1+0] [BFC] children: not-inline
- BlockContainer <div> at [62.3125,14] positioned [0+0+0 172.34375 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [234.65625,8] [172.34375+0+0 16 0+0+156.34375] [-6+0+0 16 0+0+0] children: not-inline
+ RangeInputBox <input> at [63.3125,10] [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline
+ Box <div> at [63.3125,10] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [63.3125,16] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [63.3125,16] positioned [0+0+0 64.5 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [127.8125,10] positioned [64.5+0+0 16 0+0+48.5] [-6+0+0 16 0+0+0] children: not-inline
BlockContainer <(anonymous)> at [8,28] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
SC for BlockContainer<HTML> [0,0 800x36] (z-index: auto)
- SC for BlockContainer<DIV> [234.65625,8 16x16] (z-index: auto), has_transform
+ SC for BlockContainer<DIV> [127.8125,10 16x16] (z-index: 1), has_transform
diff --git a/Tests/LibWeb/Layout/expected/input-range.txt b/Tests/LibWeb/Layout/expected/input-range.txt
index 4f5695a2f64..0951610d59c 100644
--- a/Tests/LibWeb/Layout/expected/input-range.txt
+++ b/Tests/LibWeb/Layout/expected/input-range.txt
@@ -1,21 +1,21 @@
Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
- BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 36 0+0+0] [BFC] children: not-inline
- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 20 0+0+8] children: inline
- frag 0 from RangeInputBox start: 0, length: 0, rect: [8,8 200x16] baseline: 16
- frag 1 from RangeInputBox start: 0, length: 0, rect: [208,8 200x16] baseline: 16
- RangeInputBox <input> at [8,8] inline-block [0+0+0 200 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
- Box <div> at [8,8] flex-container(row) [0+0+0 200 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [9,14] positioned flex-item [0+1+0 198 0+1+0] [0+1+0 4 0+1+0] [BFC] children: not-inline
- BlockContainer <div> at [9,14] positioned [0+0+0 99 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [108,8] [99+0+0 16 0+0+83] [-6+0+0 16 0+0+0] children: not-inline
- RangeInputBox <input> at [208,8] inline-block [0+0+0 200 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
- Box <div> at [208,8] flex-container(row) [0+0+0 200 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [209,14] positioned flex-item [0+1+0 198 0+1+0] [0+1+0 4 0+1+0] [BFC] children: not-inline
- BlockContainer <div> at [209,14] positioned [0+0+0 19.796875 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [228.796875,8] [19.796875+0+0 16 0+0+162.203125] [-6+0+0 16 0+0+0] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 38 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 22 0+0+8] children: inline
+ frag 0 from RangeInputBox start: 0, length: 0, rect: [10,10 200x16] baseline: 18
+ frag 1 from RangeInputBox start: 0, length: 0, rect: [214,10 200x16] baseline: 18
+ RangeInputBox <input> at [10,10] inline-block [2+0+0 200 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline
+ Box <div> at [10,10] flex-container(row) [0+0+0 200 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [10,16] positioned flex-item [0+0+0 200 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [10,16] positioned [0+0+0 100 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [110,10] positioned [100+0+0 16 0+0+84] [-6+0+0 16 0+0+0] children: not-inline
+ RangeInputBox <input> at [214,10] inline-block [2+0+0 200 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline
+ Box <div> at [214,10] flex-container(row) [0+0+0 200 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [214,16] positioned flex-item [0+0+0 200 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [214,16] positioned [0+0+0 26.390625 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [234,10] positioned [20+0+0 16 0+0+164] [-6+0+0 16 0+0+0] children: not-inline
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
- SC for BlockContainer<HTML> [0,0 800x36] (z-index: auto)
- SC for BlockContainer<DIV> [108,8 16x16] (z-index: auto), has_transform
- SC for BlockContainer<DIV> [228.796875,8 16x16] (z-index: auto), has_transform
+ SC for BlockContainer<HTML> [0,0 800x38] (z-index: auto)
+ SC for BlockContainer<DIV> [110,10 16x16] (z-index: 1), has_transform
+ SC for BlockContainer<DIV> [234,10 16x16] (z-index: 1), has_transform