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-resizer

v0.2.5

Published

![npm Version](https://img.shields.io/npm/v/react-svg-resizer)

Downloads

586

Readme

react-svg-resizer

npm Version

A simple React component for resizing SVG elements with ease.

Installation

pnpm add react-svg-resizer
# or 
yarn add react-svg-resizer

# or (Why?)
npm install react-svg-resizer

Usage

import React from 'react';
import SvgResizer from 'react-svg-resizer';

const MyComponent = () => {
  return (
    <SvgResizer size={30}>
      <svg width="100" height="100">
        <circle cx="50" cy="50" r="40" fill="blue" />
      </svg>
    </SvgResizer>
  );
};

export default MyComponent;

In this example, the SVG element inside SvgResizer will be scaled, so it's minimum axis would be 30 pixels (in this case 30x30 circle).

Means that if a svg shape size 50x100 was given and a size of 1 would be given, the shape would be scaled to 1x2.

Code sandbox demo

here's codesandbox with demo

react-svg-resizer

Props

SvgResizer

| Prop | Type | Default | Description | |--------------|---------------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------| | size | number | 1 | The desired size of the normalized SVG. It scales the SVG by the provided factor. | | scaleByMax | boolean | 'false' | Should the bigger axis should determine the size? by default, the final svg shape the determined relative to the smaller axis | | svgProps | React.SVGProps<SVGSVGElement> | {} | Properties that would be passed down to the underlying svg react wrapper |

This component can be used to wrap svgs element. You most probably want to use this component.

SvgGResizer

| Prop | Type | Default | Description | |----------|-----------|---------|-----------------------------------------------------------------------------------| | size | number | - | The size of the normalized inner SVG. | | center | boolean | false | Determines whether to center the SVG around the origin (0,0). Default is false. |

This component is used to wrap inner SVG elements. For example <SvgGResizer><circle>...</circle></SvgGResizer>

Example

import React from 'react';
import SvgResizer, { SvgGResizer } from 'react-svg-resizer';

function App() {
  return (
    <>
      <h1>SvgResizer</h1>
      <SvgResizerDemo />{" "}
    </>
  );
}

const SvgResizerDemo = () => {
  return (
    <>
      <h2>unmodified svg</h2>
      <svg width="100" height="100">
        <circle cx="50" cy="50" r="40" fill="blue" />
      </svg>

      <h2>scale inner svg to 30</h2>
      <svg width="100" height="100">
        <SvgGResizer size={30} center={false}>
          <circle cx="50" cy="50" r="40" fill="blue" />
        </SvgGResizer>
      </svg>

      <h2>scale entire svg to 30</h2>
      <SvgResizer size={30}>
        <svg width="100" height="100">
          <circle cx="50" cy="50" r="40" fill="blue" />
        </svg>
      </SvgResizer>
    </>
  );
};

export default App;

The result:

image

the original svg shape is 80x80, and the scaled svg shape is 30x30.

How it works

under the hood, SvgResizer keeps track of the dimensions of the children svg by using the getBBox method, and scales the provided svg to the desired size.

License

This project is licensed under the MIT License.