diff --git a/CHANGELOG.md b/CHANGELOG.md index 69410fa998..bb8b2dfa05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/components/dash-core-components/src/components/css/sliders.css b/components/dash-core-components/src/components/css/sliders.css index cb6d4e41cd..8255c2c9a7 100644 --- a/components/dash-core-components/src/components/css/sliders.css +++ b/components/dash-core-components/src/components/css/sliders.css @@ -1,6 +1,5 @@ .dash-slider-root { position: relative; - z-index: 0; display: flex; align-items: center; user-select: none; @@ -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; @@ -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; @@ -146,7 +155,6 @@ 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); } @@ -154,6 +162,28 @@ 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); diff --git a/components/dash-core-components/src/fragments/RangeSlider.tsx b/components/dash-core-components/src/fragments/RangeSlider.tsx index 2abf67b977..551e93a0dc 100644 --- a/components/dash-core-components/src/fragments/RangeSlider.tsx +++ b/components/dash-core-components/src/fragments/RangeSlider.tsx @@ -8,7 +8,11 @@ import { 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'; @@ -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 ( - + + {tooltip && ( )} - + ); })} diff --git a/components/dash-core-components/src/utils/sliderRendering.tsx b/components/dash-core-components/src/utils/sliderRendering.tsx index 0e8ae37ab9..32817a3afa 100644 --- a/components/dash-core-components/src/utils/sliderRendering.tsx +++ b/components/dash-core-components/src/utils/sliderRendering.tsx @@ -54,6 +54,37 @@ const getRadixThumbPosition = ( return {percentage, offset: thumbInBoundsOffset}; }; +// Same box Radix measures on the thumb: 16px plus a 2px border on each side +const THUMB_OUTER_SIZE = 20; + +// The inset Radix gives the span around a thumb, for content that has to +// follow a thumb without being inside it +export const getThumbInset = ( + 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' + : reverse + ? 'right' + : 'left'; + return {[startEdge]: `calc(${percentage}% + ${offset}px)`}; +}; + export const renderSliderMarks = ( renderedMarks: SliderMarks, vertical: boolean, diff --git a/components/dash-core-components/src/utils/sliderTooltip.tsx b/components/dash-core-components/src/utils/sliderTooltip.tsx index fb9de5cd41..6af95dc76c 100644 --- a/components/dash-core-components/src/utils/sliderTooltip.tsx +++ b/components/dash-core-components/src/utils/sliderTooltip.tsx @@ -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) { @@ -58,8 +65,14 @@ export const Tooltip = ({tooltip, value, index: key, id}: TooltipProps) => { } return ( -
- {formatTooltipContent(value, key)} + ); }; diff --git a/components/dash-core-components/tests/integration/sliders/test_sliders.py b/components/dash-core-components/tests/integration/sliders/test_sliders.py index 1364a6ef01..6029b876b1 100644 --- a/components/dash-core-components/tests/integration/sliders/test_sliders.py +++ b/components/dash-core-components/tests/integration/sliders/test_sliders.py @@ -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): @@ -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() == []