Theme
All components support theming through the built-in Theme system, the ActiveTheme trait provides access to the current theme colors:
use gpui_kit::component::{ActiveTheme as _};
// Access theme colors in your components
cx.theme().primary
cx.theme().background
cx.theme().foregroundSo if you want use the colors from the current theme, you should keep your component or view have App context.
#Gradient Backgrounds
const gradients = (
<View style={{ gap: 8 }}>
<Text style={{ fontSize: 14, color: tokens.foreground }}>
The NSH components use flat token colours; gradients are an application choice.
</Text>
<View style={{ flexDirection: "row", gap: 8 }}>
<View style={{ width: 120, height: 56, borderRadius: 6, backgroundImage: "linear-gradient(135deg, #54aeff, #8250df)" }} />
<View style={{ width: 120, height: 56, borderRadius: 6, backgroundImage: "linear-gradient(135deg, #22c55e, #eab308)" }} />
<View style={{ width: 120, height: 56, borderRadius: 6, backgroundImage: "linear-gradient(135deg, #f87171, #facc15)" }} />
</View>
</View>
);Theme color values remain backward compatible with the existing string format:
{
"colors": {
"button.primary.background": "#4F46E5"
}
}Background tokens that opt in to gradient rendering can also use CSS-style two-stop linear gradients:
{
"colors": {
"button.primary.background": "linear-gradient(135deg, #4F46E5, #06B6D4)",
"button.primary.hover.background": "linear-gradient(to right, red-500 25%, blue-600 75%)"
}
}Top-level theme fields, such as cx.theme().button_primary, remain solid Hsla values for compatibility. Code that needs the full resolved token can use cx.theme().tokens.button_primary; its .color field is the solid representative color, and its .background field contains the configured Background, including gradients.
#Theme Registry
const registry = (
<Row gap={12} style={{ flexWrap: "wrap" }}>
<Swatch label="background" value={() => tokens.background} />
<Swatch label="foreground" value={() => tokens.foreground} />
<Swatch label="primary" value={() => tokens.primary} />
<Swatch label="secondary" value={() => tokens.secondary} />
<Swatch label="muted" value={() => tokens.muted} />
<Swatch label="accent" value={() => tokens.accent} />
<Swatch label="danger" value={() => tokens.danger} />
<Swatch label="info" value={() => tokens.info} />
<Swatch label="success" value={() => tokens.success} />
<Swatch label="warning" value={() => tokens.warning} />
<Swatch label="border" value={() => tokens.border} />
<Swatch label="ring" value={() => tokens.ring} />
</Row>
);There have more than 20 built-in themes available in themes folder.
https://github.com/MohsenDastaran/uni-kit/tree/main/themes
And we have a ThemeRegistry to help us to load themes.
Use the name of an entry in the themes array, such as Ayu Light, when looking up a theme from the registry.
use std::path::PathBuf;
use gpui_kit::{App, SharedString};
use gpui_kit::component::{Theme, ThemeRegistry};
pub fn init(cx: &mut App) {
let theme_name = SharedString::from("Ayu Light");
// Load and watch themes from ./themes directory
if let Err(err) = ThemeRegistry::watch_dir(PathBuf::from("./themes"), cx, move |cx| {
if let Some(theme) = ThemeRegistry::global(cx)
.themes()
.get(&theme_name)
.cloned()
{
Theme::update(cx, |current| current.apply_config(&theme));
}
}) {
tracing::error!("Failed to watch themes directory: {}", err);
}
}