Convert Figma logo to code with AI

react-grid-layout logoreact-resizable

A simple React component that is resizable with a handle.

2,388
362
2,388
78

Top Related Projects

A draggable and resizable grid layout with responsive breakpoints, for React.

🖱 A resizable and draggable component for React.

A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️

Beautiful and accessible drag and drop for lists with React

Moveable! Draggable! Resizable! Scalable! Rotatable! Warpable! Pinchable! Groupable! Snappable!

Quick Overview

React-Resizable is a simple React component that adds resizable functionality to elements. It's a lightweight and flexible solution for creating resizable components in React applications, often used in conjunction with react-grid-layout for creating dynamic, resizable grid layouts.

Pros

  • Easy to integrate with existing React projects
  • Highly customizable with various props for fine-tuning behavior
  • Supports both controlled and uncontrolled components
  • Lightweight with minimal dependencies

Cons

  • Limited to resizing in two dimensions (width and height)
  • May require additional styling to achieve desired visual effects
  • Documentation could be more comprehensive
  • Occasional issues with touch events on mobile devices

Code Examples

  1. Basic Resizable Component:
import { Resizable } from 'react-resizable';

function ResizableBox() {
  return (
    <Resizable width={200} height={200}>
      <div className="box" style={{width: '100%', height: '100%'}}>
        Resize me!
      </div>
    </Resizable>
  );
}
  1. Resizable with Constraints:
import { Resizable } from 'react-resizable';

function ConstrainedResizable() {
  return (
    <Resizable
      width={300}
      height={200}
      minConstraints={[100, 100]}
      maxConstraints={[500, 300]}
    >
      <div className="box" style={{width: '100%', height: '100%'}}>
        Constrained resizing
      </div>
    </Resizable>
  );
}
  1. Controlled Resizable Component:
import React, { useState } from 'react';
import { Resizable } from 'react-resizable';

function ControlledResizable() {
  const [size, setSize] = useState({ width: 200, height: 200 });

  const onResize = (event, { size }) => {
    setSize({ width: size.width, height: size.height });
  };

  return (
    <Resizable
      width={size.width}
      height={size.height}
      onResize={onResize}
    >
      <div className="box" style={{width: size.width, height: size.height}}>
        Controlled resizing
      </div>
    </Resizable>
  );
}

Getting Started

  1. Install the package:

    npm install react-resizable
    
  2. Import the component and CSS:

    import { Resizable } from 'react-resizable';
    import 'react-resizable/css/styles.css';
    
  3. Use the Resizable component in your React application:

    function App() {
      return (
        <Resizable width={200} height={200}>
          <div>Resizable content</div>
        </Resizable>
      );
    }
    
  4. Customize the component with props as needed, such as minConstraints, maxConstraints, onResize, etc.

Competitor Comparisons

A draggable and resizable grid layout with responsive breakpoints, for React.

Pros of react-grid-layout

  • Provides a complete grid layout system with dragging and resizing capabilities
  • Offers more advanced features like collision detection and automatic packing of elements
  • Supports responsive breakpoints for different screen sizes

Cons of react-grid-layout

  • Larger bundle size due to more comprehensive functionality
  • Steeper learning curve for implementing complex layouts
  • May be overkill for simple resizing needs

Code Comparison

react-grid-layout:

import { Responsive, WidthProvider } from 'react-grid-layout';
const ResponsiveGridLayout = WidthProvider(Responsive);

<ResponsiveGridLayout
  className="layout"
  layouts={layouts}
  breakpoints={{ lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }}
  cols={{ lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }}
>
  {/* Grid items */}
</ResponsiveGridLayout>

react-resizable:

import { Resizable } from 'react-resizable';

<Resizable
  width={200}
  height={200}
  onResize={this.onResize}
  draggableOpts={{ grid: [25, 25] }}
>
  <div className="box">Contents</div>
</Resizable>

react-grid-layout offers a more comprehensive solution for creating complex, responsive grid layouts with dragging and resizing capabilities. react-resizable, on the other hand, focuses solely on resizing functionality, making it lighter and easier to implement for simpler use cases. Choose react-grid-layout for full-featured grid systems, and react-resizable for basic resizing needs.

🖱 A resizable and draggable component for React.

Pros of react-rnd

  • Supports both resizing and dragging in a single component
  • Offers more customization options for handles and boundaries
  • Provides touch device support out of the box

Cons of react-rnd

  • May have a steeper learning curve due to more configuration options
  • Less focused on grid-based layouts compared to react-resizable

Code Comparison

react-rnd:

