tachys 0.2.15

Tools for building reactivity-agnostic, renderer-generic, statically-typed view trees for user interface libraries.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use crate::{
    dom::{event_target_checked, event_target_value},
    html::{
        attribute::{
            maybe_next_attr_erasure_macros::{
                next_attr_combine, next_attr_output_type,
            },
            Attribute, AttributeKey, AttributeValue, NamedAttributeKey,
            NextAttribute,
        },
        event::{change, input, on},
        property::{prop, IntoProperty},
    },
    prelude::AddAnyAttr,
    renderer::{types::Element, RemoveEventHandler},
    view::{Position, ToTemplate},
};
use reactive_graph::{
    signal::{ReadSignal, RwSignal, WriteSignal},
    traits::{Get, Set},
    wrappers::read::Signal,
};
use send_wrapper::SendWrapper;
use wasm_bindgen::JsValue;
#[cfg(feature = "reactive_stores")]
use {
    reactive_graph::owner::Storage,
    reactive_stores::{
        ArcField, AtIndex, AtKeyed, DerefedField, Field, KeyedSubfield,
        StoreField, Subfield,
    },
    std::ops::{Deref, DerefMut, IndexMut},
};

/// `group` attribute used for radio inputs with `bind`.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Group;

impl AttributeKey for Group {
    const KEY: &'static str = "group";
}

/// Adds a two-way binding to the element, which adds an attribute and an event listener to the
/// element when the element is created or hydrated.
pub trait BindAttribute<Key, Sig, T>
where
    Key: AttributeKey,
    Sig: IntoSplitSignal<Value = T>,
    T: FromEventTarget + AttributeValue + 'static,
{
    /// The type of the element with the two-way binding added.
    type Output;

    /// Adds a two-way binding to the element, which adds an attribute and an event listener to the
    /// element when the element is created or hydrated.
    ///
    /// Example:
    ///
    /// ```ignore
    /// // You can use `RwSignal`s
    /// let is_awesome = RwSignal::new(true);
    ///
    /// // And you can use split signals
    /// let (text, set_text) = signal("Hello world".to_string());
    ///
    /// // Use `Checked` and a `bool` signal for a checkbox
    /// checkbox_element.bind(Checked, is_awesome);
    ///
    /// // Use `Group` and `String` for radio inputs
    /// radio_element.bind(Group, (text, set_text));
    ///
    /// // Use `Value` and `String` for everything else
    /// input_element.bind(Value, (text, set_text));
    /// ```
    ///
    /// Depending on the input different events are listened to.
    /// - `<input type="checkbox">`, `<input type="radio">` and `<select>` use the `change` event;
    /// - `<input>` with the rest of the types and `<textarea>` elements use the `input` event;
    fn bind(self, key: Key, signal: Sig) -> Self::Output;
}

impl<V, Key, Sig, T> BindAttribute<Key, Sig, T> for V
where
    V: AddAnyAttr,
    Key: AttributeKey,
    Sig: IntoSplitSignal<Value = T>,
    T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static,
    Signal<BoolOrT<T>>: IntoProperty,
    <Sig as IntoSplitSignal>::Read:
        Get<Value = T> + Send + Sync + Clone + 'static,
    <Sig as IntoSplitSignal>::Write: Send + Clone + 'static,
    Element: GetValue<T>,
{
    type Output = <Self as AddAnyAttr>::Output<
        Bind<
            Key,
            T,
            <Sig as IntoSplitSignal>::Read,
            <Sig as IntoSplitSignal>::Write,
        >,
    >;

    fn bind(self, key: Key, signal: Sig) -> Self::Output {
        self.add_any_attr(bind(key, signal))
    }
}

/// Adds a two-way binding to the element, which adds an attribute and an event listener to the
/// element when the element is created or hydrated.
#[inline(always)]
pub fn bind<Key, Sig, T>(
    key: Key,
    signal: Sig,
) -> Bind<Key, T, <Sig as IntoSplitSignal>::Read, <Sig as IntoSplitSignal>::Write>
where
    Key: AttributeKey,
    Sig: IntoSplitSignal<Value = T>,
    T: FromEventTarget + AttributeValue + 'static,
    <Sig as IntoSplitSignal>::Read: Get<Value = T> + Clone + 'static,
    <Sig as IntoSplitSignal>::Write: Send + Clone + 'static,
{
    let (read_signal, write_signal) = signal.into_split_signal();

    Bind {
        key,
        read_signal,
        write_signal,
    }
}

