Install react-rnd
masterYou can install react-rnd using npm or yarn to add resizable and draggable functionality to your React components.
npm i -S react-rndyarn add react-rndrepository·master·Indexed 26 days ago
https://github.com/bokuweb/react-rndA draggable and resizable React component (version 10.5.3) that allows elements to be moved and resized within a container or window. It supports both controlled and uncontrolled modes, grid snapping, boundary constraints, aspect ratio locking, and custom resize handles. The library provides a comprehensive API for managing position and size via props or an instance API, along with lifecycle callbacks for drag and resize events.
You can install react-rnd using npm or yarn to add resizable and draggable functionality to your React components.
npm i -S react-rndyarn add react-rndFor production applications, it is recommended to enable type-aware lint rules in your ESLint configuration.
parserOptions to include your tsconfig files:plugin:@typescript-eslint/recommended with plugin:@typescript-eslint/recommended-type-checked or plugin:@typescript-eslint/strict-type-checked.plugin:@typescript-eslint/stylistic-type-checked.eslint-plugin-react and add plugin:react/recommended and plugin:react/jsx-runtime to the extends list.export default {
// other rules...
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: __dirname,
},
}allowAnyClick prop to true, the component will allow dragging to be initiated by non-left-button clicks.Use the default prop to set the initial position (x, y) and size (width, height) of the component. This is useful for uncontrolled components where you don't need to manage the state manually.
<Rnd
default={{
x: 0,
y: 0,
width: 320,
height: 200,
}}
>
Rnd
</Rnd>To fully control the component's state, use the position and size props. You must update your state via the onDragStop and onResizeStop (or onResize) callbacks to keep the component in sync with your application state.
<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 Rnd component provides several callbacks to track the resizing lifecycle:
onResizeStart: Triggered when the resizing process begins. Receives the event, the direction of resizing, and the element reference.onResize: Triggered continuously during the resizing process. Receives the event, direction, element reference, the delta (change in size), and the new position.onResizeStop: Triggered when the resizing process ends. Receives the same parameters as onResize.// Example callback signatures
type RndResizeStartCallback = (
e: SyntheticMouseEvent<HTMLDivElement> | SyntheticTouchEvent<HTMLDivElement>,
dir: ResizeDirection,
refToElement: React.ElementRef<'div'>,
) => void;
type RndResizeCallback = (
e: MouseEvent | TouchEvent,
dir: ResizeDirection,
refToElement: React.ElementRef<'div'>,
delta: ResizableDelta,
position: Position,
) => void;You can programmatically control the Rnd component by accessing its instance via a ref. This allows you to update the component's size or position from outside the component's own internal state updates.
updateSize({ width, height }): Updates the component size. Accepts numbers (pixels) or strings (e.g., '300px', '50%').updatePosition({ x, y }): Updates the component position. Note: When using this method, the grid and bounds props are ignored for that specific update.class YourComponent extends Component {
update() {
// Update size
this.rnd.updateSize({ width: 200, height: 300 });
// Update position
this.rnd.updatePosition({ x: 200, y: 300 });
}
render() {
return (
<Rnd ref={c => { this.rnd = c; }} ...rest >
example
</Rnd>
);
}
}The Rnd component provides callbacks to track dragging activity:
onDragStart: Triggered when dragging begins. Receives the event and a data object containing the node, current x/y coordinates, deltaX/deltaY, and lastX/lastY.onDrag: Triggered continuously during dragging. Receives the same parameters as onDragStart.onDragStop: Triggered when dragging ends. Receives the same parameters as onDragStart.Note: Returning false from these handlers can prevent the drag action.
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;The Rnd component accepts several props to control its behavior, appearance, and constraints:
default: { x: number; y: number; width?: number | string; height?: number | string; }. Sets initial position and size.size: { width: (number | string), height: (number | string) }. Controls the component size (use for controlled components).position: { x: number, y: number }. Controls the component position (use for controlled components).minWidth / minHeight: Minimum dimensions.maxWidth / maxHeight: Maximum dimensions.resizeGrid: [number, number]. Increments for resizing snapping (default: [1, 1]).dragGrid: [number, number]. Increments for dragging snapping (default: [1, 1]).bounds: string | Element. Restricts movement (e.g., 'parent', 'window', 'body', or a CSS selector).lockAspectRatio: boolean | number. Locks aspect ratio. true uses initial size; a number (e.g., 16/9) locks a specific ratio.lockAspectRatioExtraWidth / lockAspectRatioExtraHeight: Maintains aspect ratio plus extra dimensions (e.g., for video players with sidebars/headers).scale: number. Adjusts for canvas scaling (e.g., when the parent is zoomed via CSS transforms). Defaults to 1.disableDragging: boolean. Disables dragging.dragAxis: 'x' | 'y' | 'both' | 'none'. Allowed movement direction.dragHandleClassName: string. CSS selector for the element that acts as the drag handle.cancel: string. CSS selector to prevent drag initialization.enableResizing: boolean | Enable. Controls which directions can be resized. Enable is an object specifying boolean permissions for top, right, bottom, left, topRight, bottomRight, bottomLeft, topLeft.className: string. Custom class name.style: { [key: string]: string }. Custom inline styles.resizeHandleStyles: HandleStyles. Overrides styles for specific handles (bottom, bottomLeft, etc.).resizeHandleClasses: HandleClasses. Sets class names for specific handles.resizeHandleComponent: HandleComponent. Pass custom React elements for specific handles.resizeHandleWrapperClass: string. Class for the handle wrapper (span).resizeHandleWrapperStyle: Style. Style for the handle wrapper (span).enableUserSelectHack: boolean. Prevents text selection during drag. Set to false if it interferes with your app.Rnd component provides a resizable and draggable container. You can use it in a controlled mode by providing position and size props, or in an uncontrolled mode using the default prop. It supports constraints like bounds, minWidth, minHeight, maxWidth, and maxHeight.Use the following callbacks to respond to resize events.
onResizeStart: Triggered when resizing begins. Returning false from this callback will cancel the resize.onResize: Triggered during the resize operation. Receives the event, direction, element reference, delta (width/height change), and the new position.onResizeStop: Triggered when resizing ends.onDrag and onDragStop callbacks receive DraggableData which includes the node, delta, and current position.