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(pub(crate))]
60    #[get_mut(pub(crate))]
61    #[set(pub(crate))]
62    pseudo_rules: Vec<PseudoRule>,
63    /// The media query rules for this class.
64    #[get(pub(crate))]
65    #[get_mut(pub(crate))]
66    #[set(pub(crate))]
67    media_rules: Vec<MediaRule>,
68}
69
70/// Represents a CSS @media rule attached to a class.
71///
72/// Each media rule has a query string (e.g., "(max-width: 767px)")
73/// and a style declaration string. When injected into the DOM, it produces
74/// a rule like `@media (max-width: 767px) { .class-name { font-size: 14px; } }`.
75#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
76pub struct MediaRule {
77    /// The media query condition string (e.g., "(max-width: 767px)").
78    #[get(pub(crate))]
79    #[get_mut(pub(crate))]
80    #[set(pub(crate))]
81    query: String,
82    /// The CSS style declarations inside this media rule
83    /// (e.g., "font-size: 14px; padding: 8px;").
84    #[get(pub(crate))]
85    #[get_mut(pub(crate))]
86    #[set(pub(crate))]
87    style: String,
88}
89
90/// Adapts various event value types into an `AttributeValue` for event attributes.
91///
92/// The `html!` macro generates `EventAdapter::new(expr).into_attribute(event_name)`
93/// instead of inline trait dispatch boilerplate. This eliminates the per-attribute-site
94/// generation of `__EventWrapper`, `__IsClosure`, `__ClosurePicker`, `__ValuePicker`,
95/// `__FallbackHelper`, and `__dispatch` types, significantly reducing macro output size.
96///
97/// The adapter pattern handles three cases:
98/// - `FnMut(NativeEvent)` closure → `AttributeValue::Event` via `NativeEventHandler`
99/// - `NativeEventHandler` directly → `AttributeValue::Event` as-is
100/// - `Option<NativeEventHandler>` → `AttributeValue::Event` or `AttributeValue::Text`
101#[derive(Data, New)]
102pub struct EventAdapter<T> {
103    /// The wrapped value to be adapted into an attribute.
104    #[get(pub(crate))]
105    #[get_mut(pub(crate))]
106    #[set(pub(crate))]
107    pub(crate) inner: T,
108}
109
110/// Adapts an arbitrary attribute value expression into an `AttributeValue`.
111///
112/// Handles the dispatch between event closures and reactive values without
113/// requiring the macro to generate inline trait hierarchies. The macro emits
114/// `AttrValueAdapter::new(expr).into_attribute_value()` instead of the
115/// `__IsClosure` / `__ClosurePicker` / `__ValuePicker` / `__FallbackHelper`
116/// / `__dispatch` boilerplate.
117///
118/// For event attributes (key starts with "on"), event closures are wrapped
119/// into `AttributeValue::Event`. For non-event attributes, values are
120/// converted via `IntoReactiveValue`.
121#[derive(Data, Debug, New)]
122pub struct AttrValueAdapter<T> {
123    /// The wrapped value to be adapted into an attribute.
124    #[get(pub(crate))]
125    #[get_mut(pub(crate))]
126    #[set(pub(crate))]
127    pub(crate) inner: T,
128}
129
130/// A `Sync` wrapper for single-threaded global `HashSet` access.
131///
132/// SAFETY: This type is only safe to use in single-threaded contexts
133/// (e.g., WASM). It implements `Sync` to allow usage as a `static mut`
134/// variable, but concurrent access from multiple threads would be
135/// undefined behavior.
136#[derive(Data, Debug, New)]
137pub(crate) struct InjectedClassesCell(
138    /// Interior-mutable storage for the injected classes set.
139    #[get(pub(crate))]
140    #[get_mut(pub(crate))]
141    #[set(pub(crate))]
142    pub(crate) UnsafeCell<Option<HashSet<String>>>,
143);