What is react-resizable?
The react-resizable package provides a set of React components that can be used to create resizable elements. It is particularly useful for building user interfaces where elements need to be resized by the user, such as in dashboards, forms, or any interactive layout.
What are react-resizable's main functionalities?
Resizable Component
The ResizableBox component allows you to create a resizable element. You can specify constraints such as minimum and maximum width and height.
import React from 'react';
import { ResizableBox } from 'react-resizable';
const ResizableComponent = () => (
<ResizableBox width={200} height={200} minConstraints={[100, 100]} maxConstraints={[300, 300]}>
<div style={{ width: '100%', height: '100%', border: '1px solid black' }}>
Resizable Content
</div>
</ResizableBox>
);
export default ResizableComponent;
Resizable Handle
The ResizableBox component can also accept a custom handle for resizing. This allows for more control over the appearance and behavior of the resize handle.
import React from 'react';
import { ResizableBox } from 'react-resizable';
const ResizableHandleComponent = () => (
<ResizableBox
width={200}
height={200}
minConstraints={[100, 100]}
maxConstraints={[300, 300]}
handle={<span className="custom-handle" />}
handleSize={[8, 8]}
>
<div style={{ width: '100%', height: '100%', border: '1px solid black' }}>
Resizable Content with Custom Handle
</div>
</ResizableBox>
);
export default ResizableHandleComponent;
Other packages similar to react-resizable
react-rnd
The react-rnd package provides resizable and draggable components. It offers more flexibility by combining both resizing and dragging functionalities in one package. Compared to react-resizable, react-rnd is more feature-rich and allows for more complex interactions.
react-grid-layout
The react-grid-layout package is a grid layout system for React that allows for both resizing and dragging of grid items. It is particularly useful for creating complex, responsive grid layouts. While react-resizable focuses solely on resizing, react-grid-layout provides a more comprehensive solution for grid-based layouts.
re-resizable
The re-resizable package is another alternative for creating resizable components in React. It offers a similar API to react-resizable but includes additional features such as support for resizing in all directions and more customization options. It is a good alternative if you need more control over the resizing behavior.
React-Resizable
View the Demo
A simple widget that can be resized via a handle.
You can either use the <Resizable>
element directly, or use the much simpler <ResizableBox>
element.
See the example and associated code in TestLayout 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
Using npm:
$ npm install --save react-resizable
Usage
const Resizable = require('react-resizable').Resizable;
const ResizableBox = require('react-resizable').ResizableBox;
import { Resizable, ResizableBox } from 'react-resizable';
render() {
return (
<ResizableBox width={200} height={200} draggableOpts={{...}}
minConstraints={[100, 100]} maxConstraints={[300, 300]}>
<span>Contents</span>
</ResizableBox>
);
}
Props
These props apply to both <Resizable>
and <ResizableBox>
.
{
children: React.Element<any>,
width: number,
height: number,
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
};