npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-svg-tooltip

v0.0.11

Published

React component to create tooltips for SVG elements

Downloads

8,170

Readme

React SVG Tooltip Build Status GitHub license npm version

A React component to create tooltips for SVG elements.

The library offers a Tooltip component which can be embedded into any SVG element hierarchy. The component does not actually provide a tooltip. Instead, it provides a 0-based coordinate system relative to the current mouse position, so you can place your favorite SVG elements in whichever style suits your needs. Behind the scenes, the library handles all mouse listener logic and makes sure that your tooltip is always rendered on top of all other SVG elements (by using a React portal).

You might want to read this blog post for further details.

Installation

npm install react-svg-tooltip

Note that react and react-dom peer dependencies must already be installed in version 16.3.x or above.

Basic Usage

This example demonstrates how to attach a rectangular tooltip with some text to a circle shape. The triggerRef property accepts a reference to an arbitrary element (a circle in our example), which further serves as the mouse trigger. Note how the x/y-coordinates of the tooltip contents (rect and text) can be expressed relative to the mouse position.

import * as React from 'react';
import { Tooltip } from 'react-svg-tooltip';

const App = () => {

    const circleRef = React.createRef<SVGCircleElement>();

    return (
        <div className='App'>
            <svg viewBox='0 0 100 100'>
                <circle ref={circleRef} cx={50} cy={50} r={10} fill='steelblue'/>
                <Tooltip triggerRef={circleRef}>
                    <rect x={2} y={2} width={10} height={5} rx={.5} ry={.5} fill='black'/>
                    <text x={5} y={5} fontSize={2} fill='white'>Yay!</text>
                </Tooltip>
            </svg>
        </div>
    );
};

export default App;

Edit 4wjoopo1vx

Basic Usage

Advanced Usage

By default, the tooltip is added to the SVG root element which parents the triggering element. This can be limiting in certain scenarios, e.g. if a small SVG triggers a tooltip which is larger than its own SVG root. It is therefore possible to add the tooltip to a different SVG root, making glass-pane-like usages possible. The following example illustrates how 3 small SVG tiles can display tooltips on a shared SVG glass pane. The wiring is achieved through the containerRef property.

import * as React from "react";
import * as ReactDOM from "react-dom";
import { Tooltip } from "react-svg-tooltip";
import "./index.css";

const App = () => {
  const containerRef = React.createRef<SVGSVGElement>();

  const triggerRef1 = React.createRef<SVGSVGElement>();
  const triggerRef2 = React.createRef<SVGSVGElement>();
  const triggerRef3 = React.createRef<SVGSVGElement>();

  return (
    <div className="app">

      <svg ref={containerRef} className="glasspane" viewBox="0 0 100 100" />

      <svg ref={triggerRef1} className="tile" viewBox="0 0 100 100">
        <rect width="100%" height="100%" fill="steelblue" />
        <text x="50%" y="50%">
          SVG 1
        </text>
      </svg>

      <svg ref={triggerRef2} className="tile" viewBox="0 0 100 100">
        <rect width="100%" height="100%" fill="#a2c0d9" />
        <text x="50%" y="50%">
          SVG 2
        </text>
      </svg>

      <svg ref={triggerRef3} className="tile" viewBox="0 0 100 100">
        <rect width="100%" height="100%" fill="#2a4e6c" />
        <text x="50%" y="50%">
          SVG 3
        </text>
      </svg>

      <Tooltip triggerRef={triggerRef1} containerRef={containerRef}>
        <rect x={2} y={2} width={40} height={8} rx={0.5} ry={0.5} fill="black" />
        <text x={22} y={5} fontSize={2} fill="white">
          <tspan>A tooltip triggered by SVG 1</tspan>
          <tspan x={22} dy="1em">
            yet spanning across SVG boundaries
          </tspan>
        </text>
      </Tooltip>

      <Tooltip triggerRef={triggerRef2} containerRef={containerRef}>
        <rect x={-18} y={2} width={40} height={8} rx={0.5} ry={0.5} fill="black" />
        <text x={2} y={5} fontSize={2} fill="white">
          <tspan>A tooltip triggered by SVG 2</tspan>
          <tspan x={2} dy="1em">
            yet spanning across SVG boundaries
          </tspan>
        </text>
      </Tooltip>

      <Tooltip triggerRef={triggerRef3} containerRef={containerRef}>
        <rect x={-38} y={2} width={40} height={8} rx={0.5} ry={0.5} fill="black" />
        <text x={-18} y={5} fontSize={2} fill="white">
          <tspan>A tooltip triggered by SVG 3</tspan>
          <tspan x={-18} dy="1em">
            yet spanning across SVG boundaries
          </tspan>
        </text>
      </Tooltip>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));

Edit w6vqpkv36l

Advanced Usage

Acknowledgements

Based on typescript-library-starter-lite.

License

Licensed under MIT License.

© Rahel Lüthy 2018