Skip to content

Commit 85226e4

Browse files
fix: convert example to typescript to fix build; fit locked aspect ratio with least squares (#261)
* fix(example): convert example to typescript * fix: fit locked aspect ratio with least squares --------- Co-authored-by: Kevin Brey <Kevin.Brey@breakthroughfuel.com>
1 parent c017399 commit 85226e4

5 files changed

Lines changed: 28 additions & 21 deletions

File tree

__tests__/Resizable.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,13 @@ describe('render Resizable', () => {
390390
expect(props.onResize).not.toHaveBeenCalled();
391391
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
392392
seHandler(mockEvent, { node, deltaX: w, deltaY: h });
393+
const expected = 50 + (w + h) / 2;
393394
expect(props.onResize).toHaveBeenLastCalledWith(
394395
mockEvent,
395396
expect.objectContaining({
396397
size: {
397-
height: 50 + Math.max(w, h),
398-
width: 50 + Math.max(w, h),
398+
height: expected,
399+
width: expected,
399400
},
400401
})
401402
);
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React from 'react';
22
import Resizable from '../lib/Resizable';
33
import ResizableBox from '../lib/ResizableBox';
4+
import type {ResizeCallbackData} from '../lib/propTypes';
45
import 'style-loader!css-loader!../css/styles.css';
56

6-
/* global __VERSION__, __GIT_TAG__, __GIT_COMMIT__ */
7+
declare const __VERSION__: string;
8+
declare const __GIT_TAG__: string;
9+
declare const __GIT_COMMIT__: string;
710

811
// Update the version badge in the header
912
function updateVersionBadge() {
@@ -23,7 +26,7 @@ if (typeof document !== 'undefined') {
2326
}
2427
}
2528

26-
const CustomResizeHandle = React.forwardRef((props, ref) => {
29+
const CustomResizeHandle = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement> & {handleAxis?: string}>((props, ref) => {
2730
const {handleAxis, ...restProps} = props;
2831
return (
2932
<div
@@ -34,8 +37,17 @@ const CustomResizeHandle = React.forwardRef((props, ref) => {
3437
);
3538
});
3639

37-
export default class ExampleLayout extends React.Component<{}, {width: number, height: number}> {
38-
state = {
40+
type State = {
41+
width: number;
42+
height: number;
43+
absoluteWidth: number;
44+
absoluteHeight: number;
45+
absoluteLeft: number;
46+
absoluteTop: number;
47+
};
48+
49+
export default class ExampleLayout extends React.Component<{}, State> {
50+
state: State = {
3951
width: 200,
4052
height: 200,
4153
absoluteWidth: 200,
@@ -49,12 +61,12 @@ export default class ExampleLayout extends React.Component<{}, {width: number, h
4961
};
5062

5163
// On top layout
52-
onFirstBoxResize = (event, {element, size, handle}) => {
64+
onFirstBoxResize = (_event: React.SyntheticEvent, {size}: ResizeCallbackData) => {
5365
this.setState({width: size.width, height: size.height});
5466
};
5567

5668
// On bottom layout. Used to resize the center element around its flex parent.
57-
onResizeAbsolute = (event, {element, size, handle}) => {
69+
onResizeAbsolute = (_event: React.SyntheticEvent, {size, handle}: ResizeCallbackData) => {
5870
this.setState((state) => {
5971
let newLeft = state.absoluteLeft;
6072
let newTop = state.absoluteTop;

lib/Resizable.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ export default class Resizable extends React.Component<Props, {}> {
4141
// If constraining to min and max, we need to also fit width and height to aspect ratio.
4242
if (lockAspectRatio) {
4343
const ratio = this.props.width / this.props.height;
44-
const deltaW = width - this.props.width;
45-
const deltaH = height - this.props.height;
46-
47-
// Find which coordinate was greater and should push the other toward it.
48-
// E.g.:
49-
// ratio = 1, deltaW = 10, deltaH = 5, deltaH should become 10.
50-
// ratio = 2, deltaW = 10, deltaH = 6, deltaW should become 12.
51-
if (Math.abs(deltaW) > Math.abs(deltaH * ratio)) {
52-
height = width / ratio;
53-
} else {
54-
width = height * ratio;
55-
}
44+
45+
// Project (width, height) onto the line w = ratio * h.
46+
// Distributes tracking error across both axes instead of forcing one to overshoot.
47+
// t = (w * ratio + h) / (ratio^2 + 1), new_w = t * ratio, new_h = t
48+
height = (width * ratio + height) / (ratio * ratio + 1);
49+
width = height * ratio;
5650
}
5751

5852
const [oldW, oldH] = [width, height];

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
bail: isProduction,
2929
context: __dirname,
3030
entry: {
31-
test: "./examples/example.js",
31+
test: "./examples/example.tsx",
3232
},
3333
output: {
3434
path: path.join(__dirname, "examples"),

0 commit comments

Comments
 (0)