Giter VIP home page Giter VIP logo

re-resizable's Introduction

πŸ“ A resizable component for React.

Build Status 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].

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

Related

re-resizable's People

Contributors

aduth avatar aiellochan avatar amccloud avatar andrejunges avatar ashubham avatar biallo avatar binhpv avatar bokuweb avatar brentmulligan avatar craftzdog avatar davidmason avatar davidspiess avatar dependabot[bot] avatar ellatrix avatar greenkeeper[bot] avatar haakemon avatar haraldox avatar hypnosphi avatar liorbentov avatar mcneeladesign avatar michaeldeboey avatar mull avatar paulyoung avatar pionl avatar renovate-bot avatar renovate[bot] avatar therebelrobot avatar travisdahl avatar wootencl avatar youknowriad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

re-resizable's Issues

handle icon

Hey,

I'm using the newest version of your library and would like to include an icon into a handle. I could not find an option to do that.

Best,
Jan

shouldComponentUpdate optimization prevents certain children from updating

Overview of the problem

Resizable is preventing a child component from updating when its prop, which is an object, has a property change.

I'm using react-resizable-box 2.0.6

I am sure this issue is not a duplicate?

Description

I've made the props more generic for simplicity

I'm using <Resizable /> around a function that decides if a simple or complex version of a component should be used. The simple version is used most of the time, and only accepts a single data object prop. That data prop at a minimum contains a value property.

  • When the function is wrapped in <Resizable /> and the data.value is changed upstream, the simple version of the component does not re-render.
  • Without the function being wrapped in <Resizable />, when the data.value changes, the simple version of the component re-renders correctly.
  • When I directly render the simple version of the component as a child of <Resizable />(instead of choosing it in a function), the component updates correctly when the data.value changes upstream.
  • When I pass value directly (instead of data) as a prop to the simple version generated from a function wrapped in <Resizable />, it re-renders correctly when data.value changes upstream.

Theory

I was able to recreate the issue using a simple wrapping component that uses the same optimization in shouldComponentUpdate as <Resizable />:

import React from 'react'
import isEqual from 'lodash/isEqual'

export default class ExampleBlockingWrapper extends React.Component {
  shouldComponentUpdate (nextProps, nextState) {
    return (
      !isEqual(this.props, nextProps) ||
      !isEqual(this.state, nextState)
    )
  }

  render () {
    return (
      <div>
        { this.props.children }
      </div>
    )
  }
}

This leads me to believe that this is an issue with lodash's isEqual being unable to detect deep/complex differences between this.props.children and nextProps.children.

Proposed Solution

Sure, I can change my code to work within the constraints of this library's optimization, but I don't think that's a good long-term solution. A component that wraps around children is different than a parent component that renders subcomponents. Parent components can be optimized with your !isEqual strategy because only the props & state impact the subcomponents. Wrapper components accept the children as a prop, which can make detecting equality very complex.

I found this answer to this question about optimizing wrappers helpful in understanding what was going on.

With React 16 on the horizon, I propose that we remove the optimization in Resizable's shouldComponentUpdate in favor of allowing React to decide if a wrapper should allow its children to update.

Text select while resizing in IE11

I'm using re-resizable 4.1.1

Bug appears in IE11.
I can't provide reproduced project on webpackbin, cos webpackbin doesn't work in my IE11.

When I drag resize handle, the text around became seleted sometimes.
In my case, this bug appears with complext layout, when animation framerate slows down.

I solved that by adding "-ms-user-select: none; user-select: none;" to handle style;
"user-select: none" just for safity.

resize bug on ie11

when mouse down right corner, the resizable box width and height changed
before i drag

can you fix this?

Using <Resizable/> inside <Resizable/>

Is it possible to use an Resizable component inside another Resizable component?

I tried to make the parent element with the ability to change its height and the child element with the possibility of changing its width. In this case, the width of the child element does not work correctly. At the first change of width nothing happens, the subsequent changes always lag behind one change.

working demo https://codesandbox.io/s/3lnZ2rPXr

how can I change the drag bar's height

I am using the version 4.1.2,and I used it in the left tree,and the tree's height can change when the tree opened or closed,but the drag bar's height not changed,in this case I can't resize it at the bottom of the tree

Incorrect width logic in componentWillReceiveProps

The current logic in componentWillReceiveProps doesn't look correct: newWidth is taken from this.props.width instead of the newly provided props.

It causes lag when controlling the width manually via props, which I'm currently doing to make corrections if the browser window is resized to not to fit the whole resizable container: https://github.com/airdcpp-web/airdcpp-webui/blob/master/src/routes/Sidebar/components/Sidebar.jsx#L62 (http://webdemo.airdcpp.net)

TS1086: An accessor cannot be declared in an ambient context.

Description

There is an error in type definition (index.d.ts) file.

re-resizable v4.0.0
typescript v2.5.3

Expected behavior

