Skip to main content

aurora_leptos/
lib.rs

1//! # Aurora Dark — Colliery's Leptos design system
2//!
3//! Published on crates.io as **`colliery-io-aurora`** (org-prefixed to avoid
4//! claiming generic names); the library itself is imported as `aurora_leptos`.
5//!
6//! A general dark design system for Leptos, reused across Colliery projects. It
7//! is the **core that control-plane apps (cloacina included) are built from** —
8//! everything below is first-class core, not an optional add-on:
9//!
10//! - **Components** ([`components`]) — the full Mantine-primitive + Aurora surface:
11//!   layout (Box/Group/Stack/SimpleGrid/Grid/AppShell), inputs (Button/TextInput/
12//!   Textarea/PasswordInput/NumberInput/Select/Switch/SegmentedControl/CopyButton/
13//!   ActionIcon), data + overlay (Table/Tooltip/Modal/Menu/Alert/Loader), and the
14//!   Aurora pieces (Pill/StatusBadge/Dot/Panel/PageHeader/Chip/Loading/Empty/
15//!   ErrorState).
16//! - **Widgets** ([`widgets`]) — generic data-display building blocks: `Meter`,
17//!   `Banner`, `StateCounts`, `HealthPill`, `BuildStatusBadge`, `NodeReadiness`,
18//!   `InputTable`, `StaleInputsBanner`, plus the `Input` model and
19//!   `format_ago`/`is_stale`/`freshness_pct` helpers. Apps supply their own state
20//!   labels/colors as data — no built-in vocab or branding.
21//! - **Graph** ([`graph`]) — generic graph/DAG drawing primitives: `GraphNode`,
22//!   `GraphEdge`, a dependency-free layered layout, and an SVG `Graph` component.
23//! - **Tokens + pure logic** ([`tokens`]) — semantic palette, `status_color`,
24//!   `pill_bg`, and `ApiError` error classification. Framework-agnostic Rust.
25//!
26//! Genuinely app-specific surfaces (e.g. cloacina's DAG/graph + node views) are
27//! built downstream from these primitives, not shipped here.
28//!
29//! ## Stylesheet
30//! The CSS ships inside this crate. Two ways to load it (see the workspace README
31//! for full snippets):
32//! - **Runtime:** render [`AuroraStyles`] once at the app root (or inject the
33//!   [`AURORA_CSS`] const). Simplest; possible first-paint flash.
34//! - **Linked stylesheet (no flash):** materialise it as a file and `<link>` it.
35//!   With `cargo-leptos` (builds before bundling), call [`write_css`] from
36//!   `build.rs`. With `trunk` (validates assets before building), generate it in a
37//!   `pre_build` hook via the leptos-free `aurora-css` bin.
38//!
39//! ```ignore
40//! use aurora_leptos::{components::*, tokens::token};
41//! view! { <Button>"Run"</Button> <Pill color=token::ICE>"tag"</Pill> }
42//! ```
43
44// Pure logic (no renderer) — always available.
45pub mod tokens;
46pub use tokens::*;
47
48// UI surface — requires the `components` feature (the default).
49#[cfg(feature = "components")]
50pub mod components;
51#[cfg(feature = "components")]
52pub mod graph;
53#[cfg(feature = "components")]
54pub mod widgets;
55#[cfg(feature = "components")]
56pub use components::*;
57#[cfg(feature = "components")]
58pub use graph::*;
59#[cfg(feature = "components")]
60pub use widgets::*;
61
62// ---- Stylesheet (available with or without the `components` feature) ----
63
64/// The full Aurora Dark stylesheet (IBM Plex `@font-face` + tokens + component
65/// chrome), concatenated at compile time.
66pub const AURORA_CSS: &str = concat!(
67    include_str!("../style/fonts.css"),
68    "\n",
69    include_str!("../style/tokens.css"),
70    "\n",
71    include_str!("../style/components.css"),
72);
73
74/// Just the design tokens (CSS custom properties + scales).
75pub const TOKENS_CSS: &str = include_str!("../style/tokens.css");
76/// Just the component chrome (depends on the token custom properties).
77pub const COMPONENTS_CSS: &str = include_str!("../style/components.css");
78/// Just the IBM Plex `@font-face` declarations.
79pub const FONTS_CSS: &str = include_str!("../style/fonts.css");
80
81/// Writes the full stylesheet to `dir/aurora.css` and returns the path. Leptos-free
82/// — call it from `build.rs` (cargo-leptos) or via the `aurora-css` bin in a trunk
83/// `pre_build` hook to ship Aurora Dark as a normal, render-blocking stylesheet.
84/// Use `default-features = false` so leptos isn't built for the host.
85pub fn write_css(dir: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
86    std::fs::create_dir_all(dir)?;
87    let path = dir.join("aurora.css");
88    std::fs::write(&path, AURORA_CSS)?;
89    Ok(path)
90}
91
92/// Injects the complete stylesheet as an inline `<style>` (runtime fallback for
93/// CSR-only setups). Prefer a build-time `<link>` (see [`write_css`]) to avoid a
94/// first-paint flash.
95#[cfg(feature = "components")]
96mod styles {
97    use super::AURORA_CSS;
98    use leptos::prelude::*;
99
100    #[component]
101    pub fn AuroraStyles() -> impl IntoView {
102        view! { <style>{AURORA_CSS}</style> }
103    }
104}
105#[cfg(feature = "components")]
106pub use styles::AuroraStyles;