Skip to content

Commit 449c0dc

Browse files
committed
feat: Vertical and horizontal resize handles supported
1 parent 74ef3b1 commit 449c0dc

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

css/styles.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.react-resizable {
22
position: relative;
33
}
4+
45
.react-resizable-handle {
56
position: absolute;
67
width: 20px;
@@ -15,3 +16,17 @@
1516
box-sizing: border-box;
1617
cursor: se-resize;
1718
}
19+
20+
.react-resizable-handle-x {
21+
right: 0;
22+
bottom: 50%;
23+
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2IDYiIHN0eWxlPSJiYWNrZ3JvdW5kLWNvbG9yOiNmZmZmZmYwMCIgeD0iMHB4IiB5PSIwcHgiIHdpZHRoPSI2cHgiIGhlaWdodD0iNnB4Ij48ZyBvcGFjaXR5PSIwLjMwMiI+PHBhdGggZD0iTSA2IDYgTCA0LjIgNiBMIDQuMiAwIEwgNiAwIEwgNiA2IEwgNiA2IFoiIGZpbGw9IiMwMDAwMDAiLz48L2c+PC9zdmc+');
24+
cursor: ew-resize;
25+
}
26+
27+
.react-resizable-handle-y {
28+
right: 50%;
29+
bottom: 0;
30+
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2IDYiIHN0eWxlPSJiYWNrZ3JvdW5kLWNvbG9yOiNmZmZmZmYwMCIgeD0iMHB4IiB5PSIwcHgiIHdpZHRoPSI2cHgiIGhlaWdodD0iNnB4Ij48ZyBvcGFjaXR5PSIwLjMwMiI+PHBhdGggZD0iTSA2IDYgTCAwIDYgTCAwIDQuMiBMIDYgNC4yIEwgNiA2IEwgNiA2IFoiIGZpbGw9IiMwMDAwMDAiLz48L2c+PC9zdmc+');
31+
cursor: ns-resize;
32+
}

lib/Resizable.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type DragCallbackData = {
1919
};
2020
export type ResizeCallbackData = {
2121
node: HTMLElement,
22-
size: {width: number, height: number}
22+
size: {width: number, height: number},
23+
axis: Axis,
2324
};
2425
export type Props = {
2526
children: ReactElement<any>,
@@ -157,12 +158,13 @@ export default class Resizable extends React.Component<Props, State> {
157158
* @param {String} handlerName Handler name to wrap.
158159
* @return {Function} Handler function.
159160
*/
160-
resizeHandler(handlerName: string): Function {
161+
resizeHandler(handlerName: string, resizeAxis: Axis): Function {
161162
return (e: SyntheticEvent<> | MouseEvent, {node, deltaX, deltaY}: DragCallbackData) => {
162163

163164
// Axis restrictions
164-
const canDragX = this.props.axis === 'both' || this.props.axis === 'x';
165-
const canDragY = this.props.axis === 'both' || this.props.axis === 'y';
165+
const axis = resizeAxis || this.props.axis;
166+
const canDragX = axis === 'both' || axis === 'x';
167+
const canDragY = axis === 'both' || axis === 'y';
166168

167169
// Update w/h
168170
let width = this.state.width + (canDragX ? deltaX : 0);
@@ -191,13 +193,27 @@ export default class Resizable extends React.Component<Props, State> {
191193
const hasCb = typeof this.props[handlerName] === 'function';
192194
if (hasCb) {
193195
if (typeof e.persist === 'function') e.persist();
194-
this.setState(newState, () => this.props[handlerName](e, {node, size: {width, height}}));
196+
this.setState(newState, () => this.props[handlerName](e, {node, size: {width, height}, axis }));
195197
} else {
196198
this.setState(newState);
197199
}
198200
};
199201
}
200202

203+
204+
renderAxis(axis: Axis): ReactNode {
205+
const { draggableOpts } = this.props;
206+
return <DraggableCore
207+
{...draggableOpts}
208+
key={`resizableHandle-${axis}`}
209+
onStop={this.resizeHandler('onResizeStop', axis)}
210+
onStart={this.resizeHandler('onResizeStart', axis)}
211+
onDrag={this.resizeHandler('onResize', axis)}
212+
>
213+
<span className={`react-resizable-handle ${axis === 'both' ? '' : 'react-resizable-handle-' + axis}`} />
214+
</DraggableCore>;
215+
}
216+
201217
render(): ReactNode {
202218
// eslint-disable-next-line no-unused-vars
203219
const {children, draggableOpts, width, height, handleSize,
@@ -208,6 +224,11 @@ export default class Resizable extends React.Component<Props, State> {
208224
`${p.className} react-resizable`:
209225
'react-resizable';
210226

227+
const draggableCores = [
228+
axis === 'both' ? this.renderAxis('both') : null,
229+
axis === 'both' || axis === 'x' ? this.renderAxis('x') : null,
230+
axis === 'both' || axis === 'y' ? this.renderAxis('y') : null,
231+
];
211232
// What we're doing here is getting the child of this element, and cloning it with this element's props.
212233
// We are then defining its children as:
213234
// Its original children (resizable's child's children), and
@@ -217,15 +238,7 @@ export default class Resizable extends React.Component<Props, State> {
217238
className,
218239
children: [
219240
children.props.children,
220-
<DraggableCore
221-
{...draggableOpts}
222-
key="resizableHandle"
223-
onStop={this.resizeHandler('onResizeStop')}
224-
onStart={this.resizeHandler('onResizeStart')}
225-
onDrag={this.resizeHandler('onResize')}
226-
>
227-
<span className="react-resizable-handle" />
228-
</DraggableCore>
241+
...draggableCores,
229242
]
230243
});
231244
}

0 commit comments

Comments
 (0)