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 { evaluated, js } = self.value;
30
31        Attribute::new(self.key.clone(), evaluated).into_view_parts(cx, parts);
32        Attribute::new(
33            (Unescaped::new_unchecked("data-topcoat-bind:"), self.key),
34            js,
35        )
36        .into_view_parts(cx, parts);
37    }
38}