/// Two-way binding of an attribute and an event listener
#[derive(Debug)]
pub struct Bind<Key, T, R, W>
where
    Key: AttributeKey,
    T: FromEventTarget + AttributeValue + 'static,
    R: Get<Value = T> + Clone + 'static,
    W: Set<Value = T>,
{
    key: Key,
    read_signal: R,
    write_signal: W,
}

impl<Key, T, R, W> Clone for Bind<Key, T, R, W>
where
    Key: AttributeKey,
    T: FromEventTarget + AttributeValue + 'static,
    R: Get<Value = T> + Clone + 'static,
    W: Set<Value = T> + Clone,
{
    fn clone(&self) -> Self {
        Self {
            key: self.key.clone(),
            read_signal: self.read_signal.clone(),
            write_signal: self.write_signal.clone(),
        }
    }
}

impl<Key, T, R, W> Bind<Key, T, R, W>
where
    Key: AttributeKey,
    T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static,
    R: Get<Value = T> + Clone + Send + Sync + 'static,
    W: Set<Value = T> + Clone + 'static,
    Element: ChangeEvent + GetValue<T>,
{
    /// Attaches the event listener that updates the signal value to the element.
    pub fn attach(self, el: &Element) -> RemoveEventHandler<Element> {
        el.attach_change_event::<T, W>(Key::KEY, self.write_signal.clone())
    }

    /// Creates the signal to update the value of the attribute. This signal is different
    /// when using a `"group"` attribute
    pub fn read_signal(&self, el: &Element) -> Signal<BoolOrT<T>> {
        let read_signal = self.read_signal.clone();

        if Key::KEY == "group" {
            let el = SendWrapper::new(el.clone());

            Signal::derive(move || {
                BoolOrT::Bool(el.get_value() == read_signal.get())
            })
        } else {
            Signal::derive(move || BoolOrT::T(read_signal.get()))
        }
    }

    /// Returns the key of the attribute. If the key is `"group"` it returns `"checked"`, otherwise
    /// the one which was provided originally.
    pub fn key(&self) -> &'static str {
        if Key::KEY == "group" {
            "checked"
        } else {
            Key::KEY
        }
    }
}

impl<Key, T, R, W> Attribute for Bind<Key, T, R, W>
where
    Key: AttributeKey,
    T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static,
    R: Get<Value = T> + Clone + Send + Sync + 'static,
    Signal<BoolOrT<T>>: IntoProperty,
    W: Set<Value = T> + Clone + Send + 'static,
    Element: ChangeEvent + GetValue<T>,
{
    const MIN_LENGTH: usize = 0;

    type State = (
        <Signal<BoolOrT<T>> as IntoProperty>::State,
        (Element, Option<RemoveEventHandler<Element>>),
    );
    type AsyncOutput = Self;
    type Cloneable = Bind<Key, T, R, W>;
    type CloneableOwned = Bind<Key, T, R, W>;

    fn html_len(&self) -> usize {
        0
    }

    fn to_html(
        self,
        _buf: &mut String,
        _class: &mut String,
        _style: &mut String,
        _inner_html: &mut String,
    ) {
    }

    #[inline(always)]
    fn hydrate<const FROM_SERVER: bool>(self, el: &Element) -> Self::State {
        let signal = self.read_signal(el);
        let attr_state = prop(self.key(), signal).hydrate::<FROM_SERVER>(el);

        let cleanup = self.attach(el);

        (attr_state, (el.clone(), Some(cleanup)))
    }

    #[inline(always)]
    fn build(self, el: &Element) -> Self::State {
        let signal = self.read_signal(el);
        let attr_state = prop(self.key(), signal).build(el);

        let cleanup = self.attach(el);

        (attr_state, (el.clone(), Some(cleanup)))
    }

    #[inline(always)]
    fn rebuild(self, state: &mut Self::State) {
        let (attr_state, (el, prev_cleanup)) = state;

        let signal = self.read_signal(el);
        prop(self.key(), signal).rebuild(attr_state);

        if let Some(prev) = prev_cleanup.take() {
            if let Some(remove) = prev.into_inner() {
                remove();
            }
        }
        *prev_cleanup = Some(self.attach(el));
    }

    fn into_cloneable(self) -> Self::Cloneable {
        self.into_cloneable_owned()
    }

    fn into_cloneable_owned(self) -> Self::CloneableOwned {
        self
    }

    fn dry_resolve(&mut self) {}

    async fn resolve(self) -> Self::AsyncOutput {
        self
    }

    fn keys(&self) -> Vec<NamedAttributeKey> {
        vec![]
    }
}