No Error

Actual behavior

ERROR at /.../node_modules/re-resizable/index.d.ts(103,7):
TS1086: An accessor cannot be declared in an ambient context.

Pass custom props to the container div without whitelisting

I need to pass props to the container div that aren't currently "whitelisted" by the component (such as id). Since the div container doesn't really contain any special logic, could it make sense to pass all unconsumed props to the div element as they are?

    const { componentProp1, componentProp2, ... , ...other  } = this.props;
    return (
      <div
        ref="resizable"
        style={{
          ...
        }}
        {...other}
...

Support additional height and width with lockAspectRatio

I am displaying a window with a 16:9 video and a 60 pixel tall title bar. I would like to keep the video aspect ratio consistent when resizing the window, so I tried the lockAspectRatio option. Unfortunately, it is only really useful if the resizable panel contains only the content to show at a certain aspect ratio and no title bar, sidebar, border widths, etc.

Perhaps this could be solved by introducing some additional behavior and options?

  • lockAspectRatio: boolean | number
  • lockAspectRatioAdditionalHeight: number
  • lockAspectRatioAdditionalWidth: number

The lockAspectRatio would be set to true for current behavior or 16/9 (for example) for new behavior. The other options would default to 0 to get the current behavior or some value (i.e. 60) would be subtracted from the window size before the ratio calculation takes place.

Thoughts?

Use rollup

Overview of the problem

Description

Steps to Reproduce

Expected behavior

Actual behavior

Adding custom class on resize handler

There is no possibility to add custom class on resize handler.
Like I have enabled only bottomRight to handle drag, so I need to add some custom class on the div created for the bottomRight resize handler.

custom resize handle

feature/question

Overview of the problem

I'm using re-resizable version [4.0.2]

My browser is: any

I am sure this issue is not a duplicate?
no

Description

I need an ability to pass custom handle component. Is there any way to do it?

Not compatible with IE11 (and IE9/10)

Object.assign() is not supported by IE11 (and previous versions also).
Object doesn't support property or method 'assign'
is thrown when attempting to render in IE11.

Is there a change you could replace Object.assign with a polyfill or something? I've tried to do it on my end with no luck :(

Can't resize height if it's given in percents

Overview of the problem

I'm using re-resizable version 4.0.2

https://www.webpackbin.com/bins/-KygHWlVVmSeZtaH2rc3

My browser is:

Chrome 62.0 and Firefox 56

Description

Can't resize height if it's given in %, width works fine with %.
OnResize event it firing correctly with events, but the actual shape does not change.

Steps to Reproduce

  • Set the size to something using int values.
  • Validate it resizes in both directions
  • Change width to %
  • Validate it resizes in both directions
  • Change width back to int (or not, it does not matter)
  • Change height to %
  • Can not resize height anymore

Expected behavior

Can resize heigh if given in %, same as width

An in-range update of react is breaking the build 🚨

Version 15.6.1 of react just got published.

Branch Build failing 🚨
Dependency react
Current Version 15.6.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As react is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of eslint-plugin-flowtype is breaking the build 🚨

Version 2.31.0 of eslint-plugin-flowtype just got published.

Branch Build failing 🚨
Dependency eslint-plugin-flowtype
Current Version 2.30.4
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As eslint-plugin-flowtype is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details - βœ… **continuous-integration/travis-ci/push** The Travis CI build passed [Details](https://travis-ci.org/bokuweb/react-resizable-box/builds/223242226),- ❌ **continuous-integration/travis-ci/pr** The Travis CI build could not complete due to an error [Details](https://travis-ci.org/bokuweb/react-resizable-box/builds/224525806)

Release Notes v2.31.0

<a name"2.31.0">

2.31.0 (2017-04-18)

Features

Commits

The new version differs by 1 commits0.

  • 4e191b7 feat: add no-types-missing-flow-annotation (#222)

false

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

the problem of draging picture

Drag the picture, the original picture did not follow the move, is a relatively low transparency of the picture with the mouse moving. At the end of the drag and release the mouse, the original image will be moved to the location of the end of the drag and drop, and if this time to move the mouse, the original image can be moved
qq 20170301145150

elementRef clientWidth/clientHeight on onResize callback is not updated anymore

Question

Overview of the problem

I'm using re-resizable version 2.1.0

My browser is:

Chrome

I am sure this issue is not a duplicate?

Description

I was using last react-resizable-box and wanted to get the last version in order to move to react 16 at some point.
When doing so, I noticed a different behaviour with the onResize callback.
Previously I was using the elementRef clientWidth and clientHeight to resize my element.

After the update, the width and height of the element is not changed when resizing.

Expected behavior

div clientWidth and clientHeight is updated

Actual behavior

element is not updated

Unable to active resizable component conditinally

I would like to unable resizing capabilities conditionally.

It was working fine in "react-resizable-box": "2.1.0": when resizable was disabled I was passing { enable: {@here for each was set to false@} } as a prop. Width and height has been set to 'auto'. When I wanted to activate resizable I was extending props by enabling one of the handlers and was setting width and height to specific px size, which were then set on the component.

With the latest version that behaviour is broken. When resize is enabled width and height are set to 'auto', then if I resize, they are set to specific px, later when I disable handlers width and height are not reset to 'auto'

And I would like to have Resizable component always mounted.

Error when trying to render

When I try to render a Resizable component, I always throw this error.

invariant.js:39 Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of DraggableElementsContainer.

How can I fix this?

Any reason why angles' cursors are all swapped?

I noticed that the cursors used in angles' style are all swapped. For example, here the south-west sw-resize cursor is used for the topRight angle.

I guess on Window (and maybe Mac) this is not noticeable since the ne icon is probably the same used for sw (i.e. a double-arrow line going from the top right to the bottom left), but on other OSes icons can be different. For example, here's what I see on Ubuntu:

byzanz-record

Node not found?!

I'm using react-resizable-box 4.0.0
My browser is: Mozilla Firefox / Chrome

Expected behavior

It should just remove it normally on ComponentWillUnmount

Actual behavior

image
i'm having an issue on last line of this...

console outputs "NotFoundError: Node was not found"

[Feature] Wrap resize buttons in a div/span

Hi, I would like to rotate the buttons around origin point of the parent div, I've tried to change style for every button with transform, but the result is not working.

The easiest way is to group the buttons in a div, which would have a single rotate transform. Why? Because I need to change the rotation based on the childs rotation (when you rotate more than 180deg, the drag handlers are opposite, to fix this, I can change the rotation of the buttons, not the content). Example:

- resize
-- content - rotate: -130deg
-- handlers - rotate: 49deg

This will enable dragging by the bottom to resize box down, not up. Of course, there needs to be a prop to change the style of the div. (the handlers div needs 100% width and height).

Is it possible to add this feature soon ? :) Thanks for the package, works perfectly. For the project I need it I've written rotate button that works perfectly with your package. I could post it later on.

Have a great day,
Martin

How to resize top??

I didn't realize that this component can't resize top.
Dragging the top just works like you are grabbing the bottom of the box.
How can I fix this issue???
I need my box to be able to go up by resizing

Is it possible to lock the aspect ratio anyhow?

I tried setting the width, height in the onResize function, but it seems to change the resizable-box container to the dragging values before I update the width and height via onResize.

Any ideas on how I could keep the aspect ratio of the resizable-box container?

Selecting inner text while resizing

I don't think that it's very useful to be able to select inner text while you are resizing. For instance, you can start resizing the left side to become smaller by dragging it to the right. That will change the cursor mid-drag to a text selector one and select the inner contents. I think that the cursor should be consistent through the drag process and not be able to select the text.

Allow holding ALT to resize from both sides at once

I might be able to add this functionality if it's a feature that you'd support merging.

I.e Holding ALT and resizing from the left should also resize on the right. This is how many drawing tools function.

React does not recognize the `HandleComponent` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `handlecomponent` instead. If you accidentally passed it from a parent component, remove it from the DOM element.

Overview of the problem

I'm using re-resizable version [4.1.2]

My browser is: chrome Version 63.0.3239.132

I am sure this issue is not a duplicate?

Description

I use this with handleComponent props and revcived the wraings

Steps to Reproduce

<Resizable handleComponent={{right: Handler}} defaultSize={{width:180}} minWidth={180} enable={{ top:false, right:true, bottom:false, left:false, topRight:false, bottomRight:false, bottomLeft:false, topLeft:false }} handleStyles={{right: {cursor: 'ew-resize'}}}>
    <div className='left'>
        <OtherComponent {...this.props} />
    </div>
 </Resizable>
import React, { Component } from 'react';

export default class Handler extends Component {
    constructor(props){
        super(props)
    }

	render() {
		return (
            <div className='custom-handler'>
                handleMe
            </div>
		);
	}
}

Resizing only width sets height to fixed px value

Bug

Overview of the problem

I'm using re-resizable 4.3.0

Could you please provide reproduced project on webpackbin with latest version rnd?
No, the webpackbin link is broken. https://www.webpackbin.com/bins/-Kvnrpd3GSG6-z0tpgH_

My browser is: Chrome 63, Firefox 57

Description

When resizing width, height is calculated and set to a fixed px value. This is problematic if you want height to always be initial/auto, and still be able to resize width. Setting size/defaultSize does not affect this.

It is the same problem when resizing height, then a fixed px value is set on width.

Steps to Reproduce

  1. Add resizable element
  2. Start resizing width
  3. Observe height is also set to a fixed px value.

Expected behavior

I would expect height to not be modified when only resizing width. Same goes for resizing only height, I would expect only height to be modified.

Actual behavior

Height is modified and set to a fixed px value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.