User Interaction
To specify when the Tooltip should be shown and hidden, set the showEvent and hideEvent properties. These properties can accept several events at once as well as an object.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Tooltip } from 'devextreme-react/tooltip';
- const renderContent = () => {
- return (
- <p>Tooltip content</p>
- );
- }
- class App extends React.Component {
- render() {
- return (
- <div>
- <img id="image" src="https://url/to/an/image" />
- <Tooltip
- target="#image"
- showEvent="dxhoverstart"
- hideEvent="dxhoverend"
- contentRender={renderContent}
- />
- </div>
- );
- }
- }
- export default App;
The Tooltip can also be hidden when a user clicks outside it. To control this behavior of the Tooltip, use the closeOnOutsideClick property.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Tooltip } from 'devextreme-react/tooltip';
- const renderContent = () => {
- return (
- <p>Tooltip content</p>
- );
- }
- class App extends React.Component {
- render() {
- return (
- <div>
- <img id="image" src="https://url/to/an/image" />
- <Tooltip
- target="#image"
- showEvent="dxhoverstart"
- hideEvent="dxhoverend"
- contentRender={renderContent}
- closeOnOutsideClick={false}
- />
- </div>
- );
- }
- }
- export default App;
API
To show or hide the Tooltip programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Tooltip, respectively.
The show() method called without arguments shows the Tooltip for the target specified beforehand. If you need to change the target once, call the show(target) method.
With Angular, Vue, or React, use a different technique. Bind the visible property of the Tooltip UI component to a component property. After that, change this component property, and the Tooltip will appear or disappear.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Tooltip } from 'devextreme-react/tooltip';
- import { Button } from 'devextreme-react/button';
- const renderContent = () => {
- return (
- <p>Tooltip content</p>
- );
- }
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- isTooltipVisible: false
- };
- this.showTooltip = this.showTooltip.bind(this);
- this.hideTooltip = this.hideTooltip.bind(this);
- }
- showTooltip() {
- this.setState({
- isTooltipVisible: true
- });
- }
- hideTooltip() {
- this.setState({
- isTooltipVisible: false
- });
- }
- render() {
- return (
- <div>
- <img id="image" src="https://url/to/an/image" />
- <Tooltip
- target="#image"
- visible={this.state.isTooltipVisible}
- contentRender={renderContent}
- onHiding={this.hideTooltip}
- />
- <Button
- text="Show the Tooltip"
- onClick={this.showTooltip}
- />
- <Button
- text="Hide the Tooltip"
- onClick={this.hideTooltip}
- />
- </div>
- );
- }
- }
- export default App;
Events
To execute certain commands before or after the Tooltip was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the corresponding onEventName property when you configure the UI component.
- import React from 'react';
- import 'devextreme/dist/css/dx.common.css';
- import 'devextreme/dist/css/dx.light.css';
- import { Tooltip } from 'devextreme-react/tooltip';
- class App extends React.Component {
- onShowing(e) {
- // Handler of the 'showing' event
- }
- onShown(e) {
- // Handler of the 'shown' event
- }
- onHiding(e) {
- // Handler of the 'hiding' event
- }
- onHidden(e) {
- // Handler of the 'hidden' event
- }
- render() {
- return (
- <Tooltip ...
- onShowing={this.onShowing}
- onShown={this.onShown}
- onHiding={this.onHiding}
- onHidden={this.onHidden}
- />
- );
- }
- }
- export default App;
If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.
- const hiddenEventHandler1 = function (e) {
- // First handler of the "hidden" event
- };
- const hiddenEventHandler2 = function (e) {
- // Second handler of the "hidden" event
- };
- $("#tooltipContainer").dxTooltip("instance")
- .on("hidden", hiddenEventHandler1)
- .on("hidden", hiddenEventHandler2);
See Also
- Handle Events: Angular | Vue | React | jQuery | AngularJS | Knockout
- Tooltip - Customize the Content
- Tooltip - Resize and Relocate
- Tooltip Demos
- Tooltip API Reference
If you have technical questions, please create a support ticket in the DevExpress Support Center.