Улучшить ввод пароля при входе и регистрации

This commit is contained in:
AidarKC
2026-11-21 17:14:46 +03:00
parent 8ac8f04128
commit 653015b2e7
11 changed files with 351 additions and 93 deletions
@@ -1,30 +1,101 @@
import { t } from '../i18n/index.js';
import { iconHtml } from './ui-icon.js';
/** The password is intentionally visible initially on both auth screens. */
/**
* Native password inputs are single-line only. A textarea allows long phrases to
* wrap across up to three lines. Masking uses a non-interactive bullet mirror so
* it also works in browsers without CSS text-security support.
*/
export function createExpandablePasswordTextarea() {
const input = document.createElement('textarea');
input.className = 'input auth-password-field__textarea';
input.rows = 1;
// 32 visible monospace characters per line on screens wide enough.
// Soft wrap only: never add newline characters to the actual password.
input.cols = 32;
input.wrap = 'soft';
input.autocapitalize = 'off';
input.spellcheck = false;
input.setAttribute('autocorrect', 'off');
const resize = () => {
const computed = window.getComputedStyle(input);
const lineHeight = Number.parseFloat(computed.lineHeight) || 24;
const extras = (Number.parseFloat(computed.paddingTop) || 0)
+ (Number.parseFloat(computed.paddingBottom) || 0)
+ (Number.parseFloat(computed.borderTopWidth) || 0)
+ (Number.parseFloat(computed.borderBottomWidth) || 0);
input.style.height = `${lineHeight + extras}px`;
input.style.height = `${Math.min(input.scrollHeight, 3 * lineHeight + extras)}px`;
};
input.addEventListener('input', resize);
// Initial value may be restored from a registration draft before mounting.
input.resizePasswordField = resize;
input.clearPasswordField = () => {
input.value = '';
input.updatePasswordMask?.();
resize();
};
return input;
}
/** Passwords are deliberately visible initially on registration and sign-in. */
export function createVisiblePasswordField(input) {
input.type = 'text';
const multiline = input.tagName === 'TEXTAREA';
if (!multiline) input.type = 'text';
const wrap = document.createElement('div');
wrap.className = 'auth-password-field';
wrap.className = multiline ? 'auth-password-field auth-password-field--multiline' : 'auth-password-field';
let mask = null;
let visible = true;
if (multiline) {
mask = document.createElement('div');
mask.className = 'auth-password-field__mask';
mask.setAttribute('aria-hidden', 'true');
mask.hidden = true;
const updateMask = () => {
// Preserve explicit line breaks while not copying actual password text.
mask.textContent = Array.from(input.value, char => char === '\n' ? '\n' : '•').join('');
mask.scrollTop = input.scrollTop;
mask.scrollLeft = input.scrollLeft;
};
input.addEventListener('input', updateMask);
input.addEventListener('scroll', () => {
mask.scrollTop = input.scrollTop;
mask.scrollLeft = input.scrollLeft;
});
input.updatePasswordMask = updateMask;
}
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));
const isVisible = multiline ? visible : input.type !== 'password';
toggle.innerHTML = iconHtml(isVisible ? 'eyeOff' : 'eye', false, 'key-toggle-icon');
toggle.setAttribute('aria-label', isVisible ? t('screens.visible_password_field.text_001') : t('screens.visible_password_field.text_002'));
toggle.setAttribute('title', isVisible ? t('screens.visible_password_field.text_003') : t('screens.visible_password_field.text_004'));
toggle.setAttribute('aria-pressed', String(!isVisible));
}
toggle.addEventListener('click', () => {
const start = input.selectionStart;
const end = input.selectionEnd;
input.type = input.type === 'password' ? 'text' : 'password';
if (multiline) {
visible = !visible;
wrap.classList.toggle('auth-password-field--masked', !visible);
mask.hidden = visible;
input.updatePasswordMask();
input.resizePasswordField();
} else {
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);
wrap.append(input);
if (mask) wrap.append(mask);
wrap.append(toggle);
return wrap;
}