1.4.0 • Published 4 years ago

react-tippy v1.4.0

Weekly downloads
62,335
License
MIT
Repository
github
Last release
4 years ago

React Tippy

React Version

A lightweight tooltip for React. Demo at Demo page here...

Based on tippy.js and powered by Popper.js

Example

Why you should use it?

It is designed to work friendly with React, it provides <Tooltip> element or a higher-order component.

It uses React DOM to render tooltip content. Therefore, you can fully use it in your React project without doubt.

It is an enhancement of Tippy.js for using in React.

Getting Started

yarn add react-tippy

How to use

First, you need import css

import 'react-tippy/dist/tippy.css'

There are 2 ways for you to use react Tippy

Tooltip Component

import {
  Tooltip,
} from 'react-tippy';


<Tooltip
  // options
  title="Welcome to React"
  position="bottom"
  trigger="click"
>
  <p>
    Click here to show popup
  </p>
</Tooltip>

High Order Component

withTooltip(Component, options)

import {
  withTooltip,
} from 'react-tippy';


const Header = () => (
  <h2>Header here</h2>
);

const HeaderWithTooltip = withTooltip(Header, {
  title: 'Welcome to React with tooltip',
});

Props

SettingDefaultOptionsRole
disabledfalsetrue falseShow or not show tooltip
openundefinedtrue falseJust only use it if you want to show/hide it manually. Usually, you don't need it
useContextundefinedtrue falseDefine that you're using context in your tooltip content (or html props). It's useful when you want your tooltip content can connect to redux store
onRequestClosenoop functionFunctionJust only use it if you want to show/hide it manually. This event is fired when you click outside of your tooltip, should be used with the prop interaction to keep your tooltip showing
positiontoptop bottom left rightSpecifies which direction to position the tooltip on the element. Add the suffix -start or -end to shift the position. top-end is an example.
triggermouseenter focusmouseenter focus click manualSpecifies which type of events will trigger a tooltip to show. Separate each by a space. mouseenter is for hovering and touch on mobile, and focus is for keyboard navigation. Use manual if you want to show/hide the tooltip manually/programmatically (see below).
tabIndexundefinednumberset tabIndex so element can receive focus
interactivefalsetrue falseMakes a tooltip interactive, i.e. will not close when the user hovers over or clicks on the tooltip. This lets you create a popover (similar to Bootstrap) when used in conjunction with a click trigger.
interactiveBorder2Any number (pixels)Specifies the size of the invisible border around an interactive tooltip that will prevent it from closing. Only applies to mouseenter triggered tooltips.
delay0Any integer >= 0 (milliseconds)Specifies how long it takes after a trigger event is fired for a tooltip to show.
hideDelay0Any integer >= 0 (milliseconds)Specifies how long it takes after a leave event is fired for a tooltip to hide. Not applicable when clicking on the document to hide tooltips.
animationshiftshift perspective fade scale noneSpecifies the type of transition animation a tooltip has.
arrowfalsetrue falseAdds an arrow pointing to the tooltipped element. Setting this to true disables animateFill.
arrowSizeregularsmall regular bigSpecifies how big the tooltip's arrow is.
animateFilltruetrue falseAdds a material design-esque filling animation. This is disabled if you have arrow set to true.
duration375Any integer >= 0 (milliseconds)Specifies how long the transition animation takes to complete when showing a tooltip.
distance10Any number (pixels)Specifies how far away the tooltip is from its element.
offset0Any number (pixels)Offsets the tooltip on its opposite axis. For position top and bottom, it acts as offsetX. For position left and right, it acts as offsetY.
hideOnClicktruetrue false 'persistent'Specifies whether to hide a tooltip upon clicking its element after hovering over.
multiplefalsetrue falseSpecifies whether to allow multiple tooltips open on the page (click trigger only).
followCursorfalsetrue falseSpecifies whether to follow the user's mouse cursor (mouse devices only).
inertiafalsetrue falseModifies the transition-timing-function with a cubic bezier to create a "slingshot" intertial effect.
transitionFliptruetrue falseSpecifies whether to transition between flips or not. Uses the same transition timing as duration
popperOptions{}ObjectAllows more control over tooltip positioning and behavior. See right below.
htmlnullreact elementTooltip content. If you don't define html, the title will be used
rawTemplateundefinedtemplate id or dom elementUSED WITH CAUTION. Support creating html templates from tippyjs. But IMO, it's not good to render raw html in React, it's a way to work around if you want to render some complex html generated by 3rd party such as SVG chart(in case you can not find React lib to render chart)
unmountHTMLWhenHidefalsetrue falseBy default, html component will be mounted at first show and unmount only when your tooltip component is unmounted. When you set unmountHTMLWhenHide is true, it will be unmounted whenever tooltip is hidden.
sizeregularsmall regular bigSpecifies how big the tooltip is.
stickyfalsetrue falseSpecifies whether the tooltip should stick to its element reference when it's showing (for example, if the element is animated/moves).
stickyDuration200Any number (milliseconds)Specifies the 'smoothing' transition when the popper's position updates as its element moves.
tagdivA HTML element tag name e.g. spanSpecifies the HTML element used to wrap the content that triggers the tooltip. When using a tooltip inline, span is more likely to be valid markup. When using a higher-order component with a block-level element, a div or a is more likely to be valid markup.
touchHoldfalsetrue falseChanges the trigger behavior on touch devices. It will change it from a tap to show and tap off to hide, to a tap and hold to show, and a release to hide.
onShownoopfunctionCallback when the tooltip has been triggered and has started to transition in
onShownnoopfunctionCallback when the tooltip has fully transitioned in and is showing
onHidenoopfunctionCallback when the tooltip has begun to transition out
onHiddennoopfunctionCallback when the tooltip has fully transitioned out and is hidden
themedarkdark light transparentThe CSS styling theme.
className''stringclassName of container
style{}React inline style (object)style of container

