GithubHelp home page GithubHelp logo

react-download-svg's Introduction

react-download-svg

Use react-download-svg to decorate an SVG component/element, and when triggered, will download a serialized PNG at a desired size.

Features

  • ...

Example with Trigger helper

<Wrapper> listens to an event from <Trigger>, meaning you can import the trigger elsewhere in your application, no need to have them directly connected. Unique event names can be used if you need to avoid collisions.

import {
  Wrapper,
  Trigger
} from 'react-download-svg';

const SomeComponent = React.createComponent({

  render: function () {
    return (
      <div>
        <Wrapper>
          <SomeSVGThing />
        </Wrapper>
        <div>
          <Trigger>Click to download</Trigger>
        </div>
      </div>
    );
  }
});

Example with refs

To initiate a download without using the <Trigger>, you can hit the startDownload() method directly if you're using a ref.

import {
  Wrapper
} from 'react-download-svg';

const SomeComponent = React.createComponent({

  handleClick: function () {
    ReactDOM.findDOMNode(this.wrapper).startDownload({
      width: 400,
      height: 400,
      filename: 'somefile.png'
    });
  },

  render: function () {
    return (
      <div>
        <Wrapper ref={n => this.wrapper = n}>
          <SomeSVGThing />
        </Wrapper>
        <div>
          <button onClick={this.handleClick}>Click to download</button>
        </div>
      </div>
    );
  }
});

Props

Wrapper

prop default
filename String: 'untitled.png' Sets the filename on the downloaded file.
listenFor String: 'downloadSvg' Sets the event name to listen for from the <Trigger>. Must be the same as set on desired <Trigger> instance.

Wrapper

prop default
filename String: 'untitled.png' Sets the filename on the downloaded file. This takes priority over the filename on the <Wrapper> if one is set there as well.
eventName String: 'downloadSvg' Sets the event name to send to <Wrapper>. Must be the same as set on desired <Wrapper> instance.
width Number: 400 The desired width of the png
height Number: 400 The desired height of the png
component 'button' Defaults to a button instance, otherwise use a valid element type or a react component.

react-download-svg's People

Contributors

derrickpelletier avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

react-download-svg's Issues

Support for newer version of React

Hi @derrickpelletier ,

i would like to use your lib for my project, but i failed to install it with the newer version of React

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR!   react@"^18.0.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^0.14 || ^15.0.0-rc || ^15.0" from [email protected]
npm ERR! node_modules/react-download-svg
npm ERR!   react-download-svg@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!

Parse incoming width and height

Incoming width and height (to the trigger) needs to be parsed to an Int because it has the potential to break child components that the w/h get injected into.

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.