0.2.4 • Published 2 years ago

react-minimal-tooltip v0.2.4

Weekly downloads
417
License
(MIT OR Apache-2....
Repository
github
Last release
2 years ago

React Minimal Tooltip, a small and simple tooltip library.

npm.io

Tooltips can be displayed on top of or underneath the content they're describing. The tooltips will center themselves based on the location of their element and shrink in accordance to the page width. The tooltip can display any react node, not just strings.

But before you go any further: Do you even need a tooltip library? Maybe something like the title attribute will suit your needs.

Why use this over the other libraries?

Unlike the most popular React tooltip library which works via data tags, this library is in my opinion more 'reacty' and works via wrapping the element you wish to display a tooltip for in a component.

This package is also a bit smaller than other popular alternatives.

Installation

npm install react-minimal-tooltip
yarn add react-minimal-tooltip

Use

The simplest example:

import Tooltip from 'react-minimal-tooltip'

<Tooltip tooltipChildren="I am a tooltip">
  // the content the tooltip is for
  <button>Hover over me!</button>
</Tooltip>

API

Below is the API for the default exported tooltip component. Note that this component wraps a div around the node the the tooltip is for, any prop given to the Tooltip component not mentioned below is passed through to that div:

PropTypeRequiredInfoDefault
hoverDurationUntilVisiblenumberfalseThe amount of time in ms the cursor needs to be over the element until the tooltip appears500
childrenreact nodetruethe element the tooltip is forN/A
tooltipChildrenreact nodetruethe content of the tooltipN/A
tooltipPositionstringfalsethe position of the tooltip, should be 'top' or 'bottom''top'
tooltipColorstringfalsevalid css/style color, note that this is applied separately from tooltipStyle as the color is also applied to an svg element'#d1d1d1'
tooltipStyleobjectfalsestyle object that's applied to the visible part of the tooltip wrapper{}
tooltipClassNamestringfalsecss className that's applied to the visible part of the tooltip wrapper''
tooltipZIndexnumberfalsez-index applied to the tooltip1
tooltipPointerSizenumberfalsesize of the pointy part of the tooltip12
tooltipSpacingnumberfalsespace between the tooltip and the item it's describing0
tooltipFadebooleanfalsewhether or not the tooltip should use a fade animationtrue
tooltipFadeDurationstringfalseduration of the fade animation'0.2s'
tooltipFadeEasingFunctionstringfalseeasing function used for the fade animation'east-in-out'

Advanced

Two other components are exported HoverDetector and TooltipWrapper. These are the consituent parts of the main exported Tooltip component, if you want to do something a bit more funky then looking at these components in the src will be great fun, they're not that complicated.

Advice

I recommend creating your own wrapper around this component in your project, that way you can set sensible defaults for things like colors and hover durations. You can also name the props whatever you want if you think mine are a little to wordy.

Thing to keep in mind

This component works by putting a div around the node the tooltip is describing. This both gives an element to listen to mouse events on, as well as an "anchor" to use to position the tooltip itself. Just keep that wrapping div in mind when adding tooltips to your layout. Remember that you can pass props like className or style to the tooltip and they'll be passed through to this wrapping div as mentioned above in the API section.

The tooltip will shrink to fit the screen width if necessary, but it wont reposition itself if there's not enough room to display its content. For instance if you put a tooltip with position 'top' at the very top of your page it might be partially clipped off.

Development

This was bootstrapped with Create React App and we use that for development.

To start the dev server:

yarn start

We have a custom webpack/babel setup we use to bundle the package into a module:

yarn build

Todo

  • Consider left and right positioning.
  • Document and describe the other two exposed components.
  • Tidy up the tooltip positioning logic.
  • Consider automatic positioning changing.
  • Add linting and tests, general clean up.
  • proof read this readme
0.1.35

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.1.40

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.4

2 years ago

0.1.33

3 years ago

0.1.32

3 years ago

0.1.31

3 years ago

0.1.30

3 years ago

0.1.27

3 years ago

0.1.28

3 years ago

0.1.29

3 years ago

0.1.23

3 years ago

0.1.24

3 years ago

0.1.25

3 years ago

0.1.26

3 years ago

0.1.20

3 years ago

0.1.21

3 years ago

0.1.22

3 years ago

0.1.16

3 years ago

0.1.17

3 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.13

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago