Number Field
A numeric input with increment and decrement buttons.
This demo runs the real GPUI component on WebGPU, which this browser does not expose.
Try Chrome or Edge 113+, or Safari 26+. In Firefox, enable dom.webgpu.enabled.
You can also run it natively: cargo run -p showcase number-field
The demo failed to start in this browser.
You can run it natively instead: cargo run -p showcase number-field
Live examples are not supported on most mobile browsers yet — open this page on a desktop or tablet to run the demo.
Recent phones with WebGPU may manage it (downloads ~18 MB):
#Anatomy
Import the component's parts and compose them under its root:
use base_gpui::number_field::{
NumberFieldDecrement,
NumberFieldGroup,
NumberFieldIncrement,
NumberFieldInput,
NumberFieldRoot,
NumberFieldScrubArea,
NumberFieldScrubAreaCursor,
};
NumberFieldRoot::new()
.child(
NumberFieldGroup::new()
.child(
NumberFieldDecrement::new(),
)
.child(
NumberFieldIncrement::new(),
)
.child(
NumberFieldInput::new(),
),
)
.child(
NumberFieldScrubArea::new(),
)
.child(
NumberFieldScrubAreaCursor::new(),
);
The anatomy is a structural overview. Parts with mutually exclusive modes may need separate instances; each part's section below documents its configuration.
#NumberFieldRoot
Coordinates the component's state and supplies context to its child parts.
use base_gpui::number_field::NumberFieldRoot;
NumberFieldRoot::new()
.with_field_context(/* context: FieldContext */)
.id("example-id")
.name("name")
.form(/* form: impl Into<SharedString> */)
.default_value(0.0)
.value(0.0)
.min(0.0)
.max(0.0)
.step(0.0)
.step_any()
.small_step(0.0)
.large_step(0.0)
.snap_on_step(true)
.allow_out_of_range(true)
.allow_wheel_scrub(true)
.disabled(true)
.read_only(true)
.required(true)
.on_value_change(0.0)
.on_value_committed(0.0)
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a NumberFieldRoot with its default configuration. |
.with_field_context | pub fn with_field_context(mut self, context: FieldContext) -> Self
Sets the with field context configuration for this part. |
.child | pub fn child(mut self, child: impl Into<NumberFieldChild>) -> Self
Adds one typed child to this part. |
.children | pub fn children(mut self, children: impl IntoIterator<Item = impl Into<NumberFieldChild>>,) -> Self
Adds multiple typed children in iteration order. |
.child_any | pub fn child_any(mut self, child: impl IntoElement) -> Self
Adds one arbitrary renderable child to this part. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.name | pub fn name(mut self, name: impl Into<SharedString>) -> Self
Sets the form field name used when serializing or submitting the value. |
.form | pub fn form(mut self, form: impl Into<SharedString>) -> Self
Sets the form configuration for this part. |
.default_value | pub fn default_value(mut self, default_value: Option<f64>) -> Self
Sets the initial value for uncontrolled state. Later user changes are retained by the component. |
.value | pub fn value(mut self, value: Option<f64>) -> Self
Sets the current controlled value or the value represented by this part, depending on the part's role. |
.min | pub fn min(mut self, min: f64) -> Self
Sets the min configuration for this part. |
.max | pub fn max(mut self, max: f64) -> Self
Sets the max configuration for this part. |
.step | pub fn step(mut self, step: f64) -> Self
Sets the step configuration for this part. |
.step_any | pub fn step_any(mut self) -> SelfSets the step any configuration for this part. |
.small_step | pub fn small_step(mut self, small_step: f64) -> Self
Sets the small step configuration for this part. |
.large_step | pub fn large_step(mut self, large_step: f64) -> Self
Sets the large step configuration for this part. |
.snap_on_step | pub fn snap_on_step(mut self, snap_on_step: bool) -> Self
Sets the snap on step configuration for this part. |
.allow_out_of_range | pub fn allow_out_of_range(mut self, allow_out_of_range: bool) -> Self
Sets the allow out of range configuration for this part. |
.allow_wheel_scrub | pub fn allow_wheel_scrub(mut self, allow_wheel_scrub: bool) -> Self
Sets the allow wheel scrub configuration for this part. |
.disabled | pub fn disabled(mut self, disabled: bool) -> Self
When true, prevents user interaction with this part. |
.read_only | pub fn read_only(mut self, read_only: bool) -> Self
When true, allows presentation and focus behavior without permitting value changes. |
.required | pub fn required(mut self, required: bool) -> Self
Marks the control as required for validation and accessibility state. |
.on_value_change | pub fn on_value_change(mut self, on_value_change: impl Fn(Option<f64>, NumberFieldChangeDetails, &mut Window, &mut App) + 'static,) -> Self
Registers a callback invoked when value change occurs. |
.on_value_committed | pub fn on_value_committed(mut self, on_value_committed: impl Fn(Option<f64>, NumberFieldCommitDetails, &mut Window, &mut App) + 'static,) -> Self
Registers a callback invoked when value committed occurs. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(NumberFieldRootStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. NumberFieldRoot also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#NumberFieldDecrement
Public renderable part of the Number Field Decrement component.
use base_gpui::number_field::NumberFieldDecrement;
NumberFieldDecrement::new()
.with_number_field_context(/* context: NumberFieldContext */)
.id("example-id")
.aria_label("aria label")
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a NumberFieldDecrement with its default configuration. |
.with_number_field_context | pub fn with_number_field_context(mut self, context: NumberFieldContext) -> Self
Sets the with number field context configuration for this part. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.aria_label | pub fn aria_label(mut self, aria_label: impl Into<SharedString>) -> Self
Override the default |
.child | pub fn child(mut self, child: impl IntoElement) -> Self
Adds one typed child to this part. |
.children | pub fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self
Adds multiple typed children in iteration order. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(NumberFieldDecrementStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. NumberFieldDecrement also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#NumberFieldGroup
Groups related child parts and coordinates their shared behavior.
use base_gpui::number_field::NumberFieldGroup;
NumberFieldGroup::new()
.with_number_field_context(/* context: NumberFieldContext */)
.id("example-id")
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a NumberFieldGroup with its default configuration. |
.with_number_field_context | pub fn with_number_field_context(mut self, context: NumberFieldContext) -> Self
Sets the with number field context configuration for this part. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.child | pub fn child(mut self, child: impl Into<NumberFieldGroupChild>) -> Self
Adds one typed child to this part. |
.children | pub fn children(mut self, children: impl IntoIterator<Item = impl Into<NumberFieldGroupChild>>,) -> Self
Adds multiple typed children in iteration order. |
.child_any | pub fn child_any(mut self, child: impl IntoElement) -> Self
Adds one arbitrary renderable child to this part. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(NumberFieldGroupStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. NumberFieldGroup also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#NumberFieldIncrement
Public renderable part of the Number Field Increment component.
use base_gpui::number_field::NumberFieldIncrement;
NumberFieldIncrement::new()
.with_number_field_context(/* context: NumberFieldContext */)
.id("example-id")
.aria_label("aria label")
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a NumberFieldIncrement with its default configuration. |
.with_number_field_context | pub fn with_number_field_context(mut self, context: NumberFieldContext) -> Self
Sets the with number field context configuration for this part. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.aria_label | pub fn aria_label(mut self, aria_label: impl Into<SharedString>) -> Self
Override the default |
.child | pub fn child(mut self, child: impl IntoElement) -> Self
Adds one typed child to this part. |
.children | pub fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self
Adds multiple typed children in iteration order. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(NumberFieldIncrementStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. NumberFieldIncrement also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#NumberFieldInput
Text input integrated with the component's state and behavior.
use base_gpui::number_field::NumberFieldInput;
NumberFieldInput::new()
.with_number_field_context(/* context: NumberFieldContext */)
.id("example-id")
.placeholder("placeholder")
.auto_focus(true)
.tab_index(/* tab_index: isize */)
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a NumberFieldInput with its default configuration. |
.with_number_field_context | pub fn with_number_field_context(mut self, context: NumberFieldContext) -> Self
Sets the with number field context configuration for this part. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.placeholder | pub fn placeholder(mut self, placeholder: impl Into<gpui::SharedString>) -> Self
Sets the content shown when the component has no current value. |
.auto_focus | pub fn auto_focus(mut self, auto_focus: bool) -> Self
Controls whether auto focus behavior is enabled. |
.tab_index | pub fn tab_index(mut self, tab_index: isize) -> Self
Sets the tab index configuration for this part. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(NumberFieldInputStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. NumberFieldInput also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#NumberFieldScrubArea
Public renderable part of the Number Field Scrub Area component.
use base_gpui::number_field::NumberFieldScrubArea;
NumberFieldScrubArea::new()
.with_number_field_context(/* context: NumberFieldContext */)
.id("example-id")
.direction(/* direction: NumberFieldScrubDirection */)
.pixel_sensitivity(0.0)
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a NumberFieldScrubArea with its default configuration. |
.with_number_field_context | pub fn with_number_field_context(mut self, context: NumberFieldContext) -> Self
Sets the with number field context configuration for this part. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.direction | pub fn direction(mut self, direction: NumberFieldScrubDirection) -> Self
Sets the direction configuration for this part. |
.pixel_sensitivity | pub fn pixel_sensitivity(mut self, pixel_sensitivity: f64) -> Self
Sets the pixel sensitivity configuration for this part. |
.child | pub fn child(mut self, child: impl IntoElement) -> Self
Adds one typed child to this part. |
.children | pub fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self
Adds multiple typed children in iteration order. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(NumberFieldScrubAreaStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. NumberFieldScrubArea also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#NumberFieldScrubAreaCursor
Public renderable part of the Number Field Scrub Area Cursor component.
use base_gpui::number_field::NumberFieldScrubAreaCursor;
NumberFieldScrubAreaCursor::new()
.with_number_field_context(/* context: NumberFieldContext */)
.id("example-id")
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a NumberFieldScrubAreaCursor with its default configuration. |
.with_number_field_context | pub fn with_number_field_context(mut self, context: NumberFieldContext) -> Self
Sets the with number field context configuration for this part. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.child | pub fn child(mut self, child: impl IntoElement) -> Self
Adds one typed child to this part. |
.children | pub fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self
Adds multiple typed children in iteration order. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(NumberFieldScrubAreaCursorStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. NumberFieldScrubAreaCursor also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#Accessibility
Keyboard interaction and accessibility semantics are implemented by the component, independently of visual styling. Known limitations caused by missing GPUI accessibility primitives are documented in the module source and are not silently approximated.
#Stability
Base GPUI is pre-1.0. Builder names and state types may evolve as GPUI and this port mature.