Skip to main content

topcoat_runtime/
bind_attribute.rs

1use topcoat_core::context::Cx;
2use topcoat_view::{
3    Attribute, AttributeKeyViewParts, AttributeValueViewParts, AttributeViewParts, PartsWriter,
4    Unescaped,
5};
6
7use crate::Expr;
8
9#[derive(Debug, Clone)]
10pub struct BindAttribute<K, V> {
11    key: K,
12    value: Expr<V>,
13}
14
15impl<K, V> BindAttribute<K, V> {
16    #[inline]
17    pub fn new(key: K, value: Expr<V>) -> Self {
18        Self { key, value }
19    }
20}
21
22impl<K, V> AttributeViewParts for BindAttribute<K, V>
23where
24    K: AttributeKeyViewParts + Clone,
25    V: AttributeValueViewParts,
26{
27    #[inline]
28    fn into_view_parts(self, cx: &Cx, parts: &mut PartsWriter<'_>) {
29        let Expr {
30            evaluated,
31            js,
32            is_static,
33        } = self.value;
34
35        if is_static {
36            Attribute::new(self.key, evaluated).into_view_parts(cx, parts);
37            return;
38        }
39
40        Attribute::new(self.key.clone(), evaluated).into_view_parts(cx, parts);
41        Attribute::new(
42            (Unescaped::new_unchecked("data-topcoat-bind:"), self.key),
43            js,
44        )
45        .into_view_parts(cx, parts);
46    }
47}