Documentation / Fundamentals
Layout & Style
Build layouts with stacks, then make them yours with composable modifiers.
Compose with stacks
Use stack() for a vertical layout and .direction(Direction::Horizontal) to switch to a row. h_stack() and v_stack() are shorthand constructors for the same Stack type. Their gap controls space between children. Dimensions use logical pixels; padding is nonnegative. Refract does not have a margin API.
v_stack()
.gap(16.0)
.child(text("Hello, Refract").font_size(28.0))
.child(text("A shared view. A different host."))
.padding(24.0)
.background(Color::rgb(15, 30, 52))
.foreground(Color::WHITE)
.radius(12.0)Modifier order matters
Padding and frames introduce layout wrappers. Paint and typography setters act on the current outer element, so the order in which you apply them changes the result.
Use fill_width and fill_height when the parent offers room to grow. Use width, height, and max_width to constrain the layout. Text can set its own font size, family, weight, alignment, and line height.
// The background includes the padding.
text("Room to breathe")
.padding(24.0)
.background(Color::rgb(22, 40, 64))
.radius(8.0)Borders and corners
Pass a Border value to border. Border::all(width, color) sets all four edges; Border::default() starts with no border. Override edges with top, right, bottom, and left, each accepting a width and color. These methods configure the value, while the view keeps a single border modifier.
Pass CornerRadii to radius, or use a number for equal corners. CornerRadii::all(value) sets every corner; CornerRadii::default() starts with square corners. Override top_left, top_right, bottom_right, and bottom_left independently. Both types also expose public fields for struct literals; each border edge is a BorderSide { width, color }.
Widths and radii use logical pixels and must be finite and nonnegative. Borders are solid and count toward the box's dimensions. When adjacent corners do not fit, all radii scale together. Backgrounds, gradients, image clipping, and shadows follow the selected corners on both hosts.
use refract::prelude::*;
let card = view()
.child(text("Notes"))
.border(Border::all(1.0, Color::BLACK).bottom(3.0, Color::BLUE))
.radius(CornerRadii::all(12.0).bottom_left(0.0).bottom_right(0.0));
let divider = view()
.border(Border::default().bottom(1.0, Color::BLACK));
let rounded = image("/photo.png").radius(12.0);Gradients
Both browser and native adapters support LinearGradient and circular RadialGradient backgrounds, including browser pre-rendering. Pass either directly to background_gradient; existing linear-gradient calls remain unchanged. Gradients paint over the optional solid background and follow the element's rounded border box.
LinearGradient::new(angle, start, end) spans the border box. Angles are finite degrees clockwise from up: 0° points up, 90° right, and 180° down.
RadialGradient::new(radius, start, end) creates a centered circle. The radius is a finite, positive distance in logical pixels from the center to stop 1; it does not scale with the view's dimensions. Use .center(x, y) to position it relative to the border box: (0.0, 0.0) is top-left, (0.5, 0.5) is the default center, and (1.0, 1.0) is bottom-right. Finite centers outside the box are allowed.
Both types expose a public stops: Vec<GradientStop> for custom color stops. Use GradientStop::new(offset, color) with at least two stops, ordered by finite offsets in 0–1. Equal offsets make hard edges. Colors interpolate in premultiplied sRGB, including alpha, and clamp beyond the first and last stop. Setting a new gradient replaces the previous one.
When setting Style::background_gradient directly, use Some(gradient.into()); the field is Option<Gradient>, with Gradient::Linear and Gradient::Radial variants. Set it to None to remove the gradient while keeping the solid background.
use refract::prelude::*;
let card = text("A cooler shade")
.padding(24.0)
.background_gradient(LinearGradient::new(
140.0, Color::rgb(44, 87, 145), Color::rgb(16, 34, 62),
))
.foreground(Color::WHITE)
.radius(12.0);
let avatar = h_stack()
.alignment(Alignment::Center)
.justify(Alignment::Center)
.child(text("Y").foreground(Color::BLUE))
.width(40.0)
.height(40.0)
.background(Color::rgb(220, 232, 251))
.background_gradient(
RadialGradient::new(
40.0, Color(255, 255, 255, 220), Color(117, 159, 224, 36),
).center(0.25, 0.2),
)
.radius(20.0);Elliptical, conic, and repeating gradients are not implemented.
Blur and Backdrop Blur
Use blur(amount: f32) to soften an element and its descendants as one image, including backgrounds, borders, and shadows. Use backdrop_blur(amount: f32) to soften previously painted content behind the element while keeping its own background and children sharp. Both are shared paint modifiers: concrete builders retain their type, and composed views use ViewExt from the prelude.
Amounts are Gaussian standard deviations in logical pixels, matching CSS blur(). They must be finite and nonnegative; negative values, NaN, and infinity are rejected before reaching either adapter. Zero disables the effect. Neither modifier changes layout or pointer hit regions. A blurred control remains interactive unless you also disable it.
use refract::prelude::*;
let softened = v_stack()
.gap(8.0)
.child(text("Soft focus").font_size(28.0))
.child(text("Both lines blur together"))
.blur(3.0);Blur amounts differ from BoxShadow::blur_radius: a shadow's Gaussian standard deviation is half its CSS shadow radius. The same numeric values do not produce the same softness.
Backdrop Blur
A backdrop blur needs content behind it and a transparent or translucent surface through which to see the result. In a z_stack, add the background first and the translucent surface afterward. An opaque background on that surface hides its backdrop blur.
The filtered backdrop is clipped to the surface's border box and follows radius, including individual corner radii. Its children do not enlarge that area. You do not need clipped() just to round the backdrop; use it separately when children should be clipped too.
use refract::prelude::*;
fn backdrop_blur_card() -> impl View {
let backdrop = h_stack()
.child(view().grow(1.0).background(Color::BLUE))
.child(view().grow(1.0).background(Color::rgb(238, 142, 112)))
.height(180.0);
let glass = text("Sharp foreground")
.font_size(22.0)
.padding(24.0)
.background(Color(255, 255, 255, 90))
.radius(16.0)
.backdrop_blur(12.0);
z_stack()
.child(backdrop)
.child(v_stack()
.alignment(Alignment::Center)
.justify(Alignment::Center)
.child(glass))
.width(360.0)
.height(180.0)
}Combine blur, opacity, and clipping
Blur modifiers set properties on the current outer element. Repeated calls replace the amount rather than adding another blur, and swapping blur and backdrop_blur on that element does not change the rendering order. Its backdrop is filtered first, its own content and descendants paint next, content blur follows, and group opacity applies last. If both modifiers are set, the final content blur also softens the foreground.
Ancestor clipping constrains the finished blur. The blurred element's own overflow clipping happens before its blur, so its halo can extend beyond that box. Place it inside a separate clipped container to contain the halo. Structural modifiers such as padding and frame also create new boxes and can change which element a later blur targets.
An ancestor with a positive blur or backdrop blur, or opacity below one, establishes a backdrop boundary. A descendant's backdrop blur samples the content painted within that boundary, without reaching through to earlier content outside it.
use refract::prelude::*;
let contained = view()
.child(text("Soft edges inside a sharp crop")
.font_size(24.0)
.blur(6.0))
.width(240.0)
.height(80.0)
.radius(12.0)
.clipped();