From 825912fc9e1ef349a7bac20d0a5c18c9655ab26e Mon Sep 17 00:00:00 2001 From: adammharris Date: Thu, 19 Nov 2026 03:53:45 +0000 Subject: [PATCH] feat(textfield): a password revealable by an eye TextField { kind: InputKind::Password, revealable: true } puts an eye at the field's end that shows the password as text and hides it again: the same , its type changed, so its text and caret stay. lucide's eye joins the icons. The gallery's Text card has a password field, and a Blitz test presses its eye both ways and types after. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P3EBi4NUiH6dAfkRnuv9cz --- README.md | 2 +- gallery/src/lib.rs | 6 +++++- gallery/tests/blitz.rs | 40 ++++++++++++++++++++++++++++++++++++ src/components/text_field.rs | 36 ++++++++++++++++++++++++++------ src/icon.rs | 3 +++ src/lib.rs | 21 +++++++++++++++++++ 6 files changed, 100 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 36c3474..aa23db6 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/gallery/src/lib.rs b/gallery/src/lib.rs index 0b005ad..0e451c1 100644 --- a/gallery/src/lib.rs +++ b/gallery/src/lib.rs @@ -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); @@ -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." } diff --git a/gallery/tests/blitz.rs b/gallery/tests/blitz.rs index ebb3348..4e91661 100644 --- a/gallery/tests/blitz.rs +++ b/gallery/tests/blitz.rs @@ -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); diff --git a/src/components/text_field.rs b/src/components/text_field.rs index 5a68764..b251536 100644 --- a/src/components/text_field.rs +++ b/src/components/text_field.rs @@ -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}; @@ -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 @@ -69,6 +74,9 @@ pub fn TextField( /// The action pressed, or Return in the field, given the text. onaction: Option>, #[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, /// What a browser may fill it with: `email`, `current-password`. @@ -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) @@ -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, @@ -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 { diff --git a/src/icon.rs b/src/icon.rs index 46271f4..1ca6d82 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -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. @@ -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), diff --git a/src/lib.rs b/src/lib.rs index 145ca41..63e7014 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 {