Content in React Tooltip component
18 Jan 202312 minutes to read
A text or a piece of information assigned to the Tooltip’s content
property will be displayed as the main text stream of the Tooltip.
It can be a string or a template content. If the content
property is not provided with any specific value, then it takes the value assigned to the title
attribute of the target element on which the Tooltip was initialized. The content can also dynamically be assigned to the Tooltip via AJAX.
Template content
Any text or image can be added to the Tooltip, by default. To customize the Tooltip layout or to create your own visualized element on the Tooltip, template can be used.
Refer to the following code example to add formatted HTML content to the Tooltip.
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { TooltipComponent } from '@syncfusion/ej2-react-popups';
function App() {
function content() {
return (<div id='tooltipContent'>
<p><strong>Environmentally friendly</strong> or <strong>environment-friendly</strong>, <i>(also referred to as eco-friendly, nature-friendly, and green)</i> are marketing and sustainability terms referring to goods and services, laws, guidelines and policies that inflict reduced, minimal, or no harm upon ecosystems or the environment.</p></div>);
}
let style = {
display: 'inline-block',
padding: '5px'
};
return (<p>A green home is a type of house designed to be
<TooltipComponent width="300px" height="60px" isSticky={true} content={content} style={style}>
<a><u>environmentally friendly</u></a>
</TooltipComponent> and sustainable. And also focuses on the efficient use of "energy, water, and building materials." As green homes
have become more prevalent we have also seen the emergence of green affordable housing.
</p>);
}
export default App;
ReactDom.render(<App />, document.getElementById('sample'));
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { TooltipComponent } from '@syncfusion/ej2-react-popups';
function App() {
function content() {
return(
<div id='tooltipContent'>
<p><strong>Environmentally friendly</strong> or <strong>environment-friendly</strong>, <i>(also referred to as eco-friendly, nature-friendly, and green)</i> are marketing and sustainability terms referring to goods and services, laws, guidelines and policies that inflict reduced, minimal, or no harm upon ecosystems or the environment.</p></div>
)
}
let style: object = {
display: 'inline-block',
padding : '5px'
};
return (
<p>A green home is a type of house designed to be
<TooltipComponent width="300px" height="60px" isSticky={true} content={content} style={style}>
<a><u>environmentally friendly</u></a>
</TooltipComponent> and sustainable. And also focuses on the efficient use of "energy, water, and building materials." As green homes
have become more prevalent we have also seen the emergence of green affordable housing.
</p>
);
}
export default App;
ReactDom.render(<App />,document.getElementById('sample'));
Dynamic content via AJAX
The Tooltip content can be dynamically loaded by making use of the AJAX call. The AJAX request is usually made within the beforeRender
event of the Tooltip, and then the Tooltip’s content
is assigned the value retrieved on it’s success.
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { TooltipComponent } from '@syncfusion/ej2-react-popups';
import { Ajax } from '@syncfusion/ej2-base';
function App() {
let tooltipInstance;
function onBeforeRender(args) {
tooltipInstance.content = 'Loading...';
tooltipInstance.dataBind();
let ajax = new Ajax('./tooltipdata.json', 'GET', true);
ajax.send().then((result) => {
result = JSON.parse(result);
for (let i = 0; i < result.length; i++) {
if (result[i].Id === args.target.getAttribute('data-content')) {
tooltipInstance.content = "<div class='contentWrap'>" + result[i].Sports + "</div>";
}
}
tooltipInstance.dataBind();
}, (reason) => {
tooltipInstance.content = reason.message;
tooltipInstance.dataBind();
});
}
return (<div id="container">
<h4>National Sports</h4>
<TooltipComponent id="targetContainer" ref={t => tooltipInstance = t} className="e-prevent-select" content="Loading..." target=".target" position="RightCenter" beforeRender={onBeforeRender.bind(this)}>
<div id="countrylist">
<ul>
<li className="target" title="1"><span>Australia</span></li>
<li className="target" title="2"><span>Bhutan</span></li>
<li className="target" title="3"><span>China</span></li>
<li className="target" title="4"><span>Cuba</span></li>
<li className="target" title="5"><span>India</span></li>
<li className="target" title="6"><span>Switzerland</span></li>
<li className="target" title="7"><span>United States</span></li>
</ul>
</div>
</TooltipComponent>
</div>);
}
export default App;
ReactDom.render(<App />, document.getElementById('sample'));
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { TooltipComponent, TooltipEventArgs } from '@syncfusion/ej2-react-popups';
import { Ajax } from '@syncfusion/ej2-base';
function App() {
let tooltipInstance: TooltipComponent;
function onBeforeRender(args: TooltipEventArgs): void {
tooltipInstance.content = 'Loading...';
tooltipInstance.dataBind();
let ajax: Ajax = new Ajax('./tooltipdata.json', 'GET', true);
ajax.send().then(
(result: any) => {
result = JSON.parse(result);
for (let i: number = 0; i < result.length; i++) {
if (result[i].Id === args.target.getAttribute('data-content')) {
tooltipInstance.content = "<div class='contentWrap'>" + result[i].Sports + "</div>";
}
}
tooltipInstance.dataBind();
},
(reason: any) => {
tooltipInstance.content = reason.message;
tooltipInstance.dataBind();
});
}
return (
<div id="container">
<h4>National Sports</h4>
<TooltipComponent id="targetContainer" ref={t => tooltipInstance = t} className="e-prevent-select" content="Loading..." target=".target" position="RightCenter" beforeRender={onBeforeRender.bind(this)}>
<div id="countrylist">
<ul>
<li className="target" title="1"><span>Australia</span></li>
<li className="target" title="2"><span>Bhutan</span></li>
<li className="target" title="3"><span>China</span></li>
<li className="target" title="4"><span>Cuba</span></li>
<li className="target" title="5"><span>India</span></li>
<li className="target" title="6"><span>Switzerland</span></li>
<li className="target" title="7"><span>United States</span></li>
</ul>
</div>
</TooltipComponent>
</div>
);
}
export default App;
ReactDom.render(<App />, document.getElementById('sample'));