Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

re-resizable

bokuweb2.3mMIT6.10.3TypeScript support: included

Resizable component for React.

react, resize, resizable, component

readme

📏 A resizable component for React.

Build Status Build Status

Table of Contents

Screenshot

screenshot

Live Demo

Storybook

Storybook

CodeSandbox

Edit xp9p7272m4
CodeSandbox
CodeSandbox(TypeScript)
CodeSandbox(With hooks)

Install

$ npm install --save re-resizable

Usage

Example with defaultSize

import { Resizable } from 're-resizable';

<Resizable
  defaultSize={{
    width: 320,
    height: 200,
  }}
>
  Sample with default size
</Resizable>

If you only want to set the width, you can do so by providing just the width property. The height property will automatically be set to auto, which means it will adjust 100% of its parent's height:

import { Resizable } from 're-resizable';

<Resizable
  defaultSize={{
    width: 320
  }}
>
  Sample with default size
</Resizable>

Example with size

If you use size props, please manage state by yourself.

import { Resizable } from 're-resizable';

<Resizable
  size={{ width: this.state.width, height: this.state.height }}
  onResizeStop={(e, direction, ref, d) => {
    this.setState({
      width: this.state.width + d.width,
      height: this.state.height + d.height,
    });
  }}
>
  Sample with size
</Resizable>

Props

defaultSize?: { width?: (number | string), height?: (number | string) };

Specifies the width and height that the dragged item should start at. For example, you can set 300, '300px', 50%. If both defaultSize and size omitted, set 'auto'.

defaultSize will be ignored when size set.

size?: { width?: (number | string), height?: (number | string) };

The size property is used to set the size of the component. For example, you can set 300, '300px', 50%.

Use size if you need to control size state by yourself.

className?: string;

The className property is used to set the custom className of a resizable component.

style?: { [key: string]: string };

The style property is used to set the custom style of a resizable component.

minWidth?: number | string;

The minWidth property is used to set the minimum width of a resizable component. Defaults to 10px.

It accepts viewport as well as parent relative units. For example, you can set 300, 50%, 50vw or 50vh.

Same type of values can be applied to minHeight, maxWidth and maxHeight.

minHeight?: number | string;

The minHeight property is used to set the minimum height of a resizable component. Defaults to 10px.

maxWidth?: number | string;

The maxWidth property is used to set the maximum width of a resizable component.

maxHeight?: number | string;

The maxHeight property is used to set the maximum height of a resizable component.

grid?: [number, number];

The grid property is used to specify the increments that resizing should snap to. Defaults to [1, 1].

gridGap?: [number, number];

The gridGap property is used to specify any gaps between your grid cells that should be accounted for when resizing. Defaults to [0, 0]. The value provided for each axis will always add the grid gap amount times grid cells spanned minus one.

snap?: { x?: Array<number>, y?: Array<number> };

The snap property is used to specify absolute pixel values that resizing should snap to. x and y are both optional, allowing you to only include the axis you want to define. Defaults to null.

snapGap?: number

The snapGap property is used to specify the minimum gap required in order to move to the next snapping target. Defaults to 0 which means that snap targets are always used.

resizeRatio?: number | [number, number];

The resizeRatio property is used to set the number of pixels the resizable component scales by compared to the number of pixels the mouse/touch moves. Defaults to 1 (for a 1:1 ratio). The number set is the left side of the ratio, 2 will give a 2:1 ratio.

For [number, number] means [resizeRatioX, resizeRatioY], more precise control.

lockAspectRatio?: boolean | number;

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.

lockAspectRatioExtraWidth?: number;

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.

lockAspectRatioExtraHeight?: number;

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.

bounds?: ('window' | 'parent' | HTMLElement);

Specifies resize boundaries.

boundsByDirection?: boolean;

By default max dimensions based on left and top element position. Width grow to right side, height grow to bottom side. Set true for detect max dimensions by direction. For example: enable boundsByDirection when resizable component stick on right side of screen and you want resize by left handler;

false by default.

handleStyles?: HandleStyles;

The handleStyles 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.

handleClasses?: HandleClassName;

The handleClasses property is used to set the className of one or more resize handles.

handleComponent?: HandleComponent;

