Skip to content

Commit 246b06d

Browse files
author
boev
committed
fixed bounded case for resize symmetry
1 parent bf5df64 commit 246b06d

File tree

3 files changed

+93
-24
lines changed

3 files changed

+93
-24
lines changed

src/index.tsx

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -447,28 +447,56 @@ export class Rnd extends React.PureComponent<Props, State> {
447447
const hasTop = dir.startsWith("top");
448448
const hasBottom = dir.startsWith("bottom");
449449

450-
if ((hasLeft || hasTop) && this.resizable) {
451-
const max = (selfLeft - boundaryLeft) / scale + this.resizable.size.width;
452-
this.setState({ maxWidth: max > Number(maxWidth) ? maxWidth : max });
453-
}
454-
// INFO: To set bounds in `lock aspect ratio with bounds` case. See also that story.
455-
if (hasRight || (this.props.lockAspectRatio && !hasLeft && !hasTop)) {
456-
const max = offsetWidth + (boundaryLeft - selfLeft) / scale;
457-
this.setState({ maxWidth: max > Number(maxWidth) ? maxWidth : max });
458-
}
459-
if ((hasTop || hasLeft) && this.resizable) {
460-
const max = (selfTop - boundaryTop) / scale + this.resizable.size.height;
461-
this.setState({
462-
maxHeight: max > Number(maxHeight) ? maxHeight : max,
463-
});
464-
}
465-
// INFO: To set bounds in `lock aspect ratio with bounds` case. See also that story.
466-
if (hasBottom || (this.props.lockAspectRatio && !hasTop && !hasLeft)) {
467-
const max = offsetHeight + (boundaryTop - selfTop) / scale;
468-
this.setState({
469-
maxHeight: max > Number(maxHeight) ? maxHeight : max,
470-
});
450+
if (!this.props.resizeSymmetry || this.props.resizeSymmetry == "none")
451+
{
452+
if ((hasLeft || hasTop) && this.resizable) {
453+
const max = (selfLeft - boundaryLeft) / scale + this.resizable.size.width;
454+
this.setState({ maxWidth: max > Number(maxWidth) ? maxWidth : max });
455+
}
456+
// INFO: To set bounds in `lock aspect ratio with bounds` case. See also that story.
457+
if (hasRight || (this.props.lockAspectRatio && !hasLeft && !hasTop)) {
458+
const max = offsetWidth + (boundaryLeft - selfLeft) / scale;
459+
this.setState({ maxWidth: max > Number(maxWidth) ? maxWidth : max });
460+
}
461+
if ((hasTop || hasLeft) && this.resizable) {
462+
const max = (selfTop - boundaryTop) / scale + this.resizable.size.height;
463+
this.setState({
464+
maxHeight: max > Number(maxHeight) ? maxHeight : max,
465+
});
466+
}
467+
// INFO: To set bounds in `lock aspect ratio with bounds` case. See also that story.
468+
if (hasBottom || (this.props.lockAspectRatio && !hasTop && !hasLeft)) {
469+
const max = offsetHeight + (boundaryTop - selfTop) / scale;
470+
this.setState({
471+
maxHeight: max > Number(maxHeight) ? maxHeight : max,
472+
});
473+
}
471474
}
475+
else
476+
{
477+
if ((hasLeft || hasTop || hasRight || (this.props.lockAspectRatio && !hasLeft && !hasTop)) && this.resizable) {
478+
if (this.props.resizeSymmetry == "vertical" || this.props.resizeSymmetry == "central")
479+
{
480+
const spaceLeft = (selfLeft - boundaryLeft) / scale;
481+
const spaceRight = offsetWidth - spaceLeft - this.resizable.size.width;
482+
const max = spaceRight > spaceLeft ? (this.resizable.size.width + 2 * spaceLeft) : (this.resizable.size.width + 2 * spaceRight);
483+
this.setState({ maxWidth: max > Number(maxWidth) ? maxWidth : max });
484+
}
485+
}
486+
487+
if ((hasTop || hasLeft || hasBottom || (this.props.lockAspectRatio && !hasTop && !hasLeft)) && this.resizable) {
488+
if (this.props.resizeSymmetry == "horizontal" || this.props.resizeSymmetry == "central")
489+
{
490+
const spaceTop = (selfTop - boundaryTop) / scale;
491+
const spaceBottom = offsetHeight - spaceTop - this.resizable.size.height;
492+
const max = spaceBottom > spaceTop ? (this.resizable.size.height + 2 * spaceTop) : (this.resizable.size.height + 2 * spaceBottom);
493+
494+
this.setState({
495+
maxHeight: max > Number(maxHeight) ? maxHeight : max,
496+
});
497+
}
498+
}
499+
}
472500
}
473501
} else {
474502
this.setState({
@@ -505,7 +533,7 @@ export class Rnd extends React.PureComponent<Props, State> {
505533
}
506534
}
507535
}
508-
else if (!this.props.resizeSymmetry || this.props.resizeSymmetry == "vertical")
536+
else if (this.props.resizeSymmetry == "vertical")
509537
{
510538
const left = -delta.width / 2;
511539
const top = -delta.height;
@@ -522,7 +550,7 @@ export class Rnd extends React.PureComponent<Props, State> {
522550
}
523551
}
524552
}
525-
else if (!this.props.resizeSymmetry || this.props.resizeSymmetry == "horizontal")
553+
else if (this.props.resizeSymmetry == "horizontal")
526554
{
527555
const left = -delta.width;
528556
const top = -delta.height / 2;
@@ -536,7 +564,7 @@ export class Rnd extends React.PureComponent<Props, State> {
536564
}
537565
}
538566
}
539-
else if (!this.props.resizeSymmetry || this.props.resizeSymmetry == "central")
567+
else if (this.props.resizeSymmetry == "central")
540568
{
541569
const left = -delta.width / 2;
542570
const top = -delta.height / 2;

stories/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import Issue622 from "./sandbox/issue-#622";
5454
import ResizeSymmetryVertical from "./resizeSymmetry/vertical"
5555
import ResizeSymmetryHorizontal from "./resizeSymmetry/horizontal"
5656
import ResizeSymmetryCentral from "./resizeSymmetry/central"
57+
import ResizeSymmetryBounds from "./resizeSymmetry/bounds"
5758

5859
storiesOf("bare", module).add("bare", () => <Bare />);
5960

@@ -114,4 +115,5 @@ storiesOf("min", module).add("min uncontrolled", () => <MinUncontrolled />);
114115
storiesOf("resize symmetry", module).add("vertical", () => <ResizeSymmetryVertical />);
115116
storiesOf("resize symmetry", module).add("horizontal", () => <ResizeSymmetryHorizontal />);
116117
storiesOf("resize symmetry", module).add("central", () => <ResizeSymmetryCentral />);
118+
storiesOf("resize symmetry", module).add("bounds", () => <ResizeSymmetryBounds />);
117119

stories/resizeSymmetry/bounds.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, {CSSProperties} from "react";
2+
import { Rnd } from "../../src";
3+
import { style, parentBoundary } from "../styles";
4+
5+
const innerDiv: CSSProperties = {
6+
display: "flex",
7+
alignItems: "center",
8+
justifyContent: "center",
9+
border: "dashed 1px #9C27B0",
10+
height: "120%",
11+
};
12+
const lineStyle: CSSProperties = {
13+
width: "120%",
14+
top: "50%",
15+
border: "dashed 1px #9C27B0",
16+
position: 'absolute'
17+
}
18+
19+
20+
export default () => (
21+
<div style={parentBoundary}>
22+
<Rnd
23+
style={style}
24+
bounds="parent"
25+
default={{
26+
width: 200,
27+
height: 200,
28+
x: 0,
29+
y: 0,
30+
}}
31+
resizeSymmetry="central"
32+
>
33+
<div style={lineStyle} />
34+
<div style={innerDiv}>
35+
36+
</div>
37+
</Rnd>
38+
</div>
39+
);

0 commit comments

Comments
 (0)