livebook/assets/js/password_toggle/index.js
Jakub Perżyło ace64eab37
Add show/hide button for password inputs #566 (#664)
* Added visibility toggle for password cell

* Formatted code

* Moved password toggle to separate component

* Adjusted to review

* Added password toggle for add filesystem component

* Update lib/livebook_web/helpers.ex

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
2021-11-01 16:04:11 +01:00

38 lines
875 B
JavaScript

/**
* A hook used to toggle password's input visibility via icon button.
*/
const VISIBLE_ICON = "ri-eye-off-line";
const OBSCURED_ICON = "ri-eye-line";
const PasswordToggle = {
mounted() {
this.visible = false;
this.input = this.el.querySelector("input");
this.iconButton = this.el.querySelector("i");
this.iconButton.addEventListener("click", () => {
this.visible = !this.visible;
this._updateDOM();
});
},
updated() {
this._updateDOM();
},
_updateDOM() {
if (this.visible) {
this.input.type = "text";
this.iconButton.classList.remove(OBSCURED_ICON);
this.iconButton.classList.add(VISIBLE_ICON);
} else {
this.input.type = "password";
this.iconButton.classList.remove(VISIBLE_ICON);
this.iconButton.classList.add(OBSCURED_ICON);
}
},
};
export default PasswordToggle;