Layout
A container component that owns the children it is given.
A Row or a Column hands its children to flexbox. A Layout positions them itself: layout measures the children, gives the container a size and places each child. Use it for layouts flexbox cannot express, such as a pager whose pages sit at a computed stride, a split pane sized by a dragged divider, or a header that collapses as another child scrolls.
It is an ordinary component, returned from render:
class MyRow(private val header: Component, private val body: Component) : KComponent() {
override fun ComponentScope.render(): Component =
Layout(header, body) { header, body, c -> … }
}Use components for children that come from data. The one-, two- and three-child constructors build the same container and let a fixed set of children be named at the call site:
Layout(items.map { Page(it).key(it.id) }) { measurables, c -> … } // N children
Layout(avatar, name, timestamp) { avatar, name, timestamp, c -> … } // three, namedA container is unchanged only when its layout function is the same one. It is compared with equals, never by class. An object, or a lambda that captures nothing, is the same instance on every render and lets an unchanged container skip its layout. A lambda that captures values is a new instance each render, so the container lays out again every time, which is always correct and costs only the layout.
A null child, or one that renders nothing, keeps its slot as an empty child that measures 0x0, so every child has the index it was declared at and a function naming its children always gets them all.
Give children a stable key when they come from data. Without one, a child's state is keyed by its position among its same-type siblings, so inserting at the front hands the second child the first child's state. That shows up as a video resuming at the wrong timestamp rather than as something that looks like a bug.
Children resolve into the parent's Litho tree rather than a nested one, so hook identity, prop reconciliation, incremental mount and visibility keep working across them without forwarding anything by hand.
A Layout mounts no content, so it gets a host of its own only when a style needs one. onClick, viewTag, focusable, a shadow and a transition name each create a host, as on any other component. Intercepting touch, cooperating with a nested scroll, or transforming children per frame still needs whatever mechanism you use today.
Parameters
the children, in the order they are laid out
what this container tells its own parent, and the props the framework applies to any node: size, padding, margin, background, and so on. Its padding and border are the one way it reaches the children: the layout function works inside them, so they narrow the constraints it receives. Anything else that configures the arrangement — spacing, alignment — the layout function captures, or the component returning this Layout takes as a prop.
measures and places the children
Constructors
A container over a list of children, for a count that is not known until runtime.
A container over one child, which layout receives by name rather than by index.
A container over two children, which layout receives by name rather than by index.
A container over three children, which layout receives by name rather than by index. Past three, use the components list.
Functions
This indicates the type of the Object that will be returned by mount.
Compares this component to a different one to check if they are the same
Should not be used! Components should be manually measured only as part of a LayoutState calculation. This will measure a component and set the size in the outputSize object but the measurement result will not be cached and reused for future measurements of this component.
Returns the component's resolve result for a deferred resolution. The framework invokes this instead of the resolve if the component needs to defer its resolution until layout.