Skip to content

Commit 20ae3df

Browse files
committed
[css-layout-api] Move complex example, add simpler example.
1 parent ffd237f commit 20ae3df

File tree

1 file changed

+102
-71
lines changed

1 file changed

+102
-71
lines changed

css-layout-api/Overview.bs

+102-71
Original file line numberDiff line numberDiff line change
@@ -758,85 +758,55 @@ interface LayoutFragment {
758758
};
759759
</pre>
760760

761-
A {{LayoutFragment}} represents a CSS <a>fragment</a> of a {{LayoutChild}} after layout has occurred
762-
on that child. This is produced by the {{LayoutChild/layoutNextFragment()}} method.
761+
A {{LayoutFragment}} represents a CSS [=fragment=] of a {{LayoutChild}} after layout has occurred on
762+
that child. This is produced by the {{LayoutChild/layoutNextFragment()}} method.
763763

764764
The {{LayoutFragment}} has {{LayoutFragment/inlineSize}} and {{LayoutFragment/blockSize}}
765765
attributes, which are set by the respective child's layout algorithm. They represent the <b>border
766-
box</b> size of the CSS <a>fragment</a>, and are relative to the <a>current layout's</a> writing
767-
mode.
766+
box</b> size of the CSS [=fragment=], and are relative to the [=current layout's=] writing mode.
768767

769768
The {{LayoutFragment/inlineSize}} and {{LayoutFragment/blockSize}} attributes cannot be changed. If
770-
the <a>current layout</a> requires a different {{LayoutFragment/inlineSize}} or
769+
the [=current layout=] requires a different {{LayoutFragment/inlineSize}} or
771770
{{LayoutFragment/blockSize}} the author must perform {{LayoutChild/layoutNextFragment()}} again with
772771
different arguments in order to get different results.
773772

774773
The author inside the current layout can position a resulting {{LayoutFragment}} by setting its
775774
{{LayoutFragment/inlineOffset}} and {{LayoutFragment/blockOffset}} attributes. If not set by the
776775
author they default to zero. The {{LayoutFragment/inlineOffset}} and {{LayoutFragment/blockOffset}}
777776
attributes represent the position of the {{LayoutFragment}} relative to its parent's <b>border
778-
box</b>, before transform or positioning (e.g. if a fragment is <a>relatively positioned</a>) has
777+
box</b>, before transform or positioning (e.g. if a fragment is [=relatively positioned=]) has
779778
been applied.
780779

781-
<div class="example">
782-
The layout algorithm performs a block-like layout (positioning fragments sequentially in the block
783-
direction), while centering its children in the inline direction.
784-
785-
<pre class="lang-javascript">
786-
registerLayout('block-like', class {
787-
async intrinsicSizes(children, edges, styleMap) {
788-
const childrenSizes = await Promise.all(children.map((child) => {
789-
return child.intrinsicSizes();
790-
}));
791-
792-
const maxContentSize = childrenSizes.reduce((max, childSizes) => {
793-
return Math.max(max, childSizes.maxContentSize);
794-
}, 0) + edges.all.inline;
795-
796-
const minContentSize = childrenSizes.reduce((max, childSizes) => {
797-
return Math.max(max, childSizes.minContentSize);
798-
}, 0) + edges.all.inline;
799-
800-
return {maxContentSize, minContentSize};
801-
}
802-
803-
async layout(children, edges, constraints, styleMap) {
804-
// Determine our (inner) available size.
805-
const availableInlineSize = constraints.fixedInlineSize - edges.all.inline;
806-
const availableBlockSize = constraints.fixedBlockSize ?
807-
constraints.fixedBlockSize - edges.all.block : null;
808-
809-
const childFragments = [];
810-
const childConstraints = { availableInlineSize, availableBlockSize };
780+
<div class=example>
781+
The example below shows the basic usage of a {{LayoutFragment}}.
782+
<pre class='lang-javascript'>
783+
registerLayout('example-layout-fragment', class {
784+
async layout(children, edges, constraints, styleMap) {
811785

812-
const childFragments = await Promise.all(children.map((child) => {
813-
return child.layoutNextFragment(childConstraints);
814-
}));
786+
// You must perform layout to generate a fragment.
787+
const fragment = await child.layoutNextFragment({});
815788

816-
let blockOffset = edges.all.blockStart;
817-
for (let fragment of childFragments) {
818-
// Position the fragment in a block like manner, centering it in the
819-
// inline direction.
820-
fragment.blockOffset = blockOffset;
821-
fragment.inlineOffset = Math.max(
822-
edges.all.inlineStart,
823-
(availableInlineSize - fragment.inlineSize) / 2);
789+
// You can query the size of the fragment produced:
790+
console.log(fragment.inlineSize);
791+
console.log(fragment.blockSize);
824792

825-
blockOffset += fragment.blockSize;
826-
}
793+
// You can set the position of the fragment, e.g. this will set it to the
794+
// top-left corner:
795+
fragment.inlineOffset = edges.all.inlineStart;
796+
fragment.blockOffset = edges.all.blockStart;
827797

828-
const autoBlockSize = blockOffset + edges.all.blockEnd;
798+
// Data may be passed from the child layout:
799+
console.log(fragment.data);
829800

830-
return {
831-
autoBlockSize,
832-
childFragments,
833-
};
834-
}
801+
// If the child fragmented, you can use the breakToken to produce the next
802+
// fragment in the chain.
803+
const nextFragment = await child.layoutNextFragment({}, fragment.breakToken);
804+
}
835805
});
836806
</pre>
837807
</div>
838808

839-
A <a>layout API container</a> can communicate with other <a>layout API containers</a> by using the
809+
A [=layout API container=] can communicate with other [=layout API containers=] by using the
840810
{{LayoutFragment/data}} attribute. This is set by the {{FragmentResultOptions/data}} member in the
841811
{{FragmentResultOptions}} dictionary.
842812

@@ -845,7 +815,7 @@ fragmented. If the {{LayoutFragment/breakToken}} is null the {{LayoutChild}} won
845815
{{LayoutFragment}}s for that token chain. The {{LayoutFragment/breakToken}} can be passed to the
846816
{{LayoutChild/layoutNextFragment()}} function to produce the next {{LayoutFragment}} for a
847817
particular child. The {{LayoutFragment/breakToken}} cannot be changed.
848-
If the <a>current layout</a> requires a different {{LayoutFragment/breakToken}} the author must perform
818+
If the [=current layout=] requires a different {{LayoutFragment/breakToken}} the author must perform
849819
{{LayoutChild/layoutNextFragment()}} again with different arguments.
850820

851821
Intrinsic Sizes {#intrinsic-sizes}
@@ -859,11 +829,11 @@ interface IntrinsicSizes {
859829
};
860830
</pre>
861831

862-
A {{IntrinsicSizes}} object represents the <a>min-content size</a> and <a>max-content size</a> of a
863-
CSS <a>box</a>. It has {{IntrinsicSizes/minContentSize}} and {{IntrinsicSizes/maxContentSize}}
864-
attributes which represent the <b>border box</b> min/max-content contribution of the {{LayoutChild}}
865-
for the <a>current layout</a>. The attributes are relative to the inline direction of the <a>current
866-
layout's</a> writing mode.
832+
A {{IntrinsicSizes}} object represents the [=min-content size=] and [=max-content size=] of a CSS
833+
[=box=]. It has {{IntrinsicSizes/minContentSize}} and {{IntrinsicSizes/maxContentSize}} attributes
834+
which represent the <b>border box</b> min/max-content contribution of the {{LayoutChild}} for the
835+
[=current layout=]. The attributes are relative to the inline direction of the [=current layout's=]
836+
writing mode.
867837

868838
The {{IntrinsicSizes/minContentSize}} and {{IntrinsicSizes/maxContentSize}} cannot be changed. They
869839
must not change for a {{LayoutChild}} within the current layout pass.
@@ -954,21 +924,21 @@ enum BlockFragmentationType { "none", "page", "column", "region" };
954924
</pre>
955925

956926
A {{LayoutConstraints}} object is passed into the layout method which represents the all the
957-
constraints for the <a>current layout</a> to perform layout inside. It is also used to pass
958-
information about the available space into a <a>child layout</a>.
927+
constraints for the [=current layout=] to perform layout inside. It is also used to pass information
928+
about the available space into a [=child layout=].
959929

960930
The {{LayoutConstraints}} object has {{LayoutConstraints/availableInlineSize}} and
961-
{{LayoutConstraints/availableBlockSize}} attributes. This represents the <a>available space</a> for
962-
a {{LayoutFragment}} which the layout should respect.
931+
{{LayoutConstraints/availableBlockSize}} attributes. This represents the [=available space=] for a
932+
{{LayoutFragment}} which the layout should respect.
963933

964934
Note: Some layouts may need to produce a {{LayoutFragment}} which exceed this size. For example a
965-
<a>replaced element</a>. The <a>parent layout</a> should expect this to occur and deal with it
935+
[=replaced element=]. The [=parent layout=] should expect this to occur and deal with it
966936
appropriately.
967937

968-
A <a>parent layout</a> may require the <a>current layout</a> to be exactly a particular size. If
969-
the {{LayoutConstraints/fixedInlineSize}} or {{LayoutConstraints/fixedBlockSize}} are specified the
970-
<a>current layout</a> should produce a {{LayoutFragment}} with a the specified size in the
971-
appropriate direction.
938+
A [=parent layout=] may require the [=current layout=] to be exactly a particular size. If the
939+
{{LayoutConstraints/fixedInlineSize}} or {{LayoutConstraints/fixedBlockSize}} are specified the
940+
[=current layout=] should produce a {{LayoutFragment}} with a the specified size in the appropriate
941+
direction.
972942

973943
<div class="example">
974944
The layout algorithm performs a flexbox-like distribution of spare space in the inline direction. It
@@ -1982,6 +1952,67 @@ When the user agent wants to <dfn>get a layout class instance</dfn> given |box|,
19821952
4. Return |layoutInstance|.
19831953
</div>
19841954

1955+
Examples {#examples}
1956+
====================
1957+
1958+
<div class="example">
1959+
The layout algorithm below performs a block-like layout (positioning fragments sequentially in the
1960+
block direction), while centering its children in the inline direction.
1961+
1962+
<pre class="lang-javascript">
1963+
registerLayout('block-like', class {
1964+
async intrinsicSizes(children, edges, styleMap) {
1965+
const childrenSizes = await Promise.all(children.map((child) => {
1966+
return child.intrinsicSizes();
1967+
}));
1968+
1969+
const maxContentSize = childrenSizes.reduce((max, childSizes) => {
1970+
return Math.max(max, childSizes.maxContentSize);
1971+
}, 0) + edges.all.inline;
1972+
1973+
const minContentSize = childrenSizes.reduce((max, childSizes) => {
1974+
return Math.max(max, childSizes.minContentSize);
1975+
}, 0) + edges.all.inline;
1976+
1977+
return {maxContentSize, minContentSize};
1978+
}
1979+
1980+
async layout(children, edges, constraints, styleMap) {
1981+
// Determine our (inner) available size.
1982+
const availableInlineSize = constraints.fixedInlineSize - edges.all.inline;
1983+
const availableBlockSize = constraints.fixedBlockSize ?
1984+
constraints.fixedBlockSize - edges.all.block : null;
1985+
1986+
const childFragments = [];
1987+
const childConstraints = { availableInlineSize, availableBlockSize };
1988+
1989+
const childFragments = await Promise.all(children.map((child) => {
1990+
return child.layoutNextFragment(childConstraints);
1991+
}));
1992+
1993+
let blockOffset = edges.all.blockStart;
1994+
for (let fragment of childFragments) {
1995+
// Position the fragment in a block like manner, centering it in the
1996+
// inline direction.
1997+
fragment.blockOffset = blockOffset;
1998+
fragment.inlineOffset = Math.max(
1999+
edges.all.inlineStart,
2000+
(availableInlineSize - fragment.inlineSize) / 2);
2001+
2002+
blockOffset += fragment.blockSize;
2003+
}
2004+
2005+
const autoBlockSize = blockOffset + edges.all.blockEnd;
2006+
2007+
return {
2008+
autoBlockSize,
2009+
childFragments,
2010+
};
2011+
}
2012+
});
2013+
</pre>
2014+
</div>
2015+
19852016
Security Considerations {#security-considerations}
19862017
==================================================
19872018

0 commit comments

Comments
 (0)