A resizable and draggable component for React.
https://codesandbox.io/s/xpm699v4lp
CodeSandbox(with default)
CodeSandbox(with size and position)
CodeSandbox(with typescript)
CodeSandbox(with hooks)
- use npm
npm i -S react-rnd- use yarn
yarn add react-rnd<Rnd
default={{
x: 0,
y: 0,
width: 320,
height: 200,
}}
>
Rnd
</Rnd><Rnd
size={{ width: this.state.width, height: this.state.height }}
position={{ x: this.state.x, y: this.state.y }}
onDragStop={(e, d) => { this.setState({ x: d.x, y: d.y }) }}
onResizeStop={(e, direction, ref, delta, position) => {
this.setState({
width: ref.style.width,
height: ref.style.height,
...position,
});
}}
>
001
</Rnd>The width and height property is used to set the default size of the component.
For example, you can set 300, '300px', 50%.
If omitted, set 'auto'.
The x and y property is used to set the default position of the component.
The size property is used to set size of the component.
For example, you can set 300, '300px', 50%.
Use size if you need to control size state by yourself.
The position property is used to set position of the component.
Use position if you need to control size state by yourself.
see, following example.
<Rnd
size={{ width: this.state.width, height: this.state.height }}
position={{ x: this.state.x, y: this.state.y }}
onDragStop={(e, d) => { this.setState({ x: d.x, y: d.y }) }}
onResize={(e, direction, ref, delta, position) => {
this.setState({
width: ref.offsetWidth,
height: ref.offsetHeight,
...position,
});
}}
>
001
</Rnd>The className property is used to set the custom className of the component.
The style property is used to set the custom style of the component.
The minWidth property is used to set the minimum width of the component.
For example, you can set 300, '300px', 50%.
The minHeight property is used to set the minimum height of the component.
For example, you can set 300, '300px', 50%.
The maxWidth property is used to set the maximum width of the component.
For example, you can set 300, '300px', 50%.
The maxHeight property is used to set the maximum height of the component.
For example, you can set 300, '300px', 50%.
The resizeGrid property is used to specify the increments that resizing should snap to. Defaults to [1, 1].
The dragGrid property is used to specify the increments that moving should snap to. Defaults to [1, 1].
The lockAspectRatio property is used to lock aspect ratio.
Set to true to lock the aspect ratio based on the initial size.
Set to a numeric value to lock a specific aspect ratio (such as 16/9).
If set to numeric, make sure to set initial height/width to values with correct aspect ratio.
If omitted, set false.
The lockAspectRatioExtraWidth property enables a resizable component to maintain an aspect ratio plus extra width.
For instance, a video could be displayed 16:9 with a 50px side bar.
If omitted, set 0.
Specifies the scale of the canvas you are dragging or resizing this element on. This allows
you to, for example, get the correct drag / resize deltas while you are zoomed in or out via
a transform or matrix in the parent of this element.
If omitted, set 1.
The lockAspectRatioExtraHeight property enables a resizable component to maintain an aspect ratio plus extra height.
For instance, a video could be displayed 16:9 with a 50px header bar.
If omitted, set 0.
Specifies a selector to be used as the handle that initiates drag.
Example: handle.
The resizeHandleStyles property is used to override the style of one or more resize handles.
Only the axis you specify will have its handle style replaced.
If you specify a value for right it will completely replace the styles for the right resize handle,
but other handle will still use the default styles.
export type HandleStyles = {
bottom?: React.CSSProperties,
bottomLeft?: React.CSSProperties,
bottomRight?: React.CSSProperties,
left?: React.CSSProperties,
right?: React.CSSProperties,
top?: React.CSSProperties,
topLeft?: React.CSSProperties,
topRight?: React.CSSProperties
}The resizeHandleClasses property is used to set the className of one or more resize handles.
type HandleClasses = {
bottom?: string;
bottomLeft?: string;
bottomRight?: string;
left?: string;
right?: string;
top?: string;
topLeft?: string;
topRight?: string;
}The resizeHandleComponent allows you to pass a custom React component as the resize handle.
type HandleComponent = {
top?: React.ReactElement<any>;
right?: React.ReactElement<any>;
bottom?: React.ReactElement<any>;
left?: React.ReactElement<any>;
topRight?: React.ReactElement<any>;
bottomRight?: React.ReactElement<any>;
bottomLeft?: React.ReactElement<any>;
topLeft?: React.ReactElement<any>;
}The resizeHandleWrapperClass property is used to set css class name of resize handle wrapper(span) element.
The resizeHandleWrapperStyle property is used to set css class name of resize handle wrapper(span) element.
The enableResizing property is used to set the resizable permission of the component.
The permission of top, right, bottom, left, topRight, bottomRight, bottomLeft, topLeft direction resizing.
If omitted, all resizer are enabled.
If you want to permit only right direction resizing, set { top:false, right:true, bottom:false, left:false, topRight:false, bottomRight:false, bottomLeft:false, topLeft:false }.
export type Enable = {
bottom?: boolean;
bottomLeft?: boolean;
bottomRight?: boolean;
left?: boolean;
right?: boolean;
top?: boolean;
topLeft?: boolean;
topRight?: boolean;
} | booleanThe disableDragging property disables dragging completely.
The cancel property disables specifies a selector to be used to prevent drag initialization (e.g. .body).
The direction of allowed movement (dragging) allowed ('x','y','both','none').
Specifies movement boundaries. Accepted values:
parentrestricts movement within the node's offsetParent (nearest node with position relative or absolute)window,body, Selector like.fooClassNameorElement.
By default, we add 'user-select:none' attributes to the document body
to prevent ugly text selection during drag. If this is causing problems
for your app, set this to false.
Specifies the scale of the canvas your are resizing and dragging this element on. This allows
you to, for example, get the correct resize and drag deltas while you are zoomed in or out via
a transform or matrix in the parent of this element.
If omitted, set 1.
RndResizeStartCallback type is below.
export type RndResizeStartCallback = (
e: SyntheticMouseEvent<HTMLDivElement> | SyntheticTouchEvent<HTMLDivElement>,
dir: ResizeDirection,
refToElement: React.ElementRef<'div'>,
) => void;Calls when resizable component resize start.
RndResizeCallback type is below.
export type RndResizeCallback = (
e: MouseEvent | TouchEvent,
dir: ResizeDirection,
refToElement: React.ElementRef<'div'>,
delta: ResizableDelta,
position: Position,
) => void;Calls when resizable component resizing.
RndResizeCallback type is below.
export type RndResizeCallback = (
e: MouseEvent | TouchEvent,
dir: ResizeDirection,
refToElement: React.ElementRef<'div'>,
delta: ResizableDelta,
position: Position,
) => void;Calls when resizable component resize stop.
Callback called on dragging start.
type DraggableData = {
node: HTMLElement,
x: number,
y: number,
deltaX: number, deltaY: number,
lastX: number, lastY: number
};
type DraggableEventHandler = (
e: SyntheticMouseEvent | SyntheticTouchEvent, data: DraggableData,
) => void | false;onDrag called with the following parameters:
type DraggableData = {
node: HTMLElement,
x: number,
y: number,
deltaX: number, deltaY: number,
lastX: number, lastY: number
};
type DraggableEventHandler = (
e: SyntheticMouseEvent | SyntheticTouchEvent, data: DraggableData,
) => void | false;onDragStop called on dragging stop.
type DraggableData = {
node: HTMLElement,
x: number,
y: number,
deltaX: number, deltaY: number,
lastX: number, lastY: number
};
type DraggableEventHandler = (
e: SyntheticMouseEvent | SyntheticTouchEvent, data: DraggableData,
) => void | false;Update component size.
For example, you can set 300, '300px', 50%.
- for example
class YourComponent extends Component {
...
update() {
this.rnd.updateSize({ width: 200, height: 300 });
}
render() {
return (
<Rnd ref={c => { this.rnd = c; }} ...rest >
example
</Rnd>
);
}
...
}Update component position.
grid bounds props is ignored, when this method called.
- for example
class YourComponent extends Component {
...
update() {
this.rnd.updatePosition({ x: 200, y: 300 });
}
render() {
return (
<Rnd ref={c => { this.rnd = c; }} ...rest >
example
</Rnd>
);
}
...
}If set to true, will allow dragging on non left-button clicks.
npm tIf you have a feature request, please add it as an issue or make a pull request.
If you have a bug to report, please reproduce the bug in CodeSandbox to help us easily isolate it.
- Upgrade
re-resizableto6.10.0
-
Fixes $945, When using vite and resizing from other than right and bottom - the element is shaking weirdly.
-
Upgrade
re-resizableto6.9.17 -
Fixes #942, define callback refs inline to work with latest versions of Next.js / React.
- Upgrade
re-resizableto6.9.14
- Fixed a bug,
maxHeightdoes not work with%#914
- Upgrade
re-resizableto6.9.11 - Upgrade
react-draggableto4.4.6 - Fixed a bug, wrong position in
onDrag#910
- Support Element for bounds.
- Upgrade
re-resizableto6.9.6 - Add peer deps.
- Upgrade
re-resizableto6.9.2 - Upgrade
react-draggableto v4.4.4
- Upgrade
re-resizableto6.9.1
- Fixed a bound check with locked aspect ratio (fully fixes #209)
- Fixed a bug, top and left resize issue, caused by "position" #792
- Fixed a callback position when dragAxis specified
- Fixed a glitch when dragAxis is enabled and component is being resized #780
- Fixed a bug, if set minWidth or minHeight with
px, reize dowes not work. #739
- Upgrade
react-draggableto v4.4.3 - Add
allowAnyClickprops. - Add
nodeRefprops.
- Downgrade
react-draggableto v4.2.0 #690
- Update
react-draggableto v4.3.1
- Update
re-resizableto v6.3.2
- A minor fix for a bug with forwarding of cancelling indication of an onDrag event. (#667)
- Fixes #641 without causing other issues with typing.
- Fixed a bug, react-draggable not bundling with rollup #641
- Fixed a bug, box moves when resized #622
- Fixed a bug, position is wrong when onResize #618
- Upgrade re-resizable to 6.1.1
- Upgrade react-draggable to 4.1.0
- Upgrade re-resizable to 6.1.0
- Implement resizeHandleComponent #591
- Update dependency react-draggable to v4
- Fix: Fix #526
- Feat: Add
onMouseUpcallback. - Feat: Use
React.pureComponent
- Chore: Use
re-resizablev5
- Fix: Fixes memory leak #499
- Fix: Add
scaleprops to index.js.flow.
- Feat: Add
scaleprops. #482 - Feat: Upgrade deps.
- Fix: cursor style #469
- update dependency re-resizable to v4.9.3 #444
- fix: resizeHandleWrapperClass warning shown in console #428
- fix: Allow additional props in typescript.
- fix: change
default exporttoexport#405
- fix: fixed a bug,
boundsis ignored when lock aspect ratio set. - feat: add
bodyto bounds props.
- fix: [#221] fixed a bug, maxwidth / height not applied.
- fix: fixed some position and resizing bug.
- fix: [#209] bounds
window. you can check here. - fix: [#317] add onMouseDown. i.e)
<Rnd onMouseDown={...} /> - [BREAKING] fix: [#335] add . to
dragHandleClassNameautomatically, Please pass string (i.ehandle. - [BREAKING] fix: remove
extendsProps. Please add extends props directly. i.e)<Rnd data-foo="42" /> - [BREAKING] fix: remove
zprops. Please addzIndexviastyleprops. i.e)<Rnd style={{ zIndex: 9 }} />
- fix: Upgrade
re-resizableto fix percentage size and bare behavior.
- fix: Fixed a bug, controlled position does not work correctly.
- feat: Use
typescriptinstead offlowype.
- fix: Remove dummy
<div />,isMountedstate andsetParentPosition().
- fix: Add
props,childrento dummy<div>to render children in first.
fix: isMounted and (!this.state.isMounted) return <div /> line #356
- fix: Fixed Array.from error in IE11
- fix: add
enableUserSelectHack?: boolean;.
- chore(deps): upgrade deps
- chore(deps): upgrade lint and remove unused state
- chore(deps): install prettier
- chore(deps): upgrade re-resizable
- Support for cancel feature of react-draggable #206
- Fixed a issue #199 Add enableUserSelectHack props to react-draggable
- Fixed a issue #188 maxWidth and maxHeight props don't respect after resize
- Fixed a bug,
extendPropsis not passed correctly. - Fixed a bug,
boundsis not work correctly. (#162)
- Add internal props.
- Add
size. - Add
position.
- Add
defaultinstead ofx,y,width,height. - Add
resizeHandleWrapperClassandresizeHandleWrapperStyle.
- Remove unnecessary types.
- Use rollup.
- Support % min/max size.
- Change props, remove
defaultand addx,y,width,height. - Rename
dragHandlersXXXXandresizeHandlersXXXXprops todragHandleXXXXandresizeHandleXXXX.
- Fix cursor style, set
normalto cursor style whendragHandlerClassNameis not empty.
- Add position
relativewhen component will update.
- Add
top: 0,left: 0. - Add position
relativewhen parent position equalsstatic.
- Update dependencies(
react-draggable v3,flow-bin v0.53, and other...)
- Fix bug new
zprops is not applied to state.
- Add
extendsProps. #129
- Add
disableDraggingprops.
- Fix flow error.
- Add index.js.flow
- Fix Issue #117.
- Fix
updateZIndex. - Fix
updateSize. - Fix left and top bounds.
- Fix argument events #100
- Fix example
- Update README
- Fix resize bounds.
- Modify API.
- Use original
react-draggable.
- Added
updateZIndex, method to updated componentzIndexstate.
- Pass the new position in the onResizeStop callback #60
- Pass the new position along in the resize callback #55
- Fix style props to applt zIndex chaned.
- Rename
react-rnd. - Remove
canUpdatePositionByParentproperty. - Remove
canUpdateSizeByParentproperty. - Remove
initiAsResizingproperty. - Change
x,y,widthandheightproperty toinitial. - Add
updateSize,updatePosition, method to updated conponent state. - Add
lockAspectRatioproperty to lock aspect ratio when resizing.
- Add
canUpdatePositionByParentproperty.
- Fix bug, resize and grid not work properly.
- Add
gridprops to snap grid. (thanks @paulyoung) - Fix bug, moveAxis not work properly.
- Fix situations when on dragStop you wanted to revert to 0,0 position #39
- Add
canUpdateSizeByParentprops #38
- Add object.assign transform
- Add add module exports plugin for
require
- Bug fix
- Support react v15.x
- Support left, top resizer
- Remove start props, use width, height, x, and y.
- Add handle selector
The MIT License (MIT)
Copyright (c) 2018 bokuweb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.