leptos_use_element/options.rs
1use default_struct_builder::DefaultBuilder;
2use wasm_bindgen::prelude::*;
3
4/// Options for [`use_mutation_observer_with_options`](crate::use_mutation_observer_with_options).
5#[derive(DefaultBuilder, Clone, Default)]
6pub struct UseMutationObserverOptions {
7 /// Set to `true` to extend monitoring to the entire subtree of nodes rooted at `target`.
8 /// All of the other properties are then extended to all of the nodes in the subtree
9 /// instead of applying solely to the `target` node. The default value is `false`.
10 subtree: bool,
11
12 /// Set to `true` to monitor the target node (and, if `subtree` is `true`, its descendants)
13 /// for the addition of new child nodes or removal of existing child nodes.
14 /// The default value is `false`.
15 child_list: bool,
16
17 /// Set to `true` to watch for changes to the value of attributes on the node or nodes being
18 /// monitored. The default value is `true` if either of `attribute_filter` or
19 /// `attribute_old_value` is specified, otherwise the default value is `false`.
20 attributes: bool,
21
22 /// An array of specific attribute names to be monitored. If this property isn't included,
23 /// changes to all attributes cause mutation notifications.
24 #[builder(into)]
25 attribute_filter: Option<Vec<String>>,
26
27 /// Set to `true` to record the previous value of any attribute that changes when monitoring
28 /// the node or nodes for attribute changes; See
29 /// [Monitoring attribute values](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe#monitoring_attribute_values)
30 /// for an example of watching for attribute changes and recording values.
31 /// The default value is `false`.
32 attribute_old_value: bool,
33
34 /// Set to `true` to monitor the specified target node
35 /// (and, if `subtree` is `true`, its descendants)
36 /// for changes to the character data contained within the node or nodes.
37 /// The default value is `true` if `character_data_old_value` is specified,
38 /// otherwise the default value is `false`.
39 #[builder(into)]
40 character_data: Option<bool>,
41
42 /// Set to `true` to record the previous value of a node's text whenever the text changes on
43 /// nodes being monitored. The default value is `false`.
44 character_data_old_value: bool,
45}
46
47impl From<UseMutationObserverOptions> for web_sys::MutationObserverInit {
48 fn from(val: UseMutationObserverOptions) -> Self {
49 let UseMutationObserverOptions {
50 subtree,
51 child_list,
52 attributes,
53 attribute_filter,
54 attribute_old_value,
55 character_data,
56 character_data_old_value,
57 } = val;
58
59 let init = Self::new();
60
61 init.set_subtree(subtree);
62 init.set_child_list(child_list);
63 // `attributes` is always written, so it can never be absent and cannot
64 // pick up the browser's implicit default. The DOM rejects `observe()`
65 // when `attribute_filter` or `attribute_old_value` is present while
66 // `attributes` is false, so apply the documented default here.
67 init.set_attributes(attributes || attribute_filter.is_some() || attribute_old_value);
68 init.set_attribute_old_value(attribute_old_value);
69 init.set_character_data_old_value(character_data_old_value);
70
71 if let Some(attribute_filter) = attribute_filter {
72 let array = js_sys::Array::from_iter(attribute_filter.into_iter().map(JsValue::from));
73 init.set_attribute_filter(array.unchecked_ref());
74 }
75 if let Some(character_data) = character_data {
76 init.set_character_data(character_data);
77 }
78
79 init
80 }
81}