Skip to content

Commit 6c901fc

Browse files
committed
[css-layout-api] Update examples.
1 parent c9c5e92 commit 6c901fc

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

css-layout-api/Overview.bs

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,22 @@ The layout algorithm performs a block-like layout (positioning fragments sequent
270270
direction), while centering its children in the inline direction.
271271

272272
<pre class="lang-javascript">
273-
registerLayout('block-like', class extends Layout {
274-
static blockifyChildren = true;
275-
static inputProperties = super.inputProperties;
273+
registerLayout('block-like', class {
274+
*intrinsicSizes(styleMap, children) {
275+
const childrenSizes = yield children.map((child) => {
276+
return child.intrinsicSizes();
277+
});
278+
279+
const maxContentSize = childrenSizes.reduce((max, childSizes) => {
280+
return Math.max(max, childSizes.maxContentContribution);
281+
}, 0);
282+
283+
const minContentSize = childrenSizes.reduce((max, childSizes) => {
284+
return Math.max(max, childSizes.minContentContribution);
285+
}, 0);
286+
287+
return {maxContentSize, minContentSize};
288+
}
276289

277290
*layout(space, children, styleMap, edges) {
278291
const inlineSize = resolveInlineSize(space, styleMap);
@@ -286,21 +299,20 @@ registerLayout('block-like', class extends Layout {
286299
blockSize: availableBlockSize,
287300
});
288301

289-
let maxChildInlineSize = 0;
290302
let blockOffset = edges.all.blockStart;
291303

292-
for (let child of children) {
293-
const fragment = yield child.layoutNextFragment(childConstraintSpace);
304+
const childFragments = yeild children.map((child) => {
305+
return child.layoutNextFragment(childConstraintSpace);
306+
});
294307

308+
for (let fragment of childFragments) {
295309
// Position the fragment in a block like manner, centering it in the
296310
// inline direction.
297311
fragment.blockOffset = blockOffset;
298312
fragment.inlineOffset = Math.max(
299313
edges.all.inlineStart,
300314
(availableInlineSize - fragment.inlineSize) / 2);
301315

302-
maxChildInlineSize =
303-
Math.max(maxChildInlineSize, childFragments.inlineSize);
304316
blockOffset += fragment.blockSize;
305317
}
306318

@@ -389,46 +401,65 @@ creates child constraint spaces which specify that a child should be a fixed inl
389401

390402
<pre class="lang-javascript">
391403
registerLayout('flex-distribution-like', class {
404+
*intrinsicSizes(styleMap, children) {
405+
const childrenSizes = yield children.map((child) => {
406+
return child.intrinsicSizes();
407+
});
408+
409+
const maxContentSize = childrenSizes.reduce((sum, childSizes) => {
410+
return sum + childSizes.maxContentContribution;
411+
}, 0);
412+
413+
const minContentSize = childrenSizes.reduce((max, childSizes) => {
414+
return sum + childSizes.minContentContribution;
415+
}, 0);
416+
417+
return {maxContentSize, minContentSize};
418+
}
419+
392420
*layout(space, children, styleMap, edges, breakToken) {
393421
const inlineSize = resolveInlineSize(space, styleMap);
394422

395423
const availableInlineSize = inlineSize - edges.all.inline;
396424
const availableBlockSize =
397425
resolveBlockSize(space, styleMap) - edges.all.block;
398426

399-
const unconstrainedSizes = [];
400427
const childConstraintSpace = new ConstraintSpace({
401428
inlineSize: availableInlineSize,
402429
blockSize: availableBlockSize,
403430
});
404-
let totalSize = 0;
405431

406-
// Calculate the unconstrained size for each child.
407-
for (let child of children) {
408-
const fragment = yield child.layoutNextFragment(childConstraintSpace);
409-
unconstrainedSizes.push(fragment.inlineSize);
410-
totalSize += fragment.inlineSize;
411-
}
432+
const unconstrainedChildFragments = yield children.map((child) => {
433+
return child.layoutNextFragment(childConstraintSpace);
434+
});
435+
436+
const unconstrainedSizes = [];
437+
const totalSize = unconstrainedChildFragments.reduce((sum, fragment, i) => {
438+
unconstrainedSizes[i] = fragment.inlineSize;
439+
return sum + fragment.inlineSize;
440+
}, 0);
412441

413442
// Distribute spare space between children.
414443
const remainingSpace = Math.max(0, inlineSize - totalSize);
415444
const extraSpace = remainingSpace / children.length;
416-
const childFragments = [];
417-
let inlineOffset = 0;
418-
let maxChildBlockSize = 0;
419-
for (let i = 0; i < children.length; i++) {
420-
let fragment = yield child.layoutNextFragment(new ConstraintSpace({
445+
446+
const childFragments = yield children.map((child, i) => {
447+
return child.layoutNextFragment(new ConstraintSpace({
421448
inlineSize: unconstrainedSizes[i] + extraSpace,
422449
inlineSizeFixed: true,
423450
blockSize: availableBlockSize
424451
}));
452+
});
425453

454+
// Position the fragments.
455+
let inlineOffset = 0;
456+
let maxChildBlockSize = 0;
457+
for (let fragment of childFragments) {
426458
fragment.inlineOffset = inlineOffset;
427-
inlineOffset += fragment.inlineSize;
459+
fragment.blockOffset = edges.all.blockStart;
428460

461+
inlineOffset += fragment.inlineSize;
429462
maxChildBlockSize = Math.max(maxChildBlockSize, fragment.blockSize);
430-
431-
childFragments.push(fragment);
432463
}
433464

434465
// Resolve our block size.
@@ -505,8 +536,8 @@ It returns a {{FragmentResultOptions}} with a {{FragmentResultOptions/breakToken
505536
resume the layout.
506537

507538
<pre class="lang-javascript">
508-
registerLayout('basic-inline', class extends Layout {
509-
static inputProperties = super.inputProperties;
539+
registerLayout('basic-inline', class {
540+
static displayType = 'normal';
510541

511542
*layout(space, children, styleMap, edges, breakToken) {
512543
// Resolve our inline size.
@@ -694,7 +725,7 @@ This example shows an node styled by CSS, and what its respective {{LayoutEdges}
694725
</pre>
695726

696727
<pre class="lang-javascript">
697-
registerLayout('box-edges', class extends Layout {
728+
registerLayout('box-edges', class {
698729
*layout(space, children, styleMap, edges, breakToken) {
699730
edges.padding.inlineStart; // 5 (as 10% * 50px = 5px).
700731
edges.border.blockEnd; // 2
@@ -933,13 +964,13 @@ is called, the user agent <em>must</em> run the following steps:
933964
18. If |layout|'s <code>\[[FunctionKind]]</code> internal slot is not <code>"generator"</code>,
934965
<a>throw</a> a <a>TypeError</a> and abort all these steps.
935966

936-
19. Let |intrinsizeSizes| be the result of <a>Get</a>(|prototype|,
967+
19. Let |intrinsicSizes| be the result of <a>Get</a>(|prototype|,
937968
<code>"intrinsicSizes"</code>).
938969

939-
20. If the result of <a>IsCallable</a>(|intrinsizeSizes|) is false, <a>throw</a> a
970+
20. If the result of <a>IsCallable</a>(|intrinsicSizes|) is false, <a>throw</a> a
940971
<a>TypeError</a> and abort all these steps.
941972

942-
21. If |intrinsizeSizes|'s <code>\[[FunctionKind]]</code> internal slot is not
973+
21. If |intrinsicSizes|'s <code>\[[FunctionKind]]</code> internal slot is not
943974
<code>"generator"</code>, <a>throw</a> a <a>TypeError</a> and abort all these steps.
944975

945976
22. Let |definition| be a new <a>layout definition</a> with:
@@ -948,7 +979,7 @@ is called, the user agent <em>must</em> run the following steps:
948979

949980
- <a>layout generator function</a> being |layout|.
950981

951-
- <a>intrinsic sizes generator function</a> being |intrinsizeSizes|.
982+
- <a>intrinsic sizes generator function</a> being |intrinsicSizes|.
952983

953984
- <a>constructor valid flag</a> being <b>true</b>.
954985

0 commit comments

Comments
 (0)