Custom tooltip content

You need to pass element to option html

import {
  Tooltip,
} from 'react-tippy';


<Tooltip
  html={(
    <div>
      <strong>
        Hello
      </strong>
    </div>
  )}
>
  // ...
</Tooltip>

Interactive html tooltip

You can use interactive prop and html for your interactive tooltip

  <Tooltip
    trigger="click"
    interactive
    html={(
      <div>
        <p>{tooltipContent}</p>
        <input
          type="text"
          value={tooltipContent}
          onChange={(e) => {setTooltipContent(e.target.value)}}
        />
      </div>
    )}
  >
    ...
  </Tooltip>

Show/hide your tooltip manually

<Tooltip
  title={tooltipContent}
  open={open}
  onRequestClose={() => {console.log('call'); setIsOpen(false)}}
>
  <span className="App-intro" onClick={() => { setIsOpen(true) }}>
    This will show {tooltipContent}
  </span>
</Tooltip>

Browser support

Tippy gracefully degrades on older browsers (and with JavaScript disabled) by using the browser's default title tooltip.

If you want to support older browsers, please add polyfill by yourself.

Supported browsers

Browsers which support requestAnimationFrame. See caniuse. If your audience has low Opera Mini usage (common in western countries), then support should be >96%.

Touch devices

Tippy works on touch devices almost the same as on desktop/mouse devices. However on iOS devices, in order for tooltips to close when tapping anywhere on the body and to trigger hover events on non-clickable elements, a .tippy-touch { cursor: pointer !important; } class is added to the body.

Accessibility

Tooltips have ARIA labelling to ensure accessibility.

Troubleshoot

I cannot connect html props with redux connect. Or can I use context in tooltip content?

react-tippy provides useContext for Tooltip component. It can allow you to use context in tooltip content. Therefore, you can totally connect it to redux.

<Tooltip
  trigger="click"
  useContext
  html={(
    <TooltipContent />
  )}
>
  Click here
</Tooltip>

Could I change tooltip style (width, height, ...)?

You can change css to have your tooltip width. If you use html props, you can do like this:

html={(
  <div style={{ width: 400 }}>
    // content here
  </div>
)}

License

MIT. Also check Popper.js' license.

@caravinci/arranger-components@setapp/ui-kitemotion-reproduce-external-libkaizen-chartjs@deboxsoft/webapp-componentsmymoria-ui@thesis-co/mechamittens-extensionqognify-uibimape-ui@everything-registry/sub-chunk-2618react-smart-tooltipxsolla-uikitzcontrolzcontrolfix@woonkly/wdefi-uikit@tbo-it/decommerce-admin-ui-components@tbo-it/decommerce-white-label-components@vega/components@verisys/pixel@tssbpchannel/reactutils@stokr/components-library@superops/ui-design-system@socialwell-design/tool-tipdsny_dashboard@soinlabs/ui@urkellabs/ucl@vinvol/gumshoedrepl@arranger/componentsfiver-react-ui-components@action.sustainability/storybook-dashboard@youngox9/common-modulefbk-componentsfbz@ballistix.digital/digipolis-componentsappiphonlow6-privateconversion-flowm-dash-ui@dcentralab/web3-wallet-connector@dcentralab/wc-buttonia-ui-lib@dac-software/forms-plcampus-libraryneotracker-shared-webmedlife-componentsastrazeneca-vdcatomic-book@emeraldsci/ketcher-react@freshworks/react-nucleus@engagio/engagio-ui@engagio/engagio-ui-charts@engagio/engagio-ui-core@cleartrip/bento@cleartrip/bento-mobile@clickopolis/core@clickopolis/apphippo-fwcolby-wp-react-search-again@crpt/relation-treebeeonis-editor-reactasdfghjkl05arp-prpcf-booking_widgetkembo-ui-reactcomponent-hindawi-uinext-monaco-editor@icgc-argo/uikit@infinitebrahmanuniverse/nolb-react-ti@homehero/hero-stylenpm-package-test-bilalorangecode-react-componentsoptimizely-oui@mtumi/storybook-dashboard@lyra/components@lyra/desk-tool@neotracker/corereact-revised-week-schedulerpndash@jujubes/ui-react@kfarranger/componentstabeo-componentsreact-bulma-vitaereact-composite-tablereact-compact-table@mechamittens/extensionreact-fs-componentstenders-portaltcfw-showcase-editreact-ui-frameworkpublisher-console@kudoo/componentsqbe-dyna-componentssuperset-plugin-summary-layoutsuperset-plugin-summary-panelrightd@overture-stack/arranger-componentsriada-react-ui-componentsroyaltyz-uikit@pleio/design-system
1.4.0

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

6 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

0.14.0

7 years ago

0.12.1

7 years ago

0.12.0

7 years ago

0.11.1-1

7 years ago

0.11.1

7 years ago

0.10.0

7 years ago

0.7.0

7 years ago

0.6.1-3

7 years ago

0.6.1-2

7 years ago

0.6.1-1

7 years ago

0.6.1

7 years ago

0.5.1-1

7 years ago

0.5.1

7 years ago

0.4.1

7 years ago

0.4.0-1

7 years ago

0.4.0

7 years ago

0.3.5-3

7 years ago

0.3.5-2

7 years ago

0.3.5-1

7 years ago

0.3.5

7 years ago

0.3.2-2

7 years ago

0.3.2-1

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago