Skip to main content
rect() acts as a generic container to contain other elements inside, like a box. It’s the equivalent of view/div/container in other UI models.

Usage

use freya::prelude::*;

fn app() -> impl IntoElement {
    rect()
        .expanded()
        .background((0, 255, 0))
        .child(label().text("Hello"))
}

Builder Methods

Layout

width
Size
Set the width of the rect.
rect().width(100.0)
rect().width(Size::fill())
height
Size
Set the height of the rect.
rect().height(100.0)
rect().height(Size::fill())
expanded
()
Expand both width and height using Size::fill().
rect().expanded()
padding
Gaps
Set padding inside the rect.
rect().padding(10.0)
rect().padding((10.0, 20.0))
margin
Gaps
Set margin outside the rect.
rect().margin(10.0)
min_width
Size
Set the minimum width.
rect().min_width(100.0)
min_height
Size
Set the minimum height.
rect().min_height(100.0)
max_width
Size
Set the maximum width.
rect().max_width(500.0)
max_height
Size
Set the maximum height.
rect().max_height(500.0)
position
Position
Set the position type (absolute or relative).
rect().position(Position::absolute())

Container & Content

direction
Direction
Set the layout direction (horizontal or vertical).
rect().direction(Direction::vertical())
rect().horizontal()
rect().vertical()
main_align
Alignment
Set alignment along the main axis.
rect().main_align(Alignment::center())
cross_align
Alignment
Set alignment along the cross axis.
rect().cross_align(Alignment::center())
center
()
Center content both horizontally and vertically.
rect().center()
spacing
f32
Set spacing between children.
rect().spacing(10.0)
content
Content
Set the content behavior.
rect().content(Content::flex())

Style

background
Color
Set the background color.
rect().background((255, 0, 0))
rect().background(Color::RED)
rect().background("#ff0000")
background_linear_gradient
LinearGradient
Set a linear gradient background.
rect().background_linear_gradient(
    LinearGradient::new()
        .add_stop(0.0, Color::RED)
        .add_stop(1.0, Color::BLUE)
)
background_radial_gradient
RadialGradient
Set a radial gradient background.
background_conic_gradient
ConicGradient
Set a conic gradient background.
border
Border
Add a border to the rect.
rect().border(Border::all(Color::BLACK, 2.0))
corner_radius
CornerRadius
Set corner radius for rounded corners.
rect().corner_radius(10.0)
rect().rounded()
rect().rounded_lg()
shadow
Shadow
Add a shadow effect.
rect().shadow(Shadow::new((0.0, 4.0), 10.0, Color::BLACK.with_opacity(0.25)))

Effects

overflow
Overflow
Set overflow behavior.
rect().overflow(Overflow::clip())
rotate
f32
Rotate the rect in degrees.
rect().rotate(45.0)
scale
Scale
Scale the rect.
rect().scale(1.5)
rect().scale((1.5, 2.0))
opacity
f32
Set opacity (0.0 to 1.0).
rect().opacity(0.5)
blur
f32
Apply a blur effect.
rect().blur(5.0)

Text Style (inherited by children)

color
Color
Set the text color for child text elements.
rect().color(Color::WHITE)
font_size
FontSize
Set the font size for child text elements.
rect().font_size(16.0)

Children

child
impl IntoElement
Add a child element.
rect().child(label().text("Child"))
children
impl IntoIterator<Item = Element>
Add multiple children.
rect().children(vec![
    label().text("First").into_element(),
    label().text("Second").into_element(),
])

Events

on_press
EventHandler
Handle press/click events.
rect().on_press(|_| println!("Pressed!"))
on_mouse_down
EventHandler<MouseEventData>
Handle mouse down events.
on_mouse_up
EventHandler<MouseEventData>
Handle mouse up events.
on_mouse_move
EventHandler<MouseEventData>
Handle mouse move events.

Examples

Basic Container

rect()
    .width(200.0)
    .height(200.0)
    .background(Color::BLUE)
    .rounded()

Flexbox Layout

rect()
    .expanded()
    .vertical()
    .spacing(10.0)
    .padding(20.0)
    .children(vec![
        label().text("Item 1").into_element(),
        label().text("Item 2").into_element(),
        label().text("Item 3").into_element(),
    ])

Centered Content

rect()
    .expanded()
    .center()
    .child(
        label().text("Centered Text")
    )

With Border and Shadow

rect()
    .width(150.0)
    .height(100.0)
    .background(Color::WHITE)
    .border(Border::all(Color::GRAY, 1.0))
    .rounded_lg()
    .shadow(Shadow::new(
        (0.0, 4.0),
        10.0,
        Color::BLACK.with_opacity(0.1)
    ))

Build docs developers (and LLMs) love