next_rust_view/style.rs
1//! Stylesheets created by the `css_module!` and `global_css!` macros.
2//!
3//! The macros read and transform CSS **at compile time** and produce
4//! `&'static Stylesheet` values. A stylesheet participates in a page when a
5//! [`CssClass`] from it is used in `class(..)`, or when the stylesheet itself
6//! is placed in the view tree (global CSS). The renderer emits every used
7//! stylesheet exactly once per document as a `<style>` element, so pages
8//! only ship the CSS they actually use and streaming never produces a flash
9//! of unstyled content.
10
11use crate::node::{Node, View};
12
13/// A compiled stylesheet.
14#[derive(Debug, PartialEq, Eq)]
15pub struct Stylesheet {
16 /// Content hash (stable across builds for identical CSS).
17 pub id: &'static str,
18 /// Minified CSS text.
19 pub css: &'static str,
20}
21
22impl View for &'static Stylesheet {
23 fn into_node(self) -> Node {
24 Node::Style(self)
25 }
26}
27
28/// A scoped class name from a CSS module.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub struct CssClass {
31 pub name: &'static str,
32 pub sheet: &'static Stylesheet,
33}
34
35impl CssClass {
36 pub const fn new(name: &'static str, sheet: &'static Stylesheet) -> Self {
37 Self { name, sheet }
38 }
39}
40
41impl std::fmt::Display for CssClass {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 f.write_str(self.name)
44 }
45}