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 @@ -8,6 +8,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
44 changes: 37 additions & 7 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 @@ -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,21 @@
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 {
position: absolute;
z-index: 1;
width: 16px;
height: 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 +155,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 @@
setUndefined,
} from '../utils/computeSliderMarkers';
import {snapToNearestMark} from '../utils/sliderSnapToMark';
import {renderSliderMarks, renderSliderDots} from '../utils/sliderRendering';
import {
renderSliderMarks,
renderSliderDots,
getThumbInset,
} from '../utils/sliderRendering';
import LoadingElement from '../utils/_LoadingElement';
import {Tooltip} from '../utils/sliderTooltip';
import {RangeSliderProps} from '../types';
Expand Down Expand Up @@ -36,9 +40,9 @@
disabled,
dots,
included,
allowCross,

Check warning on line 43 in components/dash-core-components/src/fragments/RangeSlider.tsx

View workflow job for this annotation

GitHub Actions / DCC Lint Tests (Python 3.10)

'allowCross' is assigned a value but never used
pushable,
count,

Check warning on line 45 in components/dash-core-components/src/fragments/RangeSlider.tsx

View workflow job for this annotation

GitHub Actions / DCC Lint Tests (Python 3.10)

'count' is assigned a value but never used
reverse,
allow_direct_input = true,
} = props;
Expand Down Expand Up @@ -476,20 +480,29 @@
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={getThumbInset(
val,
minMaxValues,
!!vertical,
!!reverse
)}
/>
)}
</RadixSlider.Thumb>
</React.Fragment>
);
})}
</RadixSlider.Root>
Expand Down
31 changes: 31 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,37 @@
return {percentage, offset: thumbInBoundsOffset};
};

// Same box Radix measures on the thumb: 16px plus a 2px border on each side
const THUMB_OUTER_SIZE = 20;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Radix measures the real thumb and uses that size for the offset. This uses a fixed 20, and sliders.css:141 fixes the anchor box at 16px plus border too. If an app makes the thumb bigger (.dash-slider-thumb { width: 24px; height: 24px }), the tooltip is no longer centered on the thumb and drifts more near min and max. Before, the tooltip followed whatever the thumb was. Can you drive both the thumb and the anchor from one CSS variable? The offset can live in CSS as well: Radix's formula is halfSize * (1 - percent / 50), so JS only needs to set the percentage.


// The inset Radix gives the span around a thumb, for content that has to
// follow a thumb without being inside it
export const getThumbInset = (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats getRadixThumbPosition a few lines up, plus direction and size. Can you extend that one with size and direction params instead of adding a second copy?

value: number,
minMaxValues: {min_mark: number; max_mark: number},
vertical: boolean,
reverse: boolean
): React.CSSProperties => {
const percentage = convertValueToPercentage(
value,
minMaxValues.min_mark,
minMaxValues.max_mark
);
const offset = getThumbInBoundsOffset(
THUMB_OUTER_SIZE,
percentage,
reverse ? -1 : 1
);
const startEdge = vertical
? reverse
? 'top'
: 'bottom'

Check warning on line 81 in components/dash-core-components/src/utils/sliderRendering.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIchLB0XxXbpNwo2ykq&open=AaIchLB0XxXbpNwo2ykq&pullRequest=4209
: reverse
? 'right'
: 'left';

Check warning on line 84 in components/dash-core-components/src/utils/sliderRendering.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIchLB0XxXbpNwo2ykr&open=AaIchLB0XxXbpNwo2ykr&pullRequest=4209
return {[startEdge]: `calc(${percentage}% + ${offset}px)`};
};

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,152 @@ 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"},
),
],
style={"padding": "60px"},
)

dash_dcc.start_server(app)
dash_dcc.wait_for_text_to_equal("#vertical-reverse-tooltip-1-content", "20")

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")) == 6

assert dash_dcc.get_logs() == []
Loading