Git Product home page Git Product logo

react-svg-tooltip's Introduction

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

react-svg-tooltip's People

Contributors

netzwerg avatar timothycoy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-svg-tooltip's Issues

Support provision of alternative SVG root element to serve as tooltip container

In some special scenarios, the tooltip might be bigger than the containing SVG, leading to ugly truncations.

As a workaround, a glasspane-like SVG spanning the complete browser window can be used as a tooltip container.

An optional containerRef property accepts such an alternative SVG root element, toggling its visibility property from hidden to visible on mouse hovers.

Two tooltips possible?

When adding a second tooltip I got this error:
[Function components cannot have refs.]

How to provide styling to tooltip.

For my use case I have to provide tooltip on an svg element at corner of the screen. The tooltip overflows from the screen which is weird. How to prevent this
image

Doesn't work in Safari?

First let me say this library has been super helpful, it does one thing well and was very easy to add to my charts.

The only thing I've noticed is that it doesn't seem to work correctly in Safari (macOS). The tooltips appear way off to the side, and then don't appear anymore. Is that a known bug? Or could it be related to my own CSS maybe?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.