المكوّنات · بحث فوري بتظليل
بحث فوري بتظليل
يصفّي وأنت تكتب، ويظلّل موضع المطابقة في النصّ الأصلي
12 تصاميم من هذه العائلة
<div class="sr sri">
<div class="fld">
<span aria-hidden="true">🔍</span>
<input type="search" aria-label="ابحث في القائمة" placeholder="ابحث في القائمة…"
data-ar-ph="ابحث في القائمة…" data-en-ph="Search the menu…">
<span class="cnt"></span>
</div>
<ul>
<li data-k="كبسة لحم Lamb kabsa"><span class="nm">كبسة لحم</span><b class="pr">48</b></li>
<li data-k="مندي دجاج Chicken mandi"><span class="nm">مندي دجاج</span><b class="pr">42</b></li>
<li data-k="شاورما عربي Arabic shawarma"><span class="nm">شاورما عربي</span><b class="pr">18</b></li>
<li data-k="مظبي غنم Mutton mathbi"><span class="nm">مظبي غنم</span><b class="pr">55</b></li>
<li data-k="سلطة فتوش Fattoush salad"><span class="nm">سلطة فتوش</span><b class="pr">16</b></li>
<li data-k="كنافة نابلسية Nabulsi kunafa"><span class="nm">كنافة نابلسية</span><b class="pr">22</b></li>
</ul>
<p class="empty" hidden data-ar="لا نتائج" data-en="No results">لا نتائج</p>
</div>
/* pt-scoped */
/* search-instant — قائمة مطعم */
.sri{width:100%;height:100%;display:flex;flex-direction:column;gap:7px;
font-size:10px;color:var(--pt-text-secondary);overflow:hidden;position:relative}
.sri .fld{display:flex;align-items:center;gap:6px;width:100%}
.sri input[type=search], .sri input[type=text]{flex:1;min-width:0;border:0;background:transparent;
color:var(--pt-text-primary);font:inherit;font-size:10px;outline:none}
.sri input::placeholder{color:var(--pt-text-muted)}
.sri ul{list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:2px;
overflow-y:auto}
.sri li[hidden]{display:none}
.sri mark{background:color-mix(in srgb,var(--pt-accent) 30%,transparent);
color:inherit;border-radius:2px;padding:0 1px}
.sri .empty{margin:0;padding:9px;text-align:center;font-size:9px;color:var(--pt-text-muted)}
.sri .cnt{font-size:8.5px;color:var(--pt-text-muted);font-variant-numeric:tabular-nums;
white-space:nowrap}
/* شخصية: حقل مؤطَّر وقائمة مسطّحة، انحناء صغير */
.sri{padding:9px}
.sri .fld{padding:6px 9px;border-radius:var(--pt-radius-sm);
border:1px solid var(--pt-border-default);background:var(--pt-bg-raised)}
.sri .fld:focus-within{border-color:var(--pt-accent)}
.sri ul{margin-top:2px}
.sri li{display:flex;align-items:center;gap:8px;padding:6px 9px;
border-radius:var(--pt-radius-sm);background:var(--pt-surface)}
.sri .nm{flex:1;color:var(--pt-text-primary)}
.sri .pr{font-size:9px;font-variant-numeric:tabular-nums;color:var(--pt-accent)}
/* يعمل داخل إطار معزول — لا يعتمد على شيء خارجه */
/* pt-remount — يعاد التركيب عند تبديل اللغة */
(function () {
var host = document.querySelector('.sri');
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 q = root.querySelector('input');
var lis = [].slice.call(root.querySelectorAll('li'));
var none = root.querySelector('.empty');
var cnt = root.querySelector('.cnt');
function money(n) {
var ar = document.documentElement.lang !== 'en';
return ar ? Math.round(n * 3.75) + ' ر.س' : '$' + n;
}
lis.forEach(function (li) { li.querySelector('.pr').textContent = money(+li.querySelector('.pr').textContent); });
function run() {
var n = 0;
lis.forEach(function (li) {
var ok = hit(li.dataset.k, q.value);
li.hidden = !ok;
if (ok) { mark(li.querySelector('.nm'), q.value); n++; }
});
none.hidden = n > 0;
cnt.textContent = n + '/' + lis.length;
}
q.addEventListener('input', run);
run();
}
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(); });
})();
# Instant Search with Highlight
**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:** Instant Search with Highlight — Filters as you type and highlights the match in the original text
**Why this component matters:** Highlighting must run on the raw string, not the normalised one: normalisation changes character counts, so an index taken from the normalised text underlines the wrong letters.
**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.