Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit ba5a275

Browse files
committed
Rudimentary Flexbox (Single pass)
1 parent be9c552 commit ba5a275

File tree

1 file changed

+65
-6
lines changed

1 file changed

+65
-6
lines changed

04 - Layout/prototype/index.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ function* Box(props) {
6060
});
6161
}
6262

63+
function* FlexibleBox({ width = 0, height = 0, background }) {
64+
const rect = yield { width, height };
65+
return $(Quad, {
66+
width: rect.width,
67+
height: rect.height,
68+
background: background,
69+
top: rect.top,
70+
left: rect.left
71+
});
72+
}
73+
6374
function Intl(props) {
6475
// TODO: Provide context
6576
return props.child;
@@ -158,14 +169,63 @@ function* Body(child) {
158169
});
159170
}
160171

172+
/**
173+
* Simplified Flexbox
174+
*/
175+
function* VerticalFlex(props, ...flexItems) {
176+
const children = [];
177+
let maxWidth = 0;
178+
let flexAccumulation = 0;
179+
let availableHeight = props.height;
180+
for (let { item, flex = 0 } of flexItems) {
181+
const { value: size, continuation } = yield item;
182+
maxWidth = size.width > maxWidth ? size.width : maxWidth;
183+
flexAccumulation += flex;
184+
if (!flex) {
185+
availableHeight -= size.height;
186+
}
187+
children.push({ flex, continuation, height: size.height });
188+
}
189+
const offset = yield { width: maxWidth, height: props.height };
190+
let accumulatedTop = 0;
191+
const positionedChildren = [];
192+
for (let { flex, continuation, height } of children) {
193+
if (flex) {
194+
height = availableHeight * (flex / flexAccumulation);
195+
}
196+
positionedChildren.push(
197+
$(continuation, {
198+
top: offset.top + accumulatedTop,
199+
left: offset.left,
200+
width: maxWidth,
201+
height: height
202+
})
203+
);
204+
accumulatedTop += height;
205+
}
206+
return positionedChildren;
207+
}
208+
209+
function Reflexity(child1, child2) {
210+
return $(VerticalFlex, { height: 300 },
211+
{ item: child1 },
212+
{ item: $(Div, { background: '#f00' }, $(Text, 'Some stuff '), $(Text, 'in between!')) },
213+
{ item: $(FlexibleBox, { background: '#00f' }), flex: 1 },
214+
{ item: child2 },
215+
{ item: $(FlexibleBox, { background: '#0f0' }), flex: 0.5 }
216+
);
217+
}
218+
161219
function App() {
162220
return $(Body,
163221
$(Div, { background: '#eee' },
164-
$(Horizontal, $(Text, 'Hello '), $(Text, 'World!')),
165-
$(Intl, {
166-
locale: 'en-US',
167-
child: $(Awesomeness)
168-
})
222+
$(Reflexity,
223+
$(Horizontal, $(Text, 'Hello '), $(Text, 'World!')),
224+
$(Intl, {
225+
locale: 'en-US',
226+
child: $(Awesomeness)
227+
})
228+
)
169229
)
170230
);
171231
}
@@ -189,7 +249,6 @@ function resolveChild(child) {
189249
break;
190250
}
191251
}
192-
// TODO: If it is a child, it needs to be resolved.
193252
return {
194253
value: rec.value,
195254
continuation: function(props) {

0 commit comments

Comments
 (0)