Skip to main content

euv_core/vdom/attribute/
struct.rs

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