<Rnd
  size={{ width: 200, height: 200 }}
  position={{ x: 0, y: 0 }}
  onDragStop={(e, d) => { console.log(d.x, d.y); }}
  onResizeStop={(e, direction, ref, delta, position) => {
    console.log(ref.style.width, ref.style.height);
  }}
>
  Resizable and draggable content
</Rnd>

react-resizable:

<Resizable
  width={200}
  height={200}
  onResizeStop={(e, data) => {
    console.log(data.size.width, data.size.height);
  }}
>
  <div>Resizable content</div>
</Resizable>

The code comparison shows that react-rnd combines resizing and dragging functionality in a single component, while react-resizable focuses solely on resizing. react-rnd also provides more granular control over positioning and size, making it more versatile for complex layouts. However, react-resizable's simpler API may be preferable for projects that only require basic resizing functionality.

A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️

Pros of react-sortable-hoc

  • More flexible and customizable for complex sorting scenarios
  • Supports both horizontal and vertical sorting out of the box
  • Provides better performance for large lists due to virtualization support

Cons of react-sortable-hoc

  • Steeper learning curve compared to react-resizable
  • Less intuitive for simple grid-based layouts
  • Requires more setup code for basic functionality

Code Comparison

react-sortable-hoc:

import { SortableContainer, SortableElement } from 'react-sortable-hoc';

const SortableItem = SortableElement(({value}) => <li>{value}</li>);
const SortableList = SortableContainer(({items}) => {
  return (
    <ul>
      {items.map((value, index) => (
        <SortableItem key={`item-${index}`} index={index} value={value} />
      ))}
    </ul>
  );
});

react-resizable:

import { Resizable } from 'react-resizable';

const ResizableBox = () => (
  <Resizable width={200} height={200}>
    <div className="box" style={{width: '100%', height: '100%'}}>
      Resizable content
    </div>
  </Resizable>
);

Beautiful and accessible drag and drop for lists with React

Pros of react-beautiful-dnd

  • Highly accessible, with built-in keyboard and screen reader support
  • Smooth animations and intuitive drag and drop experience
  • Extensive documentation and examples for easy implementation

Cons of react-beautiful-dnd

  • Limited to vertical lists and horizontal lists, not suitable for grid layouts
  • Less flexibility in terms of item resizing and positioning
  • Larger bundle size compared to more lightweight alternatives

Code Comparison

react-beautiful-dnd:

<DragDropContext onDragEnd={onDragEnd}>
  <Droppable droppableId="list">
    {(provided) => (
      <ul {...provided.droppableProps} ref={provided.innerRef}>
        {items.map((item, index) => (
          <Draggable key={item.id} draggableId={item.id} index={index}>
            {(provided) => (
              <li ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps}>
                {item.content}
              </li>
            )}
          </Draggable>
        ))}
        {provided.placeholder}
      </ul>
    )}
  </Droppable>
</DragDropContext>

react-resizable:

<Resizable
  width={200}
  height={200}
  onResize={onResize}
  draggableOpts={{ grid: [25, 25] }}
>
  <div className="box">
    Resizable Content
  </div>
</Resizable>

Moveable! Draggable! Resizable! Scalable! Rotatable! Warpable! Pinchable! Groupable! Snappable!

Pros of Moveable

  • More versatile: Supports resizing, rotating, scaling, and dragging
  • Framework-agnostic: Works with vanilla JavaScript, React, Vue, and more
  • Extensive customization options and event handling

Cons of Moveable

  • Steeper learning curve due to more features and options
  • Potentially heavier bundle size for simple resize-only use cases

Code Comparison

Moveable (React):

import Moveable from "react-moveable";

<Moveable
  target={document.querySelector(".target")}
  draggable={true}
  resizable={true}
  rotatable={true}
  onDrag={({ target, transform }) => {
    target.style.transform = transform;
  }}
/>

React-Resizable:

import { Resizable } from 'react-resizable';

<Resizable width={200} height={200} onResize={this.onResize}>
  <div className="box">
    Contents
  </div>
</Resizable>

Key Differences

  • Moveable offers a wider range of transformation options
  • React-Resizable is more focused on resizing functionality
  • Moveable uses a target-based approach, while React-Resizable wraps content
  • React-Resizable is specifically designed for React, while Moveable is more flexible

Use Cases

  • Choose Moveable for complex, multi-transformation requirements
  • Opt for React-Resizable for simpler, resize-focused React applications

Community and Maintenance

  • Both projects are actively maintained
  • Moveable has a larger community and more frequent updates
  • React-Resizable has a longer history in the React ecosystem

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React-Resizable

View the Demo

A simple widget that can be resized via one or more handles.

You can either use the <Resizable> element directly, or use the much simpler <ResizableBox> element.

