المكوّنات · اختيار معرّف بتوفّر فوري
اختيار معرّف بتوفّر فوري
يتحقّق بعد سكوت الكتابة ويقترح بدائل عند التعارض
12 تصاميم من هذه العائلة
<div class="sg sgu">
<h5 data-ar="اختر معرّفك" data-en="Choose your handle">اختر معرّفك</h5>
<label><span data-ar="المعرّف" data-en="Handle">المعرّف</span>
<span class="wrap"><i aria-hidden="true">@</i>
<input class="un" type="text" placeholder="yourname" dir="ltr" autocomplete="off">
<span class="sp" hidden aria-hidden="true"></span></span>
<span class="err"></span></label>
<div class="alt" hidden><span data-ar="متاح بدلًا منه" data-en="Available instead">متاح بدلًا منه</span>
<span class="chips"></span></div>
<button class="go" disabled data-ar="تأكيد المعرّف" data-en="Claim handle">تأكيد المعرّف</button>
</div>
/* pt-scoped */
/* signup-username — منصّة إبداعية */
.sgu{width:100%;height:100%;display:flex;flex-direction:column;
font-size:10px;color:var(--pt-text-secondary);overflow:hidden}
.sgu h5{margin:0;font-size:11px;color:var(--pt-text-primary);font-weight:600}
.sgu .sub{margin:0;font-size:8.5px;color:var(--pt-text-muted)}
.sgu label{display:flex;flex-direction:column;gap:3px}
.sgu label > span{font-size:8.5px;color:var(--pt-text-muted)}
.sgu input, .sgu select{width:100%;font:inherit;font-size:9.5px;
color:var(--pt-text-primary);outline:none}
.sgu input::placeholder{color:var(--pt-text-muted)}
.sgu .err{font-size:8px;color:var(--pt-color-danger-400);min-height:0}
.sgu .ok{color:var(--pt-accent)}
.sgu button{font:inherit;cursor:pointer}
.sgu button:focus-visible, .sgu input:focus-visible{outline:2px solid var(--pt-focus-ring);
outline-offset:1px}
/* شخصية: حقل واحد بارز مع مؤشّر حالة، انحناء كامل، بدائل كرقاقات */
.sgu{padding:11px;gap:7px;justify-content:center;
border:1px solid var(--pt-border-subtle);border-radius:var(--pt-radius-2xl);
background:var(--pt-surface)}
.sgu .wrap{display:flex;align-items:center;gap:5px;padding:7px 11px;
border-radius:var(--pt-radius-full);border:1px solid var(--pt-border-default);
background:var(--pt-bg-inset)}
.sgu .wrap i{font-style:normal;color:var(--pt-text-muted);font-size:10px}
.sgu .un{flex:1;min-width:0;border:0;background:transparent;font-size:10px;
color:var(--pt-text-primary);outline:none}
.sgu .wrap.ok{border-color:var(--pt-accent)}
.sgu .wrap.bad{border-color:var(--pt-color-danger-400)}
.sgu .sp{width:11px;height:11px;border-radius:50%;flex:none;
border:1.5px solid var(--pt-border-strong);border-top-color:var(--pt-accent);
animation:sgspin .7s linear infinite}
@keyframes sgspin{to{transform:rotate(360deg)}}
@media(prefers-reduced-motion:reduce){.sgu .sp{animation:none}
}
.sgu .alt{display:flex;flex-direction:column;gap:4px;font-size:8px;color:var(--pt-text-muted)}
.sgu .chips{display:flex;flex-wrap:wrap;gap:4px}
.sgu .chips button{padding:3px 8px;border-radius:var(--pt-radius-full);
border:1px solid var(--pt-border-default);background:transparent;
color:var(--pt-text-secondary);font-size:8px}
.sgu .chips button:hover{border-color:var(--pt-accent);color:var(--pt-accent)}
.sgu .go{padding:7px;border-radius:var(--pt-radius-full);border:0;background:var(--pt-accent);
color:var(--pt-color-ink-900);font-size:9.5px;font-weight:600}
.sgu .go:disabled{opacity:.45;cursor:not-allowed}
/* يعمل داخل إطار معزول — لا يعتمد على شيء خارجه */
/* pt-remount — يعاد التركيب عند تبديل اللغة */
(function () {
var host = document.querySelector('.sgu');
if (!host) return;
var snapshot = host.innerHTML;
var timers = [];
var ac = null;
/* الجسم كما كتبه المولِّد. يستقبل بدائل مظلَّلة لـ root وdocument
والمؤقّتات، فينظّف نفسه دون أن يعرف أنه قابل لإعادة التركيب. */
function run(root, document, setTimeout, setInterval) {
/* الخطأ يظهر بعد مغادرة الحقل لا مع كل حرف: القراءة أثناء الكتابة
تُشعر المستخدم بالفشل قبل أن ينتهي من الإدخال. */
function bind(input, check, msg) {
var box = input.closest('label') || input.parentElement;
var err = box.querySelector('.err') || box.parentElement.querySelector('.err');
var touched = false;
function paint() {
var good = check(input.value);
input.setAttribute('aria-invalid', String(touched && !good));
if (err) err.textContent = touched && !good ? msg : '';
return good;
}
input.addEventListener('blur', function () { touched = true; paint(); });
input.addEventListener('input', function () { if (touched) paint(); });
return paint;
}
var TAKEN = ['noura', 'design', 'studio', 'ahmad', 'creator', 'salma'];
var un = root.querySelector('.un'), wrap = root.querySelector('.wrap');
var sp = root.querySelector('.sp'), err = root.querySelector('.err');
var alt = root.querySelector('.alt'), chips = root.querySelector('.chips');
var go = root.querySelector('.go');
var FMT = /^[a-z0-9_]{3,20}$/;
var timer = null, seq = 0;
function shape(v) { return FMT.test(v); }
function suggest(v) {
return [v + '_', v + '01', v + 'sa'].filter(function (s) {
return TAKEN.indexOf(s) === -1 && shape(s);
}).slice(0, 3);
}
function state(cls, msg, free) {
wrap.className = 'wrap ' + cls;
err.textContent = msg;
go.disabled = !free;
}
function check() {
var v = un.value.trim().toLowerCase();
alt.hidden = true;
if (!v) { sp.hidden = true; state('', '', false); return; }
if (!shape(v)) { sp.hidden = true;
state('bad', 'ثلاثة أحرف فأكثر، إنجليزية صغيرة وأرقام وشرطة سفلية', false); return; }
sp.hidden = false;
state('', '', false);
/* كل طلب يحمل رقمه، ولا يُعرض إلّا إن كان الأحدث — وإلّا كتب ردٌّ بطيء
قديم فوق ردٍّ جديد صحيح. */
var my = ++seq;
setTimeout(function () {
if (my !== seq) return;
sp.hidden = true;
var free = TAKEN.indexOf(v) === -1;
if (free) { state('ok', '✓ متاح', true); }
else {
state('bad', 'هذا المعرّف محجوز', false);
var s = suggest(v);
if (s.length) {
chips.innerHTML = s.map(function (x) {
return '<button type="button">@' + x + '</button>';
}).join('');
alt.hidden = false;
}
}
}, 420);
}
un.addEventListener('input', function () {
un.value = un.value.toLowerCase().replace(/[^a-z0-9_]/g, '');
clearTimeout(timer);
timer = setTimeout(check, 300); // انتظار سكوت الكتابة
});
chips.addEventListener('click', function (e) {
var b = e.target.closest('button');
if (!b) return;
un.value = b.textContent.slice(1);
check();
});
un.value = 'noura';
check();
}
function mount() {
ac = new AbortController();
var real = window.document;
/* وسيط يمرّر كل شيء إلى المستند الحقيقي، ويعلّق كل مستمع بإشارة
تُقطع عند إعادة التركيب — فلا يبقى مستمع من نسخة ماتت. */
var doc = new Proxy(real, {
get: function (t, k) {
if (k === 'addEventListener') {
return function (type, fn, opts) {
var o = (opts && typeof opts === 'object') ? Object.assign({}, opts)
: { capture: !!opts };
o.signal = ac.signal;
return t.addEventListener(type, fn, o);
};
}
var v = t[k];
return typeof v === 'function' ? v.bind(t) : v;
}
});
function track(fn) {
return function (f, ms) { var id = fn(f, ms); timers.push(id); return id; };
}
run(host, doc, track(window.setTimeout.bind(window)),
track(window.setInterval.bind(window)));
}
function unmount() {
if (ac) ac.abort();
// المعرّفان يتشاركان فضاءً واحدًا في المتصفّح، فالإلغاء المزدوج آمن
timers.forEach(function (id) { window.clearTimeout(id); window.clearInterval(id); });
timers = [];
host.innerHTML = snapshot;
}
mount();
addEventListener('pt-lang', function () { unmount(); mount(); });
})();
# Username with Live Availability
**Role:** You are an Arabic-first UI designer and front-end engineer. Read the whole
specification before writing a single line, then follow it literally. If two requirements
conflict, favour usability over visual effect.
**Goal:** Username with Live Availability — Checks after typing stops and suggests alternatives on collision
**Why this component matters:** Debounce the availability check, and always render the response for the current value — a slow earlier request arriving late will otherwise overwrite a newer, correct answer.
**Direction:** Genuine RTL — not a mirrored Latin layout. Use logical properties
(`margin-inline-start`, `padding-inline`, `inset-inline-start`, `text-align: start`),
never physical ones (`margin-left`, `left`). The layout must flip with `dir` on its own,
with no duplicated rules.
**Typography:** Cairo for Arabic, IBM Plex Sans for Latin. Arabic line-height must be
`0.18` higher than the Latin equivalent — dots, hamzas and maddas need more vertical room.
Arabic font-size runs `1.06×` the Latin size at the same optical weight.
**Numerals:** Western digits in both languages (`1,234.50`). Only the currency changes:
`ر.س` **after** the number in Arabic, `$` **before** it in English. Jordanian dinar
uses **three** decimal places, not two.
**Colors:** Dark mode — background `#0D142B` · surfaces `#211F54` · borders `#545C91` · text `#918FC2`.
Accent `#ADA9E8` must never exceed 8% of the screen area.
**Language:** Bilingual — every string carries both Arabic and English, and the layout
flips with `dir` automatically.
**States:** `default` · `hover` · `focus` · `active` · `disabled` · `loading` · `error`.
Do not design the resting state alone.
**Accessibility:** Contrast ratio at least `4.5:1`, complete keyboard navigation,
visible focus rings, and honour `prefers-reduced-motion`.
**Output:** Clean HTML and CSS in two separate files. No frameworks, no libraries,
no external dependencies.