Files
SHiNE-server/shine-UI/js/components/visible-password-field.js
T

31 lines
1.3 KiB
JavaScript

import { t } from '../i18n/index.js';
import { iconHtml } from './ui-icon.js';
/** The password is intentionally visible initially on both auth screens. */
export function createVisiblePasswordField(input) {
input.type = 'text';
const wrap = document.createElement('div');
wrap.className = 'auth-password-field';
const toggle = document.createElement('button');
toggle.type = 'button';
toggle.className = 'auth-password-field__toggle';
function updateToggle() {
const visible = input.type !== 'password';
toggle.innerHTML = iconHtml(visible ? 'eyeOff' : 'eye', false, 'key-toggle-icon');
toggle.setAttribute('aria-label', visible ? t('screens.visible_password_field.text_001') : t('screens.visible_password_field.text_002'));
toggle.setAttribute('title', visible ? t('screens.visible_password_field.text_003') : t('screens.visible_password_field.text_004'));
toggle.setAttribute('aria-pressed', String(!visible));
}
toggle.addEventListener('click', () => {
const start = input.selectionStart;
const end = input.selectionEnd;
input.type = input.type === 'password' ? 'text' : 'password';
updateToggle();
input.focus();
try { if (start !== null && end !== null) input.setSelectionRange(start, end); } catch { /* unsupported input method */ }
});
updateToggle();
wrap.append(input, toggle);
return wrap;
}