Skip to content

Commit 9c10f5d

Browse files
authored
Tooltip enhancements (#861)
* Add fade prop to Tooltip * Allow tooltips to be controlled by callbacks * Add example to tooltip docs
1 parent 371ad15 commit 9c10f5d

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

docs/components_page/components/tooltip.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ Use the `placement` argument to control the placement of the tooltip. The basic
1717

1818
{{example:components/tooltip/placement.py:tooltips}}
1919

20+
## Controlling the Tooltip with callbacks
21+
22+
The tooltip's visibility can be controlled with callbacks much like a `Popover` component. Simply set the desired value of `is_open` in a callback. If you are manually controlling the value of `is_open`, you may wish to also set `trigger=None`. By default the `Tooltip` will show when the target element is hovered or focused.
23+
24+
{{example:components/tooltip/tooltip_callback.py:tooltip}}
25+
2026
{{apidoc:src/components/Tooltip.js}}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import dash_bootstrap_components as dbc
2+
from dash import Input, Output, State, html
3+
4+
tooltip = html.Div(
5+
[
6+
dbc.Button(
7+
"Toggle",
8+
id="toggle",
9+
color="success",
10+
className="me-4",
11+
n_clicks=0,
12+
),
13+
dbc.Button("Target", id="target", color="danger", n_clicks=0),
14+
dbc.Tooltip(
15+
"This is a tooltip",
16+
id="tooltip",
17+
is_open=False,
18+
target="target",
19+
trigger=None,
20+
),
21+
]
22+
)
23+
24+
25+
@app.callback(
26+
Output("tooltip", "is_open"),
27+
[Input("toggle", "n_clicks")],
28+
[State("tooltip", "is_open")],
29+
)
30+
def toggle_tooltip(n, is_open):
31+
if n:
32+
return not is_open
33+
return is_open

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ exclude =
5151
docs/components_page/components/toast/auto_dismiss.py,
5252
docs/components_page/components/toast/icon_dismiss.py,
5353
docs/components_page/components/toast/position.py,
54+
docs/components_page/components/tooltip/tooltip_callback.py,
5455
ignore = E203, W503
5556

5657
[isort]

src/components/tooltip/Tooltip.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ const Tooltip = props => {
1616
const {
1717
id,
1818
children,
19+
is_open,
1920
loading_state,
2021
className,
2122
class_name,
2223
style,
24+
fade,
2325
...otherProps
2426
} = props;
2527

@@ -28,8 +30,9 @@ const Tooltip = props => {
2830
data-dash-is-loading={
2931
(loading_state && loading_state.is_loading) || undefined
3032
}
31-
{...omit(['setProps'], otherProps)}
32-
trigger="hover focus"
33+
defaultShow={is_open}
34+
{...otherProps}
35+
transition={fade}
3336
>
3437
<TooltipTemplate
3538
id={id}
@@ -46,7 +49,9 @@ Tooltip.defaultProps = {
4649
delay: {show: 0, hide: 50},
4750
placement: 'auto',
4851
flip: true,
49-
autohide: true
52+
autohide: true,
53+
fade: true,
54+
trigger: 'hover focus'
5055
};
5156

5257
Tooltip.propTypes = {
@@ -128,6 +133,33 @@ Tooltip.propTypes = {
128133
*/
129134
autohide: PropTypes.bool,
130135

136+
/**
137+
* If True, a fade animation will be applied when `is_open` is toggled. If
138+
* False the Alert will simply appear and disappear.
139+
*/
140+
fade: PropTypes.bool,
141+
142+
/**
143+
* Space separated list of triggers (e.g. "click hover focus legacy"). These
144+
* specify ways in which the target component can toggle the tooltip. If
145+
* omitted you must toggle the tooltip yourself using callbacks. Options
146+
* are:
147+
* - "click": toggles the popover when the target is clicked.
148+
* - "hover": toggles the popover when the target is hovered over with the
149+
* cursor.
150+
* - "focus": toggles the popover when the target receives focus
151+
* - "legacy": toggles the popover when the target is clicked, but will also
152+
* dismiss the popover when the user clicks outside of the popover.
153+
*
154+
* Default is "hover focus"
155+
*/
156+
trigger: PropTypes.string,
157+
158+
/**
159+
* Whether the Tooltip is open or not.
160+
*/
161+
is_open: PropTypes.bool,
162+
131163
/**
132164
* Object that holds the loading state object coming from dash-renderer
133165
*/

0 commit comments

Comments
 (0)