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

react-tooltpz

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-tooltpz

Flexible Tooltip Components with zero dependencies

  • 3.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22
increased by340%
Maintainers
1
Weekly downloads
 
Created
Source

React Tooltpz

NPM version NPM license NPM total downloads NPM monthly downloads

Low-level components for creating menus, tooltips, hints, dropdown and other popups

Flexible Tooltip Components with zero dependencies

  • Automatically find best position
  • Hover, Click and Focus logic out of the box
  • Support custom logic
  • Portal to document.body
  • No extra DOM nodes
  • Lightweight (2.8kb minified+gzipped)

Try demo

Getting Started

  • Installation:

    npm install --save react-tooltpz
    
  • Importing:

    import { TooltipParent, Tooltip, useHoverTooltip } from 'react-tooltpz';
    

    You also can import directly what you want

    import TooltipParent from 'react-tooltpz/lib/TooltipParent';
    import Tooltip from 'react-tooltpz/lib/Tooltip';
    import useHoverTooltip from 'react-tooltpz/lib/useHoverTooltip';
    
  • Basic Usage:

    import React from 'react';
    import { TooltipParent, Tooltip, useHoverTooltip } from 'react-tooltpz';
    
    const TitleWithTooltip = ({ title, tooltip }) => (
        <TooltipParent tooltip={useHoverTooltip}>
            {({ innerRef, ...rest }) => (
                <div {...rest} ref={innerRef}>
                    {title}
                </div>
            )}
            <Tooltip>
                {({ innerRef, ...rest }) => (
                    <div {...rest} ref={innerRef}>
                        {tooltip}
                    </div>
                )}
            </Tooltip>
        </TooltipParent>
    );
    
    export default TitleWithTooltip;
    

Components

  • TooltipParent

    Props

    nametypedefaultdescription
    innerRefobjectnullParent ref object
    tooltipfunction (react hook)requiredTooltip opened logic
    children({ innerRef, ...rest }, { opened, setOpened, ...tooltipRest }) => jsxnullParent render function
  • Tooltip

    Props

    nametypedefaultdescription
    innerRefobjectnullTooltip ref object
    parentRefobjectnullParent ref object
    zIndexnumber0Tooltip default z-index
    marginnumber4Margin between parent and tooltip
    positionone of [bottom, top, left, right]bottomTooltip position
    alignone of [start, center, end]startTooltip align
    children({ innerRef, style, ...rest }, { parentSize, tooltipSize, setOpened } ) => jsxnullTooltip render function
    styleobjectnullTooltip style
    setOpened(opened) => voidnullTooltip opened change function
    portalNodeDOM nodedocument.bodyContainer for ReadDOM.createPortal

Logic hooks

  • useHoverTooltip

    open tooltip by a mouse enter and close by mouse leave

  • useClickTooltip

    open tooltip by a click and close by a click and outside click

  • useFocusTooltip

    open tooltip by focus and close by blur

Contexts

  • PortalNodeContext

    provide portalNode to all tooltips

  • ZIndexContext

    provide zIndex to all tooltips

Helpers

  • useOutsideClick

    track click outside element with portal support

    const { onMouseDown, onTouchStart } = useOutsideClick(onOutsideClick);
    

    Accepts: function

    Returns: object with onMouseDown and onTouchStart props, you should set this event handlers on target element

Write your own logic hook

  • API

    const [
        parentProps,
        { opened, setOpened, ...tooltipProps }
    ] = useMyOwnLogicHook({ parentRef, tooltipRef });
    

    Accepts: object with parentRef and tooltipRef props

    Returns: array with parentProps and tooltipProps items

    *tooltipProps required opened and setOpened props

  • Usage

    const useSimpleClickTooltip = () => {
        const [opened, setOpened] = useState(false);
        const onClick = useCallback(() => {
            setOpened((value) => !value);
        });
    
        return [{ onClick }, { opened, setOpened }];
    };
    

    *You should use useCallback, useMemo and useRef hooks to prevent unnecessary re renders

Keywords

FAQs

Package last updated on 25 Feb 2021

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