impl<Key, T, R, W> NextAttribute for Bind<Key, T, R, W>
where
    Key: AttributeKey,
    T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static,
    R: Get<Value = T> + Clone + Send + Sync + 'static,
    Signal<BoolOrT<T>>: IntoProperty,
    W: Set<Value = T> + Clone + Send + 'static,
    Element: ChangeEvent + GetValue<T>,
{
    next_attr_output_type!(Self, NewAttr);

    fn add_any_attr<NewAttr: Attribute>(
        self,
        new_attr: NewAttr,
    ) -> Self::Output<NewAttr> {
        next_attr_combine!(self, new_attr)
    }
}

impl<Key, T, R, W> ToTemplate for Bind<Key, T, R, W>
where
    Key: AttributeKey,
    T: FromEventTarget + AttributeValue + 'static,
    R: Get<Value = T> + Clone + 'static,
    W: Set<Value = T> + Clone,
{
    #[inline(always)]
    fn to_template(
        _buf: &mut String,
        _class: &mut String,
        _style: &mut String,
        _inner_html: &mut String,
        _position: &mut Position,
    ) {
    }
}

/// Splits a combined signal into its read and write parts.
///
/// This allows you to either provide a `RwSignal` or a tuple `(ReadSignal, WriteSignal)`.
pub trait IntoSplitSignal {
    /// The actual contained value of the signal
    type Value;
    /// The read part of the signal
    type Read: Get<Value = Self::Value>;
    /// The write part of the signal
    type Write: Set<Value = Self::Value>;
    /// Splits a combined signal into its read and write parts.
    fn into_split_signal(self) -> (Self::Read, Self::Write);
}

impl<T> IntoSplitSignal for RwSignal<T>
where
    T: Send + Sync + 'static,
    ReadSignal<T>: Get<Value = T>,
{
    type Value = T;
    type Read = ReadSignal<T>;
    type Write = WriteSignal<T>;

    fn into_split_signal(self) -> (ReadSignal<T>, WriteSignal<T>) {
        self.split()
    }
}

impl<T, R, W> IntoSplitSignal for (R, W)
where
    R: Get<Value = T>,
    W: Set<Value = T>,
{
    type Value = T;
    type Read = R;
    type Write = W;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        self
    }
}

#[cfg(feature = "reactive_stores")]
impl<Inner, Prev, T> IntoSplitSignal for Subfield<Inner, Prev, T>
where
    Self: Get<Value = T> + Set<Value = T> + Clone,
{
    type Value = T;
    type Read = Self;
    type Write = Self;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        (self.clone(), self.clone())
    }
}

#[cfg(feature = "reactive_stores")]
impl<T, S> IntoSplitSignal for Field<T, S>
where
    Self: Get<Value = T> + Set<Value = T> + Clone,
    S: Storage<ArcField<T>>,
{
    type Value = T;
    type Read = Self;
    type Write = Self;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        (self, self)
    }
}

#[cfg(feature = "reactive_stores")]
impl<Inner, Prev, K, T> IntoSplitSignal for KeyedSubfield<Inner, Prev, K, T>
where
    Self: Get<Value = T> + Set<Value = T> + Clone,
    for<'a> &'a T: IntoIterator,
{
    type Value = T;
    type Read = Self;
    type Write = Self;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        (self.clone(), self.clone())
    }
}

#[cfg(feature = "reactive_stores")]
impl<Inner, Prev, K, T> IntoSplitSignal for AtKeyed<Inner, Prev, K, T>
where
    Self: Get<Value = T> + Set<Value = T> + Clone,
    for<'a> &'a T: IntoIterator,
{
    type Value = T;
    type Read = Self;
    type Write = Self;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        (self.clone(), self.clone())
    }
}

#[cfg(feature = "reactive_stores")]
impl<Inner, Prev> IntoSplitSignal for AtIndex<Inner, Prev>
where
    Prev: Send + Sync + IndexMut<usize> + 'static,
    Inner: Send + Sync + Clone + 'static,
    Self: Get<Value = Prev::Output> + Set<Value = Prev::Output> + Clone,
    Prev::Output: Sized,
{
    type Value = Prev::Output;
    type Read = Self;
    type Write = Self;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        (self.clone(), self.clone())
    }
}

#[cfg(feature = "reactive_stores")]
impl<S> IntoSplitSignal for DerefedField<S>
where
    Self: Get<Value = <S::Value as Deref>::Target>
        + Set<Value = <S::Value as Deref>::Target>
        + Clone,
    S: Clone + StoreField + Send + Sync + 'static,
    <S as StoreField>::Value: Deref + DerefMut,
    <S::Value as Deref>::Target: Sized,
{
    type Value = <S::Value as Deref>::Target;
    type Read = Self;
    type Write = Self;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        (self.clone(), self.clone())
    }
}

