-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynamicStack.tsx
333 lines (312 loc) · 8.71 KB
/
DynamicStack.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import * as React from "react";
import { PropertyControls, ControlType, Frame, Scroll } from "framer";
enum Direction {
Vertical = "vertical",
Horizontal = "horizontal"
}
enum Alignment {
Start = "start",
Center = "center",
End = "end"
}
interface Padding {
top: number;
right: number;
bottom: number;
left: number;
}
interface Props {
items: React.Component[]; // the frames to be displayed in in the stack
visibleItems: boolean[]; // array of booleans for whether each frame in the stack should be visible
gap: number; // the number of pixels seperating each item in the stack
direction: Direction; // the direction of the stack
alignment: Alignment; // how items will be aligned in the axis perpindicular to the direction of the stack
scrollable: boolean; // whether or not to embed the stack content inside of a scroll view
paddingUniversal: number; // padding to be applied to all side if `paddingPerSide` is false
paddingPerSide: boolean; // whether `universalPadding` should be used for padding or `paddingTop`, `paddingLeft`...
paddingTop: number;
paddingRight: number;
paddingLeft: number;
paddingBottom: number;
width: number;
height: number;
}
/**
* @description Default content, shown when a component has no connected frames
*/
export const DefaultContent = () => {
return (
<div
style={{
height: "100%",
background: "rgb(255, 170, 34)",
color: "rgba(0, 0, 0, 1)",
padding: 10,
textAlign: "center",
fontSize: 14
}}
>
<div>
<b>1.</b> Connect to items.
</div>
<div>
<b>2.</b> Override the `visibleItems` prop to control which items are
shown
</div>
</div>
);
};
/**
* @description Provides an alternative to the Stack component where items can be hidden via code overrides by setting the `visibleItems` prop.
*/
export class DynamicStack extends React.Component<Props> {
static defaultProps = {
gap: 10,
direction: Direction.Vertical,
alignment: Alignment.Center,
scrollable: true,
paddingUniversal: 0,
paddingPerSide: false,
paddingTop: 0,
paddingRight: 0,
paddingLeft: 0,
paddingBottom: 0,
height: 400,
width: 250
};
static propertyControls: PropertyControls = {
scrollable: {
type: ControlType.Boolean,
title: "Scrollable"
},
direction: {
type: ControlType.SegmentedEnum,
options: [Direction.Horizontal, Direction.Vertical],
title: "Direction",
defaultValue: "vertical"
},
alignment: {
type: ControlType.SegmentedEnum,
options: [Alignment.Start, Alignment.Center, Alignment.End],
optionTitles: props =>
props.direction === Direction.Vertical
? ["Left", "Center", "Right"]
: ["Top", "Center", "Bottom"],
title: "Align",
defaultValue: "start"
},
items: {
type: ControlType.Array,
title: "Items",
propertyControl: { type: ControlType.ComponentInstance },
defaultValue: []
},
visibleItems: {
type: ControlType.Array,
title: "Visible Items",
propertyControl: { type: ControlType.Boolean },
defaultValue: []
},
gap: {
type: ControlType.Number,
title: "Gap",
default: 17,
min: 0,
step: 1
},
paddingUniversal: {
type: ControlType.FusedNumber,
toggleKey: "paddingPerSide",
toggleTitles: ["Padding", "Padding per side"],
valueKeys: ["paddingTop", "paddingRight", "paddingBottom", "paddingLeft"],
valueLabels: ["t", "r", "b", "l"],
min: 0,
title: "Padding",
defaultValue: 0
}
};
padding = (): Padding => {
const {
paddingUniversal,
paddingPerSide,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft
} = this.props;
return paddingPerSide
? {
top: paddingTop,
right: paddingRight,
bottom: paddingBottom,
left: paddingLeft
}
: {
top: paddingUniversal,
right: paddingUniversal,
bottom: paddingUniversal,
left: paddingUniversal
};
};
distanceFromEdgeInStackDirection = (
index: number,
items: React.Component[],
gap: number,
padding: Padding,
direction: Direction
): number => {
const itemsBeforeIndex = items.slice(0, index);
return direction === Direction.Vertical
? itemsBeforeIndex.reduce(
(top: Number, item) => top + item.props.height + gap,
padding.top
)
: itemsBeforeIndex.reduce(
(left: Number, item) => left + item.props.width + gap,
padding.left
);
};
distanceFromEdgePerpindicularToStackDirection = (
itemDimension: number,
stackDimension: number,
alignment: Alignment,
padding: Padding,
direction: Direction
): number => {
switch (alignment) {
case Alignment.Start: {
return direction === Direction.Vertical ? padding.top : padding.left;
}
case Alignment.Center: {
return Math.max((stackDimension - itemDimension) / 2, 0);
}
case Alignment.End: {
return Math.max(
stackDimension -
itemDimension -
(Direction.Vertical ? padding.right : padding.bottom) / 2,
0
);
}
}
};
filterOutHiddenItems = (
items: React.Component[],
visibleItems: boolean[]
): React.Component[] => {
return items.filter(
(_, index) => (index < visibleItems.length ? visibleItems[index] : true) //TODO: fix this
);
};
stackContentHeight = (
items: React.Component[],
gap: number,
direction: Direction,
padding: Padding
): number => {
return direction === Direction.Vertical
? items.reduce(
(acc: number, item: React.Component) => acc + item.props.height,
padding.top + padding.bottom + Math.max((items.length - 1) * gap, 0)
)
: items.reduce(
(acc: number, item: React.Component) => acc + item.props.width,
padding.left + padding.right + Math.max((items.length - 1) * gap, 0)
);
};
render() {
const {
items,
visibleItems,
gap,
width: stackWidth,
height: stackHeight,
direction,
alignment,
scrollable
} = this.props;
const padding = this.padding();
if (items.length === 0) {
return <DefaultContent />;
} else {
const filteredItems = this.filterOutHiddenItems(items, visibleItems);
const stackFrame = (
<Frame
height={
direction === Direction.Vertical
? Math.max(
this.stackContentHeight(
filteredItems,
gap,
direction,
padding
),
stackHeight
)
: stackHeight
}
width={
direction === Direction.Horizontal
? Math.max(
this.stackContentHeight(
filteredItems,
gap,
direction,
padding
),
stackWidth
)
: stackWidth
}
background="none"
top={0}
left={0}
>
{filteredItems.map((item, index) => {
return React.cloneElement(item, {
left:
direction === Direction.Vertical
? this.distanceFromEdgePerpindicularToStackDirection(
item.props.width,
stackWidth,
alignment,
padding,
direction
)
: this.distanceFromEdgeInStackDirection(
index,
filteredItems,
gap,
padding,
direction
),
top:
direction === Direction.Vertical
? this.distanceFromEdgeInStackDirection(
index,
filteredItems,
gap,
padding,
direction
)
: this.distanceFromEdgePerpindicularToStackDirection(
item.props.height,
stackHeight,
alignment,
padding,
direction
)
});
})}
</Frame>
);
return scrollable ? (
<Scroll height={stackHeight} width={stackWidth} top={0} left={0}>
{stackFrame}
</Scroll>
) : (
stackFrame
);
}
}
}