Skip to content

Commit 463299e

Browse files
authored
added grid gap prop to support adding any gaps to width/height (#823)
1 parent ded2aa0 commit 463299e

3 files changed

Lines changed: 97 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ The `maxHeight` property is used to set the maximum height of a resizable compon
150150

151151
The `grid` property is used to specify the increments that resizing should snap to. Defaults to `[1, 1]`.
152152

153+
#### `gridGap?: [number, number];`
154+
155+
The `gridGap` property is used to specify any gaps between your grid cells that should be accounted for when resizing. Defaults to `[0, 0]`.
156+
The value provided for each axis will always add the grid gap amount times grid cells spanned minus one.
157+
153158
#### `snap?: { x?: Array<number>, y?: Array<number> };`
154159

155160
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`.

src/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export interface ResizableProps {
8282
style?: React.CSSProperties;
8383
className?: string;
8484
grid?: [number, number];
85+
gridGap?: [number, number];
8586
snap?: {
8687
x?: number[];
8788
y?: number[];
@@ -129,7 +130,11 @@ interface State {
129130
}
130131

131132
const clamp = (n: number, min: number, max: number): number => Math.max(Math.min(n, max), min);
132-
const snap = (n: number, size: number): number => Math.round(n / size) * size;
133+
const snap = (n: number, size: number, gridGap: number): number => {
134+
const v = Math.round(n / size);
135+
136+
return v * size + gridGap * (v - 1);
137+
};
133138
const hasDirection = (dir: 'top' | 'right' | 'bottom' | 'left', target: string): boolean =>
134139
new RegExp(dir, 'i').test(target);
135140

@@ -242,6 +247,7 @@ const definedProps = [
242247
'style',
243248
'className',
244249
'grid',
250+
'gridGap',
245251
'snap',
246252
'bounds',
247253
'boundsByDirection',
@@ -373,6 +379,7 @@ export class Resizable extends React.PureComponent<ResizableProps, State> {
373379
},
374380
style: {},
375381
grid: [1, 1],
382+
gridGap: [0, 0],
376383
lockAspectRatio: false,
377384
lockAspectRatioExtraWidth: 0,
378385
lockAspectRatioExtraHeight: 0,
@@ -803,8 +810,8 @@ export class Resizable extends React.PureComponent<ResizableProps, State> {
803810
newHeight = newSize.newHeight;
804811

805812
if (this.props.grid) {
806-
const newGridWidth = snap(newWidth, this.props.grid[0]);
807-
const newGridHeight = snap(newHeight, this.props.grid[1]);
813+
const newGridWidth = snap(newWidth, this.props.grid[0], this.props.gridGap ? this.props.gridGap[0] : 0);
814+
const newGridHeight = snap(newHeight, this.props.grid[1], this.props.gridGap ? this.props.gridGap[1] : 0);
808815
const gap = this.props.snapGap || 0;
809816
const w = gap === 0 || Math.abs(newGridWidth - newWidth) <= gap ? newGridWidth : newWidth;
810817
const h = gap === 0 || Math.abs(newGridHeight - newHeight) <= gap ? newGridHeight : newHeight;

stories/grid.stories.tsx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as React from 'react';
2+
import { Resizable } from '../src';
3+
import { storiesOf } from '@storybook/react';
4+
import { style } from './style';
5+
6+
const cell: React.CSSProperties = {
7+
display: 'flex',
8+
alignItems: 'center',
9+
justifyContent: 'center',
10+
width: '100px',
11+
height: '50px',
12+
backgroundColor: '#f8f8f8',
13+
border: '1px solid #f0f0f0',
14+
boxSizing: 'border-box',
15+
};
16+
17+
const container: React.CSSProperties = {
18+
display: 'flex',
19+
gap: '3px',
20+
};
21+
22+
const verticalContainer: React.CSSProperties = {
23+
display: 'flex',
24+
flexDirection: 'column',
25+
gap: '3px',
26+
};
27+
28+
storiesOf('grid', module)
29+
.add('default', () => (
30+
<Resizable
31+
style={style}
32+
grid={[100, 100]}
33+
defaultSize={{ width: 100, height: 100 }}
34+
onResize={a => {
35+
console.log(a);
36+
}}
37+
>
38+
001
39+
</Resizable>
40+
))
41+
.add('grid gap', () => (
42+
<div style={container}>
43+
<div style={verticalContainer}>
44+
<div style={cell} />
45+
{Array.from({ length: 3 }, (_, idx) => (
46+
<div style={cell}>h: 50px</div>
47+
))}
48+
</div>
49+
<div style={verticalContainer}>
50+
<div style={container}>
51+
{Array.from({ length: 3 }, (_, idx) => (
52+
<div key={idx} style={cell}>
53+
w: 100px
54+
</div>
55+
))}
56+
</div>
57+
<Resizable
58+
style={style}
59+
grid={[100, 50]}
60+
gridGap={[3, 3]}
61+
defaultSize={{ width: 100, height: 50 }}
62+
maxWidth={306}
63+
maxHeight={156}
64+
enable={{
65+
top: false,
66+
topRight: false,
67+
right: true,
68+
bottomRight: true,
69+
bottom: true,
70+
bottomLeft: false,
71+
left: false,
72+
topLeft: false,
73+
}}
74+
onResize={a => {
75+
console.log(a);
76+
}}
77+
>
78+
001
79+
</Resizable>
80+
</div>
81+
</div>
82+
));

0 commit comments

Comments
 (0)