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 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.
$ npm install --save react-resizable
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
.
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.
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, {element, 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>
);
}
}
const {ResizableBox} = require('react-resizable');
// ES6
import { ResizableBox } from 'react-resizable';
class Example extends React.Component {
render() {
return (
<ResizableBox width={200} height={200} draggableOpts={{...}}
minConstraints={[100, 100]} maxConstraints={[300, 300]}>
<span>Contents</span>
</ResizableBox>
);
}
}
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<'s' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne'> = ['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.