1use crate::*;
2
3impl PartialEq for AttributeValue {
10 fn eq(&self, other: &Self) -> bool {
11 match (self, other) {
12 (AttributeValue::Text(a_val), AttributeValue::Text(b_val)) => a_val == b_val,
13 (AttributeValue::Signal(a_sig), AttributeValue::Signal(b_sig)) => {
14 a_sig.get() == b_sig.get()
15 }
16 (AttributeValue::Signal(a_sig), AttributeValue::Text(b_val)) => a_sig.get() == *b_val,
17 (AttributeValue::Text(a_val), AttributeValue::Signal(b_sig)) => *a_val == b_sig.get(),
18 (AttributeValue::Event(_), AttributeValue::Event(_)) => true,
19 (AttributeValue::Css(a_css), AttributeValue::Css(b_css)) => {
20 a_css.get_name() == b_css.get_name()
21 }
22 (AttributeValue::Dynamic(a_dyn), AttributeValue::Dynamic(b_dyn)) => a_dyn == b_dyn,
23 _ => false,
24 }
25 }
26}
27
28impl PartialEq for AttributeEntry {
33 fn eq(&self, other: &Self) -> bool {
34 self.get_name() == other.get_name() && self.get_value() == other.get_value()
35 }
36}
37
38impl PartialEq for TextNode {
43 fn eq(&self, other: &Self) -> bool {
44 self.get_content() == other.get_content()
45 }
46}
47
48impl PartialEq for CssClass {
53 fn eq(&self, other: &Self) -> bool {
54 self.get_name() == other.get_name()
55 }
56}
57
58impl PartialEq for VirtualNode {
68 fn eq(&self, other: &Self) -> bool {
69 match (self, other) {
70 (VirtualNode::Text(a_text), VirtualNode::Text(b_text)) => a_text == b_text,
71 (
72 VirtualNode::Element {
73 tag: a_tag,
74 attributes: a_attrs,
75 children: a_children,
76 ..
77 },
78 VirtualNode::Element {
79 tag: b_tag,
80 attributes: b_attrs,
81 children: b_children,
82 ..
83 },
84 ) => {
85 a_tag == b_tag
86 && a_attrs.len() == b_attrs.len()
87 && a_attrs.iter().zip(b_attrs.iter()).all(|(a, b)| a == b)
88 && a_children.len() == b_children.len()
89 && a_children
90 .iter()
91 .zip(b_children.iter())
92 .all(|(a, b)| a == b)
93 }
94 (VirtualNode::Fragment(a_children), VirtualNode::Fragment(b_children)) => {
95 a_children.len() == b_children.len()
96 && a_children
97 .iter()
98 .zip(b_children.iter())
99 .all(|(a, b)| a == b)
100 }
101 (VirtualNode::Dynamic(_), VirtualNode::Dynamic(_)) => true,
102 (VirtualNode::Empty, VirtualNode::Empty) => true,
103 _ => false,
104 }
105 }
106}
107
108impl Attribute {
110 pub fn as_str(&self) -> String {
112 match self {
113 Attribute::AccessKey => "accesskey".to_string(),
114 Attribute::Action => "action".to_string(),
115 Attribute::Alt => "alt".to_string(),
116 Attribute::AriaLabel => "aria-label".to_string(),
117 Attribute::AutoComplete => "autocomplete".to_string(),
118 Attribute::AutoFocus => "autofocus".to_string(),
119 Attribute::Checked => "checked".to_string(),
120 Attribute::Class => "class".to_string(),
121 Attribute::Cols => "cols".to_string(),
122 Attribute::ContentEditable => "contenteditable".to_string(),
123 Attribute::Data(name) => format!("data-{}", name),
124 Attribute::Dir => "dir".to_string(),
125 Attribute::Disabled => "disabled".to_string(),
126 Attribute::Draggable => "draggable".to_string(),
127 Attribute::EncType => "enctype".to_string(),
128 Attribute::For => "for".to_string(),
129 Attribute::Form => "form".to_string(),
130 Attribute::Height => "height".to_string(),
131 Attribute::Hidden => "hidden".to_string(),
132 Attribute::Href => "href".to_string(),
133 Attribute::Id => "id".to_string(),
134 Attribute::Lang => "lang".to_string(),
135 Attribute::Max => "max".to_string(),
136 Attribute::MaxLength => "maxlength".to_string(),
137 Attribute::Method => "method".to_string(),
138 Attribute::Min => "min".to_string(),
139 Attribute::MinLength => "minlength".to_string(),
140 Attribute::Multiple => "multiple".to_string(),
141 Attribute::Name => "name".to_string(),
142 Attribute::Pattern => "pattern".to_string(),
143 Attribute::Placeholder => "placeholder".to_string(),
144 Attribute::ReadOnly => "readonly".to_string(),
145 Attribute::Required => "required".to_string(),
146 Attribute::Rows => "rows".to_string(),
147 Attribute::Selected => "selected".to_string(),
148 Attribute::Size => "size".to_string(),
149 Attribute::SpellCheck => "spellcheck".to_string(),
150 Attribute::Src => "src".to_string(),
151 Attribute::Step => "step".to_string(),
152 Attribute::Style => "style".to_string(),
153 Attribute::TabIndex => "tabindex".to_string(),
154 Attribute::Target => "target".to_string(),
155 Attribute::Title => "title".to_string(),
156 Attribute::Type => "type".to_string(),
157 Attribute::Value => "value".to_string(),
158 Attribute::Width => "width".to_string(),
159 Attribute::Other(name) => name.clone(),
160 }
161 }
162}
163
164impl Clone for DynamicNode {
166 fn clone(&self) -> Self {
167 DynamicNode {
168 render_fn: Rc::clone(&self.render_fn),
169 hook_context: self.hook_context,
170 }
171 }
172}
173
174impl AsNode for VirtualNode {
176 fn as_node(&self) -> Option<VirtualNode> {
177 Some(self.clone())
178 }
179}
180
181impl AsNode for &VirtualNode {
183 fn as_node(&self) -> Option<VirtualNode> {
184 Some((*self).clone())
185 }
186}
187
188impl AsNode for String {
190 fn as_node(&self) -> Option<VirtualNode> {
191 Some(VirtualNode::Text(TextNode::new(self.clone(), None)))
192 }
193}
194
195impl AsNode for &str {
197 fn as_node(&self) -> Option<VirtualNode> {
198 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
199 }
200}
201
202impl AsNode for i32 {
204 fn as_node(&self) -> Option<VirtualNode> {
205 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
206 }
207}
208
209impl AsNode for i64 {
211 fn as_node(&self) -> Option<VirtualNode> {
212 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
213 }
214}
215
216impl AsNode for usize {
218 fn as_node(&self) -> Option<VirtualNode> {
219 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
220 }
221}
222
223impl AsNode for f32 {
225 fn as_node(&self) -> Option<VirtualNode> {
226 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
227 }
228}
229
230impl AsNode for f64 {
232 fn as_node(&self) -> Option<VirtualNode> {
233 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
234 }
235}
236
237impl AsNode for bool {
239 fn as_node(&self) -> Option<VirtualNode> {
240 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
241 }
242}
243
244impl<T> AsNode for Signal<T>
246where
247 T: Clone + PartialEq + std::fmt::Display + 'static,
248{
249 fn as_node(&self) -> Option<VirtualNode> {
250 Some(self.as_reactive_text())
251 }
252}
253
254impl IntoNode for VirtualNode {
256 fn into_node(self) -> VirtualNode {
257 self
258 }
259}
260
261impl<F> IntoNode for F
266where
267 F: FnMut() -> VirtualNode + 'static,
268{
269 fn into_node(self) -> VirtualNode {
270 VirtualNode::Dynamic(DynamicNode {
271 render_fn: Rc::new(RefCell::new(self)),
272 hook_context: crate::reactive::create_hook_context(),
273 })
274 }
275}
276
277impl IntoNode for String {
279 fn into_node(self) -> VirtualNode {
280 VirtualNode::Text(TextNode::new(self, None))
281 }
282}
283
284impl IntoNode for &str {
286 fn into_node(self) -> VirtualNode {
287 VirtualNode::Text(TextNode::new(self.to_string(), None))
288 }
289}
290
291impl IntoNode for i32 {
293 fn into_node(self) -> VirtualNode {
294 VirtualNode::Text(TextNode::new(self.to_string(), None))
295 }
296}
297
298impl IntoNode for usize {
300 fn into_node(self) -> VirtualNode {
301 VirtualNode::Text(TextNode::new(self.to_string(), None))
302 }
303}
304
305impl IntoNode for bool {
307 fn into_node(self) -> VirtualNode {
308 VirtualNode::Text(TextNode::new(self.to_string(), None))
309 }
310}
311
312impl<T> IntoNode for Signal<T>
314where
315 T: Clone + PartialEq + std::fmt::Display + 'static,
316{
317 fn into_node(self) -> VirtualNode {
318 self.as_reactive_text()
319 }
320}
321
322impl VirtualNode {
324 pub fn get_element_node(tag_name: &str) -> Self {
326 VirtualNode::Element {
327 tag: Tag::Element(tag_name.to_string()),
328 attributes: Vec::new(),
329 children: Vec::new(),
330 key: None,
331 }
332 }
333
334 pub fn get_text_node(content: &str) -> Self {
336 VirtualNode::Text(TextNode::new(content.to_string(), None))
337 }
338
339 pub fn with_attribute(mut self, name: &str, value: AttributeValue) -> Self {
341 if let VirtualNode::Element {
342 ref mut attributes, ..
343 } = self
344 {
345 attributes.push(AttributeEntry::new(name.to_string(), value));
346 }
347 self
348 }
349
350 pub fn with_child(mut self, child: VirtualNode) -> Self {
352 if let VirtualNode::Element {
353 ref mut children, ..
354 } = self
355 {
356 children.push(child);
357 }
358 self
359 }
360
361 pub fn is_component(&self) -> bool {
363 matches!(
364 self,
365 VirtualNode::Element {
366 tag: Tag::Component(_),
367 ..
368 }
369 )
370 }
371
372 pub fn tag_name(&self) -> Option<String> {
374 match self {
375 VirtualNode::Element { tag, .. } => match tag {
376 Tag::Element(name) => Some(name.clone()),
377 Tag::Component(name) => Some(name.clone()),
378 },
379 _ => None,
380 }
381 }
382
383 pub fn try_get_prop(&self, name: &Attribute) -> Option<String> {
385 let name_str: String = name.as_str();
386 if let VirtualNode::Element { attributes, .. } = self {
387 for attr in attributes {
388 if attr.get_name() == &name_str {
389 match attr.get_value() {
390 AttributeValue::Text(value) => return Some(value.clone()),
391 AttributeValue::Signal(signal) => return Some(signal.get()),
392 _ => {}
393 }
394 }
395 }
396 }
397 None
398 }
399
400 pub fn try_get_signal_prop(&self, name: &Attribute) -> Option<Signal<String>> {
405 let name_str: String = name.as_str();
406 if let VirtualNode::Element { attributes, .. } = self {
407 for attr in attributes {
408 if attr.get_name() == &name_str
409 && let AttributeValue::Signal(signal) = attr.get_value()
410 {
411 return Some(*signal);
412 }
413 }
414 }
415 None
416 }
417
418 pub fn get_children(&self) -> Vec<VirtualNode> {
420 if let VirtualNode::Element { children, .. } = self {
421 children.clone()
422 } else {
423 Vec::new()
424 }
425 }
426
427 pub fn try_get_text(&self) -> Option<String> {
429 match self {
430 VirtualNode::Text(text_node) => Some(text_node.get_content().clone()),
431 VirtualNode::Element { children, .. } => {
432 children.first().and_then(VirtualNode::try_get_text)
433 }
434 _ => None,
435 }
436 }
437
438 pub fn try_get_event(
440 &self,
441 name: &NativeEventName,
442 ) -> Option<crate::event::NativeEventHandler> {
443 let name_str: String = name.as_str();
444 if let VirtualNode::Element { attributes, .. } = self {
445 for attr in attributes {
446 if attr.get_name() == &name_str
447 && let AttributeValue::Event(handler) = attr.get_value()
448 {
449 return Some(handler.clone());
450 }
451 }
452 }
453 None
454 }
455
456 pub fn try_get_callback(&self, name: &str) -> Option<crate::event::NativeEventHandler> {
458 if let VirtualNode::Element { attributes, .. } = self {
459 for attr in attributes {
460 if attr.get_name() == name
461 && let AttributeValue::Event(handler) = attr.get_value()
462 {
463 return Some(handler.clone());
464 }
465 }
466 }
467 None
468 }
469}
470
471impl<T> AsReactiveText for Signal<T>
473where
474 T: Clone + PartialEq + std::fmt::Display + 'static,
475{
476 fn as_reactive_text(&self) -> VirtualNode {
477 let signal: Signal<T> = *self;
478 let initial: String = signal.get().to_string();
479 let string_signal: Signal<String> = {
480 let boxed: Box<SignalInner<String>> = Box::new(SignalInner::new(initial.clone()));
481 Signal::from_inner(Box::leak(boxed) as *mut SignalInner<String>)
482 };
483 let source_signal: Signal<T> = *self;
484 let string_signal_clone: Signal<String> = string_signal;
485 source_signal.subscribe({
486 let source_signal: Signal<T> = source_signal;
487 move || {
488 let new_value: String = source_signal.get().to_string();
489 string_signal_clone.set(new_value);
490 }
491 });
492 VirtualNode::Text(TextNode::new(initial, Some(string_signal)))
493 }
494}
495
496impl Style {
498 pub fn property<N, V>(mut self, name: N, value: V) -> Self
503 where
504 N: AsRef<str>,
505 V: AsRef<str>,
506 {
507 self.get_mut_properties().push(StyleProperty::new(
508 name.as_ref().replace('_', "-"),
509 value.as_ref().to_string(),
510 ));
511 self
512 }
513
514 pub fn to_css_string(&self) -> String {
516 self.get_properties()
517 .iter()
518 .map(|p| format!("{}: {};", p.get_name(), p.get_value()))
519 .collect::<Vec<String>>()
520 .join(" ")
521 }
522}
523
524impl Default for Style {
526 fn default() -> Self {
527 Self::new(Vec::new())
528 }
529}
530
531impl StyleProperty {
533 pub fn new(name: String, value: String) -> Self {
535 let mut prop: StyleProperty = StyleProperty::default();
536 prop.set_name(name);
537 prop.set_value(value);
538 prop
539 }
540}
541
542impl CssClass {
544 pub fn new(name: String, style: String) -> Self {
548 let mut css_class: CssClass = CssClass::default();
549 css_class.set_name(name);
550 css_class.set_style(style);
551 css_class.inject_style();
552 css_class
553 }
554
555 pub fn inject_style(&self) {
562 #[cfg(target_arch = "wasm32")]
563 {
564 let style_id: &str = "euv-css-injected";
565 let document: web_sys::Document = web_sys::window()
566 .expect("no global window exists")
567 .document()
568 .expect("no document exists");
569 let style_element: web_sys::HtmlStyleElement = match document
570 .get_element_by_id(style_id)
571 {
572 Some(el) => el.dyn_into::<web_sys::HtmlStyleElement>().unwrap(),
573 None => {
574 let el: web_sys::HtmlStyleElement = document
575 .create_element("style")
576 .unwrap()
577 .dyn_into::<web_sys::HtmlStyleElement>()
578 .unwrap();
579 el.set_id(style_id);
580 let keyframes: &str = "@keyframes euv-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } @keyframes euv-fade-in { from { opacity: 0; } to { opacity: 1; } } @keyframes euv-scale-in { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } } @keyframes euv-pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.2); } } @keyframes euv-slide-up { from { transform: translateY(100%); } to { transform: translateY(0); } } @keyframes euv-slide-left { from { transform: translateX(-100%); } to { transform: translateX(0); } } @keyframes euv-fade-in-up { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }";
581 let global: &str = "html, body, #app { height: 100%; margin: 0; padding: 0; overflow: hidden; } * { -webkit-tap-highlight-color: transparent; }";
582 let media_queries: &str = "@media (max-width: 767px) { .c_app_nav { display: none; } .c_app_main { padding: 20px 16px; max-width: 100%; } .c_page_title { font-size: 22px; } .c_page_subtitle { font-size: 14px; } .c_card { padding: 16px; margin: 12px 0; border-radius: 10px; } .c_card_title { font-size: 16px; } .c_form_grid { grid-template-columns: 1fr; } .c_browser_api_row { grid-template-columns: 1fr; } .c_modal_content { max-width: 100%; width: calc(100% - 32px); border-radius: 16px 16px 0 0; position: fixed; bottom: 0; left: 16px; height: 80vh; animation: euv-slide-up 0.25s ease; } .c_modal_overlay { align-items: flex-end; } .c_event_stats { gap: 12px; flex-wrap: wrap; } .c_event_section_row { gap: 12px; flex-wrap: wrap; } .c_event_section_col { min-width: 100%; } .c_counter_value { font-size: 20px; } .c_timer_value { font-size: 36px; } .c_not_found_code { font-size: 56px; } .c_not_found_container { padding: 40px 20px; } .c_list_input_row { flex-direction: column; } .c_vconsole_button { bottom: 16px; right: 16px; width: 44px; height: 44px; border-radius: 12px; } .c_tab_bar { flex-wrap: wrap; } .c_primary_button { padding: 10px 18px; font-size: 14px; } .c_badge { padding: 4px 10px; font-size: 11px; } .c_badge_outline { padding: 4px 10px; font-size: 11px; } .c_browser_info_grid { grid-template-columns: 1fr; } .c_anim_spin { font-size: 36px; } .c_anim_spin_stopped { font-size: 36px; } .c_anim_pulse { font-size: 36px; } .c_anim_pulse_stopped { font-size: 36px; } }";
583 el.set_inner_text(&format!("{} {} {}", global, keyframes, media_queries));
584 document.head().unwrap().append_child(&el).unwrap();
585 el
586 }
587 };
588 let existing_css: String = style_element.inner_text();
589 let class_rule: String = format!(".{} {{ {} }}", self.get_name(), self.get_style());
590 if !existing_css.contains(&class_rule) {
591 let new_css: String = if existing_css.is_empty() {
592 class_rule
593 } else {
594 format!("{}\n{}", existing_css, class_rule)
595 };
596 style_element.set_inner_text(&new_css);
597 }
598 }
599 }
600}
601
602impl std::fmt::Display for CssClass {
607 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
608 write!(f, "{}", self.get_name())
609 }
610}