/// Returns self from an event target.
pub trait FromEventTarget {
    /// Returns self from an event target.
    fn from_event_target(evt: &web_sys::Event) -> Self;
}

impl FromEventTarget for bool {
    fn from_event_target(evt: &web_sys::Event) -> Self {
        event_target_checked(evt)
    }
}

impl FromEventTarget for String {
    fn from_event_target(evt: &web_sys::Event) -> Self {
        event_target_value(evt)
    }
}

/// Attaches the appropriate change event listener to the element.
/// - `<input>` with text types and `<textarea>` elements use the `input` event;
/// - `<input type="checkbox">`, `<input type="radio">` and `<select>` use the `change` event;
pub trait ChangeEvent {
    /// Attaches the appropriate change event listener to the element.
    fn attach_change_event<T, W>(
        &self,
        key: &str,
        write_signal: W,
    ) -> RemoveEventHandler<Self>
    where
        T: FromEventTarget + AttributeValue + 'static,
        W: Set<Value = T> + 'static,
        Self: Sized;
}

impl ChangeEvent for web_sys::Element {
    fn attach_change_event<T, W>(
        &self,
        key: &str,
        write_signal: W,
    ) -> RemoveEventHandler<Self>
    where
        T: FromEventTarget + AttributeValue + 'static,
        W: Set<Value = T> + 'static,
    {
        if key == "group" {
            let handler = move |evt| {
                let checked = event_target_checked(&evt);
                if checked {
                    write_signal.try_set(T::from_event_target(&evt));
                }
            };

            on::<_, _>(change, handler).attach(self)
        } else {
            let handler = move |evt| {
                write_signal.try_set(T::from_event_target(&evt));
            };

            if key == "checked" || self.tag_name() == "SELECT" {
                on::<_, _>(change, handler).attach(self)
            } else {
                on::<_, _>(input, handler).attach(self)
            }
        }
    }
}

/// Get the value attribute of an element (input).
/// Reads `value` if `T` is `String` and `checked` if `T` is `bool`.
pub trait GetValue<T> {
    /// Get the value attribute of an element (input).
    fn get_value(&self) -> T;
}

impl GetValue<String> for web_sys::Element {
    fn get_value(&self) -> String {
        self.get_attribute("value").unwrap_or_default()
    }
}

impl GetValue<bool> for web_sys::Element {
    fn get_value(&self) -> bool {
        self.get_attribute("checked").unwrap_or_default() == "true"
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Bool or a type. Needed to make the `group` attribute work. It is decided at runtime
/// if the derived signal value is a bool or a type `T`.
pub enum BoolOrT<T> {
    /// We have definitely a boolean value for the `group` attribute
    Bool(bool),
    /// Standard case with some type `T`
    T(T),
}

impl<T> IntoProperty for BoolOrT<T>
where
    T: IntoProperty<State = (Element, JsValue)>
        + Into<JsValue>
        + Clone
        + 'static,
{
    type State = (Element, JsValue);
    type Cloneable = Self;
    type CloneableOwned = Self;

    fn hydrate<const FROM_SERVER: bool>(
        self,
        el: &Element,
        key: &str,
    ) -> Self::State {
        match self.clone() {
            Self::T(s) => {
                s.hydrate::<FROM_SERVER>(el, key);
            }
            Self::Bool(b) => {
                <bool as IntoProperty>::hydrate::<FROM_SERVER>(b, el, key);
            }
        };

        (el.clone(), self.into())
    }

    fn build(self, el: &Element, key: &str) -> Self::State {
        match self.clone() {
            Self::T(s) => {
                s.build(el, key);
            }
            Self::Bool(b) => {
                <bool as IntoProperty>::build(b, el, key);
            }
        }

        (el.clone(), self.into())
    }

    fn rebuild(self, state: &mut Self::State, key: &str) {
        let (el, prev) = state;

        match self {
            Self::T(s) => s.rebuild(&mut (el.clone(), prev.clone()), key),
            Self::Bool(b) => <bool as IntoProperty>::rebuild(
                b,
                &mut (el.clone(), prev.clone()),
                key,
            ),
        }
    }

    fn into_cloneable(self) -> Self::Cloneable {
        self
    }

    fn into_cloneable_owned(self) -> Self::CloneableOwned {
        self
    }
}

impl<T> From<BoolOrT<T>> for JsValue
where
    T: Into<JsValue>,
{
    fn from(value: BoolOrT<T>) -> Self {
        match value {
            BoolOrT::Bool(b) => b.into(),
            BoolOrT::T(t) => t.into(),
        }
    }
}