Documentation / Reference

View Modifiers

Configure views with shared modifiers and concrete canvas, shader, shape, text, and image methods.

Dimensions and spacing

Dimensions, padding, gaps, and growth factors must be finite and nonnegative. Minimum dimensions cannot exceed their maximum. Refract uses logical pixels and has no margin modifier.

MethodBehavior
padding(impl Into<Insets>)Adds a wrapper with uniform f32 padding or explicit Insets. Insets::symmetric(horizontal, vertical) configures pairs of edges.
frame(width: f32, height: f32)Adds a wrapper with fixed dimensions.
width(f32), height(f32)Sets a dimension on the current outer element.
min_width(f32), max_width(f32)Constrains width.
min_height(f32), max_height(f32)Constrains height.
fill_width(), fill_height()Requests available space from the parent.
grow(f32)Sets the relative growth factor within the parent layout.

Paint and clipping

Paint applies to the current outer element. A shadow does not affect layout, hit testing, or scroll extents.

MethodBehavior
background(Color)Sets a solid background.
background_gradient(impl Into<Gradient>)Layers a linear or radial gradient over the optional background color.
foreground(Color)Sets foreground paint.
border(Border)Sets independent edge widths and colors.
radius(impl Into<CornerRadii>)Sets individual corners; a number rounds all corners equally.
blur(f32)Blurs the element and its descendants together; zero disables it.
backdrop_blur(f32)Blurs content behind the rounded border box; foreground content stays sharp.
focus_ring(FocusRing)Configures the direct-focus indicator. FocusRing::none() suppresses it; does not request focus or change layout.
focus_within(FocusRing)Adds an indicator while this element or a descendant is focused. Ancestor clips still apply.
shadow(BoxShadow)Adds a single outer rounded-box shadow.
opacity(f32)Sets opacity in the inclusive range 0–1.
clipped()Clips content to the element's bounds.
z_index(i32)Sets sibling stacking order on web and native. Higher values paint in front and receive pointer input first; equal values retain declaration order. Defaults to zero; negative values are supported. Apply after structural wrappers to order the whole view.

Text and image modifiers

Typography belongs to Text and Field, truncation to Text, and image fitting to Image. These are concrete builder methods, not blanket ViewExt methods. Configure them before structural wrappers or type erasure.

MethodsReference
font_size(f32), font_weight(u16), font_family(impl Into<String>)text typography
italic(), letter_spacing(f32), word_spacing(f32)text styling and spacing; also available on Field.
line_height(f32), text_align(Alignment)text layout
selection(binding), selection_visible(bool)field selection and host caret/selection painting.
text_spans(Vec<TextSpan>), text_highlight(Option<TextHighlight>)field inline formatting and independent text decoration.
truncate()text single-line truncation on web and native.
fit(ImageFit)image fitting

Identity, environment, and semantics

These modifiers describe mounted identity, shared values, or semantic information. They are available to composed views as well as primitives.

