6.0.1 • Published 4 months ago

react-tooltpz v6.0.1

Weekly downloads
133
License
MIT
Repository
github
Last release
4 months ago

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>;
6.0.1

4 months ago

6.0.0

4 months ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.2.0

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.4.0

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago