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

  • 6.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React Tooltpz

NPM version NPM license NPM total downloads NPM monthly downloads

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

💬 Flexible Tooltip Components with zero dependencies

  • Automatically find best position
  • With Portal
  • No extra DOM nodes
  • Tiny

Try demo

Basic Usage:

import { useState } from 'react';
import { Tooltip } from 'react-tooltpz';

type Props = {
    title: string;
    tooltip: string;
};

const TitleWithHoverTooltip = ({
  title,
  tooltip,
}: Props) => {
  const [opened, setOpened] = useState(false);
  const ref = useRef<HTMLDivElement | null>(null);

  return (
    <div
      ref={ref}
      onMouseEnter={() => setOpened(true)}
      onMouseLeave={() => setOpened(false)}
    >
      {title}
      {opened && (
        <Tooltip parentRef={ref}>
          {({ ref, style }) => (
            <div ref={ref} style={style}>
              {tooltip}
            </div>
          )}
        </Tooltip>
      )}
    </div>
  );
};

Installation:

npm install --save react-tooltpz

Importing:

import { Tooltip } from 'react-tooltpz';

Advanced usage

Using parent width

import { useState } from 'react';
import { Tooltip } from 'react-tooltpz';

type Props = Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange'> & {
    options?: string[];
    onChange?: (nextValue: string) => void;
};

const Autocomplete = ({
  ...inputProps,
  options,
  onChange,
}: Props) => {
  const [opened, setOpened] = useState(false);
  const [focused, setFocused] = useState(false);
  const ref = useRef<HTMLInputElement | null>(null);

  return (
    <>
      <input
        {...inputProps}
        ref={ref}
        onChange={(event) =>
          onChange(event.target.value)
        }
        onFocus={() => setFocused(true)}
        onBlur={() => setFocused(false)}
      />
      {focused && !!options?.length && (
        <Tooltip parentRef={ref}>
          {({ ref, style }, { parentRect }) => (
            <div
              ref={ref}
              style={{
                ...style,
                width: parentRect?.width,
              }}
            >
              {options.map((option) => (
                <div
                  onClick={() => onChange(option)}
                >
                  {option}
                </div>
              ))}
            </div>
          )}
        </Tooltip>
      )}
    </>
  );
};

API

Tooltip

Props

type Props = {
  parentRef: MutableRefObject<AnyWithGetBoundingClientRect / null>;
  innerRef?: MutableRefObject<AnyWithGetBoundingClientRect / null>;
  margin?: number;
  position?: Position;
  align?: Align;
  allowedPositions?: Position[];
  children?: (
          props: { ref: MutableRefObject<AnyWithGetBoundingClientRect / null>; style: CSSProperties },
          additionalData?: { parentRect: Rect / null; tooltipRect: Rect / null },
  ) => ReactNode;
  style?: CSSProperties;
  zIndex?: number;
  portalNode?: HTMLElement;
  withParentRect?: boolean;
  withTooltipRect?: boolean;
};
nametypedefaultdescription
parentRefMutableRefObject<AnyWithGetBoundingClientRect / null>requiredTooltip ref object.
It can be any object with current prop.
current prop should be null or any object with getBoundingClientRect method
innerRefMutableRefObject<AnyWithGetBoundingClientRect / null>-Tooltip ref object.
Similar to parentRef
marginnumber4Margin between parent and tooltip
position'bottom' / 'top' / 'left' / 'right''bottom'Tooltip position
align'start' / 'center' / 'end''start'Tooltip align
allowedPositions('bottom' / 'top' / 'left' / 'right')[][]Tooltip allowed positions array. Empty value means all positions are allowed.
children(props: { ref: MutableRefObject<AnyWithGetBoundingClientRect / null>; style: CSSProperties },additionalData?: { parentRect: Rect / null; tooltipRect: Rect / null }) => ReactNode-Tooltip render function
styleCSSProperties-Tooltip style object
zIndexnumber0Tooltip default z-index
portalNodeHTMLElement-second parameter for ReadDOM.createPortal
withParentRectboolean-If true parentRect prop of second parameter of children will be updated
withTooltipRectboolean-If true tooltipRect prop of second parameter of children will be updated

PortalNodeContext

provide portalNode to Tooltip

export const PortalNodeContext: React.Context<HTMLElement | null>;

ZIndexContext

provide zIndex to Tooltip

export const ZIndexContext: React.Context<number>;

Keywords

FAQs

Package last updated on 17 Dec 2023

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