Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ unit = { git = "https://github.com/diaryx-org/unit", branch = "main" }
| Buttons | `Button` (default, primary, ghost, danger), `ToggleButton`, each with an `icon`; `Pressable`, a page's own row or tile that Tab reaches and Return and Space press |
| Icons | `Icon`: a path on a 24-unit grid stroked in `currentColor`, the text's or an `Ink` of the theme's; `icon::paths` |
| Choices | `Checkbox`, `Switch`, `RadioGroup`, `Segmented`, `Slider`, `Chips` (each with a colour's dot and a count, if it has them; or `single`, a radio group drawn as pills) |
| Text | `TextField` (text, search, email, URL, password, number; with an `action` at its end, pressed by Return too), `TextArea`, `CodeField` (a one-time code: a cell for each digit, one field to assistive tech, a whole code pasted or filled in at once) |
| Text | `TextField` (text, search, email, URL, password, number; with an `action` at its end, pressed by Return too; a password `revealable` by an eye, and drawn as bullets on Blitz too with diaryx-org/blitz's `feat/password-input`), `TextArea`, `CodeField` (a one-time code: a cell for each digit, one field to assistive tech, a whole code pasted or filled in at once) |
| Menus | `MenuButton`, `Select`, `ContextMenu` (the secondary button, or a finger held); items with shortcuts, ticks, icons, separators, submenus, and destructive ones; a `palette`, a few items side by side in one row (Copy, Share, Delete; or Reflow, Pages, Source, ticked where they stand), which keeps a long menu short |
| Lists | `Section` (rows under a caption), `Row` (icon, title, subtitle, a note under it, value, a chevron or a tick, a control at the right, set in by a `depth`) |
| Navigation | `Tabs`, `ListBox` (arrows, Home, End, typeahead), `Disclosure`, `NavBar` (back, forward, a title to press), `TabBar` (with a button of its own at the end), `Tree` and `TreeRow` |
Expand Down
6 changes: 5 additions & 1 deletion gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ fn Page(
let mut volume = use_signal(|| 0.4f64);
let mut stepped = use_signal(|| 3.0f64);
let mut name = use_signal(String::new);
let mut secret = use_signal(String::new);
let mut reader = use_signal(String::new);
let mut notes = use_signal(String::new);
let mut code = use_signal(String::new);
Expand Down Expand Up @@ -581,8 +582,11 @@ fn Page(
Card { title: "Text",
TextField { label: "Name", value: name(), placeholder: "Your name",
oninput: move |v| name.set(v) }
TextField { label: "Password", kind: InputKind::Password, revealable: true,
value: secret(), autocomplete: "new-password",
oninput: move |v| secret.set(v) }
TextArea { label: "Notes", value: notes(), rows: 3, oninput: move |v| notes.set(v) }
Text { dim: true, "Hello, {name}." }
Text { dim: true, "Hello, {name}. Your password is {secret().chars().count()} characters long." }
}
Card { title: "Sign in",
Text { dim: true, "The code sent to ana@example.com. 123456 is right." }
Expand Down
40 changes: 40 additions & 0 deletions gallery/tests/blitz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,46 @@ fn a_fields_action_waits_for_its_text_and_is_pressed_by_return_too() {
assert_eq!(said(&doc), "Added Bo.");
}

#[test]
fn a_password_is_shown_by_its_eye_and_hidden_again() {
let mut doc = page(1200, 800);
let field = labelled(&doc, "input", "Password");
assert_eq!(attr(&doc, field, "type").as_deref(), Some("password"));
click(&mut doc, field);
type_text(&mut doc, "hunter2");
let eye = labelled(&doc, ".unit-field-action button", "Show password");
assert_eq!(attr(&doc, eye, "aria-pressed").as_deref(), Some("false"));
click(&mut doc, eye);
let field = labelled(&doc, "input", "Password");
assert_eq!(
attr(&doc, field, "type").as_deref(),
Some("text"),
"shown as text"
);
let eye = labelled(&doc, ".unit-field-action button", "Hide password");
assert_eq!(attr(&doc, eye, "aria-pressed").as_deref(), Some("true"));
click(&mut doc, eye);
let field = labelled(&doc, "input", "Password");
assert_eq!(
attr(&doc, field, "type").as_deref(),
Some("password"),
"hidden again"
);
// The field kept its text throughout.
click(&mut doc, field);
key(&mut doc, Key::Named(NamedKey::End));
type_text(&mut doc, "!");
let said = {
let inner = doc.inner.borrow();
let all = inner.query_selector_all(".unit-card .unit-text").unwrap();
all.iter()
.map(|&n| inner.get_node(n).unwrap().text_content())
.find(|t| t.contains("Your password"))
.unwrap()
};
assert!(said.contains("8 characters"), "{said}");
}

#[test]
fn a_brief_toast_goes_by_itself() {
let mut doc = page(1200, 800);
Expand Down
36 changes: 30 additions & 6 deletions src/components/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use dioxus::prelude::*;

use crate::components::button::{Button, Variant};
use crate::components::button::{Button, ToggleButton, Variant};
use crate::icon::{Icon, Ink, paths};
use crate::unit::{Kind, next_id, use_focus};

Expand Down Expand Up @@ -46,6 +46,11 @@ impl InputKind {
/// it while the text would not do: an empty name, an address not yet
/// whole.
///
/// A [`InputKind::Password`] field is drawn as bullets, by a browser and by
/// Blitz alike. With `revealable`, an eye at its end shows the password as
/// text, and hides it again: the field stays the same field, its text and
/// caret kept, only its `type` changing.
///
/// `name`, `autocomplete`, `required`, `minlength` and `maxlength` are the
/// input's own attributes: a browser keeps to the length, and fills and
/// saves the field from a password manager by its `autocomplete`. Blitz does
Expand All @@ -69,6 +74,9 @@ pub fn TextField(
/// The action pressed, or Return in the field, given the text.
onaction: Option<EventHandler<String>>,
#[props(default)] action_disabled: bool,
/// A password's eye at the field's end, which shows it and hides it.
#[props(default)]
revealable: bool,
/// The field's name in a form.
name: Option<String>,
/// What a browser may fill it with: `email`, `current-password`.
Expand All @@ -79,6 +87,13 @@ pub fn TextField(
) -> Element {
let focus = use_focus(Kind::Text, |_, _| false);
let id = use_hook(next_id);
let mut shown = use_signal(|| false);
let reveals = revealable && kind == InputKind::Password;
let kind_attr = if reveals && shown() {
"text"
} else {
kind.attr()
};
let inline_class = if inline { " unit-inline" } else { "" };
let act = (!disabled && !action_disabled)
.then_some(onaction)
Expand All @@ -88,7 +103,7 @@ pub fn TextField(
input {
class: "unit-input{inline_class}{focus.class()}",
id: "unit-{id}",
r#type: kind.attr(),
r#type: kind_attr,
value: "{value}",
placeholder: "{placeholder}",
disabled,
Expand Down Expand Up @@ -120,12 +135,21 @@ pub fn TextField(
if let Some(l) = label.as_ref().filter(|_| !inline) {
label { class: "unit-field-label", r#for: "unit-{id}", "{l}" }
}
if let Some(a) = action {
if reveals || action.is_some() {
div { class: "unit-field-action",
{input}
Button { variant: Variant::Ghost, disabled: disabled || action_disabled,
onpress: move |_| if let Some(f) = onaction { f.call(value.clone()) },
"{a}"
if reveals {
ToggleButton { variant: Variant::Ghost, pressed: shown(), disabled,
icon: if shown() { paths::EYE_OFF } else { paths::EYE },
label: if shown() { "Hide password" } else { "Show password" },
onchange: move |on| shown.set(on),
}
}
if let Some(a) = action {
Button { variant: Variant::Ghost, disabled: disabled || action_disabled,
onpress: move |_| if let Some(f) = onaction { f.call(value.clone()) },
"{a}"
}
}
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ pub mod paths {
pub const SPARKLE: &str = "M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594z";
/// Lucide's `shuffle`.
pub const SHUFFLE: &str = "M18 14l4 4-4 4M18 2l4 4-4 4M2 18h1.973a4 4 0 0 0 3.3-1.7l5.454-8.6a4 4 0 0 1 3.3-1.7H22M2 6h1.972a4 4 0 0 1 3.6 2.2M22 18h-6.041a4 4 0 0 1-3.3-1.8l-.359-.45";
/// Lucide's `eye`. Seen: shown as it is.
pub const EYE: &str = "M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0z";
/// Lucide's `eye-off`. Not seen: hidden from someone.
pub const EYE_OFF: &str = "M10.733 5.076a10.744 10.744 0 0 1 11.205 6.575 1 1 0 0 1 0 .696 10.747 10.747 0 0 1-1.444 2.49M14.084 14.158a3 3 0 0 1-4.242-4.242M17.479 17.499a10.75 10.75 0 0 1-15.417-5.151 1 1 0 0 1 0-.696 10.75 10.75 0 0 1 4.446-5.143M2 2l20 20";
/// Lucide's `keyboard-off`. Puts the keyboard away.
Expand Down Expand Up @@ -224,6 +226,7 @@ pub mod paths {
("BOLT", BOLT),
("SPARKLE", SPARKLE),
("SHUFFLE", SHUFFLE),
("EYE", EYE),
("EYE_OFF", EYE_OFF),
("KEYBOARD_DOWN", KEYBOARD_DOWN),
("PLAY", PLAY),
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,27 @@ mod tests {
assert_eq!(html.matches("aria-required").count(), 1, "{html}");
}

#[test]
fn a_password_field_can_be_revealed_by_its_eye() {
fn Page() -> Element {
rsx! {
UnitRoot {
TextField { value: "", oninput: |_| {}, label: "Password",
kind: InputKind::Password, revealable: true }
TextField { value: "", oninput: |_| {}, label: "Other", revealable: true }
}
}
}
let html = render(Page);
assert!(html.contains("type=\"password\""), "{html}");
assert_eq!(
html.matches("aria-label=\"Show password\"").count(),
1,
"{html}"
);
assert!(html.contains("aria-pressed=\"false\""), "{html}");
}

#[test]
fn a_page_can_leave_the_stylesheet_to_a_file_of_its_own() {
fn Page() -> Element {
Expand Down
Loading