Open-source project
bokuweb/react-rnd avatar
bokuweb/react-rnd

react-rnd can hold size and position in your state through controlled props

🖱 A resizable and draggable component for React.

4,328 stars327 forksTypeScriptMIT

At a glance

What is it?
react-rnd is a MIT licensed React component for dragging and resizing a box. It takes either a `default` size and position or controlled `size` and `position` props that the caller updates from drag and resize callbacks.
Who is it for?
Adopt react-rnd when you need one movable box inside a parent you control and want either an uncontrolled initial size and position or controlled values managed by your application. Its grid, aspect ratio and handle styling props are enough to build a small canvas-like editor without a drag and drop system.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Activity is slowing. The repository last received commits 6 months ago.
What is it written in?
Mainly TypeScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • bokuweb/react-rnd4,328 ★ · +3
Star history
Repository2026-09-232026-11-25
bokuweb/react-rnd4.3k4.3k

3 Stars in the last 63 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The component is a controlled box, not a source of truth

react-rnd can run from a `default` object, or controlled, where position and size belong to the caller and the component reports what the user did.

The shortest working form passes a single object:

javascript
<Rnd
  default={{
    x: 0,
    y: 0,
    width: 320,
    height: 200,
  }}
>
  Rnd
</Rnd>

With that object the values are read once for the initial render. From that point the box can still be dragged and resized; use callbacks when your application needs the updated values. A user drags the box and nothing re-renders anywhere else, which suits a demo page or the Storybook at the package homepage, and is the wrong choice the moment the box has to be persisted or validated.

npm or yarn, then a first drag you can see

Installation is a single package with no peer tooling to configure. The README offers two package managers and nothing else:

sh
npm i -S react-rnd
sh
yarn add react-rnd

The package publishes compiled output rather than source you must transpile: main points at an ES5 bundle, module and jsnext:main point at a modern bundle, and types point at a declaration file, so a TypeScript project picks the types up with no extra configuration. The repository ships an examples directory with a vite project, a Storybook setup started on port 6016 through the storybook script, and a published storybook build.

For a first run, render the default example above and drag it. Then replace the default object with size and position props and add the two stop handlers.

Stop callbacks write numbers for drags and strings for resizes

The controlled pattern puts the write-back inside two handlers. The resize handler receives the element reference and the new position, and reads size off the DOM node rather than out of a value you computed:

javascript
<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>

Read the two handlers against each other, because they do not agree. Drag stops hand you computed numbers for x and y. Resize stops hand you whatever the browser put on the node, which is a CSS string carrying a px suffix, and the sample code deliberately reads the style rather than the numeric offset. Both land in the same state keys. Change one handler to its live counterpart and the shape of that state value shifts under your arithmetic.

dragGrid and resizeGrid turn free movement into snapped steps

Movement that ignores pixel boundaries feels wrong in a canvas editor, so react-rnd rounds both axes. The drag grid sets the increments that moving snaps to, the resize grid sets the same for resizing, and both default to pixel steps of one, which is free movement with rounding to whole pixels.

Set the drag grid to a pair of tens and the box stops on ten pixel multiples. Set the resize grid to a pair of fifties and every handle lands on a fifty pixel step.

A CSS transform on the parent silently breaks drag arithmetic

Put the component inside a parent that is zoomed with a CSS transform and every delta divides wrong. The pointer moves fewer screen pixels than the box moves in layout space, so a ten pixel drag on screen arrives at the handler as a different number. The scale prop exists to correct that, and it defaults to one.

You compute that value from whatever produced the transform: a zoom slider, a pinch handler, a canvas library. With any zoom above the box, this is the first prop to get wrong and the hardest to notice, because the symptom looks like sluggish or overshooting dragging rather than an error.

A numeric aspect ratio moves the frame, a boolean one does not

Aspect ratio locking takes either a boolean or a number. As a boolean it pins the ratio to whatever the initial size happened to be, so a first render of 320 by 200 locks the box to 8:5 for its lifetime. As a number it targets a specific ratio such as 16 over 9.

The numeric form carries a prerequisite the documentation states plainly: set the initial width and height to values that already satisfy the ratio. The two extra props, for extra width and extra height, both default to zero, and they cover layout cases a bare ratio cannot express, such as a video at 16:9 with a 50px side bar or header bar. They are separate props rather than one box model, so a layout needing both has to set both.

Eight handles can be restyled one axis at a time

Handles are individually addressable, which is what makes the component fit a design system. Resize handle styles take a partial object keyed top, right, bottom, left, topLeft, topRight, bottomLeft and bottomRight, each value a set of React CSS properties. A classes prop takes the same eight keys as strings, and a third prop lets you substitute a whole React element per key.

Overrides apply per axis, not globally. Setting right replaces the styles on the right handle and leaves the other seven at their defaults, so a partially themed set looks inconsistent until every key you care about is filled in. A drag handle class name does the equivalent job for dragging, except it takes a single selector string rather than a keyed object.

updateSize and updatePosition write past the render cycle

The README's table of contents lists an instance API with two methods: updateSize, taking a width and height as number or string, and updatePosition, taking an x and a y.

Editorial conclusion

Adopt react-rnd when you need one movable box inside a parent you control and want either an uncontrolled initial size and position or controlled values managed by your application. Its grid, aspect ratio and handle styling props are enough to build a small canvas-like editor without a drag and drop system. Skip it for sortable lists, drop targets and anything crossing window boundaries, since it positions one element rather than relaying data between elements. For a uniformly scaled parent, test drag and resize coordinates and supply the matching `scale` value when needed. Before you commit, drag a child inside a parent carrying a CSS transform and confirm that scale matches the transform, then check that your resize handler is prepared for size arriving as a CSS string such as px rather than a number.

Frequently asked questions

what is react rnd

react-rnd is an MIT licensed React component described in its own package.json as a draggable and resizable React Component. Size and position come from props you control, and the drag and resize handlers report changes back to your state.

react rnd vs react dnd

react-rnd moves and resizes one box that you position yourself. React DnD handles drag and drop between elements, such as reordering lists or dropping into targets, which react-rnd does not attempt.

How do I snap react-rnd dragging to a grid?

Pass dragGrid with the increments you want, and dragging snaps to those multiples. resizeGrid does the same for the resize handles, and both default to pixel steps of one.

How do I keep a fixed aspect ratio while resizing?

Set lockAspectRatio to a number such as 16/9 for a 16:9 box, and make sure the initial width and height already match that ratio. Setting it to true instead locks the ratio taken from the initial size, and the extra width and extra height props add space outside the ratio.

Does react-rnd ship TypeScript types and Flow definitions?

Yes. package.json points types at the declaration file in lib, the repository is written in TypeScript, and a copy script puts a Flow definition file into lib for the older bundle as well.

Official sources

  1. bokuweb/react-rnd on GitHub
  2. License: MIT
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/bokuweb-react-rnd.svg)](https://hysenlabs.com/projects/bokuweb-react-rnd)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/bokuweb-react-rnd.svg)](https://hysenlabs.com/projects/bokuweb-react-rnd)

Was this useful?