The handleComponent property is used to pass a React Component to be rendered as one or more resize handle. For example, this could be used to use an arrow icon as a handle..

handleWrapperStyle?: { [key: string]: string };

The handleWrapperStyle property is used to override the style of resize handles wrapper.

handleWrapperClass?: string;

The handleWrapperClass property is used to override the className of resize handles wrapper.

enable?: ?Enable | false;

The enable property is used to set the resizable permission of a resizable 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 }.

onResizeStart?: ResizeStartCallBack;

ResizeStartCallBack type is below.

type ResizeStartCallback = (
  e: SyntheticMouseEvent<HTMLDivElement> | SyntheticTouchEvent<HTMLDivElement>,
  dir: ResizableDirection,
  refToElement: HTMLDivElement,
) => void;

Calls when resizable component resize start.

onResize?: ResizeCallback;

scale?: number;

The scale property is used in the scenario where the resizable element is a descendent of an element using css scaling (e.g. - transform: scale(0.5)).

as?: string | React.ComponentType;

By default the Resizable component will render a div as a wrapper. The as property is used to change the element used.

Basic

ResizeCallback type is below.

type ResizeCallback = (
  event: MouseEvent | TouchEvent,
  direction: ResizableDirection,
  refToElement: HTMLDivElement,
  delta: NumberSize,
) => void;

Calls when resizable component resizing.

onResizeStop?: ResizeCallback;

ResizeCallback type is below.

type ResizeCallback = (
  event: MouseEvent | TouchEvent,
  direction: ResizableDirection,
  refToElement: HTMLDivElement,
  delta: NumberSize,
) => void;

Calls when resizable component resize stop.

Instance API

* updateSize(size: { width: number | string, height: number | string }): void

Update component size.

grid, snap, max/minWidth, max/minHeight props is ignored, when this method called.

  • for example
class YourComponent extends Component {

  // ...

  update() {
    this.resizable.updateSize({ width: 200, height: 300 });
  }

  render() {
    return (
      <Resizable ref={c => { this.resizable = c; }}>
        example
      </Resizable>
    );
  }

  // ...
}

Contribute

If 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.

Test

npm test

changelog

Changelog

6.10.1 (2024-11-06)

:bug: Bug Fix

  • Revert #827 because regression

6.10.0 (2024-09-22)

:nail_care: Enhancement

  • Match focus order to visual order #827

6.9.18 (2024-09-10)

:nail_care: Enhancement

  • added grid gap prop to support adding any gaps to width/height. Fixes #822

:bug: Bug Fix

  • fix-boundary-scale: adds scaling to boundary Fixes #820

[6.9.17 (2024-05-25)]

  • Define callback refs inline to work with latest versions of Next.js / React #819

6.9.16 (2024-04-25)

:nail_care: Enhancement

  • Fixes resizable field always being null in React 18.3.

6.9.14 (2024-04-21)

:bug: Bug Fix

  • Fixed a bug, onResize fired before snapping to grid #783

6.9.11 (2023-08-10)

:nail_care: Enhancement

  • improve enable type.

6.9.9 (2022-04-26)

:nail_care: Enhancement

  • use native endsWith.
  • remove fast-memoize.

6.9.8 (2022-04-22)

:nail_care: Enhancement

  • use flushSync in mouseMove.

6.9.6 (2022-04-22)

:nail_care: Enhancement

  • add react and react-dom to peer deps.

6.9.5 (2022-03-14)

:bug: Bug Fix: Fixed a bug, calculate parent height even when the parent is a flex container #765

6.9.2 (2022-02-24)

:bug: Bug Fix: Fixed a bug, lockAspectRatio is not work when snap is set. #759

6.9.1 (2021-09-14)

:nail_care: Enhancement

  • Fixed a issue, using CTRL when resizing doesn't work #747

6.8.0 (2021-11-05)

:nail_care: Enhancement

  • Feature: boundsByDirection #689

6.6.1 (2020-09-22)

:bug: Bug Fix

  • Fixed a bug, resize is not work when set xxxpx to max/min width/height

6.6.0 (2020-09-22)

