Color Slider
Adjust a single channel of a color.
use herogpui::components::color_picker::{ColorChannel, ColorSlider};ColorSlider on docs.rsSource at v0.14.0
Usage
Showing Usage, live: HeroGPUI compiled to WebAssembly. The canvas has no accessibility tree; each example's description and code below carry the same information.
Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider};
use herogpui::gpui::prelude::*;
use herogpui::gpui::px;
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children(vec![ColorSlider::new(
"color-slider",
value,
ColorChannel::Hue,
)
.default_value(value)
.into_any_element()])Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider};
use herogpui::gpui::prelude::*;
use herogpui::gpui::px;
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children(vec![ColorSlider::new(
"color-slider-disabled",
value,
ColorChannel::Hue,
)
.is_disabled(true)
.into_any_element()])Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider, Orientation};
use herogpui::gpui::prelude::*;
use herogpui::gpui::px;
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_wrap()
.items_center()
.justify_center()
.gap(px(12.))
.children(vec![ColorSlider::new(
"color-slider-vertical",
value,
ColorChannel::Hue,
)
.orientation(Orientation::Vertical)
.length(px(160.))
.into_any_element()])Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ActiveTheme, ColorChannel, ColorSlider, ColorSwatch, PickerColor};
use herogpui::gpui::prelude::*;
use herogpui::gpui::{App, px};
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children(vec![
ColorSlider::new("color-slider-controlled", value, ColorChannel::Hue)
.on_change(cx.listener(|this, c: &PickerColor, _, cx| {
this.picker_color = *c;
cx.notify();
}))
.into_any_element(),
gpui::div()
.flex()
.flex_wrap()
.items_center()
.justify_center()
.gap(px(12.))
.children(vec![
ColorSwatch::new(value).into_any_element(),
para(&format!("Value: {}", value.to_hex()), cx),
])
.into_any_element(),
])Also calls para (included when you copy).
Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider};
use herogpui::gpui::prelude::*;
use herogpui::gpui::px;
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children(vec![ColorSlider::new(
"color-slider-render-state",
value,
ColorChannel::Hue,
)
.default_value(value)
.thumb(|state| {
gpui::div()
.absolute()
.inset_0()
.flex()
.items_center()
.justify_center()
.child(
gpui::div()
.size(px(if state.is_dragging { 6. } else { 4. }))
.rounded_full()
.bg(if state.is_hovered {
gpui::black()
} else {
gpui::white()
}),
)
.into_any_element()
})
.into_any_element()])Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider};
use herogpui::gpui::prelude::*;
use herogpui::gpui::px;
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children(vec![ColorSlider::new(
"color-slider-alpha",
value,
ColorChannel::Alpha,
)
.show_label(true)
.into_any_element()])Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider, ColorSpace};
use herogpui::gpui::prelude::*;
use herogpui::gpui::px;
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children([
ColorChannel::Hue,
ColorChannel::Saturation,
ColorChannel::Lightness,
]
.iter()
.map(|ch| {
ColorSlider::new(format!("color-slider-hsl-{ch:?}"), value, *ch)
.color_space(ColorSpace::Hsl)
.show_label(true)
}))Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider, PickerColor};
use herogpui::gpui::prelude::*;
use herogpui::gpui::px;
use herogpui::gpui;
let value = self.picker_color;
let channels = [
ColorChannel::Hue,
ColorChannel::Saturation,
ColorChannel::Brightness,
ColorChannel::Alpha,
];
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children(channels
.iter()
.map(|ch| {
ColorSlider::new(format!("color-slider-{ch:?}"), value, *ch)
.on_change(cx.listener(
|this, c: &PickerColor, _, cx| {
this.picker_color = *c;
cx.notify();
},
))
// `ColorSlider.Output`'s render function is
// handed the colour: a swatch beside the value.
.output(|color, text| {
gpui::div()
.flex()
.items_center()
.gap(px(6.))
.child(
gpui::div()
.size(px(10.))
.rounded_full()
.bg(color.to_hsla()),
)
.child(text.to_owned())
.into_any_element()
})
}))Runs in a view's render: self is the view holding this example's state and cx is its Context<Self>.
use herogpui::prelude::{ColorChannel, ColorSlider, PickerColor};
use herogpui::gpui::prelude::*;
use herogpui::gpui::{px, rgb};
use herogpui::gpui;
let value = self.picker_color;
gpui::div()
.flex()
.flex_col()
.items_start()
.justify_center()
.gap(px(12.))
.children([
ColorChannel::Red,
ColorChannel::Green,
ColorChannel::Blue,
]
.iter()
.map(|ch| {
ColorSlider::new(format!("color-slider-rgb-{ch:?}"), value, *ch)
.on_change(cx.listener(|this, c: &PickerColor, _, cx| {
this.picker_color = *c;
cx.notify();
}))
}))Helpers used by these examples
Plain functions over the public API, defined once here and copied with each example that calls them.
fn para(text: &str, cx: &App) -> gpui::AnyElement {
let colors = cx.colors();
gpui::div()
.text_size(px(14.5))
.line_height(px(24.))
.text_color(colors.foreground)
.max_w(px(720.))
.child(text.to_owned())
.into_any_element()
}Anatomy
Rust types
Color Slider is assembled from these builders. The API reference lists each one.
Customization
Shared appearance belongs on ThemeBuilder::components and .recipe("name"). Typed builders change how Color Slider looks at one call site. Every builder also carries sx for placement, width and flex: it takes GPUI's own styling methods and refines them over the component's root after the theme, so an instance override wins. Behaviour builders live in the API reference below.
Styling
| Method | Values | Description |
|---|---|---|
orientation(Orientation) | horizontal | vertical | Switches the value axis and vertical layout direction. |
// The one slot for caller-owned low-level styling: GPUI's
// styling methods, refined over the root element last.
.sx(|el| {
el.bg(gpui::rgba(0xffa500ff)) // background
.text_color(gpui::rgba(0x000000ff)) // text
.w(gpui::px(13.)) // size
.h(gpui::px(12.))
})API reference
Against HeroUI v3.2.6: 18 entries ported, 6 partial, 5 not ported. Partial rows are marked in the tables; entries not ported are listed under each table with the reason.
Builders
| Builder | Values | Default | Description |
|---|---|---|---|
new( | ColorChannel | — | Required channel supplied positionally and edited with its pinned range and step. |
color_space(Partial | ColorSpace | ColorSpace:: | Selects HSB, HSL, or RGB interpretation; invalid channel/space pairs are accepted rather than auto-corrected with a warning. |
new( | string | | — | Controlled color waits for owner acceptance after pointer and keyboard reports. |
default_value( | string | | — | Seeds picker-owned color state once. |
on_change( | Fn( | — | Reports each changed pointer coordinate or keyboard step. |
on_change_end( | Fn( | — | Reports once on release and once for each completed keyboard change. |
orientation( | horizontal | | Orientation:: | Switches the value axis and vertical layout direction. |
is_disabled( | true | | false | Removes focus, pointer, keyboard, and successful form submission while masking thumb interaction state. |
name( | string | — | Registers the live channel number for FormData. |
show_label / output / thumbPartial | AnyElement | | — | The monolithic Rust root composes built-in label, output, track, and thumb parts; root replacement is unavailable. |
length(Partial | string / render closure | — | Browser class customization and root-element substitution are unavailable; length provides geometry customization. |
output( | AnyElement | | formatted value | Replacement output receives current color and formatted channel text. |
built-in trackPartial | AnyElement | | ColorSlider. | The built-in track owns the thumb; independent track render content and hover state are unavailable. |
thumb( | AnyElement | | — | Caller content receives color, dragging, hovered, focused, focus-visible, and disabled state. |
Not ported5 HeroUI entries not ported
| HeroUI v3 | Why it is not ported |
|---|---|
ColorSlider.aria-label | GPUI exposes no accessibility-tree attribute surface. |
ColorSliderRenderProps.color / orientation / isDisabled | The values are computed by the control but a root children render function is not exposed. |
ColorSlider.Output.className | Browser classes are unavailable. |
ColorSlider.Track.className / style | Browser classes and inline-style render functions are unavailable. |
ColorSlider.Thumb.className / style | Browser class and inline-style render functions are unavailable. |
Parts
| Part | Description |
|---|---|
ColorSlider | Value, orientation, form, label/output layout, and interaction owner. |
ColorSliderPartial | Built-in channel name; arbitrary label composition is unavailable. |
ColorSlider | Formatted value or caller-rendered color/value content. |
ColorSliderPartial | Gradient spans the thumb travel and meets matching endpoint colors under one rounded clip with the alpha checkerboard. Unblurred inset shadows shade the edges without an extra outline; the thumb stays outside the clip. Independent child render state is unavailable. |
ColorSlider | Current-color indicator, focus ring, and complete shared ColorThumb render state. |
States
| State | Builder | Description |
|---|---|---|
Disabled | is_disabled | Track dims, thumb uses default fill, interaction is removed, FormData is omitted, and thumb state is masked. |
Dragging | dragging keyed state | Same-value and changed-value presses expose live dragging state until global release. |
Hovered | thumb_hovered keyed state | Enabled thumb hover reaches its render function. |
Focused | focus_handle | Pointer and keyboard focus reach thumb render state. |
Focus visible | with_focus_ring(thumb) | Keyboard-origin focus rings the thumb rather than the whole track. |