MethodBehavior
key(impl ToString)Returns KeyView with a sibling-local identity. Use stable keys when children reorder.
memo<T: PartialEq + 'static>(props: T)Returns MemoView. Reuses the subtree when explicit props and provider scope match; reactive dependencies can still invalidate it.
environment<K: EnvironmentKey>(K::Value)Returns Provided with a typed value for the subtree.
environment_object<T: 'static>(Rc<T>)Provides a shared model by identity for cx.object() reads.
label(impl Into<String>)Sets a semantic label on the current outer element (aria-label on web), and also sets alternative text on images. For Field, use its label builder to label the editable control directly.
geometry_tag(impl Into<String>)Includes the element in tagged geometry snapshots for spatial interactions. Read them through ActionContext::tagged_geometry() during pointer and scroll dispatch; see element geometry. Preserves concrete builders and targets the current outer element after wrappers.
scrollable()Returns a configurable ScrollView.
modify(wrap: bool, f: impl Fn(&mut Element) + 'static)Low-level modifier that updates an element or forces a new box when wrap is true.

Event Handlers

Shared event methods preserve concrete builder types. Callbacks receive &mut ActionContext first; use cx.runtime() to update bindings. Multiple callbacks of the same family run in registration order. See Input & Focus for payloads, propagation, default prevention, and pointer capture.

Canvas pointer callbacks can use cx.local_position(event.position) and cx.local_bounds() to observe the current handler's border-box geometry. These are context methods, not view modifiers; see local coordinates.

Field also provides on_input and on_submit; ScrollView provides on_scroll. Configure those on their concrete builders before adding structural wrappers.

MethodBehavior
on_key(action)Receives &KeyEvent for key down and up before native defaults. Bubbles through mounted ancestors.
on_pointer(action)Receives &PointerEvent for down, move, up, cancel, enter, and leave. Down, move, up, and cancel bubble; enter and leave are targeted.
on_hover(action)Receives bool when the mouse or pen enters or leaves the element and its descendants. Touch does not produce hover callbacks.
on_focus_change(action)Receives bool for confirmed direct focus changes. Focus on a descendant does not focus its ancestor.
on_focus_within_change(action)Receives bool when confirmed focus enters or leaves this element or its subtree. Moving between children does not blur their common ancestors.
on_wheel(action)Receives &WheelEvent before host scrolling. Bubbles; prevent the default to suppress scrolling. See wheel input.
on_clipboard(action)Receives &ClipboardEvent for copy, cut, and paste, including host edit-menu commands. Bubbles; see clipboard commands for synchronous content and default handling.

Focus

Focus uses ordinary graph bindings. These methods preserve concrete primitives, fields, and scroll views; the ViewExt fallback returns Modified and targets the current outer element. Attach a field's binding before padding or framing it. See Input & Focus for examples and host behavior.

A binding requests focus after commit and receives the actual host selection. Bindings do not add tab stops. Each binding/value must identify one mounted target; invalid requests reconcile to actual focus, while duplicate attachments and conflicting requests report errors.

MethodBehavior
focused(Binding<bool>)Binds direct focus on one element. true requests focus; false only blurs the target when this binding owns focus.
focused_value<T>(Binding<Option<T>>, T)Associates an element with a shared selection value. T: Clone + PartialEq + 'static. Some(value) requests that element; None clears focus owned by the selection.
focusable(bool)Controls Tab participation without adding button semantics. Fields and activation targets default to included. false retains pointer and programmatic focus.
i

Use on_focus_change for direct observation and on_focus_within_change for subtree observation. Use focus_ring or focus_within for built-in paint, or update ordinary state in callbacks to change other properties.

Activation

on_press and disabled are shared ViewExt methods. They work before or after styling on view, text, and images. Equivalent builders preserve the concrete primitive or scroll type so child, semantic, and scroll configuration can continue.

Text and images retain their host element. Opaque views receive an outer activation box; their content must not contain another interactive control. Existing roles are preserved; otherwise the target receives button semantics. Activation is targeted and is not an editable-control event hook.

On the web, activation targets and their labels suppress user text selection, including when disabled. This prevents repeated clicks from selecting button text. Ordinary text outside an activation target remains selectable in the browser; editable fields retain their selection behavior.

MethodBehavior
on_press(action)Adds pointer-click and Enter/Space keyboard activation. The callback receives &mut ActionContext. Multiple callbacks run in registration order in one mutation batch, preserving other handlers such as scroll observers.
disabled(bool)Suppresses user input and keyboard focus traversal on the current element, not its descendants. Focus-loss notifications still run. The element remains in layout.
i

Apply on_press after padding to include the padded area, or before it to target only the content. Apply disabled to the same target. For typed keyboard and pointer events, hover, focus, and bubbling, see Input & Focus.

Modifier targets and return types

Shared modifiers act on the current outer element. See Layout & Style for composition examples and paint effects for gradients, blur, and borders.

Modifier groupTarget and return type
Dimensions, paint, labels, and disabled statePreserve element builders, including Canvas<I>. ShapeView<S> uses ViewExt wrappers; configure its fill and stroke first.
Canvas, shader, and shape configurationview_box, schedule, and animatable_inputs preserve Canvas<I>. schedule and animatable_inputs preserve ShaderView. fill, no_fill, and stroke preserve ShapeView<S>. Configure these before wrappers.
padding, frameAlways add a box wrapper and return Modified. Later paint modifiers target that wrapper.
Typography and image fittingConfigure Text / Field typography or Image fitting before introducing a wrapper.
ViewExt on composed viewsModifies a direct element description when available; otherwise adds a box. Shared style methods return Modified.

Presentation transforms

Transforms change presentation without changing layout allocation. They compose around the configured anchor; displayed drawing, clipping, and hit testing follow the result. See Animation.

MethodBehavior
offset(x: f32, y: f32)Translates by logical pixels.
scale_effect(scale: f32)Scales both axes uniformly.
rotation_effect(degrees: f32)Rotates around the anchor, in degrees.
transform_anchor(UnitPoint)Sets the normalized anchor; the default is UnitPoint::CENTER.

Animation and transitions

Apply canvas-, shader-, and shape-specific methods before these wrappers. Animation intent controls presentation updates, including opted-in canvas inputs and shader uniforms; it does not turn on a continuous drawing clock. See Animation for transactions, interruptions, and supported properties.

MethodBehavior
animation(animation: impl Into<Option<Animation>>, value)Returns TransactionView. When the PartialEq + 'static value changes, scopes animation intent to the subtree. None makes that change immediate.
transaction(impl Fn(&mut Transaction) + 'static)Returns TransactionView. Overrides inherited animation intent for a subtree; the closure must be pure.
transition(Transition)Configures insertion/removal effects on the current element. Needs animation intent to animate, and preserves concrete element builders.

Canvas and shape modifiers

These methods belong to concrete drawing builders, not blanket ViewExt. Configure them before wrapper modifiers. Canvas dimensions, paint, and typed event callbacks preserve Canvas<I>; shared style modifiers on ShapeView<S> wrap it, so set fill and stroke before width or height.

GraphicsContext operations such as clipped, transformed, and layer act inside a drawing callback. Layer::blur and Layer::opacity affect that drawing group; the shared view blur and opacity modifiers affect the outer view and its descendants.

MethodBehavior
view_boxFits logical drawing bounds into the allocated content box, centered and preserving aspect ratio. Width and height must be positive and all values finite. This changes drawing coordinates, not layout size; use width and height to allocate space.
scheduleChooses OnChange (the default), Animation, or Paused. OnChange draws at time zero when inputs or size change. Animation advances visible elapsed time and requests frames. Paused freezes elapsed time while still allowing input and size updates. CanvasSchedule::animation() constructs Animation.
animatable_inputsOpts prepared inputs into existing animation transactions. A subsequent update needs animation intent, such as with_animation; this method alone starts no motion. Built-in inputs include f32, f64, Point, Size, Color, and pairs of Animatable values. Without this modifier, prepared inputs update immediately.
fillSets the shape's fill to a solid Color or a Paint gradient. Shapes start with a black fill. Filling does not remove an existing stroke; the fill is drawn first.
no_fillRemoves the fill. Use before stroke for an outline-only shape. A shape with neither a fill nor a stroke draws no ink.
strokeSets the outline paint and stroke style. Width uses logical pixels; caps, joins, dashes, and dash offset come from StrokeStyle. Strokes are centered on the path, and ink outside the canvas content box is clipped. This does not remove the fill or inset the geometry.

Shader modifiers

These methods belong to ShaderView, returned by shader and shader_with. Configure them before wrapper modifiers. Shared dimensions, paint, labels, and typed events preserve the concrete shader builder.

Shader::uniform, image, and fallback configure the program and its inputs before it becomes a view. They are not view modifiers. Shader views have no view_box; use ShaderFrame.position, size, and uv in WGSL. See Shaders for source, image, and fallback usage.

MethodBehavior
scheduleChooses OnChange (the default), Animation, or Paused. OnChange renders at time zero when inputs or dimensions change. Animation advances visible time and requests frames; Paused retains the clock phase while still accepting input and size updates. ShaderSchedule aliases CanvasSchedule and follows its visibility and reduced-motion policy.
animatable_inputsOpts bound scalar and vector uniforms into animation transactions. Use with_animation or a value-scoped animation wrapper to supply animation intent. Keep bound names and vector dimensions stable. Images and fallback colors switch immediately. Uniform transitions can continue while the shader clock is paused.