fc-dioxus 0.1.2

Typed, accessible Dioxus components over future-css
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
//! Labelled form fields, inputs, input groups, choice cards, and the error summary, from
//! field.css and form-variants.css.
use crate::{
    core::{
        assets,
        attrs::{availability_attributes, component_attributes},
    },
    prelude::*,
    theme::use_stylesheets,
};
const FIELD: Bem = Bem::new("fc-field");
const INPUT: Bem = Bem::new("fc-input");
const GROUP: Bem = Bem::new("fc-input-group");
const SUMMARY: Bem = Bem::new("fc-error-summary");
const CHOICE: Bem = Bem::new("fc-choice");
const CHECK: Bem = Bem::new("fc-check");
fc_modifier! { enum FieldFlow for FIELD { Stacked => None, Inline => "inline" } }
fc_modifier! {
    /// Input sizes.
    pub enum InputSize for INPUT {
        /// Default.
        Md => None,
        /// Small.
        Sm => "sm",
        /// Large.
        Lg => "lg",
    }
}
fc_modifier! {
    /// Input surface treatments.
    pub enum InputVariant for INPUT {
        /// A bordered box.
        Outlined => None,
        /// A tinted fill.
        Filled => "filled",
        /// A soft, borderless tint.
        Soft => "soft",
        /// A single underline.
        Underlined => "underlined",
    }
}
fc_modifier! { enum InputGrowth for INPUT { Fixed => None, Autosize => "autosize" } }

fn stylesheets() -> Element {
    rsx! {
        if use_stylesheets() {
            document::Stylesheet { href: assets::COMPONENTS_FIELD_CSS }
            document::Stylesheet { href: assets::COMPONENTS_FORM_VARIANTS_CSS }
        }
    }
}

/// Whether a field is marked as required or optional.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Necessity {
    /// No marker.
    #[default]
    Unmarked,
    /// Marked with an asterisk, and the control gets `required`.
    Required,
    /// Marked "(optional)".
    Optional,
}

/// What a [`Field`] tells the control inside it, so labels and descriptions are wired
/// by construction rather than by hand.
#[derive(Debug, Clone, PartialEq)]
struct FieldContext {
    control: Id,
    hint: Option<Id>,
    error: Option<Id>,
    required: bool,
}

/// Control wiring from the enclosing field, or none outside a field.
struct Wiring {
    id: Option<String>,
    describedby: Option<String>,
    invalid: bool,
    required: bool,
}
impl Wiring {
    /// Inside a field the field owns the control's id; elsewhere the author's id stands,
    /// so a [`ControlLabel`](super::button_group::ControlLabel) can target it.
    fn protected(&self, names: &[&'static str]) -> Vec<&'static str> {
        let mut names = names.to_vec();
        if self.id.is_some() {
            names.push("id");
        }
        names
    }
}
fn use_wiring(invalid: bool) -> Wiring {
    let context = try_consume_context::<Signal<FieldContext>>().map(|c| c.read().clone());
    let describedby = context.as_ref().and_then(|c| {
        let ids: Vec<&str> = [c.hint.as_ref(), c.error.as_ref()]
            .into_iter()
            .flatten()
            .map(Id::as_str)
            .collect();
        (!ids.is_empty()).then(|| ids.join(" "))
    });
    Wiring {
        id: context.as_ref().map(|c| c.control.to_string()),
        describedby,
        invalid: invalid || context.as_ref().is_some_and(|c| c.error.is_some()),
        required: context.is_some_and(|c| c.required),
    }
}

/// A visible label, an optional hint and error, and one control, wired together.
///
/// The control inside ([`Input`], [`Textarea`], [`NativeSelect`]) takes its `id`,
/// `aria-describedby`, and `aria-invalid` from the field, so the label is always
/// associated and the error is always announced with it.
#[component]
pub fn Field(
    label: Element,
    #[props(default)] hint: Option<Element>,
    #[props(default)] error: Option<Element>,
    #[props(default)] necessity: Necessity,
    /// Label beside the control instead of above it.
    #[props(default)]
    inline: bool,
    /// The control's id, when something else links to it, such as an [`ErrorSummary`].
    /// Generated when omitted.
    #[props(default)]
    control: Option<Id>,
    #[props(default)] class: Option<String>,
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    let generated = use_id();
    let control = control.unwrap_or(generated);
    let context = FieldContext {
        hint: hint.is_some().then(|| control.suffixed("hint")),
        error: error.is_some().then(|| control.suffixed("error")),
        required: necessity == Necessity::Required,
        control,
    };
    let mut shared = use_context_provider(|| Signal::new(context.clone()));
    if *shared.peek() != context {
        shared.set(context.clone());
    }
    let attributes = component_attributes(
        attributes,
        ClassList::new(FIELD.block())
            .with_modifier(
                FIELD,
                if inline {
                    FieldFlow::Inline
                } else {
                    FieldFlow::Stacked
                },
            )
            .extend(class.as_deref()),
        &[],
    );
    rsx! {
        {stylesheets()}
        div { ..attributes,
            label { class: FIELD.element("label"), r#for: "{context.control}",
                {label}
                match necessity {
                    Necessity::Required => rsx! { span { class: FIELD.element("required"), aria_hidden: "true", "*" } },
                    Necessity::Optional => rsx! { span { class: FIELD.element("optional"), "(optional)" } },
                    Necessity::Unmarked => rsx! {},
                }
            }
            if let (Some(hint), Some(id)) = (hint, context.hint.clone()) { p { class: FIELD.element("hint"), id: "{id}", {hint} } }
            {children}
            if let (Some(error), Some(id)) = (error, context.error.clone()) { p { class: FIELD.element("error"), id: "{id}", {error} } }
        }
    }
}

/// The HTML input types this library styles. Text is the default and is not written,
/// as in upstream's markup.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum InputType {
    /// Free text.
    #[default]
    Text,
    /// An email address; mobile keyboards adapt.
    Email,
    /// A password, masked.
    Password,
    /// A search query.
    Search,
    /// A URL.
    Url,
    /// A telephone number.
    Tel,
    /// A number.
    Number,
    /// A calendar date; the native picker is the fastest way to type a known date.
    Date,
    /// A time of day.
    Time,
}
impl InputType {
    const fn attribute(self) -> Option<&'static str> {
        match self {
            Self::Text => None,
            Self::Email => Some("email"),
            Self::Password => Some("password"),
            Self::Search => Some("search"),
            Self::Url => Some("url"),
            Self::Tel => Some("tel"),
            Self::Number => Some("number"),
            Self::Date => Some("date"),
            Self::Time => Some("time"),
        }
    }
}

