Package Exports
- react-tooltpz
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-tooltpz) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
React Tooltpz
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
name type default description innerRef object null Parent refobjecttooltip function (react hook) required Tooltip openedlogicchildren ({ innerRef, ...rest }, { opened, setOpened, ...tooltipRest }) => jsx null Parent render function Tooltip
Props
name type default description innerRef object null Tooltip refobjectparentRef object null Parent refobjectzIndex number 0 Tooltip default z-indexmargin number 4 Margin between parent and tooltip position one of [bottom, top, left, right] bottom Tooltip position align one of [start, center, end] start Tooltip align children ({ innerRef, style, ...rest }, { parentSize, tooltipSize, setOpened } ) => jsx null Tooltip render function style object null Tooltip stylesetOpened (opened) => void null Tooltip openedchange function
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
Write your own logic hook
API
const [ parentProps, { opened, setOpened, ...tooltipProps } ] = useMyOwnLogicHook({ parentRef, tooltipRef });
Accepts:
objectwithparentRefandtooltipRefpropsReturns:
arraywithparentPropsandtooltipPropsitems*
tooltipPropsrequiredopenedandsetOpenedpropsUsage
const useSimpleClickTooltip = () => { const [opened, setOpened] = useState(false); const onClick = useCallback(() => { setOpened((value) => !value); }); return [{ onClick }, { opened, setOpened }]; };
*You should use
useCallback,useMemoanduseRefhooks to prevent unnecessary re renders