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-use-resizable

v0.2.0

Published

A react hook for creating resizable elements

Downloads

652

Readme

React Resizable Hook

A lightweight (1.3kB gzipped!), hooks-based, easy-to-use alternative to react-resizable.

Check out the demo

Installation

npm i react-use-resizable

How to use

The hook returns an object with the following properties:

  • rootRef: A ref to the root element of the resizable component
  • getRootProps: A function that returns the props to be spread on the root element
  • getHandleProps: A function that returns the props to be spread on the handle element

The hooks behavior can be customized by passing an options object to the hook. The options object can have the following properties: | Property | Type | Description | Optional | Default | | --- | --- | --- | --- | --- | | maxHeight | number | The maximum height of the component | Yes | Infinity | | maxWidth | number | The maximum width of the component | Yes | Infinity | | minHeight | number | The minimum height of the component | Yes | 0 | | minWidth | number | The minimum width of the component | Yes | 0 | | lockHorizontal | boolean | Whether the component should be horizontally resizable | Yes | false | | lockVertical | boolean | Whether the component should be vertically resizable | Yes | false | | onResize | function | A callback function that is called when the component is resized | Yes | () => {} | | onDragEnd | function | A callback function that is called when the component is done resizing | Yes | () => {} | | onDragStart | function | A callback function that is called when the component starts resizing | Yes | () => {} | | disabled | boolean | Whether the component should be resizable | Yes | false | | interval | number | The interval at which the resize will occur (i.e. which sizes the component will snap to) | Yes | 1 | | initialHeight | number | The initial height of the component | Yes | 100 | | initialWidth | number | The initial width of the component | Yes | 100 | | maintainAspectRatio | boolean | Whether the component should maintain its aspect ratio (ratio between initialHeight and initialWidth) when resizing | Yes | false |

Most of these properties can be overridden for each handle by passing passing an options object to the getHandleProps function. The options object can have the following properties: | Property | Type | Description | Optional | Default | | --- | --- | --- | --- | --- | | maxHeight | number | The maximum height of the component | Yes | Infinity | | maxWidth | number | The maximum width of the component | Yes | Infinity | | minHeight | number | The minimum height of the component | Yes | 0 | | minWidth | number | The minimum width of the component | Yes | 0 | | lockHorizontal | boolean | Whether the component should be horizontally resizable | Yes | false | | lockVertical | boolean | Whether the component should be vertically resizable | Yes | false | | onResize | function | A callback function that is called when the component is resized | Yes | () => {} | | onDragEnd | function | A callback function that is called when the component is done resizing | Yes | () => {} | | onDragStart | function | A callback function that is called when the component starts resizing | Yes | () => {} | | disabled | boolean | Whether the component should be resizable | Yes | false | | interval | number | The interval at which the resize will occur (i.e. which sizes the component will snap to) | Yes | 1 | | maintainAspectRatio | boolean | Whether the component should maintain its aspect ratio (ratio between initialHeight and initialWidth) when resizing | Yes | false | | parent | RefObject<HTMLElement> | A ref to the parent element of the handle | Yes | null | | reverse | boolean | Whether the handle should be reversed | Yes | false |

Passing the same property to both the hook and the getHandleProps function will override the hook's property with the getHandleProps property.

The onResize, onDragEnd and onDragStart functions will be invoked with an object containing the following properties: | Property | Type | Description | | --- | --- | --- | newHeight | number | The new height of the component | heightDiff | number | The difference in height between the previous and the new height | newWidth | number | The new width of the component | widthDiff | number | The difference in width between the previous and the new width |

Example

A simple example of how to use the hook:

import React from 'react';
import { useResizable } from 'react-use-resizable';

const FreeMoving = () => {
  const { getRootProps, getHandleProps } = useResizable({
    initialWidth: 150,
    initialHeight: 150
  });

  return (
    <div>
      <div
        className="bg-blue-500 relative rounded flex justify-center items-center"
        {...getRootProps()}
      >
        <div className="text-white text-center">Free moving</div>

        <div
          className="bg-blue-700 absolute bottom-0 right-0 p-2 rounded-tl-lg rounded-br text-white"
          {...getHandleProps()}
        >
          Handle
        </div>
      </div>
    </div>
  );
};

Many more examples can be found in the demo.