Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-tooltip-controller

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-tooltip-controller

A feature-rich React component for controlling tooltips

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
increased by300%
Maintainers
1
Weekly downloads
 
Created
Source

React-Tooltip-Controller

This is a feature-rich React component for controlling tooltips. Not only for tooltips, but you can use it for various interaction requirements.

Visit the examples page to discover all the functionalities.

Basic TooltipAnimated TooltipAdvanced Tooltip
screen7screen8screen6
Highlights
  • Supports click, hover, hover-hold and hover-interactable detection.
  • You can individually animate each tooltip.
  • You can set whether the tooltip closes when clicked on it.
  • You can assign a variable to close the tooltip manually.
  • You can retrieve the state of the tooltip (whether open or not).
  • You can set a timeout to automatically close the tooltip.
  • You can position the tooltip relative to the triggering element.
  • You can automatically center the tooltip along the X axis for dynamically sized elements.

Installing

npm install react-tooltip-controller

After installing the module, import it:

import ToolTipController from 'react-tooltip-controller'

Basic Usage

<button className="random-classname">Click me</button>

<ToolTipController
  select="random-classname"
  detect="click"
  offsetY={20}>

  <TooltipComponent/>

</ToolTipController>

Anything wrapped by <ToolTipController> is portalled to the bottom of the <body> tag in the DOM.

You can either wrap a component or JSX Markup with <ToolTipController>.

By referring to the className property of the <button>, <TooltipComponent/> is attached to <button> and set to be triggered by a click action.

By default, the tooltip wrapped is positioned relative to the left-bottom of the selected element.

Properties Table

PropsDescriptionPossible ValuesDefaultData Type
idAssigns an ID number to the tooltip container class to be able to distinguish between multiple tooltipsE.g. "1", "2"nullString
selectclassName of the element that will control the tooltip--String
detectDetermines how to trigger the tooltip. Note that timeOut and id props should be defined in order to use the "hover-interactable” option"click",
"hover",
"hover-hold",
"hover-interactable”
"click"String
closeOnClickDetermines whether the tooltip closes when clicked on ittrue, falsetrueBoolean
triggerCloseA Boolean value of true closes the tooltip manuallyBoolean variable-Boolean
returnStateReturns the state of the tooltip - If it’s open or notFunction-Function
timeOutDetermines if the tooltip closes automatically after a certain amount of time in milliseconds.positive integersnullInteger
offsetXDetermines the offset along the X axis relative to left bottom of the elementpositive/negative integers0Integer
offsetYDetermines the offset along the Y axis relative to left bottom of the element. If set to "center" along with width prop, automatically aligns to middle of the triggering elementpositive/negative integers or "center"0Integer, String
widthWidth value of the tooltip
Required for offsetX = "center"
positive integersnullInteger
animationDetermines the name of the animation class-nullString
durationDetermines the duration of the animation in units of milliseconds(ms) or seconds(s)E.g. "500ms" or "0.5s"nullString
timingDetermines the timing function of the animation. You can use standard CSS timing functions such as "linear", "ease" or can define a specific Cubic Bezier curveE.g. "linear" or "ease"nullString
propertiesDetermines the properties to be animated. Can be a string or an array of stringsE.g. "all" or ["opacity", "transform"][ ]String, Array

Examples

Minimal Example

import React, { Component } from 'react'
import ToolTipController from 'react-tooltip-controller'
import './styles/main.css'

const ToolTip = () =>
  <div className="toolTip">
    Tooltip
  </div>

class Example extends Component {

  render() {
    return (
      <div className="App">
        <button className="btn">Hello There</button>

        <ToolTipController
          select="btn"
          detect="hover">
          <ToolTip/>
        </ToolTipController>

      </div>

    )
  }
}

export default Example

Animation Example

Stylus File

.react-tooltip-1
  opacity: 0
  transform: translateY(10px)
  &.fadeIn
    opacity: 1
    transform: translateY(0)

JSX file

import React, { Component } from 'react'
import ToolTipController from 'react-tooltip-controller'
import './styles/main.css'

const ToolTip = () =>
  <div className="toolTip">
    Tooltip
  </div>

class Example extends Component {

  render() {
    return (
      <div className="App">
        <button className="btn">Hello There</button>

        <ToolTipController
          id="1"
          select="btn"
          detect="hover"
          animation="fadeIn"
          duration=".3s"
          timing="ease"
          properties={["opacity", "transform"]}>
          <ToolTip/>
        </ToolTipController>

      </div>

    )
  }
}

export default Example

Note that react-tooltip is a built-in class name and since the id prop is set to "1", we refer to this tooltip with the specific class name of react-tooltip-1.

Always set the id prop for the animated tooltips in order to prevent any class name conflicts.

Side note: If you don't set the properties prop, all the properties for the tooltip animates. This is prominent when you resize the browser window as the tooltip is open.

Use of triggerClose prop

import React, { Component } from 'react'
import ToolTipController from 'react-tooltip-controller'
import './styles/main.css'

const ToolTip = (props) =>
  <div className="toolTip">
    Tooltip
    <button onClick={null}></button>
    <button onClick={props.closeTriggerFunction}></button>
  </div>

class Example extends Component {

  state = {
    trigger: false
  }

  close = () => {
    this.setState({trigger: true})
  }

  render() {
    return (
      <div className="App">
        <button className="btn">Hello There</button>

        <ToolTipController
          select="btn"
          detect="click"
          closeOnClick={false}
          triggerClose={this.state.trigger}>
          <ToolTip closeTriggerFunction={this.close}/>
        </ToolTipController>

      </div>

    )
  }
}

export default Example

By using the triggerClose prop, the tooltip can be closed manually. To do so, variable passed to triggerClose prop should be set to true.

This example demonstrates how to close the tooltip by setting the state of the triggering variable to true. To prevent the other click actions on the tooltip from closing it, closeOnClick is set to false. Note that clicking outside of the tooltip closes it independent of the triggerClose prop.

Use of returnState prop

import React, { Component } from 'react'
import ToolTipController from 'react-tooltip-controller'
import './styles/main.css'

const ToolTip = (props) =>
  <div className="toolTip">
    Tooltip
  </div>

class Example extends Component {

  state = {
    tooltipState: false
  }

  getTooltipState = (data) => {
    this.setState({tooltipState: data})
  }

  render() {
    return (
      <div className="App">
        <button className="btn">Hello There</button>

        <ToolTipController
          select="btn"
          detect="hover"
          returnState={this.getTooltipState}>
          <ToolTip/>
        </ToolTipController>

      </div>

    )
  }
}

export default Example

You can pass a function as a prop through returnState in order to get the state of the tooltip, whether it's open or not.

License

MIT License

Keywords

FAQs

Package last updated on 24 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc