Skip to main content

impulse_ui_kit/
lib.rs

1//! UI Kit framework built on top of Leptos.
2//!
3//! Selects between three rendering modes via mutually exclusive Cargo features:
4//! `csr` (client-side rendering), `hydrate` (client hydration of SSR markup,
5//! reserved for the upcoming iteration), and `ssr` (server-side rendering, used
6//! together with `impulse-server-kit` under its `leptos-ssr` feature).
7
8#![allow(non_snake_case)]
9#![warn(missing_docs)]
10#![deny(warnings, clippy::todo, clippy::unimplemented)]
11
12#[cfg(not(any(feature = "csr", feature = "hydrate", feature = "ssr")))]
13compile_error!("impulse-ui-kit: enable exactly one of `csr`, `hydrate`, `ssr` features");
14
15#[cfg(any(
16  all(feature = "csr", feature = "hydrate"),
17  all(feature = "csr", feature = "ssr"),
18  all(feature = "hydrate", feature = "ssr"),
19))]
20compile_error!("impulse-ui-kit: features `csr`, `hydrate`, `ssr` are mutually exclusive");
21
22#[cfg(all(feature = "websocket", not(any(feature = "csr", feature = "hydrate"))))]
23compile_error!("impulse-ui-kit: feature `websocket` requires `csr` or `hydrate`");
24
25#[cfg(all(feature = "webtransport", not(any(feature = "csr", feature = "hydrate"))))]
26compile_error!("impulse-ui-kit: feature `webtransport` requires `csr` or `hydrate`");
27
28pub mod router;
29pub mod utils;
30
31#[cfg(feature = "websocket")]
32pub mod ws;
33#[cfg(feature = "webtransport")]
34pub mod wt;
35
36#[cfg(feature = "ssr")]
37pub mod ssr;
38
39pub mod prelude;
40
41use leptos::prelude::*;
42
43/// Application entrypoint.
44///
45/// On `csr`, mounts the app to the document body via [`leptos::mount::mount_to_body`].
46/// On `hydrate`, hydrates pre-rendered SSR markup (placeholder until hydration
47/// support lands; see `impulse-ssr-support` roadmap).
48/// On `ssr`, this is a no-op — the server entrypoint is
49/// `impulse_server_kit::leptos_ssr::leptos_router`.
50///
51/// ```rust,ignore
52/// fn main() {
53///   setup_app(log::Level::Info, Box::new(move || view! { <App /> }.into_any()))
54/// }
55/// ```
56#[cfg(feature = "csr")]
57pub fn setup_app(#[allow(unused_variables)] log_level: log::Level, children: Children) {
58  console_error_panic_hook::set_once();
59  #[cfg(debug_assertions)]
60  console_log::init_with_level(log::Level::Debug).unwrap();
61  #[cfg(not(debug_assertions))]
62  console_log::init_with_level(log_level).unwrap();
63  leptos::mount::mount_to_body(move || view! { {children()} })
64}
65
66/// Application entrypoint (hydration mode).
67///
68/// Hydrates server-rendered HTML in place via [`leptos::mount::hydrate_body`].
69/// The wasm bundle that calls `setup_app` must export a `hydrate` function via
70/// `#[wasm_bindgen]` so the SSR-emitted bootstrap `<script>` can drive
71/// hydration:
72///
73/// ```rust,ignore
74/// #[wasm_bindgen::prelude::wasm_bindgen]
75/// pub fn hydrate() {
76///   impulse_ui_kit::setup_app(log::Level::Info, Box::new(move || view! { <App/> }.into_any()))
77/// }
78/// ```
79#[cfg(feature = "hydrate")]
80pub fn setup_app(#[allow(unused_variables)] log_level: log::Level, children: Children) {
81  console_error_panic_hook::set_once();
82  #[cfg(debug_assertions)]
83  console_log::init_with_level(log::Level::Debug).unwrap();
84  #[cfg(not(debug_assertions))]
85  console_log::init_with_level(log_level).unwrap();
86  leptos::mount::hydrate_body(move || view! { {children()} })
87}
88
89/// Application entrypoint (SSR mode).
90///
91/// No-op on the server. Server-side rendering is driven by
92/// `impulse-server-kit::leptos_ssr::leptos_router`, which sets up per-request
93/// contexts and renders your `App` component into HTML.
94#[cfg(feature = "ssr")]
95pub fn setup_app(_log_level: log::Level, _children: Children) {}