Use a button to show/hide password in the management area

When using a link, people using screen readers might think they're going
to a new page where the password is going to be shown. With a button,
they get a better idea about what to expect.

Furthermore, with a button, we can use the `aria-pressed` attribute to
indicate whether the password is currently being shown.
This commit is contained in:
Javi Martín
2025-02-26 08:57:30 +01:00
parent 5df85bac4e
commit 573f0e62cc
4 changed files with 13 additions and 7 deletions

View File

@@ -39,14 +39,13 @@
e.stopPropagation();
$("#user_password").val(App.Managers.generatePassword());
});
$(".show-password").on("click", function(e) {
e.preventDefault();
e.stopPropagation();
$(".show-password").on("click", function() {
if ($("#user_password").is("input[type='password']")) {
App.Managers.togglePassword("text");
} else {
App.Managers.togglePassword("password");
}
$(this).attr("aria-pressed", !JSON.parse($(this).attr("aria-pressed")));
});
}
};

View File

@@ -3,4 +3,8 @@
[type=text] {
margin-bottom: 0 !important;
}
.show-password {
cursor: pointer;
}
}

View File

@@ -10,10 +10,10 @@
<div class="input-group">
<%= f.password_field :password, class: "input-group-field", label: false, value: nil %>
<span class="input-group-label">
<a href="#" class="show-password">
<button type="button" class="show-password" aria-pressed="false">
<span class="icon-eye"></span>
<span class="show-for-sr"><%= t("management.account.edit.password.show") %></span>
</a>
</button>
</span>
</div>

View File

@@ -60,14 +60,17 @@ describe "Account" do
new_password = find_field("user_password").value
expect(page).to have_field "Password", type: :password
expect(page).to have_css "button[aria-pressed=false]", exact_text: "Show password"
click_link "Show password"
click_button "Show password"
expect(page).to have_field "Password", type: :text
expect(page).to have_css "button[aria-pressed=true]", exact_text: "Show password"
click_link "Show password"
click_button "Show password"
expect(page).to have_field "Password", type: :password
expect(page).to have_css "button[aria-pressed=false]", exact_text: "Show password"
click_button "Save password"