المكوّنات · بحث بإكمال تلقائي
بحث بإكمال تلقائي
يقترح ويُختار بالأسهم، ويحفظ آخر ما بحثتَ عنه
12 تصاميم من هذه العائلة
<div class="sr sra">
<div class="fld" role="combobox" aria-expanded="false" aria-haspopup="listbox">
<span aria-hidden="true">🔍</span>
<input type="text" aria-label="ابحث عن دواء" placeholder="اسم الدواء…"
autocomplete="off" data-ar-ph="اسم الدواء…" data-en-ph="Medicine name…">
</div>
<ul class="sug" role="listbox" hidden></ul>
<div class="recent">
<span class="rl" data-ar="آخر ما بحثت عنه" data-en="Recent">آخر ما بحثت عنه</span>
<div class="chips"></div>
</div>
</div>
/* pt-scoped */
/* search-autocomplete — صيدلية */
.sra{width:100%;height:100%;display:flex;flex-direction:column;gap:7px;
font-size:10px;color:var(--pt-text-secondary);overflow:hidden;position:relative}
.sra .fld{display:flex;align-items:center;gap:6px;width:100%}
.sra input[type=search], .sra input[type=text]{flex:1;min-width:0;border:0;background:transparent;
color:var(--pt-text-primary);font:inherit;font-size:10px;outline:none}
.sra input::placeholder{color:var(--pt-text-muted)}
.sra ul{list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:2px;
overflow-y:auto}
.sra li[hidden]{display:none}
.sra mark{background:color-mix(in srgb,var(--pt-accent) 30%,transparent);
color:inherit;border-radius:2px;padding:0 1px}
.sra .empty{margin:0;padding:9px;text-align:center;font-size:9px;color:var(--pt-text-muted)}
.sra .cnt{font-size:8.5px;color:var(--pt-text-muted);font-variant-numeric:tabular-nums;
white-space:nowrap}
/* شخصية: كبسولة كبيرة الانحناء، اقتراحات عائمة، رقاقات للسجلّ */
.sra{padding:10px;gap:8px}
.sra .fld{padding:7px 12px;border-radius:var(--pt-radius-full);
border:1px solid var(--pt-border-default);background:var(--pt-surface)}
.sra .fld:focus-within{border-color:var(--pt-accent)}
.sra .sug{position:absolute;inset-inline:10px;inset-block-start:44px;z-index:6;
padding:4px;border-radius:var(--pt-radius-lg);background:var(--pt-bg-overlay);
border:1px solid var(--pt-border-default);box-shadow:var(--pt-shadow-4);max-height:110px}
.sra .sug li{padding:6px 10px;border-radius:var(--pt-radius-md);cursor:pointer;
color:var(--pt-text-secondary);display:flex;align-items:center;gap:7px}
.sra .sug li[aria-selected="true"]{background:var(--pt-accent);color:var(--pt-color-ink-900)}
.sra .sug li[aria-selected="true"] mark{background:transparent;font-weight:700}
.sra .sug .cat{margin-inline-start:auto;font-size:8px;opacity:.7}
.sra .recent{display:flex;flex-direction:column;gap:5px;margin-top:auto}
.sra .rl{font-size:8.5px;color:var(--pt-text-muted)}
.sra .chips{display:flex;gap:5px;flex-wrap:wrap}
.sra .chips button{padding:4px 10px;border-radius:var(--pt-radius-full);
border:1px solid var(--pt-border-subtle);background:var(--pt-bg-raised);
color:var(--pt-text-muted);font:inherit;font-size:9px;cursor:pointer}
.sra .chips button:hover{border-color:var(--pt-accent);color:var(--pt-accent)}
/* يعمل داخل إطار معزول — لا يعتمد على شيء خارجه */
/* pt-remount — يعاد التركيب عند تبديل اللغة */
(function () {
var host = document.querySelector('.sra');
if (!host) return;
var snapshot = host.innerHTML;
var timers = [];
var ac = null;
/* الجسم كما كتبه المولِّد. يستقبل بدائل مظلَّلة لـ root وdocument
والمؤقّتات، فينظّف نفسه دون أن يعرف أنه قابل لإعادة التركيب. */
function run(root, document, setTimeout, setInterval) {
function norm(s) {
return String(s).replace(/[\u064B-\u0652\u0670\u0640]/g, '')
.replace(/[\u0622\u0623\u0625\u0671]/g, '\u0627')
.replace(/\u0629/g, '\u0647').replace(/\u0649/g, '\u064A')
.replace(/\u0624/g, '\u0648').replace(/\u0626/g, '\u064A')
.replace(/[\u0660-\u0669]/g, function (d) { return d.charCodeAt(0) - 0x0660; })
.toLowerCase().replace(/\s+/g, ' ').trim();
}
function stripAl(w) { return w.length > 4 && w.indexOf('\u0627\u0644') === 0 ? w.slice(2) : w; }
function hit(hay, q) {
var terms = norm(q).split(' ').filter(Boolean);
if (!terms.length) return true;
var h = norm(hay);
return terms.every(function (t) { return h.indexOf(t) !== -1 || h.indexOf(stripAl(t)) !== -1; });
}
/* التظليل يعمل على النصّ الأصلي لا المطبَّع: الأطوال تختلف بعد التطبيع،
فالمطابقة على المطبَّع تظلّل الحرف الخطأ. نبحث عن أطول بادئة مطابقة. */
function mark(el, q) {
var raw = el.dataset.raw || el.textContent;
el.dataset.raw = raw;
var t = norm(q).split(' ').filter(Boolean)[0];
if (!t) { el.textContent = raw; return; }
for (var i = 0; i < raw.length; i++) {
for (var j = raw.length; j > i; j--) {
if (norm(raw.slice(i, j)) === t) {
el.innerHTML = '';
el.appendChild(document.createTextNode(raw.slice(0, i)));
var m = document.createElement('mark');
m.textContent = raw.slice(i, j);
el.appendChild(m);
el.appendChild(document.createTextNode(raw.slice(j)));
return;
}
}
}
el.textContent = raw;
}
var DATA = [
{ n: 'بنادول أدفانس', c: 'مسكّن' }, { n: 'بروفين 400', c: 'مسكّن' },
{ n: 'أوجمنتين 1g', c: 'مضاد حيوي' }, { n: 'زيرتك', c: 'حساسية' },
{ n: 'فيتامين د 5000', c: 'مكمّل' }, { n: 'أوميغا 3', c: 'مكمّل' },
{ n: 'ڤنتولين بخّاخ', c: 'تنفّس' }
];
var fld = root.querySelector('.fld');
var q = root.querySelector('input');
var sug = root.querySelector('.sug');
var chips = root.querySelector('.chips');
var sel = -1, shown = [];
var KEY = 'sr-recent';
function recent() { try { return JSON.parse(localStorage.getItem(KEY) || '[]'); } catch (e) { return []; } }
function remember(v) {
var list = recent().filter(function (x) { return x !== v; });
list.unshift(v);
try { localStorage.setItem(KEY, JSON.stringify(list.slice(0, 4))); } catch (e) {}
paintRecent();
}
function paintRecent() {
chips.innerHTML = '';
recent().forEach(function (v) {
var b = document.createElement('button');
b.textContent = v;
b.addEventListener('click', function () { q.value = v; run(); q.focus(); });
chips.appendChild(b);
});
}
function open(v) { sug.hidden = !v; fld.setAttribute('aria-expanded', String(v)); }
function run() {
var v = q.value.trim();
shown = v ? DATA.filter(function (d) { return hit(d.n + ' ' + d.c, v); }) : [];
sug.innerHTML = '';
shown.forEach(function (d, i) {
var li = document.createElement('li');
li.id = 'sug-' + i;
li.setAttribute('role', 'option');
li.setAttribute('aria-selected', String(i === sel));
var nm = document.createElement('span');
nm.textContent = d.n;
mark(nm, v);
var cat = document.createElement('span');
cat.className = 'cat'; cat.textContent = d.c;
li.appendChild(nm); li.appendChild(cat);
li.addEventListener('click', function () { pick(i); });
sug.appendChild(li);
});
open(shown.length > 0);
// aria-activedescendant يخبر قارئ الشاشة أي خيار مُبرَز دون نقل التركيز
if (sel >= 0 && shown[sel]) q.setAttribute('aria-activedescendant', 'sug-' + sel);
else q.removeAttribute('aria-activedescendant');
}
function pick(i) {
if (!shown[i]) return;
q.value = shown[i].n;
remember(shown[i].n);
open(false); sel = -1;
}
q.addEventListener('input', function () { sel = -1; run(); });
q.addEventListener('keydown', function (e) {
if (sug.hidden) return;
if (e.key === 'ArrowDown') { e.preventDefault(); sel = (sel + 1) % shown.length; run(); }
else if (e.key === 'ArrowUp') { e.preventDefault(); sel = (sel - 1 + shown.length) % shown.length; run(); }
else if (e.key === 'Enter') { e.preventDefault(); pick(sel < 0 ? 0 : sel); }
else if (e.key === 'Escape') { open(false); sel = -1; }
});
document.addEventListener('click', function (e) { if (!root.contains(e.target)) open(false); });
paintRecent();
}
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(); });
})();
# Autocomplete Search
**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:** Autocomplete Search — Suggests, navigates by arrows, and remembers your last searches
**Why this component matters:** A combobox needs role="combobox" with aria-expanded and aria-activedescendant, or a screen reader announces a plain text field while sighted users see a list of options.
**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.
**Important:** This component has no equivalent in any global library. Do not imitate a
Latin pattern and translate its labels — build it from Gulf market requirements directly.