struct InGroup;

/// A single-line text input. Inside a [`Field`] it is labelled automatically; anywhere
/// else, give it an `aria_label`.
#[component]
pub fn Input(
    #[props(default)] r#type: InputType,
    #[props(default)] size: InputSize,
    #[props(default)] variant: InputVariant,
    #[props(default)] value: Option<ReadSignal<String>>,
    #[props(default)] default_value: String,
    #[props(default)] oninput: Option<EventHandler<String>>,
    #[props(default)] invalid: bool,
    #[props(default)] readonly: bool,
    #[props(default)] availability: Availability,
    #[props(default)] class: Option<String>,
    #[props(extends = GlobalAttributes, extends = input)] attributes: Vec<Attribute>,
) -> Element {
    let state = use_controllable(value, || default_value, oninput);
    let wiring = use_wiring(invalid);
    let grouped = try_consume_context::<std::rc::Rc<InGroup>>().is_some();
    // In a group the group draws the box, so the control drops the input styling.
    let classes = if grouped {
        ClassList::new(&GROUP.element("control"))
    } else {
        ClassList::new(INPUT.block())
            .with_modifier(INPUT, variant)
            .with_modifier(INPUT, size)
            .extend(super::group_control_class().as_deref())
    }
    .extend(class.as_deref());
    let mut attributes = component_attributes(
        attributes,
        classes,
        &wiring.protected(&["type", "aria-invalid", "aria-describedby", "value"]),
    );
    attributes.extend(availability_attributes(&availability));
    rsx! {
        {stylesheets()}
        input { r#type: r#type.attribute(), id: wiring.id, aria_describedby: wiring.describedby,
            aria_invalid: wiring.invalid.then_some("true"), required: wiring.required, readonly,
            value: state.get(), oninput: move |event| state.set(event.value()), ..attributes }
    }
}

/// A multi-line text input; `autosize` grows it with its content where supported.
#[component]
pub fn Textarea(
    #[props(default)] size: InputSize,
    #[props(default)] variant: InputVariant,
    #[props(default)] autosize: bool,
    #[props(default)] value: Option<ReadSignal<String>>,
    #[props(default)] default_value: String,
    #[props(default)] oninput: Option<EventHandler<String>>,
    #[props(default)] invalid: bool,
    #[props(default)] readonly: bool,
    #[props(default)] availability: Availability,
    #[props(default)] class: Option<String>,
    #[props(extends = GlobalAttributes, extends = textarea)] attributes: Vec<Attribute>,
) -> Element {
    let state = use_controllable(value, || default_value, oninput);
    let wiring = use_wiring(invalid);
    let classes = ClassList::new(INPUT.block())
        .with_modifier(INPUT, variant)
        .with_modifier(INPUT, size)
        .with_modifier(
            INPUT,
            if autosize {
                InputGrowth::Autosize
            } else {
                InputGrowth::Fixed
            },
        )
        .extend(class.as_deref());
    let mut attributes = component_attributes(
        attributes,
        classes,
        &wiring.protected(&["aria-invalid", "aria-describedby", "value"]),
    );
    attributes.extend(availability_attributes(&availability));
    rsx! {
        {stylesheets()}
        textarea { id: wiring.id, aria_describedby: wiring.describedby,
            aria_invalid: wiring.invalid.then_some("true"), required: wiring.required, readonly,
            value: state.get(), oninput: move |event| state.set(event.value()), ..attributes }
    }
}

