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
use crate::{FieldContext, FieldLabel, Reactive};
use leptos::prelude::*;
/// A simple component containing all parts of a slider.
#[component]
pub fn Slider(
#[prop(optional)] children: Option<Children>,
/// Sets the default value of the element. Setting `value` sets this once at page load.
/// Use this for subsequent updates.
#[prop(optional, into)]
default: MaybeProp<f64>,
/// The reactive value signal of this input. Also sets initial `default` value, but doesn't
/// update it.
#[prop(optional, into)]
value: Reactive<f64>,
// LEPTOS ATTRIBUTES
/// A reactive reference to a DOM node that can be used with the node_ref attribute.
#[prop(optional, into)]
node_ref: MaybeProp<NodeRef<leptos::html::Input>>,
// RANGE ATTRIBUTES
//
/// Controls [autocomplete](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/autocomplete).
///
/// Works with `color`, `date`, `datetime-local`, `email`, `hidden`, `month`, `number`,
/// `password`, `range`, `search`, `tel`, `text`, `time`, `url`, and `week`.
#[prop(optional, into)]
autocomplete: MaybeProp<String>,
/// Toggle whether or not the input is disabled.
#[prop(optional, into)]
disabled: MaybeProp<bool>,
/// Associate this element with a form element that may not be its parent by its `id`.
#[prop(optional, into)]
form: MaybeProp<String>,
/// Link this input with a `<datalist>` element for value suggestions with its `id`.
#[prop(optional, into)]
list: MaybeProp<String>,
/// The greatest value in the range of permitted values.
#[prop(optional, into)]
max: MaybeProp<f64>,
/// The lowest value in the range of permitted values.
#[prop(optional, into)]
min: MaybeProp<f64>,
/// Name of this element. Submitted with the form as part of a name/value pair.
#[prop(optional, into)]
name: MaybeProp<String>,
/// Define the granularity of expected input value.
#[prop(optional, into)]
step: MaybeProp<f64>,
// GLOBAL ATTRIBUTES
//
/// A space separated list of keys to focus this element. The first key available on the user's
/// keyboard layout is used.
#[prop(optional, into)]
accesskey: MaybeProp<String>,
/// Sets whether the input value should be capitalized and how. If a parent `<form>` has
/// `autocapitalize` rules set, it will override any rules set here.
///
/// Accepted values: "none" or "off" | "sentences" or "on" | "words" | "characters".
#[prop(optional, into)]
autocapitalize: MaybeProp<String>,
/// Grabs focus once the page has finished loading. Only one element on the page can be focused
/// at a time.
#[prop(optional, into)]
autofocus: MaybeProp<bool>,
/// Apply classes to the element.
#[prop(optional, into)]
class: MaybeProp<String>,
/// Allows client-side editing of the element by the user.
///
/// Accepted values: "true" | "false" | "plaintext-only"
#[prop(optional, into)]
contenteditable: MaybeProp<String>,
/// Indicate directionality of the element's text.
///
/// Accepted values: "ltr" | "rtl" | "auto"
#[prop(optional, into)]
dir: MaybeProp<String>,
/// Toggle whether the element can be dragged.
#[prop(optional, into)]
draggable: MaybeProp<bool>,
/// Modifies the appearance of the enter key on virtual keyboards.
#[prop(optional, into)]
enterkeyhint: MaybeProp<String>,
/// Expose elements in the shadow DOM to be manipulated by the DOM.
#[prop(optional, into)]
exportparts: MaybeProp<String>,
/// Controls hidden status of the element.
#[prop(optional, into)]
hidden: MaybeProp<String>,
/// Set the id of this element.
#[prop(optional, into)]
id: MaybeProp<String>,
/// Toggle if the browser reacts to input events from this element.
#[prop(optional, into)]
inert: MaybeProp<bool>,
/// Hints to the browser of what type of virtual keyboard to display when editing this element
/// or its children.
#[prop(optional, into)]
inputmode: MaybeProp<String>,
/// Used to render a standard element as a custom element.
#[prop(optional, into)]
is: MaybeProp<String>,
/// Unique global identifier of an item.
#[prop(optional, into)]
itemid: MaybeProp<String>,
/// Used to add properties to an item.
#[prop(optional, into)]
itemprop: MaybeProp<String>,
/// Used to associate an item with a related non-parent element that's using `itemscope`.
#[prop(optional, into)]
itemref: MaybeProp<String>,
/// Used to declare that children elements are related to a particular item.
#[prop(optional, into)]
itemscope: MaybeProp<String>,
/// URL of data used to define `itemprops`.
#[prop(optional, into)]
itemtype: MaybeProp<String>,
/// Defines the language of an element.
#[prop(optional, into)]
lang: MaybeProp<String>,
/// Cryptographic "number used once".
#[prop(optional, into)]
nonce: MaybeProp<String>,
/// List of the part names of the element.
#[prop(optional, into)]
part: MaybeProp<String>,
/// Designate an element as a popover element.
#[prop(optional, into)]
popover: MaybeProp<String>,
/// Define the semantic meaning of content.
#[prop(optional, into)]
role: MaybeProp<String>,
/// Assigns a slot to an element.
#[prop(optional, into)]
slot: MaybeProp<String>,
/// Toggle spellcheck for this input.
///
/// Accepted values: "default" | "true" | "false".
#[prop(optional, into)]
spellcheck: MaybeProp<String>,
/// Define CSS to be applied to the element.
#[prop(optional, into)]
style: MaybeProp<String>,
/// Controls how an element behaves when a user navigates using the tab key.
#[prop(optional, into)]
tabindex: MaybeProp<usize>,
/// Describes the content of the element to screen readers.
#[prop(optional, into)]
title: MaybeProp<String>,
/// Defines localization behavior for the element.
#[prop(optional, into)]
translate: MaybeProp<String>,
) -> impl IntoView {
let slider_ref = {
if let Some(node_ref) = node_ref.get_untracked() {
node_ref
} else {
NodeRef::<leptos::html::Input>::new()
}
};
let max_default = 100.;
let min_default = 0.;
let step_default = 1.;
let init_value = {
if let Some(default_value) = default.get_untracked() {
default_value
} else {
value.get_untracked()
}
};
if let Some(slider) = slider_ref.get_untracked() {
slider.set_value_as_number(init_value);
};
let slider_value = RwSignal::new({
let max = max.get_untracked().unwrap_or(max_default);
let min = min.get_untracked().unwrap_or(min_default);
let percent = if max == min {
0.
} else {
((init_value - min) / (max - min)) * 100.
};
format!("{}%", &percent.to_string())
});
// On default
Effect::new(move || {
if let Some(default) = default.get()
&& let Some(slider) = slider_ref.get_untracked()
{
slider.set_default_value(&default.to_string());
}
});
// On disabled
Effect::new(move || {
if let Some(slider) = slider_ref.get_untracked() {
slider.set_disabled(disabled.get().unwrap_or_default());
}
});
let update_slider = move |min: f64, max: f64, current_value: f64| {
// Update slider
let percent: f64 = if max == min {
0.
} else {
((current_value - min) / (max - min)) * 100.
};
slider_value.set(format!("{}%", &percent.to_string()));
};
let on_input = move |ev| {
let target_value = event_target_value(&ev);
if let Ok(mut current_value) = target_value.parse::<f64>() {
let min = min.get_untracked().unwrap_or(min_default);
let max = max.get_untracked().unwrap_or(max_default);
if current_value < min {
current_value = min
};
if current_value > max {
current_value = max
};
value.set(current_value);
update_slider(min, max, current_value);
}
};
let global_attrs_1 = view! {
<{..}
accesskey=move || accesskey.get()
autocapitalize=move || autocapitalize.get()
autofocus=move || autofocus.get()
contenteditable=move || contenteditable.get()
dir=move || dir.get()
draggable=move || draggable.get()
enterkeyhint=move || enterkeyhint.get()
exportparts=move || exportparts.get()
hidden=move || hidden.get()
inert=move || inert.get()
inputmode=move || inputmode.get()
is=move || is.get()
itemid=move || itemid.get()
/>
};
let global_attrs_2 = view! {
<{..}
itemprop=move || itemprop.get()
itemref=move || itemref.get()
itemscope=move || itemscope.get()
itemtype=move || itemtype.get()
lang=move || lang.get()
nonce=move || nonce.get()
part=move || part.get()
popover=move || popover.get()
role=move || role.get()
slot=move || slot.get()
spellcheck=move || spellcheck.get()
style=move || style.get()
tabindex=move || tabindex.get()
title=move || title.get()
translate=move || translate.get()
/>
};
let range_attrs = view! {
<{..}
autocomplete=move || autocomplete.get()
form=move || form.get()
list=move || list.get()
name=move || name.get()
/>
};
let update_max = move || {
let current_value = value.get_untracked();
let max = max.get().unwrap_or(max_default);
let min = min.get_untracked().unwrap_or(min_default);
if current_value > max {
value.set(max)
}
update_slider(min, max, current_value);
max
};
let update_min = move || {
let current_value = value.get_untracked();
let max = max.get_untracked().unwrap_or(max_default);
let min = min.get().unwrap_or(min_default);
if current_value < min {
value.set(min)
}
update_slider(min, max, current_value);
min
};
let update_prop_value = move || {
let mut new_value = value.get();
let max = max.get_untracked().unwrap_or(max_default);
let min = min.get_untracked().unwrap_or(min_default);
// Make sure value is in range
if new_value > max {
new_value = max;
value.set(max);
} else if new_value < min {
new_value = min;
value.set(min);
}
update_slider(min, max, new_value);
new_value.to_string()
};
let update_step = move || {
let current_value = value.get();
let max = max.get_untracked().unwrap_or(max_default);
let min = min.get_untracked().unwrap_or(min_default);
let step = step.get().unwrap_or(step_default);
update_slider(min, max, current_value);
step
};
let input_id = uuid::Uuid::new_v4();
let label_id = uuid::Uuid::new_v4();
let has_children = children.is_some();
let slider_attrs = view! {
<{..}
aria_describedby=move || {
if let Some(field) = use_context::<FieldContext>() {
let description_id = field.description_id.get();
if description_id.is_empty() { None } else { Some(description_id) }
} else {
None
}
}
aria_labelledby=move || {
if let Some(field) = use_context::<FieldContext>() {
Some(field.label_id.get())
} else if has_children { Some(label_id.to_string()) } else { None }
}
class=move || format!("singlestage-input {}", class.get().unwrap_or_default())
disabled=disabled.get_untracked()
max=update_max
min=update_min
node_ref=slider_ref
on:input=on_input
prop:value=update_prop_value
step=update_step
style:--slider-value=move || slider_value.get()
type="range"
value=init_value.to_string()
/>
};
if let Some(children) = children {
view! {
{if use_context::<FieldContext>().is_some() {
view! {
<FieldLabel
class=class.get_untracked()
label_for=id.get_untracked().unwrap_or(input_id.to_string())
>
{children()}
</FieldLabel>
}
.into_any()
} else {
view! {
<label
class=move || {
format!(
"singlestage-label singlestage-slider-label {}",
class.get().unwrap_or_default(),
)
}
for=move || id.get().unwrap_or(input_id.to_string())
id=label_id.to_string()
>
{children()}
</label>
}
.into_any()
}}
<input
id=move || id.get().unwrap_or(input_id.to_string())
{..global_attrs_1}
{..global_attrs_2}
{..range_attrs}
{..slider_attrs}
/>
}
.into_any()
} else {
view! {
<input
id={if let Some(field) = use_context::<FieldContext>() {
if let Some(id) = id.get_untracked() {
field.input_id.set(id.clone());
Some(id)
} else {
field.input_id.set(input_id.to_string());
Some(input_id.to_string())
}
} else {
id.get_untracked()
}}
{..global_attrs_1}
{..global_attrs_2}
{..range_attrs}
{..slider_attrs}
/>
}
.into_any()
}
}