:bug: Bug Fix

  • Fixed a bug, a base element is removed even though there are other resizable components. (#667)

:nail_care: Enhancement

  • Expose NumberSize.

6.5.5 (2020-08-28)

:nail_care: Enhancement

  • fix: instanceof check fails when window is a proxy (#659)

6.5.4 (2020-07-13)

:bug: Bug Fix

  • Fixed a bug, when touched in mobile some execption throwed.

6.5.2 (2020-06-26)

:bug: Bug Fix

  • Fixes #522 - Resize without page scrolling on mobile

6.5.1 (2020-06-25)

:bug: Bug Fix

  • Make as optional

6.5.0 (2020-06-17)

:bug: Bug Fix

  • Fix ES Module Output #634

6.4.0 (2020-05-14)

:nail_care: Enhancement

  • Support the "as" prop to change the wrapper #614

6.3.2 (2020-03-28)

:nail_care: Enhancement

  • Avoid a useless re-render #587

6.3.1 (2020-03-28)

:nail_care: Enhancement

  • Makes the component window agnostic, which means that the component can be run inside an iframe. (#598)

6.2.0 (2020-02-05)

:bug: Bug Fix

  • Fixed a bug, resizing does not work when flex-basis set.

6.1.1 (2019-11-30)

:bug: Bug Fix

  • Fixed a bug, Handle loses mouse as edge gets further away from other side #537

6.1.0 (2019-09-28)

:nail_care: Enhancement

  • Improve perf #529
  • Support vh and vw for max size #526

6.0.0 (2019-08-12)

:nail_care: Enhancement

  • Fix deprecated componentWillRecieveProps lifecycle method usage #504
  • Feature request: Allow early exiting for onResizeStart #494

:zap: Breaking changes

  • use PureComponent

5.0.0 (2019-06-05)

Please see also 5.0.0-beta.0 change.

:nail_care: Enhancement

  • Add snapGap property #446

:house: Internal

  • Upgrade some deps.

5.0.0-beta.0 (2019-03-17)

:nail_care: Enhancement

  • Use typeScript instead of flowtype in [#413]
  • Improve some perf.
  • Support vw and vh. Please see story.

:zap: Breaking changes

  • Support only named import. Please import like following.
import { Resizable } from 're-resizable';

:memo: Documentation

:house: Internal

  • Update react & react-dom to v16.7.0 (#395)

4.11.0 (2018-12-14)

:rocket: New Feature

:house: Internal

  • Update npm-run-all to v4.1.5 (#389)
  • Update react & react-dom to v16.6.3 (#387)
  • Update sinon to v7.2.2 (#393)
  • Update rollup-plugin-node-resolve to v4.0.0 (#392)
  • Update flow-bin to v0.89.0 (#385)
  • Update prettier to v1.15.3 (#386)

4.10.0 (2018-11-16)

:rocket: New Feature

:house: Internal

  • Update react & react-dom to v16.6.1 (#384)
  • Update prettier to v1.15.1 (#383)
  • Update sinon to v7.1.1 (#379)
  • Update flow-bin to v0.85.0 (#378)
  • Update eslint-plugin-flowtype to v3.2.0 (#375)
  • Update rollup-plugin-node-globals to v1.4.0 (#344)

4.9.3 (2018-11-06)

:bug: Bug Fix

:house: Internal

  • Update sinon to v7.1.0 (#373)
  • Update react & react-dom to v16.6.0 (#371)
  • Update gh-pages to v2.0.1 (#352)
  • Update flow-bin to v0.84.0 (#342)

4.9.2 (2018-10-26)

:bug: Bug Fix

:house: Internal

  • Update eslint-plugin-jsx-a11y to v6.1.2 (#363)
  • Update react & react-dom to v16.5.2 (#357)
  • Update rollup-plugin-commonjs to v9.2.0 (#356)
  • Update @​storybook/addon-info & @​storybook/react to v3.4.11 (#355)

4.9.1 (2018-10-21)

:bug: Bug Fix

:nail_care: Enhancement

:memo: Documentation

:house: Internal

  • Update rollup to v0.65.2 (#347)
  • Update react & react-dom to v16.5.1 (#350)
  • Update sinon to v7.0.0 (#368)
  • Update eslint-plugin-flowtype to v3.0.0 (#367)
  • Update rollup-plugin-replace to v2.1.0 (#365)
  • Update rollup-plugin-replace to v10.0.1 (#360)
  • Update prettier to v1.14.3 (#359)

4.9.0 (2018-10-13)

:rocket: New Feature

:memo: Documentation

:house: Internal

  • Update rollup to v0.65.0 (#339)
  • Update rollup-plugin-commonjs to v9.1.6 (#338)
  • Update react & react-dom to v16.5.0 (#348)
  • Update sinon to v6.3.1 (#345)

4.8.1 (2018-08-24)

:bug: Bug Fix

:memo: Documentation

:house: Internal

  • Update flow-bin to v0.79.1 (#336)
  • Update sinon to v6.1.5 (#327)
  • Update rollup-plugin-babel to v3.0.7 (#305)
  • Update rollup to v0.64.1 (#296)

4.8.0 (2018-08-23)

:rocket: New Feature

:memo: Documentation

:house: Internal

  • Update prettier to v1.14.2 (#311, #312 & #329)
  • Update flow-bin to v0.78.0 (#298, #320, #326 & #332)
  • Update @​storybook/addon-info & @​storybook/react to v3.4.10 (#297 & #331)
  • Update flow-copy-source to v2.0.2 (#313 & #324)
  • Update sinon to v6.1.3 (#309, #314 & #317)
  • Update eslint-plugin-react to v7.11.1 (#310, #334 & #335)
  • Update eslint-plugin-import to v2.14.0 (#308 & #333)
  • Update prettier-eslint to v8.8.2 (#301)
  • Update eslint-plugin-jsx-a11y to v6.1.1 (#315 & #323)
  • Update babel-eslint to v8.2.6 (#302 & #322)
  • Update flow-typed to v2.5.1 (#318)
  • Update eslint-plugin-flowtype to v2.50.0 (#321)
  • Update react & react-dom to v16.4.2 (#330)
  • Update rollup-plugin-commonjs to v9.1.5 (#328)

4.7.1 (2018-06-24)

:bug: Bug Fix

4.7.0 (2018-06-24)

:bug: Bug Fix

4.6.1 (2018-06-23)

:bug: Bug Fix

4.6.0 (2018-06-23)

Note: this release has a critical issue and was deprecated. Please update to 4.6.1 or higher.

:bug: Bug Fix

4.5.2 (2018-06-23)

Note: this release has a critical issue and was deprecated. Please update to 4.6.1 or higher.

:bug: Bug Fix

:house: Internal

  • Update rollup to v0.61.0 (#290 & #295)
  • Update @​storybook/addon-info & @​storybook/react to v3.4.7 (#288)
  • Update prettier to v1.13.5 (#285)
  • Update sinon to v6.0.0 (#289)
  • Update flow-copy-source to v2.0.0 (#280)
  • Update eslint-plugin-react to v7.9.1 (#279)
  • Update avaron to v0.2.0 (#300)

4.5.1 (2018-06-19)

:bug: Bug Fix

:house: Internal

  • Update react & react-dom to v16.4.1 (#291)

4.5.0 (2018-06-19)

:bug: Bug Fix

:house: Internal

  • Drop Node 6/7 support in CI (@bokuweb in 1b6480cf)
  • Update flow-bin to v0.74.0 (#284)
  • Update sinon to v5.1.0 (#282)
  • Update rollup to v0.60.1 (#281)

4.4.10 (2018-06-07)

:bug: Bug Fix

4.4.9 (2018-06-07)

:bug: Bug Fix

:memo: Documentation

:house: Internal

4.4.8 (2018-03-27)

:bug: Bug Fix

:house: Internal

  • Update sinon to v4.4.9 (#221)

4.4.7 (2018-03-26)

:bug: Bug Fix

:memo: Documentation

:house: Internal

  • Update eslint to v4.19.1 (#217)
  • Update sinon to v4.4.8 (#216)

4.4.6 (2018-03-21)

:bug: Bug Fix

:house: Internal

  • Update rollup to v0.57.1 (#211)
  • Update rollup-plugin-node-resolve to v3.3.0 (#212)

v4.4.5

  • chore: upgrade flow-bin

v4.4.4

  • fix: base finder
  • fix: add mouse leave

v4.4.3

  • fix: fix type issues in index.d.ts.

v4.4.2

  • fix: fixed bug where base could not be found

v4.4.1

  • fix: add guard to avoid error without parent

v4.4.0

  • fix: bug behavior with flex layout
  • chore: refactor
  • chore: update deps
  • chore: update d.ts
  • chore: add some stories

v4.3.2

  • Fixed a bug, when resizing sometimes causes text-selection in some browser #182

v4.3.1

  • Fixed a bug, auto overwritten by px value #179

v4.3.0

  • Allow 0 as minWidth and minHeight #178

v4.2.0

  • Add a option for passing custom handle components #170

v4.1.2

  • Fixed a bug, Text select while resizing in IE11 #166

v4.1.1

  • Fixed a bug, Element width id="__resizable0" breaks my layout #162

v4.1.0

  • Additional height and width with lockAspectRatio #163

v4.0.3

  • Use ES5-compatible prototype methods #160

v4.0.2

  • Fix using right click on resize #152
  • Add workaround when base Node not found.

v4.0.1

  • Update index.d.ts, Fixes #153

v4.0.0

  • Remove width and height.
  • Add defaultSize and size,

v3.0.0

  • Fix flowtype annotation.
  • Remove extendsProps.

You can add extendsProps as follows.

<Resizable data-foo="foo" />

v3.0.0-beta.3

  • fix typo. ResizeStartCallBack -> ResizeStartCallback.

v3.0.0-beta.2

  • export ResizeDirection type.
  • rename Callback to ResizeCallback.

v3.0.0-beta.1

  • Fix flow filename.
  • Change logo

v3.0.0-beta.0

  • Change package name, react-resizable-box -> re-resizable.
  • Add handleWrapperStyle and handleWrapperClass props.
  • Change behavior that is set percentage size to width or height as props.
  • Support percentage max/min size.
  • Use rollup.
  • Fix props name.
    • handersClasses -> handleClasses
    • handersStyles -> handleStyles

v2.1.0

  • Remove shouldUpdateComponent (#135).
  • Remove lodash.isEqual.

v2.0.6

  • Update README.

v2.0.5

  • Fix remove event listener

v2.0.4

  • Fix receiveProps. (related #85)

v2.0.3

  • Update dev dependencies.
  • Modify index.js.flow.

v2.0.2

  • Remove offset state.
  • Use border-box.
  • Fix boundary size.

v2.0.1

  • Add offset state for rnd component.

v2.0.0

  • Update index.js.flow

v2.0.0-rc.2

  • Use flowtype.
  • Change callback args.
  • Change some props name.
    • isResizable => enable.
    • customClass => className.
    • customStyle => style.
    • handleStyle => handlerStyles.
    • handleClass => handlerClasses.
  • Add bounds feature.
  • Fix min/max size checker when aspect ratio locked.

v1.8.4

  • Fix cursor

v1.8.3

  • Fix npm readme

v1.8.2

  • Add index.d.ts.
  • Fix resize glitch when aspct ratio locked.

v1.8.1

  • Fixing issue on resizing with touch events

v1.8.0

  • Add extendsProps prop to other props (e.g. data-*, aria-*, and other ).

v1.7.0

  • Support siver side rendering #43

v1.6.0

  • Add updateSize method.

v1.5.1

  • Add lockAspectRatio property.

v1.4.3

  • Avoid unnecessary rendering on resizer

v1.4.2

  • Fix onTouchStart bind timing to avoid re-rendering

v1.4.1

  • Support preserving auto size #40 (thanks @noradaiko)

v1.4.0

  • Add grid props to snap grid. (thanks @paulyoung)

v1.3.0

  • Add userSelect: none when resize get srated.
  • Add shouldComponentUpdate.
  • Add handle custom className.

v1.2.0

  • Add module export plugin for require.

v1.1.3

  • Update document.

v1.1.2

  • Add size argument to resizeStart callback.
  • Fix bug

v1.1.1

  • Fix delta value bug

v1.1.0

  • Add delta argument to onResize and onResizeStop callback.

v1.0.0

  • Rename and add resizer.

v0.4.2

  • Support react v15
  • ESLint run when push

v0.4.1

  • Add mousedown event object to onResizeStart callback argument.

v0.4.0

  • Support 'px' and '%' for width and height props.