المكوّنات · جدول بفرز يحترم النوع
جدول بفرز يحترم النوع
الأرقام تُقارن عدديًّا والعربية بترتيبها، لا نصًّا في الحالتين
12 تصاميم من هذه العائلة
<div class="tb tbs">
<div class="head"><h4 data-ar="مبيعات الفروع" data-en="Branch sales">مبيعات الفروع</h4>
<span class="cnt"></span></div>
<div class="wrap"><table>
<thead><tr>
<th scope="col" data-k="br" data-t="text" aria-sort="none" tabindex="0" data-ar="الفرع" data-en="Branch">الفرع</th>
<th scope="col" data-k="mg" data-t="text" aria-sort="none" tabindex="0" data-ar="المدير" data-en="Manager">المدير</th>
<th scope="col" class="e" data-k="qt" data-t="num" aria-sort="none" tabindex="0" data-ar="الكمّية" data-en="Units">الكمّية</th>
<th scope="col" class="e" data-k="rv" data-t="num" aria-sort="descending" tabindex="0" data-ar="الإيراد" data-en="Revenue">الإيراد</th>
</tr></thead>
<tbody></tbody>
</table></div>
</div>
/* pt-scoped */
/* table-sort — تقرير مبيعات */
.tbs{width:100%;height:100%;display:flex;flex-direction:column;gap:6px;
font-size:10px;color:var(--pt-text-secondary);overflow:hidden;padding:9px 10px}
.tbs .wrap{flex:1;min-height:0;overflow:auto;border-radius:var(--pt-radius-sm);
border:1px solid var(--pt-border-subtle)}
.tbs table{width:100%;border-collapse:collapse;font-size:8.5px}
.tbs th, .tbs td{padding:5px 7px;text-align:start;white-space:nowrap}
.tbs thead th{position:sticky;inset-block-start:0;z-index:2;background:var(--pt-bg-inset);
color:var(--pt-text-muted);font-weight:500;font-size:8px;
border-bottom:1px solid var(--pt-border-default)}
.tbs tbody tr{border-bottom:1px solid var(--pt-border-subtle)}
.tbs tbody tr:last-child{border-bottom:0}
.tbs tbody tr:hover{background:var(--pt-surface-hover)}
.tbs tbody td{color:var(--pt-text-secondary)}
.tbs .e{text-align:end;font-variant-numeric:tabular-nums}
.tbs .head{display:flex;align-items:center;gap:7px;flex-wrap:wrap}
.tbs .head h4{margin:0;font-size:10px;color:var(--pt-text-primary);font-weight:600}
.tbs .cnt{font-size:8px;color:var(--pt-text-muted);font-variant-numeric:tabular-nums;
margin-inline-start:auto}
.tbs button{font:inherit;cursor:pointer}
.tbs button:focus-visible, .tbs input:focus-visible, .tbs select:focus-visible, .tbs th:focus-visible{outline:2px solid var(--pt-focus-ring);outline-offset:-2px}
.tbs .empty{padding:12px;text-align:center;font-size:9px;color:var(--pt-text-muted)}
/* شخصية: رؤوس قابلة للنقر بسهم، انحناء صغير */
.tbs thead th{cursor:pointer;user-select:none}
.tbs thead th:hover{color:var(--pt-text-primary)}
.tbs thead th::after{content:'↕';margin-inline-start:4px;opacity:.32;font-size:8px}
.tbs thead th[aria-sort=ascending]::after{content:'↑';opacity:1;color:var(--pt-accent)}
.tbs thead th[aria-sort=descending]::after{content:'↓';opacity:1;color:var(--pt-accent)}
.tbs thead th[aria-sort]:not([aria-sort=none]){color:var(--pt-accent)}
/* يعمل داخل إطار معزول — لا يعتمد على شيء خارجه */
/* pt-remount — يعاد التركيب عند تبديل اللغة */
(function () {
var host = document.querySelector('.tbs');
if (!host) return;
var snapshot = host.innerHTML;
var timers = [];
var ac = null;
/* الجسم كما كتبه المولِّد. يستقبل بدائل مظلَّلة لـ root وdocument
والمؤقّتات، فينظّف نفسه دون أن يعرف أنه قابل لإعادة التركيب. */
function run(root, document, setTimeout, setInterval) {
var ar = document.documentElement.lang !== 'en';
var n = function (v, d) { return Number(v).toLocaleString('en-US', {
minimumFractionDigits: d || 0, maximumFractionDigits: d || 0 }); };
/* العملة: د.ب (BHD) — سوق البحرين.
الأرقام مخزَّنة بـالريال وتُحوَّل، فتبقى
المبالغ معقولة في كل سوق. والتحويل لا التبديل: «18,500» راتبًا بالريال
معقول، وبالدينار الأردني ثروة.
وثلاث خانات عشرية — الدينار يُقسَّم إلى ألف فلس. */
var money = (function () {
var RATE = 0.376, DEC = 3;
return function (v, d) {
var out = (Number(v) / 3.75) * RATE;
var places = d === undefined ? DEC : d;
var s = out.toLocaleString('en-US', {
minimumFractionDigits: places, maximumFractionDigits: places });
return ar ? s + ' د.ب' : 'BHD ' + s;
};
})();
/* المدن: سوق البحرين — تتبع العملة */
var SPOT = ar ? ['الرفاع — الرفاع الشرقي', 'مدينة عيسى — الأندلس', 'المنامة — السيف', 'المحرّق — عراد']
: ['Riffa — East Riffa', 'Isa Town — Andalus', 'Manama — Seef', 'Muharraq — Arad'];
/* المدن: متنوّعة عبر العرب — وجهات أو أسماء لا ترتبط بعملة المكوّن */
var ANYSPOT = ar ? ['الدمّام — الكورنيش', 'الوكرة — الوكير', 'المحرّق — عراد', 'الإسكندرية — سموحة', 'دبي — الخليج التجاري', 'الزرقاء — الجديدة']
: ['Dammam — Corniche', 'Al Wakrah — Al Wukair', 'Muharraq — Arad', 'Alexandria — Smouha', 'Dubai — Business Bay', 'Zarqa — Jadeeda'];
var ROWS = [
{ br: SPOT[0], mg: 'مصطفى', qt: 1840, rv: 486200 },
{ br: SPOT[1], mg: 'لمى', qt: 2110, rv: 512900 },
{ br: SPOT[2], mg: 'رامي', qt: 960, rv: 274500 },
{ br: SPOT[3], mg: 'لينا', qt: 430, rv: 98700 },
{ br: ANYSPOT[4], mg: 'دينا', qt: 1275, rv: 331400 },
{ br: ANYSPOT[5], mg: 'سامي', qt: 1690, rv: 402800 }
];
var tb = root.querySelector('tbody');
var ths = [].slice.call(root.querySelectorAll('thead th'));
var key = 'rv', dir = -1;
/* مُقارِن عربي حقيقي: المقارنة الافتراضية ترتّب بنقاط اليونيكود لا
بترتيب الحروف، فتضع «ب» بعد «ي». */
var coll = new Intl.Collator('ar', { numeric: true, sensitivity: 'base' });
function draw() {
var t = ths.find(function (h) { return h.dataset.k === key; }).dataset.t;
var rows = ROWS.slice().sort(function (a, b) {
/* النوع يحدّد المقارنة: الفرز النصّي يضع 10 قبل 9 */
return t === 'num' ? (a[key] - b[key]) * dir : coll.compare(a[key], b[key]) * dir;
});
tb.innerHTML = rows.map(function (r) {
return '<tr><td>' + r.br + '</td><td>' + r.mg + '</td>' +
'<td class="e">' + n(r.qt) + '</td>' +
'<td class="e">' + money(r.rv, 0) + '</td></tr>';
}).join('');
ths.forEach(function (h) {
h.setAttribute('aria-sort', h.dataset.k === key
? (dir === 1 ? 'ascending' : 'descending') : 'none');
});
var tot = ROWS.reduce(function (a, r) { return a + r.rv; }, 0);
root.querySelector('.cnt').textContent =
ROWS.length + (ar ? ' فروع · ' : ' branches · ') + money(tot, 0);
}
function toggle(h) {
if (key === h.dataset.k) dir = -dir;
else { key = h.dataset.k; dir = h.dataset.t === 'num' ? -1 : 1; }
draw();
}
ths.forEach(function (h) {
h.addEventListener('click', function () { toggle(h); });
h.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); toggle(h); }
});
});
draw();
}
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(); });
})();
# Type-aware Sortable Table
**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:** Type-aware Sortable Table — Numbers compare numerically and Arabic by its own collation — never as plain text
**Why this component matters:** Sort by the column’s real type: a lexical sort puts “10” before “9”, and comparing Arabic with a default collator ignores letter order. Declare the type per column and pick the comparator from it.
**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.