Layouts combine layers to define the appearance and structure of an application's interface. By positioning and sizing layers, you can create individual components and complete screens.
Tutorial video
This video provides a concise overview of layouts in mos. It covers the underlying concepts and places special emphasis on constraints.
Layout files
./layout directory
Layer style files are stored in the ./layout directory.
./layout/
├── button.json
└── homeView.json
Layout file structure
A layout consists of a name field and a layers array. The name identifies the layout and must match the filename.
Each entry in the layers array defines a layer. The order of the layers determines their stacking order: layers listed later appear above layers listed earlier.
{
"name": "button",
"layers": [
{
"type": "color"
},
{
"type": "text"
}
]
}
Layers listed later in the array appear higher in the layer stack. This allows layers to overlap, for example, to display text on top of a button.
Basic attributes
Every layer requires a type attribute, which defines the kind of layer. Layers also share a set of common attributes for appearance, behavior, and state.
| Key | Type | Description | Default value |
|---|
actions | ActionList | Actions executed when the layer is tapped. | null |
borderColor | Color | Border color specified as a hexadecimal color value. | null |
borderRadius | UnitSize | Radius applied to all corners. The radius is set even when borderWidth is 0, in which case no border is drawn. | 0 |
borderWidth | UnitSize | Width of the border. | 0 |
bottomLeftRadius | UnitSize | Radius of the bottom-left corner. | 0 |
bottomRightRadius | UnitSize | Radius of the bottom-right corner. | 0 |
classes | Array[String] | Class selectors used by layer styles. | null |
conditions | Conditions | Conditions that determine the layer state. | null |
constraints | Constraints | Constraints defining the layer's position and size. | null |
consumesKeyboard | Boolean | Indicates whether the layer shrinks when the keyboard is displayed. | null |
dataKey | String | Key used to assign data to the layer. | null |
hidden | Boolean | Indicates whether the layer is hidden. | false |
layerRotation | Number | Rotation angle of the layer in degrees. | 0 |
name | String | Unique name of the layer within the layout, used for alignment or actions. | null |
opacity | Number | Opacity of the layer. | 1 |
overInsetBottom | Boolean | Determines whether the layer extends into the bottom safe area of the device. | false |
overInsetTop | Boolean | Determines whether the layer extends into the top safe area of the device. | false |
safeArea | SafeAreaTypeObject | Safe area configuration. See the linked type for details. | null |
shadowElevation | Number | Elevation used to cast shadows. | 0 |
state | String | Default state of the layer. | "default" |
states | LayerStates | Named states that affect the layer's appearance. | null |
topLeftRadius | UnitSize | Radius of the top-left corner. | 0 |
topRightRadius | UnitSize | Radius of the top-right corner. | 0 |
touchFeedback | TouchFeedback | Determines whether touch feedback is displayed when the layer is touched. | "visible" |
trackingKey | String | Key used to identify a scroll event. | null |
value | Value | Initial value of the layer. | null |
Layout basics
Each layer is treated as a rectangle in mos. Its position and size can be defined dyanmically using a grid or by defining an absolute or relative position and size using anchors.
Anchor basics
Anchors define the position of a layer relative to the screen or another layer. They can also be used to determine the layer's size.
The available anchors are:
| Anchor | Description |
|---|
n | North |
e | East |
s | South |
w | West |
c | Center |
nw | North-west |
ne | North-east |
se | South-east |
sw | South-west |
In mos., the n, e, s, and w anchors represent edges rather than individual points.
The top-left corner is the default starting point in mos. Therefore, the nw anchor is used by default and does not need to be specified explicitly.
Anchoring
Anchors can attach one edge or point of a layer to an anchor on another layer. The anchor attribute defines the anchor on the current layer, while relativeAnchor defines the corresponding anchor on the reference layer.
The n, e, s, and w anchors represent edges rather than fixed points. For example, attaching the w anchor of one layer to the e anchor of another does not determine the vertical position along the shared edges.
Layout constraints
Constraints define the position and size of a layer. The appropriate constraint depends on the intended layout and how it should adapt to different devices and screen sizes.
Dimensions can be specified in pt, dp, %, or px. See UnitSize for details.
pos constraint
The pos constraint positions a layer relative to the screen, the current view, or another layer.
| Key | Description | Default value |
|---|
x | Horizontal offset from the reference point. Positive values move the layer right; negative values move it left. | null |
y | Vertical offset from the reference point. Positive values move the layer down; negative values move it up. | null |
anchor | Anchor on the current layer to which the position values are applied. | nw |
relativeAnchor | Anchor on the reference layer. Defaults to the top-left anchor of the screen or surrounding layer. | nw |
relativeTo | Name of the layer used as the positioning reference. | null |
{
"type": "pos",
"x": "12pt",
"y": "6pt",
"anchor": "sw",
"relativeAnchor": "ne",
"relativeTo": "myLayer"
}
size constraint
The size constraint defines the dimensions of a layer.
| Key | Description | Default value |
|---|
width | Width of the layer in dp, pt, or %. | null |
height | Height of the layer in dp, pt, or %. | null |
relativeTo | Name of the layer used as the sizing reference. | null |
{
"type": "size",
"height": "240pt",
"width": "144pt"
}
grid constraint
The grid constraint is used for layers inside a grid. It defines how many columns a layer occupies and how its height is determined.
| Key | Description | Default value |
|---|
colSpan | Number of grid columns occupied by the layer. | null |
aspectRatio | Height of the layer relative to its width. | null |
height | Height of the layer in dp, pt, or %. | null |
{
"type": "grid",
"colSpan": "4",
"aspectRatio": "2"
}
Specify only one of the vertical sizing attributes, height or aspectRatio, at a time.
aspect constraint
The aspect constraint defines a layer's size based on its aspect ratio in relation to a size specified by another constraint.
| Key | Description | Default value |
|---|
ratio | Height of the layer relative to its width. | null |
{
"type": "aspect",
"ratio": "1.25"
}
A ratio of 1 represents a 1:1 aspect ratio, resulting in a square.
The special ratio value content can be used with image layers to preserve the image's original aspect ratio.
This constraint is supported only by certain layer types, such as image layers.
Advanced sizing
Dynamic content sizing
Fixed dimensions can cause content to be cut off or leave gaps when content length or aspect ratios vary. Text and container layers can be configured to adjust their size dynamically based on their content.
The following example defines the horizontal boundaries of a text layer:
{
"name": "myText",
"type": "text",
"value": "This text will differ in length depending on the response of something outside the app.",
"constraints": [
{
"type": "pos",
"x": "0pt",
"y": "0pt"
},
{
"type": "pos",
"anchor": "e",
"relativeAnchor": "e",
"x": "0pt"
}
]
}
The top-left anchor defines the layer's origin, while the east anchor defines its right edge. If the content exceeds the available width, the text wraps onto additional lines until all content is displayed, provided that lineBreakMode is configured accordingly in the text layer.
Anchor spanning/corner pinning
A layer's size can also be defined by pinning two opposite corners of its rectangle. This sets its boundaries in both dimensions.
Layout examples
The following examples demonstrate different ways to position and size layers.
Using Relative positioning
This example positions text at the center of a button by attaching the center anchor of the text layer to the center anchor of the button layer.
The following snippet shows two layer objects in a layout:
[
{
"type": "color",
"name": "button",
"constraints": [
{
"type": "pos",
"x": "0",
"y": "0"
},
{
"type": "size",
"height": "40pt",
"width": "120pt"
}
]
},
{
"type": "text",
"name": "buttonText",
"value": "Click me!",
"constraints": [
{
"type": "pos",
"anchor": "c",
"relativeAnchor": "c",
"relativeTo": "button",
"x": "0pt",
"y": "0pt"
},
{
"type": "size",
"height": "40pt",
"width": "120pt"
}
]
}
]
Using grid constraints
This example defines a grid with 12 columns. The color layer occupies 6 columns and has a height of 240pt.
{
"name": "myGrid",
"type": "grid",
"columns": "12",
"constraints": [
{
"type": "pos",
"x": "0pt",
"y": "0pt"
},
{
"type": "size",
"height": "100%",
"width": "100%"
}
],
"children": [
{
"name": "colorArea",
"type": "color",
"value": "#2f96b4",
"constraints": [
{
"type": "grid",
"colSpan": "6",
"height": "240pt"
}
]
}
]
}
Pinning opposite corners
This example shows how to size a layer to cover the available area. The nw anchor is positioned at the top-left corner of the screen, while the se anchor is positioned at the opposite corner.
{
"name": "colorArea",
"type": "color",
"value": "#2f96b4",
"constraints": [
{
"type": "pos",
"x": "0pt",
"y": "0pt"
},
{
"type": "pos",
"anchor": "se",
"relativeAnchor": "se",
"x": "0pt",
"y": "0pt"
}
]
}
Troubleshooting
If a layout does not appear as expected, check the following:
- Are the layers listed in the correct order?
- Are the constraints configured correctly?
- Do
anchor, relativeAnchor, and relativeTo reference the intended points and layers?
- If a layer is inside a grid, is its
colSpan valid, and is only one vertical sizing attribute specified?