26
loading...
This website collects cookies to deliver better user experience
:hover
selector, but there are two limitations that you can encounter::hover
selector through JavaScriptSyntheticEvent
, and the second employs an npm library called React Hover. The latter allows you to show other components on hover.onclick
becomes onClick
false
to prevent a default behavior as you would in JavaScript; instead, you must explicitly call preventDefault
SyntheticEvent
SyntheticEvent
is React's cross-browser wrapper that wraps around the browser's native event, which allows your events to work identically across all browsers.SyntheticEvent
types, but for this article, our main concern is Mouse Events.onHover
, which would allow you to create a hover event.onClick onContextMenu
onDoubleClick onDrag
onDragEnd onDragEnter
onDragExit onDragLeave
onDragOver onDragStart
onDrop onMouseDown
onMouseEnter onMouseLeave
onMouseMove onMouseOut
onMouseOver onMouseUp
SyntheticEvent
. They are onMouseEnter
and onMouseLeave
.npm install create-react-app
App.js
and index.css
src
folder and name them css
and component
css
folder, create a new file called Tooltip.css
component
folder created earlier, create a new file called Tooltip.js
Tooltip.js
, let's write some code. We’ll write the code for Tooltip.css
later in the article.React
, useState
from React
, and the css
file we created earlier. You'll need useState
to manage the application state when the user chooses to view the tooltip.// src/components/Tooltip.js
import React, { useState } from 'react'
import '../css/Tooltip.css'
Tooltip
, which is a function that returns data.// src/components/Tooltip.js
import React, { useState } from 'react'
import '../css/Tooltip.css'
const Tooltip = (props) => {
// All the code that will make the
// tooltip work resides here
}
Tooltip
component, you'll do the following:useState
onMouseEnter
and onMouseLeave
events handlers attached to itonMouseEnter
, which will be the function that shows the tooltiponMouseLeave
, which will be the function that hides the tooltip// src/components/Tooltip.js
// Code truncated, check the previous
// code block.
const Tooltip = (props) => {
// All the code that will make the
// tooltip work resides here
// Set up timer and state
let TooltipTimeout;
const [activeToolTip, setActiveToolTip] = useState(false);
// Write a function to show the tooltip
const showToolTip = () => {
TooltipTimeout = setTimeout(() => {
setActiveToolTip(true);
}, props.delay || 300);
};
// Write a function to hide the tooltip
const hideToolTip = () => {
setActiveToolTip(false);
clearInterval(TooltipTimeout);
};
// Return JSX which contains the HTML
// data for the tooltip
// Note the usage of the 2 supported event handlers
// mentioned earlier in this article. They make
// it is possible to create the hover event in React.
return (
<div
className="Tooltip-Container"
onMouseEnter={showToolTip}
onMouseLeave={hideToolTip}
>
{props.children}
{activeToolTip && (
<div className={`Tooltip-Content ${props.direction} || "top"}`}>
{props.content}
</div>
)}
</div>
);
};
// Export the tooltip
export default Tooltip
App.js
(or any other component where you’d find it useful).// App.js
import React from "react"
import Tooltip from './components/Tooltip';
import './index.css';
const App = () => {
return (
<div className="App">
<div className="tooltip-wrapper">
<Tooltip content="I am a tooltip" direction="top">
Hover your mouse here
</Tooltip>
</div>
</div>
)
}
export default App
Tooltip.css
file, and write the following:/* css/Tooltip.css */
/**
* The CSS class name starts with a
* capital letter to indicate it's a
* component.
*/
.Tooltip-Container {
position: relative;
display: inline-block;
}
.Tooltip-Content {
position: absolute;
left: 50%;
padding: 8px;
color: #ffffff;
background: #1a1a1a;
font-size: 0.85em;
border-radius: 6px;
transform: translateX(-50%);
z-index: 1;
white-space: nowrap;
}
.Tooltip-Content::before {
left: 50%;
position: absolute;
content: " ";
border: 6px solid transparent;
margin-left: -6px;
}
.Tooltip-Content.top {
top: -30px;
}
.Tooltip-Content.top::before {
top: 100%;
border-top-color: #1a1a1a;
}
/**
* The following styles are
* variations of the tooltip when you
* change the value if the "direction" attribute
* in the App component.
*/
.Tooltip-Content.right {
top: 50%;
left: calc(100% + 20px);
transform: translateY(-50%);
}
.Tooltip-Content.right::before {
top: 50%;
left: -6px;
transform: translateY(-50%);
border-right-color: #1a1a1a;
}
.Tooltip-Content.bottom::before {
bottom: 100%;
border-bottom-color: #1a1a1a;
}
.Tooltip-Content.left {
top: 50%;
right: calc(100% + 30px);
left: auto;
transform: translateY(-50%);
}
.Tooltip-Content.left::before {
top: 50%;
right: -12px;
left: auto;
transform: translateY(-50%);
border-left-color: #1a1a1a;
}
index.css
, which should still be empty, and write the following:/* index.css */
.App {
font-family: "Trebuchet MS", Verdana, Geneva, Tahoma, sans-serif;
padding-top: 16px;
padding-right: 16px;
padding-bottom: 120px;
padding-left: 16px;
}
.tooltip-wrapper {
padding: 16px 120px;
}
onHover
method, but you've also leveraged two event handlers supported by React’s SyntheticEvent
(onMouseEnter
and onMouseLeave
) to create it.npm install --save react-hover
package.json
file.<ReactHover>
: You'll wrap this around two things, which are the <Trigger>
and <Hover>
components<Trigger>
: This is the wrapper for the <Trigger>
component<Hover>
: This is the wrapper for the <Hover>
componentoptions
: This is an attribute of <ReactHover>
, and its value is an object that determines the behavior and position of the <Hover>
component when you move the cursor over the <Trigger>
component. The object accepts the next three properties:
followCursor
: Accepts a boolean value that determines whether the <Hover>
component will follow the cursor when you move the cursor over the <Trigger>
componentshiftX
: Determines the position of the <Hover>
component along the X-axis, i.e. left or rightshiftY
: This determines the position of the <Hover>
component along the Y-axis, i.e. top or bottomtype
: This attribute identifies the component as a trigger or a hover; therefore, its value would be <Trigger>
for the trigger component, and <Hover>
for the hover component<Trigger>
and <Hover>
respectively.TriggerComponent.js
and HoverComponent.js
. Switch to your editor and type the next code block in TriggerComponent.js
:// components/TriggerComponent.js
import React from 'react'
const TriggerComponent = () => {
return (
<p>Hover on me</p>
)
}
export default TriggerComponent
HoverComponent.js
and type the following:// components/HoverComponent.js
import React from 'react'
const HoverComponent = () => {
return (
<p>I am a hover component.</p>
)
}
export default HoverComponent</pre>
App.js
or any other location in your app. Mind you, in App.js
, you'll write the object that you'll pass to the options
attribute. As stated earlier, this object will determine the behavior of the hovered component when the cursor is moved over the <Trigger>
component.App.js
:// App.js
import React from 'react'
import ReactHover, { Trigger, Hover } from 'react-hover'
import TriggerComponent from './components/TriggerComponent'
import HoverComponent from './components/HoverComponent'
// Set the option that determines the position
// and behavior of the hover component
const OptionsCursorTrueWithMargins = {
followCursor: true,
shiftX: 20,
shiftY: 0
}
// The App function
const App = () => {
return (
<ReactHover options={OptionsCursorTrueWithMargins}>
<Trigger type="trigger">
<TriggerComponent />
</Trigger>
<Hover type="hover">
<HoverComponent />
</Hover>
</ReactHover>
)
};
export default App
SyntheticEvent
, and the second option incorporates React Hover, a JavaScript library available on the npm registry.