3.3.5 • Published 7 years ago

react-tooltip-currenttarget v3.3.5

Weekly downloads
171
License
MIT
Repository
github
Last release
7 years ago

react-tooltip

Version js-standard-style npm download Build Status

Looking for maintainers

I learnt a lot from creating and maintaining react-toolip, but now I start putting my focus on other challenges, so just let me know by sending email to wayne.wang0821@gmail.com if you have interests in maintaining the project :)

Maintainers

huumanoid

Installation

npm install react-tooltip

Usage

Using NPM

1 . Require react-tooltip after installation

import ReactTooltip from 'react-tooltip'

2 . Add data-tip = "your placeholder" to your element

<p data-tip="hello world">Tooltip</p>

3 . Include react-tooltip component

<ReactTooltip />

Standalone

You can import node_modules/react-tooltip/standalone/react-tooltip.min.js into your page. Please make sure that you have already imported react and react-dom into your page.

Options

Notes:

  • The tooltip sets type: dark place: top effect: float as default attributes. You don't have to add these options if you don't want to change the defaults
  • The option you set on <ReactTooltip /> component will be implemented on every tooltip in a same page: <ReactTooltip effect="solid" />
  • The option you set on a specific element, for example: <a data-type="warning"></a> will only affect this specific tooltip

Check example: React-tooltip Test

GlobalSpecificTypeValuesDescription
placedata-placeStringtop, right, bottom, leftplacement
typedata-typeStringsuccess, warning, error, info, lighttheme
effectdata-effectStringfloat, solidbehaviour of tooltip
eventdata-eventStringe.g. clickcustom event to trigger tooltip
eventOffdata-event-offStringe.g. clickcustom event to hide tooltip (only makes effect after setting event attribute)
globalEventOffStringe.g. clickglobal event to hide tooltip (global only)
isCapturedata-iscaptureBooltrue, falsewhen set to true, custom event's propagation mode will be capture
offsetdata-offsetObjecttop, right, bottom, leftdata-offset="{'top': 10, 'left': 10}" for specific and offset={{top: 10, left: 10}} for global
multilinedata-multilineBooltrue, falsesupport <br>, <br /> to make multiline
classNamedata-classStringextra custom class, can use !important to overwrite react-tooltip's default class
htmldata-htmlBooltrue, false<p data-tip="<p>HTML tooltip</p>" data-html={true}></p> or <ReactTooltip html={true} />
delayHidedata-delay-hideNumber<p data-tip="tooltip" data-delay-hide='1000'></p> or <ReactTooltip delayHide={1000} />
delayShowdata-delay-showNumber<p data-tip="tooltip" data-delay-show='1000'></p> or <ReactTooltip delayShow={1000} />
insecurenullBooltrue, falseWhether to inject the style header into the page dynamically (violates CSP style-src but is a convenient default)
borderdata-borderBooltrue, falseAdd one pixel white border
getContentnullFunc or Array() => {}, () => {}, IntervalGenerate the tip content dynamically
afterShownullFunc() => {}Function that will be called after tooltip show
afterHidenullFunc() => {}Function that will be called after tooltip hide
disabledata-tip-disableBooltrue, falseDisable the tooltip behaviour, default is false
scrollHidedata-scroll-hideBooltrue, falseHide the tooltip when scrolling, default is true
resizeHidenullBooltrue, falseHide the tooltip when resizing the window, default is true
wrappernullStringdiv, spanSelecting the wrapper element of the react tooltip, default is div

Using react component as tooltip

Check the example React-tooltip Test

Note:
  1. data-tip is necessary, because <ReactTooltip /> finds the tooltip via this attribute
  2. data-for corresponds to the id of <ReactTooltip />
  3. When using react component as tooltip, you can have many <ReactTooltip /> in a page but they should have different ids

Static Methods

ReactTooltip.hide(target)

Hide the tooltip manually, the target is optional, if no target passed in, all existing tooltips will be hidden

import {findDOMNode} from 'react-dom'
import ReactTooltip from 'react-tooltip'

<p ref='foo' data-tip='tooltip'></p>
<button onClick={() => { ReactTooltip.hide(findDOMNode(this.refs.foo)) }}></button>
<ReactTooltip />

ReactTooltip.rebuild()

Rebinding all tooltips

ReactTooltip.show(target)

Show specific tooltip manually, for example:

import {findDOMNode} from 'react-dom'
import ReactTooltip from 'react-tooltip'

<p ref='foo' data-tip='tooltip'></p>
<button onClick={() => { ReactTooltip.show(this.refs.foo) }}></button>
<ReactTooltip />

Troubleshooting

1. Using tooltip within the modal (e.g. react-modal)

The component was designed to set <ReactTooltip /> once and then use tooltip everywhere, but a lot of people get stuck when using this component in a modal. You can read the discussion here. To solve this problem:

  1. Place <ReactTooltip /> outside of the <Modal>
  2. Use ReactTooltip.rebuild() when opening the modal
  3. If your modal's z-index happens to be higher than the tooltip's, use the attribute className to custom your tooltip's z-index

I suggest always putting <ReactTooltip /> in the Highest level or smart component of Redux, so you might need these static method to control tooltip's behaviour in some situations

2. Hide tooltip when getContent returns undefined

When you set getContent={() => { return }} you will find the tooltip will display true. That's because React will set the value of data-* to be 'true' automatically if there is no value to be set. So you have to set data-tip='' in this situation.

<p data-tip='' data-for='test'></p>
<ReactTooltip id='test' getContent={() => { return null }}/>

Same for empty children, if you don't want show the tooltip when the children is empty

<p data-tip='' data-for='test'></p>
<ReactTooltip id='test'>{}</ReactTooltip>

Article

How I insert sass into react component

Authors

see AUTHORS

License

MIT