See the example and associated code in ExampleLayout and ResizableBox for more details.

Make sure you use the associated styles in /css/styles.css, as without them, you will have problems with handle placement and visibility.

You can pass options directly to the underlying DraggableCore instance by using the prop draggableOpts. See the demo for more on this.

Installation

$ npm install --save react-resizable

Compatibility

React-Resizable 3.x is compatible with React >= 16.3. React-Resizable 2.x has been skipped. React-Resizable 1.x is compatible with React 14-17.

Usage

This package has two major exports:

  • <Resizable>: A raw component that does not have state. Use as a building block for larger components, by listening to its callbacks and setting its props.
  • <ResizableBox>: A simple <div {...props} /> element that manages basic state. Convenient for simple use-cases.

<Resizable>

const {Resizable} = require('react-resizable');

// ES6
import { Resizable } from 'react-resizable';

// ...
class Example extends React.Component {
  state = {
    width: 200,
    height: 200,
  };

  // On top layout
  onResize = (event, {node, size, handle}) => {
    this.setState({width: size.width, height: size.height});
  };

  render() {
    return (
      <Resizable height={this.state.height} width={this.state.width} onResize={this.onResize}>
        <div className="box" style={{width: this.state.width + 'px', height: this.state.height + 'px'}}>
          <span>Contents</span>
        </div>
      </Resizable>
    );
  }
}

<ResizableBox>

const {ResizableBox} = require('react-resizable');

// ES6
import { ResizableBox } from 'react-resizable';

class Example extends React.Component {
  render() {
    return (
      <ResizableBox width={200} height={200} draggableOpts={{grid: [25, 25]}}
          minConstraints={[100, 100]} maxConstraints={[300, 300]}>
        <span>Contents</span>
      </ResizableBox>
    );
  }
}

Props

These props apply to both <Resizable> and <ResizableBox>. Unknown props that are not in the list below will be passed to the child component.

type ResizeCallbackData = {
  node: HTMLElement,
  size: {width: number, height: number},
  handle: ResizeHandleAxis
};
type ResizeHandleAxis = 's' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne';

type ResizableProps =
{
  children: React.Element<any>,
  width: number,
  height: number,
  // Either a ReactElement to be used as handle, or a function returning an element that is fed the handle's location as its first argument.
  handle: ReactElement<any> | (resizeHandle: ResizeHandleAxis, ref: ReactRef<HTMLElement>) => ReactElement<any>,
  // If you change this, be sure to update your css
  handleSize: [number, number] = [10, 10],
  lockAspectRatio: boolean = false,
  axis: 'both' | 'x' | 'y' | 'none' = 'both',
  minConstraints: [number, number] = [10, 10],
  maxConstraints: [number, number] = [Infinity, Infinity],
  onResizeStop?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
  onResizeStart?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
  onResize?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
  draggableOpts?: ?Object,
  resizeHandles?: ?Array<ResizeHandleAxis> = ['se']
};

The following props can also be used on <ResizableBox>:

{
  style?: Object // styles the returned <div />
}

If a width or height is passed to <ResizableBox>'s style prop, it will be ignored as it is required for internal function.

Resize Handle

If you override the resize handle, we expect that any ref passed to your new handle with represent the underlying DOM element.

This is required, as react-resizable must be able to access the underlying DOM node to attach handlers and measure position deltas.

There are a few ways to do this:

Native DOM Element

This requires no special treatment.

<Resizable handle={<div className="foo" />} />
Custom React Component

You must forward the ref and props to the underlying DOM element.

Class Components
class MyHandleComponent extends React.Component {
  render() {
    const {handleAxis, innerRef, ...props} = this.props;
    return <div ref={innerRef} className={`foo handle-${handleAxis}`} {...props} />
  }
}
const MyHandle = React.forwardRef((props, ref) => <MyHandleComponent innerRef={ref} {...props} />);

<Resizable handle={<MyHandle />} />
Functional Components
const MyHandle = React.forwardRef((props, ref) => {
  const {handleAxis, ...restProps} = props;
  return <div ref={ref} className={`foo handle-${handleAxis}`} {...restProps} />;
});

<Resizable handle={<MyHandle />} />
Custom Function

You can define a function as a handle, which will simply receive an axis (see above ResizeHandleAxis type) and ref. This may be more clear to read, depending on your coding style.

const MyHandle = (props) => {
  return <div ref={props.innerRef} className="foo" {...props} />;
};

<Resizable handle={(handleAxis, ref) => <MyHandle innerRef={ref} className={`foo handle-${handleAxis}`} {...props} />} />

NPM DownloadsLast 30 Days