2.0.2 • Published 2 years ago

react-tooltip-z v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Description

It is wrap the tooltip for the element. (Customize tooltip - html/css/complex)

Append the tooltip outside the root. (portal element).


Usage

Install the package

npm install --save react-tooltip-z

Import the module in the place you want to use:

import { Tooltip } from "react-tooltip-z";
// import { Tooltip, useTooltipContext } from "react-tooltip-z";

Snippet

simple
<Tooltip
    // tooltipContent={'This is simple tooltip'}
    tooltipContent={<b>This is jsx<br /> Tooltip</b>} // or Component
    tooltipClassName="my-class"
    placement="right"
    // trigger="click"
    // onClick={() => alert('event active')} // myClick()
>
    <button>this is children</button>
</Tooltip>
simple: trigger=manual (with api)
// here is the sample, please update it exactly with your source

const [sync, setSync] = useState(false)
// fetch api // do something
fetchApi() {
    if (sync) {
        setSync(false)
        return
    }

    fetch('api').then(....).then(rs => {
        setTodo(todo)
        setSync(true)
    })
}

<Tooltip
    tooltipContent={<div>{todo.title}e<br />fds</div>}
    trigger="manual"
    placement='right'
    showSync={sync} // manual simple
>
    <button>Tooltip of todo</button>
</Tooltip>

<button onClick={fetchApi}>Fetch</button>
More complex (with api + useTooltipContext)
// here is the sample, please update it exactly with your source

import { Tooltip, useTooltipContext } from "react-tooltip-z";

// ListData.js
<ul>
    {
        post.map(item => {
            return (
                <Tooltip
                    tagName="li"  // wrap all child
                    trigger="manual"
                    placement="right"
                >
                    <ListDataItem {...} />
                </Tooltip>
            )
        })
    }
</ul>

// ListDataItem.js
// use TooltipContext to update tooltip
const { showTooltip, hideTooltip, isShow, tooltipId} = useTooltipContext()

// do something and show tooltip
fetchApi() {
    showTooltip('tooltip data after fetch.')
}

<div
    onClick={fetchApi}
    onMouseLeave={hideTooltip}
>
    This is list data item index.
</div>

props (mains)

propstypedescription
tagNameStringIf you wrap all children component, use this
tooltipClassNameString
tooltipContentString/Component/ jsxTooltip data
placementtop/right/left/bottomit relies on the element's height, width and position to display.
triggerhover/click/manualDefault hover
backgroundColorStringDefault #282727
tooltipColorStringDefault #ffffff
delayShowNumber (250) minisecondTime delay show tooltip
displayfade/zoomAnimate tooltip
limitWidthBooleanFix max-width: 200px. Default: true.
onShownfunctionEvent after show
onHiddenfunctionEvent after hide
showSyncBooleanSimple trigger = manual
openBooleanShow tooltip
hideIfResizeBooleanhide tooltip if resize screen (Default true)
hideIfScrollBooleanhide tooltip if scroll screen (Default false)
centerArrowBooleanShow arrow and tooltip on center element (Default false)
forceHideanywhen change forceHide, tooltip will be forced hidden (Default null)
...propsotherOther props of child elements

top || right || bottom || left || placement

Note

`...props` here are the props added to the child components. (You should add on child)

`showSync` or `useTooltipContext`: should be used when trigger = `manual`
  • placement: if the position of the tooltip goes beyond the screen. It try will move to the right position to be visible.

RUN

LIVE EXAMPLE

License

MIT