Skip to main content

tessera_components/
spacer.rs

1//! An invisible component for creating empty space in a layout.
2//!
3//! ## Usage
4//!
5//! Use to add gaps between components or to create flexible, expanding regions.
6use tessera_ui::{
7    ComputedData, LayoutResult, MeasurementError, Modifier,
8    layout::{MeasureScope, layout},
9    tessera,
10};
11
12#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
13struct SpacerLayout;
14
15impl tessera_ui::LayoutModifierNode for SpacerLayout {
16    fn measure(&self, input: &mut MeasureScope<'_>) -> Result<LayoutResult, MeasurementError> {
17        Ok(LayoutResult::new(ComputedData::min_from_constraint(
18            input.parent_constraint().as_ref(),
19        )))
20    }
21}
22
23/// # spacer
24///
25/// Renders an empty, flexible space to influence layout.
26///
27/// ## Usage
28///
29/// Add fixed-size gaps or create flexible space that pushes other components
30/// apart.
31///
32/// ## Parameters
33///
34/// - `modifier` — modifier chain applied to the spacer node.
35///
36/// ## Examples
37///
38/// ```
39/// use tessera_components::{modifier::ModifierExt as _, row::row, spacer::spacer, text::text};
40/// use tessera_ui::Modifier;
41///
42/// # use tessera_ui::tessera;
43/// # #[tessera]
44/// # fn component() {
45/// row().children(|| {
46///     text("Left");
47///     spacer().modifier(Modifier::new().weight(1.0));
48///     text("Right");
49/// });
50/// # }
51/// # tessera_ui::testing::layout_test(component).run();
52/// ```
53#[tessera]
54pub fn spacer(modifier: Option<Modifier>) {
55    let modifier = modifier.unwrap_or_default();
56    layout().modifier(modifier).policy(SpacerLayout);
57}