/// A native `<select>` styled as an input, for short fixed lists. Children are `option`s.
#[component]
pub fn NativeSelect(
    #[props(default)] size: InputSize,
    #[props(default)] variant: InputVariant,
    #[props(default)] onchange: Option<EventHandler<String>>,
    #[props(default)] invalid: bool,
    #[props(default)] availability: Availability,
    #[props(default)] class: Option<String>,
    #[props(extends = GlobalAttributes, extends = select)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    let wiring = use_wiring(invalid);
    let classes = ClassList::new(INPUT.block())
        .with_modifier(INPUT, variant)
        .with_modifier(INPUT, size)
        .extend(super::group_item_class().as_deref())
        .extend(class.as_deref());
    let mut attributes = component_attributes(
        attributes,
        classes,
        &wiring.protected(&["aria-invalid", "aria-describedby"]),
    );
    attributes.extend(availability_attributes(&availability));
    rsx! {
        {stylesheets()}
        select { id: wiring.id, aria_describedby: wiring.describedby,
            aria_invalid: wiring.invalid.then_some("true"), required: wiring.required,
            onchange: move |event| if let Some(onchange) = onchange { onchange.call(event.value()); },
            ..attributes, {children}
        }
    }
}

/// One box holding an [`Input`] with [`InputAddon`]s before or after it.
#[component]
pub fn InputGroup(
    #[props(default)] class: Option<String>,
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    use_context_provider(|| std::rc::Rc::new(InGroup));
    let attributes = component_attributes(
        attributes,
        ClassList::new(GROUP.block()).extend(class.as_deref()),
        &[],
    );
    rsx! {
        {stylesheets()}
        div { ..attributes, {children} }
    }
}

/// Fixed text beside an input, such as a unit or a URL scheme.
#[component]
pub fn InputAddon(
    /// Hide a symbol whose meaning the label already carries, such as "$" for "Budget".
    #[props(default)]
    decorative: bool,
    children: Element,
) -> Element {
    rsx! { span { class: GROUP.element("addon"), aria_hidden: decorative.then_some("true"), {children} } }
}

/// One problem listed in an [`ErrorSummary`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldError {
    /// The id of the control to jump to.
    pub field: Id,
    /// What is wrong, as an instruction.
    pub message: String,
}

/// The list of problems after a submit, linking to each field. It takes focus whenever it
/// appears with errors, so keyboard and screen reader users land on it.
#[component]
pub fn ErrorSummary(
    title: Element,
    #[props(default = HeadingLevel::H2)] heading: HeadingLevel,
    errors: Vec<FieldError>,
    #[props(default)] class: Option<String>,
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
) -> Element {
    let attributes = component_attributes(
        attributes,
        ClassList::new(SUMMARY.block()).extend(class.as_deref()),
        &["tabindex"],
    );
    let focus = !errors.is_empty();
    rsx! {
        {stylesheets()}
        div { tabindex: "-1",
            onmounted: move |event| async move { if focus { let _ = event.set_focus(true).await; } },
            ..attributes,
            {super::heading(heading, SUMMARY.element("title"), title)}
            ul { class: SUMMARY.element("list"),
                for error in errors {
                    li { key: "{error.field}", a { class: SUMMARY.element("link"), href: "#{error.field}", "{error.message}" } }
                }
            }
        }
    }
}

/// A radio drawn as a card with a title and description, inside a [`CheckGroup`] given
/// `radios`.
#[component]
pub fn Choice(
    title: Element,
    #[props(default)] description: Option<Element>,
    value: String,
    #[props(default)] availability: Availability,
    #[props(default)] class: Option<String>,
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
) -> Element {
    let choice = try_consume_context::<Signal<Option<RadioChoice>>>()
        .and_then(|context| context.read().clone());
    let name = choice.as_ref().map(|c| c.name.to_string());
    let checked = choice
        .as_ref()
        .is_some_and(|c| c.value.as_deref() == Some(value.as_str()));
    let onchange = choice.and_then(|c| availability.guard(c.onchange));
    let blocked = availability != Availability::Enabled;
    let attributes = component_attributes(
        attributes,
        ClassList::new(CHOICE.block()).extend(class.as_deref()),
        &[],
    );
    let state_attrs = availability_attributes(&availability);
    rsx! {
        {stylesheets()}
        if use_stylesheets() { document::Stylesheet { href: assets::COMPONENTS_CHECK_CSS } }
        label { ..attributes,
            input { class: CHECK.element("input"), r#type: "radio", name, value: value.clone(), checked,
                onclick: move |event| if blocked { event.prevent_default(); },
                onchange: move |_| if let Some(onchange) = onchange { onchange.call(value.clone()); },
                ..state_attrs,
            }
            span { class: CHOICE.element("content"),
                strong { {title} }
                if let Some(description) = description { span { class: CHOICE.element("description"), {description} } }
            }
        }
    }
}