Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- [#4118](https://github.com/plotly/dash/pull/4118) The JupyterLab extension now ships prebuilt, so JupyterLab 3 and 4 load it without asking for a rebuild. JupyterLab 2 is no longer supported. Fixes [#3828](https://github.com/plotly/dash/issues/3828).

### Fixed
- [#4209](https://github.com/plotly/dash/pull/4209) Fix `dcc.Slider` and `dcc.RangeSlider` tooltips being drawn underneath a slider placed right after them. The tooltip element is now a sibling of the thumb, not a child of it. Fixes [#3632](https://github.com/plotly/dash/issues/3632).
- [#3957](https://github.com/plotly/dash/pull/3957) Fix `running=` with a pattern-matching `ALL`/`ALLSMALLER` id crashing the renderer when none of the matching components are on the current page. Fixes [#3297](https://github.com/plotly/dash/issues/3297).
- [#4144](https://github.com/plotly/dash/pull/4144) Fix `Patch.update` changing the caller's dictionary, so reusing one dictionary across `update` calls with different keywords no longer changes earlier queued merges. Fixes [#4143](https://github.com/plotly/dash/issues/4143).
- [#4197](https://github.com/plotly/dash/pull/4197) Fix `dcc.Input` with `type="number"` and `debounce` sometimes putting back an old value over what the user typed after using the stepper buttons.
Expand Down
57 changes: 48 additions & 9 deletions components/dash-core-components/src/components/css/sliders.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.dash-slider-root {
position: relative;
z-index: 0;
display: flex;
align-items: center;
user-select: none;
Expand Down Expand Up @@ -57,8 +56,8 @@
position: relative;
z-index: 1;
display: block;
width: 16px;
height: 16px;
width: var(--Dash-Slider-Thumb-Size, 16px);
height: var(--Dash-Slider-Thumb-Size, 16px);
background-color: var(--Dash-Fill-Interactive-Strong);
border: 2px solid var(--Dash-Fill-Inverse-Strong);
border-radius: 50%;
Expand All @@ -78,11 +77,6 @@
outline: 2px solid var(--Dash-Fill-Interactive-Strong);
}

.dash-slider-thumb:focus .dash-slider-tooltip,
.dash-slider-thumb:hover .dash-slider-tooltip {
display: block;
}

.dash-slider-thumb[data-disabled],
.dash-range-slider-input:disabled {
cursor: not-allowed;
Expand Down Expand Up @@ -136,6 +130,30 @@
background-color: var(--Dash-Fill-Disabled);
}

/*
* Stands in for the thumb: same box and same position, so the tooltip is laid
* out against it. The z-index only reaches past the slider while the root is
* not a stacking context.
*/
.dash-slider-tooltip-anchor {
/* Radix's in-bounds offset for the thumb, from half its border box */
--dash-slider-thumb-inset: calc(
var(--dash-slider-thumb-percent) * 1% +
(var(--Dash-Slider-Thumb-Size, 16px) / 2 + 2px) *
(
var(--dash-slider-thumb-direction) -
var(--dash-slider-thumb-percent) / 50
)
);
position: absolute;
z-index: 1;
width: var(--Dash-Slider-Thumb-Size, 16px);
height: var(--Dash-Slider-Thumb-Size, 16px);
border: 2px solid transparent;
transform: var(--radix-slider-thumb-transform);
pointer-events: none;
}

.dash-slider-tooltip {
display: none;
position: absolute;
Expand All @@ -146,14 +164,35 @@
box-shadow: 0 0 8px var(--Dash-Shading-Strong);
background-color: var(--Dash-Fill-Inverse-Strong);
user-select: none;
z-index: 1000;
fill: var(--Dash-Fill-Inverse-Strong);
}

.dash-slider-tooltip.always-visible {
display: block;
}

/*
* The anchor comes right after the span Radix renders around its thumb. In a
* form, Radix puts a hidden input between the two.
*/
.dash-slider-root > span:hover + .dash-slider-tooltip-anchor,
.dash-slider-root > span:hover + input + .dash-slider-tooltip-anchor,
.dash-slider-root > span:focus-within + .dash-slider-tooltip-anchor,
.dash-slider-root > span:focus-within + input + .dash-slider-tooltip-anchor {
z-index: 2;
}

.dash-slider-root > span:hover + .dash-slider-tooltip-anchor > *,
.dash-slider-root > span:hover + input + .dash-slider-tooltip-anchor > *,
.dash-slider-root > span:focus-within + .dash-slider-tooltip-anchor > *,
.dash-slider-root
> span:focus-within
+ input
+ .dash-slider-tooltip-anchor
> * {
display: block;
}

/* Include property to mimic rc-slider behavior */
.dash-slider-root:not([data-included='false']) .dash-slider-range {
background-color: var(--Dash-Fill-Interactive-Strong);
Expand Down
25 changes: 19 additions & 6 deletions components/dash-core-components/src/fragments/RangeSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
setUndefined,
} from '../utils/computeSliderMarkers';
import {snapToNearestMark} from '../utils/sliderSnapToMark';
import {renderSliderMarks, renderSliderDots} from '../utils/sliderRendering';
import {
renderSliderMarks,
renderSliderDots,
getThumbFollowerStyle,
} from '../utils/sliderRendering';
import LoadingElement from '../utils/_LoadingElement';
import {Tooltip} from '../utils/sliderTooltip';
import {RangeSliderProps} from '../types';
Expand Down Expand Up @@ -476,20 +480,29 @@ export default function RangeSlider(props: RangeSliderProps) {
index + 1
}`;

// The tooltip is a sibling of the thumb, not a child. Radix
// wraps the thumb in a transformed span, and nothing inside
// that span can be drawn above another slider's thumb.
return (
<RadixSlider.Thumb
key={'thumb' + index}
className={thumbClassName}
>
<React.Fragment key={'thumb' + index}>
<RadixSlider.Thumb
className={thumbClassName}
/>
{tooltip && (
<Tooltip
id={id}
index={index}
value={val}
tooltip={tooltip}
anchorStyle={getThumbFollowerStyle(
val,
minMaxValues,
!!vertical,
!!reverse
)}
/>
)}
</RadixSlider.Thumb>
</React.Fragment>
);
})}
</RadixSlider.Root>
Expand Down
23 changes: 23 additions & 0 deletions components/dash-core-components/src/utils/sliderRendering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ const getRadixThumbPosition = (
return {percentage, offset: thumbInBoundsOffset};
};

// Custom properties and the start edge for an element that has to follow a
// thumb without being inside it. sliders.css turns them into the same inset
// Radix gives the span around the thumb.
export const getThumbFollowerStyle = (
value: number,
minMaxValues: {min_mark: number; max_mark: number},
vertical: boolean,
reverse: boolean
) => {
const [startEdge, endEdge] = vertical
? ['bottom', 'top']
: ['left', 'right'];
return {
'--dash-slider-thumb-percent': convertValueToPercentage(
value,
minMaxValues.min_mark,
minMaxValues.max_mark
),
'--dash-slider-thumb-direction': reverse ? -1 : 1,
[reverse ? endEdge : startEdge]: 'var(--dash-slider-thumb-inset)',
} as React.CSSProperties;
};

export const renderSliderMarks = (
renderedMarks: SliderMarks,
vertical: boolean,
Expand Down
19 changes: 16 additions & 3 deletions components/dash-core-components/src/utils/sliderTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ type TooltipProps = {
index: number;
value: number;
tooltip: SliderTooltip;
anchorStyle: React.CSSProperties;
};

export const Tooltip = ({tooltip, value, index: key, id}: TooltipProps) => {
export const Tooltip = ({
tooltip,
value,
index: key,
id,
anchorStyle,
}: TooltipProps) => {
const formatTooltipContent = (value: number, index: number) => {
let displayValue: string | number = value;
if (tooltip?.transform) {
Expand Down Expand Up @@ -58,8 +65,14 @@ export const Tooltip = ({tooltip, value, index: key, id}: TooltipProps) => {
}

return (
<div className={classNames.join(' ')} style={position}>
{formatTooltipContent(value, key)}
<div
className="dash-slider-tooltip-anchor"
style={anchorStyle}
aria-hidden="true"
>
<div className={classNames.join(' ')} style={position}>
{formatTooltipContent(value, key)}
</div>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from multiprocessing import Lock
from dash import Dash, Input, Output, dcc, html
from dash.testing.wait import until
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


def test_slsl001_always_visible_slider(dash_dcc):
Expand Down Expand Up @@ -761,3 +764,170 @@ def test_slsl020_empty_mark_labels_keep_tick_alignment(dash_dcc):
assert labeled_mark.size["height"] == 12
assert empty_mark.size["height"] == labeled_mark.size["height"]
assert dash_dcc.get_logs() == []


def test_slsl021_tooltip_above_next_slider(dash_dcc):
def tooltip(always_visible):
return {"placement": "bottom", "always_visible": always_visible}

app = Dash(__name__)
app.layout = html.Div(
[
dcc.Slider(0, 100, 1, value=10, id="first", tooltip=tooltip(True)),
# same value as the slider above, so its thumb is under that tooltip
dcc.Slider(0, 100, 1, value=10, id="under-first"),
dcc.RangeSlider(
0, 100, 1, value=[50, 70], id="range", tooltip=tooltip(True)
),
dcc.Slider(0, 100, 1, value=30, id="hover", tooltip=tooltip(False)),
dcc.Slider(0, 100, 1, value=30, id="under-hover"),
# in a form, Radix adds a hidden input next to each thumb
html.Form(
dcc.Slider(0, 100, 1, value=80, id="in-form", tooltip=tooltip(False))
),
]
)

dash_dcc.start_server(app)
dash_dcc.wait_for_text_to_equal("#first-tooltip-1-content", "10")

# Tooltips ignore the pointer, which also hides them from elementFromPoint
dash_dcc.driver.execute_script(
"""
const style = document.createElement('style');
style.textContent = '.dash-slider-tooltip { pointer-events: auto; }';
document.head.appendChild(style);
"""
)

def covered_points(content_id):
# Sample a grid of points inside the tooltip and count the ones where
# another element is painted on top of it.
return dash_dcc.driver.execute_script(
"""
const tip = document.getElementById(arguments[0]).parentElement;
const rect = tip.getBoundingClientRect();
let covered = 0;
for (let fx = 0.1; fx < 1; fx += 0.2) {
for (let fy = 0.1; fy < 1; fy += 0.2) {
const el = document.elementFromPoint(
rect.left + rect.width * fx,
rect.top + rect.height * fy
);
if (!tip.contains(el)) {
covered += 1;
}
}
}
return covered;
""",
content_id,
)

assert covered_points("first-tooltip-1-content") == 0
assert covered_points("range-tooltip-1-content") == 0
assert covered_points("range-tooltip-2-content") == 0

hover_tooltip = dash_dcc.find_element("#hover .dash-slider-tooltip")
assert not hover_tooltip.is_displayed()

thumb = dash_dcc.find_element("#hover .dash-slider-thumb")
ActionChains(dash_dcc.driver).move_to_element(thumb).perform()
until(hover_tooltip.is_displayed, 3)
assert covered_points("hover-tooltip-1-content") == 0

thumb.send_keys(Keys.ARROW_RIGHT)
ActionChains(dash_dcc.driver).move_to_element(
dash_dcc.find_element("#first")
).perform()
dash_dcc.wait_for_text_to_equal("#hover-tooltip-1-content", "31")
assert hover_tooltip.is_displayed()
assert covered_points("hover-tooltip-1-content") == 0

form_tooltip = dash_dcc.find_element("#in-form .dash-slider-tooltip")
assert not form_tooltip.is_displayed()
ActionChains(dash_dcc.driver).move_to_element(
dash_dcc.find_element("#in-form .dash-slider-thumb")
).perform()
until(form_tooltip.is_displayed, 3)

assert dash_dcc.get_logs() == []


def test_slsl022_tooltip_follows_thumb(dash_dcc):
tooltip = {"always_visible": True}
app = Dash(__name__)
app.layout = html.Div(
[
dcc.Slider(0, 100, 1, value=10, id="plain", tooltip=tooltip),
dcc.Slider(
0, 100, 1, value=10, id="reverse", reverse=True, tooltip=tooltip
),
dcc.RangeSlider(0, 100, 1, value=[0, 100], id="range", tooltip=tooltip),
html.Div(
[
dcc.Slider(
0,
100,
1,
value=20,
id="vertical",
vertical=True,
tooltip=tooltip,
),
dcc.Slider(
0,
100,
1,
value=20,
id="vertical-reverse",
vertical=True,
reverse=True,
tooltip=tooltip,
),
],
style={"display": "flex", "height": "300px"},
),
html.Div(
[
dcc.RangeSlider(
0, 100, 1, value=[0, 100], id="big", tooltip=tooltip
),
dcc.Slider(
0,
100,
1,
value=10,
id="big-reverse",
reverse=True,
tooltip=tooltip,
),
],
style={"--Dash-Slider-Thumb-Size": "24px"},
),
],
style={"padding": "60px"},
)

dash_dcc.start_server(app)
dash_dcc.wait_for_text_to_equal("#big-reverse-tooltip-1-content", "10")
assert dash_dcc.find_element("#big .dash-slider-thumb").size["width"] == 28

def rects(selector):
return dash_dcc.driver.execute_script(
"""
return [...document.querySelectorAll(arguments[0])].map(el => {
const {x, y, width, height} = el.getBoundingClientRect();
return [x, y, width, height];
});
""",
selector,
)

# Radix sets the thumb offset one render late, once it has measured the thumb
until(
lambda: rects(".dash-slider-thumb") == rects(".dash-slider-tooltip-anchor"), 3
)
assert len(rects(".dash-slider-thumb")) == 9

assert dash_dcc.get_logs() == []