An intuitive React component set for multi-column resizing. It is easy to use and it also can customize the behavior of resizing in a very simple way. Working on every modern browser which support flexible box layout.
Using yarn:
yarn add react-simple-resizerOr via npm:
npm install react-simple-resizerWe have create several demos on CodeSandbox, check the Demos to see more. Here is the minimal example for two column layout:
import React from 'react';
import { Container, Section, Bar } from 'react-simple-resizer';
export default () => (
<Container style={{ height: '500px' }}>
<Section style={{ background: '#d3d3d3' }} minSize={100}/>
<Bar size={10} style={{ background: '#888888', cursor: 'col-resize' }} />
<Section style={{ background: '#d3d3d3' }} minSize={100} />
</Container>
);Literally, as the container of the other components.
import { HTMLAttributes } from 'react';
interface ContainerProps extends HTMLAttributes<HTMLDivElement> {
vertical?: boolean;
onActivate?: () => void;
beforeApplyResizer?: (resizer: Resizer) => void;
afterResizing?: () => void;
}Determine that whether using vertical layout or not, default is
false.
Triggered when any
Baractivated. i.e, onMouseDown or onTouchStart.
Used to customize resize behavior. In this method, you don't need to call
applyResizerto apply the resize result. Please note that you should not do any side effect on this method. If you want to do something after the resizing, seeafterResizingbelow.
Triggered after a resizing section is completed. Which means that it will be triggered after the onMouseUp and onTouchEnd events. If you want to do something after each time the size of section has changed, using the
onSizeChangedprops on theSectioninstead.
import React from 'react';
class Container extends React.PureComponent<ContainerProps> {
public getResizer(): Resizer
public applyResizer(resizer: Resizer): void
}Used to get the newest
Resizer. But any change won't apply unless you pass theResizertoapplyResizer.
Apply the passed
ResizertoContainer.
import { HTMLAttributes, RefObject } from 'react';
interface SectionProps extends HTMLAttributes<HTMLDivElement> {
size?: number;
defaultSize?: number;
maxSize?: number;
minSize?: number;
disableResponsive?: boolean;
onSizeChanged?: () => void;
innerRef?: RefObject<HTMLDivElement>;
}Used to set the
Section's size. Ifsizeexists,Sectionwill ignore the value ofdefaultSize,maxSizeandminSize.
Used to set the default size of
Section.
Used to set the max size of
Section.
Used to set the min size of
Section.
Each
Sectionis responsive as default, unlesssizeis exists. the responsive means that theSection's size is related withContainer's size, ifContainer's size turn smaller, theSection's size also turn smaller automatically. Otherwise, the changes ofContainersize won't affect theSectionthatdisableResponsiveistrue.
Will be triggered each time the size has changed.
Used to get the actual DOM ref of
Section.
import { HTMLAttributes, RefObject } from 'react';
interface ExpandInteractiveArea {
top?: number;
left?: number;
right?: number;
bottom?: number;
}
interface BarProps extends HTMLAttributes<HTMLDivElement> {
size: number;
expandInteractiveArea?: ExpandInteractiveArea;
onStatusChanged?: (isActive: boolean) => void;
innerRef?: RefObject<HTMLDivElement>;
}Required, used to set the size of the
Bar.
Used to expanding the interactive area of the
Bar.
Triggered when the state of the
Barhas changed.
Used to get the actual DOM ref of
Bar.
If you want to customize the behavior of resizing, then the Resizer is what you need to know.
There is two ways to get the
Resizer. One is the methodbeforeApplyResizerdefined on the props ofContainer, and the other is the methodgetResizerdefined on the instance ofContainer
interface Resizer {
resizeSection: (indexOfSection: number, config: { toSize: number; preferMoveLeftBar?: boolean }) => void;
moveBar: (indexOfBar: number, config: { withOffset: number; }) => void;
discard: () => void;
isSectionResized: (indexOfSection: number) => boolean;
isBarActivated: (indexOfBar: number) => boolean;
getSectionSize: (indexOfSection: number) => number | -1;
getTotalSize: () => number;
}Used to set the size of the nth
Section. In multi-column layout, there is not only oneBarcould change theSection's size. Therefor, you could usepreferMoveLeftBarto try to use the left sideBarto resizing.
Used to move the nth
Barto resizing. If the value of A is negative, moveBarto the left. Onceverticalistrue, move up.
Discard resizing at this time.
Used to determine whether the nth
Section's size is changed at current resizing section or not.
Used to determine whether the nth
Baris active or not.
Used to get the size of the nth
Section. if there is no nthSection, return-1.
Used to get the total size of the
Section.
The main purpose of this repository is to continue to evolve react-simple-resizer core, making it faster, smaller and easier to use. We are grateful to the community for contributing bugfixes and improvements.
Feel free to let us know that you have create some new customized resize behavior. If you want, you could make a PR to let more people see your works. Also, if you find some behavior that you can not create, let us know too.