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-component-to-image

v1.1.1

Published

Capture React components as images with PNG, JPEG, SVG support. Based on react-to-image.

Downloads

11

Readme

image

NPM Version Size Language License

A simple component that captures React components as images with ease, supporting PNG, JPEG, SVG, and more. Built on top of react-to-image

[!TIP] The banner above was generated with this component. See example :)

Installation

To install react-component-to-image, use npm or yarn:

npm install react-component-to-image # or pnpm install react-component-to-image

Usage

Import the RenderImage component and pass your desired component as the component prop:

import React from 'react';
import { RenderImage } from 'react-component-to-image';

function MyComponent() {
  return (
    <div>
      <h1>My Component</h1>
      <p>This is the content of my component.</p>
    </div>
  );
}

function App() {
  return (
    <div>
      <RenderImage
        component={<MyComponent />}
        method='toPng'
        buttonText='Capture as PNG'
        onSuccess={(data) => console.log('PNG data:', data)}
        onError={(error) => console.error('Error:', error)}
      />
    </div>
  );
}

export default App;

Props

The RenderImage component accepts the following props:

| Prop | Type | Default | Description | | --------------- | --------------------------------------------------------------------------- | ----------- | ---------------------------------------------------------- | | component | React.ReactNode | (required) | The React component to capture as an image. | | method | 'toBlob' \| 'toCanvas' \| 'toJpeg' \| 'toPixelData' \| 'toPng' \| 'toSvg' | 'toPng' | Conversion method for capturing the component as an image. | | buttonText | string | 'Convert' | Text displayed on the conversion button. | | buttonClassName | string | '' | CSS class name for the conversion button. | | onSuccess | (data: any) => void | undefined | Callback function on successful image conversion. | | onError | (error: string) => void | undefined | Callback function on conversion error. | | buttonComponent | React.ReactNode | undefined | Custom button component to replace the default button. |

Conversion Methods

react-component-to-image supports the following conversion methods:

| Method | Description | | ------------- | ------------------------------------------------- | | toBlob | Converts the component to a Blob object. | | toCanvas | Converts the component to an HTML canvas element. | | toJpeg | Converts the component to a JPEG image. | | toPixelData | Converts the component to pixel data. | | toPng | Converts the component to a PNG image. | | toSvg | Converts the component to an SVG image. |

Custom Button Component

By default, it uses the button component for the conversion button. However, you can optionally provide your own custom button component using the buttonComponent prop.

Your custom button component will receive the following props:

  • onClick: The function to be called when the button is clicked.
  • disabled: A boolean value indicating whether the button should be disabled.

Here's an example of using a custom button component:

import React from 'react';
import { RenderImage } from 'react-component-to-image';

function MyButton({ onClick, disabled }) {
  return (
    <button onClick={onClick} disabled={disabled}>
      Custom Button
    </button>
  );
}

function App() {
  return (
    <div>
      <RenderImage
        component={<MyComponent />}
        method='toJpeg'
        onSuccess={(data) => console.log('JPEG data:', data)}
        onError={(error) => console.error('Error:', error)}
        buttonComponent={<MyButton />}
      />
    </div>
  );
}

export default App;

Examples

Here are a few examples of how you can use react-component-to-image in your React application:

Capturing a Component as a PNG Image

import React from 'react';
import { RenderImage } from 'react-component-to-image';

function MyComponent() {
  return (
    <div>
      <h1>My Component</h1>
      <p>This is the content of my component.</p>
    </div>
  );
}

function App() {
  return (
    <div>
      <RenderImage
        component={<MyComponent />}
        method='toPng'
        buttonText='Capture as PNG'
        onSuccess={(data) => console.log('PNG data:', data)}
        onError={(error) => console.error('Error:', error)}
      />
    </div>
  );
}

export default App;

Capturing a Component as a JPEG Image

import React from 'react';
import { RenderImage } from 'react-component-to-image';

function MyComponent() {
  return (
    <div>
      <h1>My Component</h1>
      <p>This is the content of my component.</p>
    </div>
  );
}

function App() {
  return (
    <div>
      <RenderImage
        component={<MyComponent />}
        method='toJpeg'
        buttonText='Capture as JPEG'
        onSuccess={(data) => console.log('JPEG data:', data)}
        onError={(error) => console.error('Error:', error)}
      />
    </div>
  );
}

export default App;

Capturing a Component as an SVG Image

import React from 'react';
import { RenderImage } from 'react-component-to-image';

function MyComponent() {
  return (
    <div>
      <h1>My Component</h1>
      <p>This is the content of my component.</p>
    </div>
  );
}

function App() {
  return (
    <div>
      <RenderImage
        component={<MyComponent />}
        method='toSvg'
        buttonText='Capture as SVG'
        onSuccess={(data) => console.log('SVG data:', data)}
        onError={(error) => console.error('Error:', error)}
      />
    </div>
  );
}

export default App;

Contributing

[!NOTE] I am not a perfect developer. Hence, there is more room for improvement! If you find errors or have suggestions, please open an issue or submit a pull request. Thanks!

License

[!IMPORTANT] react-component-to-image is open-source software licensed under the MIT License © Aidre Cabrera.


That's all! 👀 @aidrecabrera. Hope this works better for you!