Skip to main content

euv_core/vdom/attribute/
struct.rs

1use crate::*;
2
3/// Represents a single attribute on a virtual DOM node.
4///
5/// Combines an attribute name with its corresponding value.
6#[derive(Clone, CustomDebug, Data, New)]
7pub struct AttributeEntry {
8    /// The name of the attribute.
9    #[get_mut(pub(crate))]
10    #[set(pub(crate))]
11    pub(crate) name: String,
12    /// The value of the attribute.
13    #[debug(skip)]
14    #[get(pub(crate))]
15    #[get_mut(pub(crate))]
16    #[set(pub(crate))]
17    pub(crate) value: AttributeValue,
18}
19
20/// Represents a CSS pseudo-class or pseudo-element rule attached to a class.
21///
22/// Each rule has a selector suffix (e.g., ":hover", "::before", ":focus")
23/// and a style declaration string. When injected into the DOM, it produces
24/// a rule like `.class-name:hover { background: red; }`.
25#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
26pub struct PseudoRule {
27    /// The CSS pseudo selector suffix appended to the class name
28    /// (e.g., ":hover", ":focus", ":active", ":disabled", "::before", "::after",
29    /// ":first-child", ":last-child", ":nth-child(2n)", etc.).
30    #[get(pub(crate))]
31    #[get_mut(pub(crate))]
32    #[set(pub(crate))]
33    selector: String,
34    /// The CSS style declarations for this pseudo rule
35    /// (e.g., "background: rgba(79, 70, 229, 0.04); color: #4f46e5;").
36    #[get(pub(crate))]
37    #[get_mut(pub(crate))]
38    #[set(pub(crate))]
39    style: String,
40}
41
42/// Represents a CSS class with a name, its style declarations, and optional pseudo rules.
43///
44/// Created by the `class!` macro and used in `html!` via the `class:` attribute.
45/// When the renderer encounters a `Css`, it injects the styles into the
46/// DOM's `<style>` element on first use and applies the class name to the element.
47#[derive(Clone, Data, Debug, Default, New)]
48pub struct Css {
49    /// The CSS class name used in the DOM.
50    #[get_mut(pub(crate))]
51    #[set(pub(crate))]
52    name: String,
53    /// The CSS style declarations (e.g., "max-width: 800px; margin: 0 auto;").
54    #[get_mut(pub(crate))]
55    #[set(pub(crate))]
56    style: String,
57    /// The pseudo-class and pseudo-element rules for this class
58    /// (e.g., ":hover", ":focus", ":active", "::before", etc.).
59    #[get_mut(pub(crate))]
60    #[set(pub(crate))]
61    pseudo_rules: Vec<PseudoRule>,
62    /// The media query rules for this class.
63    #[get_mut(pub(crate))]
64    #[set(pub(crate))]
65    media_rules: Vec<MediaRule>,
66}
67
68/// Represents a CSS @media rule attached to a class.
69///
70/// Each media rule has a query string (e.g., "(max-width: 767px)"),
71/// a style declaration string, and optional nested pseudo-element rules.
72/// When injected into the DOM, it produces a rule like:
73/// `@media (max-width: 767px) { .class-name { font-size: 14px; } .class-name::-webkit-scrollbar { width: 0px; } }`.
74#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
75pub struct MediaRule {
76    /// The media query condition string (e.g., "(max-width: 767px)").
77    #[get(pub(crate))]
78    #[get_mut(pub(crate))]
79    #[set(pub(crate))]
80    query: String,
81    /// The CSS style declarations inside this media rule
82    /// (e.g., "font-size: 14px; padding: 8px;").
83    #[get(pub(crate))]
84    #[get_mut(pub(crate))]
85    #[set(pub(crate))]
86    style: String,
87    /// The pseudo-element rules nested inside this media rule
88    /// (e.g., `::-webkit-scrollbar { width: "0px"; }`).
89    #[get(pub(crate))]
90    #[get_mut(pub(crate))]
91    #[set(pub(crate))]
92    pseudo_rules: Vec<PseudoRule>,
93}
94
95/// Adapts various event value types into an `AttributeValue` for event attributes.
96///
97/// The `html!` macro generates `EventAdapter::new(expr).into_attribute(event_name)`
98/// instead of inline trait dispatch boilerplate. This eliminates the per-attribute-site
99/// generation of `__EventWrapper`, `__IsClosure`, `__ClosurePicker`, `__ValuePicker`,
100/// `__FallbackHelper`, and `__dispatch` types, significantly reducing macro output size.
101///
102/// The adapter pattern handles three cases:
103/// - `FnMut(NativeEvent)` closure → `AttributeValue::Event` via `NativeEventHandler`
104/// - `NativeEventHandler` directly → `AttributeValue::Event` as-is
105/// - `Option<NativeEventHandler>` → `AttributeValue::Event` or `AttributeValue::Text`
106#[derive(Data, New)]
107pub struct EventAdapter<T> {
108    /// The wrapped value to be adapted into an attribute.
109    #[get(pub(crate))]
110    #[get_mut(pub(crate))]
111    #[set(pub(crate))]
112    pub(crate) inner: T,
113}
114
115/// Adapts an arbitrary attribute value expression into an `AttributeValue`.
116///
117/// Handles the dispatch between event closures and reactive values without
118/// requiring the macro to generate inline trait hierarchies. The macro emits
119/// `AttrValueAdapter::new(expr).into_attribute_value()` instead of the
120/// `__IsClosure` / `__ClosurePicker` / `__ValuePicker` / `__FallbackHelper`
121/// / `__dispatch` boilerplate.
122///
123/// For event attributes (key starts with "on"), event closures are wrapped
124/// into `AttributeValue::Event`. For non-event attributes, values are
125/// converted via `IntoReactiveValue`.
126#[derive(Data, Debug, New)]
127pub struct AttrValueAdapter<T> {
128    /// The wrapped value to be adapted into an attribute.
129    #[get(pub(crate))]
130    #[get_mut(pub(crate))]
131    #[set(pub(crate))]
132    pub(crate) inner: T,
133}
134
135/// A `Sync` wrapper for single-threaded global `HashSet` access.
136///
137/// SAFETY: This type is only safe to use in single-threaded contexts
138/// (e.g., WASM). It implements `Sync` to allow usage as a `static mut`
139/// variable, but concurrent access from multiple threads would be
140/// undefined behavior.
141#[derive(Data, Debug, New)]
142pub(crate) struct InjectedClassesCell(
143    /// Interior-mutable storage for the injected classes set.
144    #[get(pub(crate))]
145    #[get_mut(pub(crate))]
146    #[set(pub(crate))]
147    pub(crate) UnsafeCell<Option<HashSet<String>>>,
148);