$ npm install react-lightweight-tooltip
npm install react-lightweight-tooltip --save
import {Tooltip} from 'react-lightweight-tooltip';
<Tooltip content="Yes, the default one">Simple black tooltip</Tooltip>
The children represent the element(s) the tooltip is wrapped around.
The content is the actual content of the tooltip. It can be string or array of React Elements.
Note that each React Element in the array has to have its unique key prop.
<Tooltip
content={
[
'This repo is hosted on ',
<a href="https://github.com" key="githublink" target="_blank">Github</a>,
]
}>
Tooltip with a link
</Tooltip>
The styles prop is used to override the default styles of the tooltip. When passed to the component, the component merges them with its default styles.
This is especaially handy when writing specialized components.
const greenRoundedStyle = {
content: {
backgroundColor: 'green',
color: '#000',
},
tooltip: {
backgroundColor: 'green',
borderRadius: '10px',
},
arrow: {
borderTop: 'solid green 5px',
},
};
const GreenRoundedTooltip = () => {
return (
<Tooltip
content={
[
'This repo is hosted on ',
<a href="https://github.com" key="githublink" target="_blank">Github</a>,
]
}
styles={greenRoundedStyle}>
Green tooltip with rounded corners and a link
</Tooltip>
);
}
export default GreenRoundedTooltip;
You can easily override the default styles by passing your own styles to the styles prop. When your styles get passed, the component merges them with its default styles.
The default styles are the following:
{
wrapper: {
position: 'relative',
display: 'inline-block',
zIndex: '98',
color: '#555',
cursor: 'help',
},
tooltip: {
position: 'absolute',
zIndex: '99',
background: '#000',
bottom: '100%',
left: '50%',
marginBottom: '10px',
padding: '5px',
WebkitTransform: 'translateX(-50%)',
msTransform: 'translateX(-50%)',
OTransform: 'translateX(-50%)',
transform: 'translateX(-50%)',
},
content: {
background: '#000',
color: '#fff',
fontSize: '.8em',
padding: '.3em 1em',
whiteSpace: 'nowrap',
},
arrow: {
position: 'absolute',
width: '0',
height: '0',
bottom: '-5px',
left: '50%',
marginLeft: '-5px',
borderLeft: 'solid transparent 5px',
borderRight: 'solid transparent 5px',
borderTop: 'solid #000 5px',
},
gap: {
position: 'absolute',
width: '100%',
height: '20px',
bottom: '-20px',
},
}
Special thanks to @no23reason for his help with the “on touch outside” handling. This project uses the react-component-boilerplate.
react-lightweight-tooltip is available under MIT license. See LICENSE for more details.
© 2010 - cnpmjs.org x YWFE | Home | YWFE