@kashi-ui/react 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/ActivityHeatmap.d.ts +13 -0
- package/dist/ActivityHeatmap.js +34 -0
- package/dist/Calendar.d.ts +6 -1
- package/dist/Calendar.js +4 -3
- package/dist/Carousel.d.ts +22 -0
- package/dist/Carousel.js +55 -0
- package/dist/Chart.d.ts +115 -0
- package/dist/Chart.js +197 -0
- package/dist/ColorPicker.d.ts +23 -0
- package/dist/ColorPicker.js +60 -0
- package/dist/ContextMenu.d.ts +15 -0
- package/dist/ContextMenu.js +71 -0
- package/dist/DropdownMenu.d.ts +7 -1
- package/dist/DropdownMenu.js +31 -29
- package/dist/InlineEdit.d.ts +23 -0
- package/dist/InlineEdit.js +77 -0
- package/dist/Meter.d.ts +22 -0
- package/dist/Meter.js +13 -0
- package/dist/ProvinceSelect.d.ts +7 -0
- package/dist/ProvinceSelect.js +9 -0
- package/dist/Splitter.d.ts +22 -0
- package/dist/Splitter.js +45 -0
- package/dist/Tour.d.ts +26 -0
- package/dist/Tour.js +73 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +10 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -20,8 +20,8 @@ export function SignUp() {
|
|
|
20
20
|
}
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
کامپوننتها: Button · Input · Textarea · Select · Checkbox · RadioGroup · Switch · Badge · Card · Alert · Avatar · AvatarGroup · Tabs · Dialog · Toast · Tooltip · DropdownMenu · DataTable · Pagination · Breadcrumb · Accordion · Stepper · Progress · Spinner · Skeleton · EmptyState · Divider · Kbd · RelativeTime · Combobox · CommandPalette · TagInput · FileUpload · Slider · NumberInput · Popover · StatCard · Timeline · SideNav · SegmentedControl · CopyButton · Banner · Rating · TreeView · ButtonGroup · ChipGroup · PasswordStrength
|
|
23
|
+
کامپوننتها: Button · Input · Textarea · Select · Checkbox · RadioGroup · Switch · Badge · Card · Alert · Avatar · AvatarGroup · Tabs · Dialog · Toast · Tooltip · DropdownMenu · DataTable · Pagination · Breadcrumb · Accordion · Stepper · Progress · Spinner · Skeleton · EmptyState · Divider · Kbd · RelativeTime · Combobox · CommandPalette · TagInput · FileUpload · Slider · NumberInput · Popover · StatCard · Timeline · SideNav · SegmentedControl · CopyButton · Banner · Rating · TreeView · ButtonGroup · ChipGroup · PasswordStrength · BarChart · LineChart · ContextMenu · InlineEdit · PieChart · BarList · ProportionBar · Sparkline · Carousel · ColorPicker · Tour · Meter · Splitter
|
|
24
24
|
|
|
25
|
-
فارسی: DatePicker و Calendar شمسی · DateRangePicker · TimeInput · PostalCodeInput · PlateInput · LandlineInput · MonthPicker · Countdown · NationalIdInput · CardNumberInput · IbanInput · MobileInput · PriceInput · OtpInput
|
|
25
|
+
فارسی: DatePicker و Calendar شمسی · DateRangePicker · TimeInput · PostalCodeInput · PlateInput · LandlineInput · MonthPicker · Countdown · ActivityHeatmap · ProvinceSelect · NationalIdInput · CardNumberInput · IbanInput · MobileInput · PriceInput · OtpInput
|
|
26
26
|
|
|
27
27
|
نیازمند React ۱۸ یا بالاتر. مستندات کامل: [kashi.sepantapouya.com](https://kashi.sepantapouya.com) · کد: [GitHub](https://github.com/Sepantapouya/Kashi) · لایسنس MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ActivityHeatmapProps {
|
|
2
|
+
/** تعداد فعالیت در هر روز؛ کلید تاریخ میلادی YYYY-MM-DD */
|
|
3
|
+
data: Record<string, number>;
|
|
4
|
+
/** سال شمسی @default سال جاری */
|
|
5
|
+
year?: number;
|
|
6
|
+
/** نام دسترسپذیر، مثل «فعالیت تیم در سال ۱۴۰۵» */
|
|
7
|
+
label?: string;
|
|
8
|
+
/** واحد در راهنما @default «فعالیت» */
|
|
9
|
+
unit?: string;
|
|
10
|
+
className?: string;
|
|
11
|
+
}
|
|
12
|
+
/** نقشهٔ فعالیت سالانه با ماههای شمسی؛ هفته از شنبه شروع میشود */
|
|
13
|
+
export declare function ActivityHeatmap({ data, year, label, unit, className }: ActivityHeatmapProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { JALALI_MONTHS, formatJalali, formatNumber, jalaliMonthLength, toGregorian, toISODate, toJalali } from "@kashi-ui/persian";
|
|
3
|
+
import { useRef, useState } from "react";
|
|
4
|
+
import { cx } from "./cx.js";
|
|
5
|
+
import { ChartTip } from "./Chart.js";
|
|
6
|
+
const WEEK = ["ش", "", "د", "", "چ", "", "ج"];
|
|
7
|
+
/** نقشهٔ فعالیت سالانه با ماههای شمسی؛ هفته از شنبه شروع میشود */
|
|
8
|
+
export function ActivityHeatmap({ data, year = toJalali(new Date()).jy, label, unit = "فعالیت", className }) {
|
|
9
|
+
const days = [];
|
|
10
|
+
for (let m = 1; m <= 12; m++)
|
|
11
|
+
for (let d = 1; d <= jalaliMonthLength(year, m); d++) {
|
|
12
|
+
const date = toGregorian(year, m, d);
|
|
13
|
+
days.push({ date, value: data[toISODate(date)] ?? 0 });
|
|
14
|
+
}
|
|
15
|
+
const max = Math.max(0, ...days.map((d) => d.value));
|
|
16
|
+
const level = (v) => (v <= 0 || max === 0 ? 0 : Math.max(1, Math.ceil((v / max) * 4)));
|
|
17
|
+
// شنبه سطر اول
|
|
18
|
+
const row = (d) => (d.getDay() + 1) % 7;
|
|
19
|
+
const offset = row(days[0].date);
|
|
20
|
+
const col = (i) => Math.floor((i + offset) / 7);
|
|
21
|
+
const monthStarts = JALALI_MONTHS.map((name, m) => ({ name, col: col(days.findIndex((d) => toJalali(d.date).jm === m + 1)) }));
|
|
22
|
+
const total = days.reduce((s, d) => s + d.value, 0);
|
|
23
|
+
const monthly = JALALI_MONTHS.map((name, m) => ({ name, sum: days.filter((d) => toJalali(d.date).jm === m + 1).reduce((s, d) => s + d.value, 0) }));
|
|
24
|
+
const box = useRef(null);
|
|
25
|
+
const [tip, setTip] = useState(null);
|
|
26
|
+
const name = label ?? `${unit} در سال ${formatNumber(year, { digits: "fa" }).replace(/٬/g, "")}`;
|
|
27
|
+
return (_jsxs("figure", { ref: box, className: cx("k-heatmap", className), style: { margin: 0 }, onPointerLeave: () => setTip(null), children: [_jsx("div", { className: "k-heatmap__scroll", children: _jsxs("div", { role: "img", "aria-label": `${name}: ${formatNumber(total)} ${unit}`, children: [_jsx("div", { className: "k-heatmap__months", "aria-hidden": "true", children: monthStarts.map((m) => _jsx("span", { style: { gridColumn: m.col + 1 }, children: m.name }, m.name)) }), _jsxs("div", { className: "k-heatmap__body", "aria-hidden": "true", children: [_jsx("div", { className: "k-heatmap__days", children: WEEK.map((w, i) => _jsx("span", { children: w }, i)) }), _jsxs("div", { className: "k-heatmap__grid", onPointerOver: (e) => {
|
|
28
|
+
const cell = e.target.closest("[data-i]");
|
|
29
|
+
if (!cell || !box.current)
|
|
30
|
+
return setTip(null);
|
|
31
|
+
const b = box.current.getBoundingClientRect(), r = cell.getBoundingClientRect();
|
|
32
|
+
setTip({ x: r.left + r.width / 2 - b.left, y: r.top - b.top - 2, i: Number(cell.dataset.i) });
|
|
33
|
+
}, children: [Array.from({ length: offset }, (_, i) => _jsx("span", { className: "k-heatmap__cell", "data-empty": "" }, `e${i}`)), days.map((d, i) => (_jsx("span", { className: "k-heatmap__cell", "data-l": level(d.value), "data-i": i, "data-active": tip?.i === i ? "" : undefined }, d.date.getTime())))] })] })] }) }), tip && _jsx(ChartTip, { x: tip.x, y: tip.y, width: box.current?.clientWidth, title: formatJalali(days[tip.i].date, { style: "full" }), rows: [{ value: `${formatNumber(days[tip.i].value)} ${unit}` }] }), _jsxs("figcaption", { className: "k-heatmap__foot", children: [_jsxs("span", { children: [formatNumber(total), " ", unit, " \u062F\u0631 ", formatNumber(year).replace(/٬/g, "")] }), _jsxs("span", { className: "k-heatmap__scale", "aria-hidden": "true", children: ["\u06A9\u0645\u062A\u0631 ", [0, 1, 2, 3, 4].map((l) => _jsx("span", { className: "k-heatmap__cell", "data-l": l }, l)), " \u0628\u06CC\u0634\u062A\u0631"] })] }), _jsxs("table", { className: "k-sr-only", children: [_jsxs("caption", { children: [name, "\u060C \u0628\u0647 \u062A\u0641\u06A9\u06CC\u06A9 \u0645\u0627\u0647"] }), _jsx("tbody", { children: monthly.map((m) => _jsxs("tr", { children: [_jsx("th", { scope: "row", children: m.name }), _jsx("td", { children: formatNumber(m.sum) })] }, m.name)) })] })] }));
|
|
34
|
+
}
|
package/dist/Calendar.d.ts
CHANGED
|
@@ -21,6 +21,11 @@ export interface CalendarProps {
|
|
|
21
21
|
max?: Date;
|
|
22
22
|
/** غیرفعال کردن روزهای خاص، مثلاً جمعهها */
|
|
23
23
|
isDateDisabled?: (date: Date) => boolean;
|
|
24
|
+
/**
|
|
25
|
+
* نمایش تعطیلات: true برای جمعهها و تعطیلات ثابت ایران،
|
|
26
|
+
* یا تابعی که نام تعطیلی آن روز (یا null) را برمیگرداند، مثلاً با getIranHoliday و تعطیلات قمری همان سال
|
|
27
|
+
*/
|
|
28
|
+
holidays?: boolean | ((date: Date) => string | null | false | undefined);
|
|
24
29
|
/** @default "fa" */
|
|
25
30
|
digits?: "fa" | "latin";
|
|
26
31
|
/** فوکوس روی روز انتخابشده (یا امروز) هنگام نمایش */
|
|
@@ -39,4 +44,4 @@ export interface CalendarProps {
|
|
|
39
44
|
"aria-label"?: string;
|
|
40
45
|
}
|
|
41
46
|
/** تقویم شمسی با الگوی grid استاندارد WAI-ARIA و پیمایش کامل با کیبورد */
|
|
42
|
-
export declare function Calendar({ value, defaultValue, onChange, min, max, isDateDisabled, digits, autoFocus, yearRange, labels, footer, range, className, "aria-label": ariaLabel, }: CalendarProps): import("react").JSX.Element;
|
|
47
|
+
export declare function Calendar({ value, defaultValue, onChange, min, max, isDateDisabled, holidays, digits, autoFocus, yearRange, labels, footer, range, className, "aria-label": ariaLabel, }: CalendarProps): import("react").JSX.Element;
|
package/dist/Calendar.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useEffect, useId, useRef, useState } from "react";
|
|
3
|
-
import { JALALI_MONTHS, WEEKDAYS, WEEKDAYS_SHORT, formatJalali, jalaliMonthLength, jalaliWeekday, toFaDigits, toGregorian, toJalali, } from "@kashi-ui/persian";
|
|
3
|
+
import { JALALI_MONTHS, WEEKDAYS, WEEKDAYS_SHORT, formatJalali, getIranHoliday, jalaliMonthLength, jalaliWeekday, toFaDigits, toGregorian, toJalali, } from "@kashi-ui/persian";
|
|
4
4
|
import { cx } from "./cx.js";
|
|
5
5
|
export const sameDay = (a, b) => !!a && !!b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
|
|
6
6
|
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
@@ -22,7 +22,7 @@ function clamp(d, min, max) {
|
|
|
22
22
|
return d;
|
|
23
23
|
}
|
|
24
24
|
/** تقویم شمسی با الگوی grid استاندارد WAI-ARIA و پیمایش کامل با کیبورد */
|
|
25
|
-
export function Calendar({ value, defaultValue = null, onChange, min, max, isDateDisabled, digits = "fa", autoFocus = false, yearRange, labels = {}, footer, range, className, "aria-label": ariaLabel = "تقویم", }) {
|
|
25
|
+
export function Calendar({ value, defaultValue = null, onChange, min, max, isDateDisabled, holidays, digits = "fa", autoFocus = false, yearRange, labels = {}, footer, range, className, "aria-label": ariaLabel = "تقویم", }) {
|
|
26
26
|
const id = useId();
|
|
27
27
|
const [inner, setInner] = useState(defaultValue);
|
|
28
28
|
const selected = value !== undefined ? value : inner;
|
|
@@ -106,7 +106,8 @@ export function Calendar({ value, defaultValue = null, onChange, min, max, isDat
|
|
|
106
106
|
const [lo, hi] = rs && (re || previewEnd) ? [rs, (re ?? previewEnd)].sort((a, b) => +a - +b) : [null, null];
|
|
107
107
|
const inRange = !!lo && !!hi && d >= lo && d <= hi;
|
|
108
108
|
const isSelected = range ? inRange || sameDay(d, rs) : sameDay(d, selected);
|
|
109
|
-
|
|
109
|
+
const holiday = holidays === true ? getIranHoliday(d) : holidays ? holidays(d) || null : null;
|
|
110
|
+
return (_jsx("td", { role: "gridcell", "aria-selected": isSelected, "data-in-range": range && inRange && re ? "" : undefined, "data-preview": range && inRange && !re ? "" : undefined, "data-range-start": range && (lo ? sameDay(d, lo) : sameDay(d, rs)) ? "" : undefined, "data-range-end": range && (hi ? sameDay(d, hi) : sameDay(d, rs)) ? "" : undefined, children: _jsx("button", { type: "button", className: "k-calendar__day", "data-day": dayKey(d), tabIndex: sameDay(d, focused) ? 0 : -1, "aria-label": formatJalali(d, { style: "full", digits }) + (holiday ? `، تعطیل${holiday === "جمعه" ? "" : `: ${holiday}`}` : ""), title: holiday && holiday !== "جمعه" ? holiday : undefined, "data-holiday": holiday ? "" : undefined, "aria-current": sameDay(d, today) ? "date" : undefined, "aria-disabled": isDisabled || undefined, onClick: () => {
|
|
110
111
|
setFocused(d);
|
|
111
112
|
select(d);
|
|
112
113
|
}, onKeyDown: onKeyDown, onPointerEnter: range ? () => setHover(d) : undefined, onFocus: range ? () => setHover(d) : undefined, children: n(toJalali(d).jd) }) }, di));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface CarouselProps {
|
|
3
|
+
/** هر فرزند یک اسلاید */
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
/** نام اسلایدر، مثل «محصولات پیشنهادی» (الزامی) */
|
|
6
|
+
label: string;
|
|
7
|
+
/** تعداد اسلاید در هر نما @default 1 */
|
|
8
|
+
perView?: number;
|
|
9
|
+
/** نمایش نقطهها @default true */
|
|
10
|
+
dots?: boolean;
|
|
11
|
+
className?: string;
|
|
12
|
+
labels?: {
|
|
13
|
+
prev?: string;
|
|
14
|
+
next?: string;
|
|
15
|
+
slide?: (n: number, total: number) => string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* اسلایدر روی scroll-snap بومی: با کشیدن، چرخ موس، دکمهها و کلیدهای جهت کار میکند.
|
|
20
|
+
* چرخش خودکار ندارد، چون خواندن را برای همه سخت میکند (WCAG 2.2.2).
|
|
21
|
+
*/
|
|
22
|
+
export declare function Carousel({ children, label, perView, dots, className, labels }: CarouselProps): import("react").JSX.Element;
|
package/dist/Carousel.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Children, useEffect, useId, useRef, useState } from "react";
|
|
3
|
+
import { toFaDigits } from "@kashi-ui/persian";
|
|
4
|
+
import { cx } from "./cx.js";
|
|
5
|
+
const Arrow = ({ next }) => (_jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 1.8, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", "data-k-mirror": "", children: _jsx("path", { d: next ? "m8 5 5 5-5 5" : "m12 5-5 5 5 5" }) }));
|
|
6
|
+
/**
|
|
7
|
+
* اسلایدر روی scroll-snap بومی: با کشیدن، چرخ موس، دکمهها و کلیدهای جهت کار میکند.
|
|
8
|
+
* چرخش خودکار ندارد، چون خواندن را برای همه سخت میکند (WCAG 2.2.2).
|
|
9
|
+
*/
|
|
10
|
+
export function Carousel({ children, label, perView = 1, dots = true, className, labels = {} }) {
|
|
11
|
+
const id = useId();
|
|
12
|
+
const { prev = "قبلی", next = "بعدی", slide = (n, t) => `${toFaDigits(n)} از ${toFaDigits(t)}` } = labels;
|
|
13
|
+
const slides = Children.toArray(children);
|
|
14
|
+
const track = useRef(null);
|
|
15
|
+
const [index, setIndex] = useState(0);
|
|
16
|
+
const pages = Math.max(1, slides.length - perView + 1);
|
|
17
|
+
// اسلاید فعال از روی موقعیت اسکرول
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const el = track.current;
|
|
20
|
+
if (!el)
|
|
21
|
+
return;
|
|
22
|
+
const items = [...el.children];
|
|
23
|
+
const io = new IntersectionObserver((entries) => {
|
|
24
|
+
const vis = entries.filter((e) => e.isIntersecting && e.intersectionRatio > 0.6).map((e) => items.indexOf(e.target));
|
|
25
|
+
if (vis.length)
|
|
26
|
+
setIndex(Math.min(...vis));
|
|
27
|
+
}, { root: el, threshold: [0.6, 1] });
|
|
28
|
+
items.forEach((s) => io.observe(s));
|
|
29
|
+
return () => io.disconnect();
|
|
30
|
+
}, [slides.length]);
|
|
31
|
+
const go = (i) => {
|
|
32
|
+
const n = Math.max(0, Math.min(pages - 1, i));
|
|
33
|
+
const el = track.current?.children[n];
|
|
34
|
+
if (!el || !track.current)
|
|
35
|
+
return;
|
|
36
|
+
// scrollIntoView صفحه را هم اسکرول میکند؛ فقط خود ریل را جابهجا میکنیم
|
|
37
|
+
const t = track.current;
|
|
38
|
+
const rtl = getComputedStyle(t).direction === "rtl";
|
|
39
|
+
const offset = rtl ? -(t.getBoundingClientRect().right - el.getBoundingClientRect().right) + t.scrollLeft : el.getBoundingClientRect().left - t.getBoundingClientRect().left + t.scrollLeft;
|
|
40
|
+
t.scrollTo({ left: offset });
|
|
41
|
+
setIndex(n);
|
|
42
|
+
};
|
|
43
|
+
return (_jsxs("section", { className: cx("k-carousel", className), "aria-roledescription": "\u0627\u0633\u0644\u0627\u06CC\u062F\u0631", "aria-label": label, style: { "--k-carousel-per-view": perView }, children: [_jsx("div", { ref: track, className: "k-carousel__track", tabIndex: 0, "aria-live": "polite", onKeyDown: (e) => {
|
|
44
|
+
const rtl = getComputedStyle(e.currentTarget).direction === "rtl";
|
|
45
|
+
const step = { ArrowLeft: rtl ? 1 : -1, ArrowRight: rtl ? -1 : 1 }[e.key];
|
|
46
|
+
if (step) {
|
|
47
|
+
e.preventDefault();
|
|
48
|
+
go(index + step);
|
|
49
|
+
}
|
|
50
|
+
else if (e.key === "Home")
|
|
51
|
+
go(0);
|
|
52
|
+
else if (e.key === "End")
|
|
53
|
+
go(pages - 1);
|
|
54
|
+
}, children: slides.map((s, i) => (_jsx("div", { className: "k-carousel__slide", role: "group", "aria-roledescription": "\u0627\u0633\u0644\u0627\u06CC\u062F", "aria-label": slide(i + 1, slides.length), id: `${id}-s${i}`, children: s }, i))) }), pages > 1 && (_jsxs("div", { className: "k-carousel__bar", children: [dots ? (_jsx("ul", { className: "k-carousel__dots", "aria-label": `اسلایدهای ${label}`, children: Array.from({ length: pages }, (_, i) => (_jsx("li", { children: _jsx("button", { type: "button", className: "k-carousel__dot", "aria-label": slide(i + 1, pages), "aria-current": i === index, "aria-controls": `${id}-s${i}`, onClick: () => go(i) }) }, i))) })) : _jsx("span", {}), _jsxs("div", { className: "k-carousel__nav", children: [_jsx("button", { type: "button", className: "k-btn k-btn--secondary k-btn--icon k-btn--sm", "aria-label": prev, disabled: index === 0, onClick: () => go(index - 1), children: _jsx(Arrow, {}) }), _jsx("button", { type: "button", className: "k-btn k-btn--secondary k-btn--icon k-btn--sm", "aria-label": next, disabled: index >= pages - 1, onClick: () => go(index + 1), children: _jsx(Arrow, { next: true }) })] })] }))] }));
|
|
55
|
+
}
|
package/dist/Chart.d.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface TipRow {
|
|
3
|
+
name?: string;
|
|
4
|
+
value: string;
|
|
5
|
+
color?: string;
|
|
6
|
+
}
|
|
7
|
+
/** راهنمای شناور مشترک نمودارها؛ x و y نسبت به ظرف نمودار */
|
|
8
|
+
export declare function ChartTip({ x, y, title, rows, width }: {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
title?: ReactNode;
|
|
12
|
+
rows: TipRow[];
|
|
13
|
+
width?: number;
|
|
14
|
+
}): import("react").JSX.Element;
|
|
15
|
+
export interface BarChartProps {
|
|
16
|
+
/** عنوان نمودار؛ نام دسترسپذیر هم هست */
|
|
17
|
+
title: string;
|
|
18
|
+
/** یک سری */
|
|
19
|
+
data?: {
|
|
20
|
+
label: string;
|
|
21
|
+
value: number;
|
|
22
|
+
}[];
|
|
23
|
+
/** یا چند سری روی هم (ستون انباشته)، حداکثر چهار؛ با labels */
|
|
24
|
+
series?: LineSeries[];
|
|
25
|
+
labels?: string[];
|
|
26
|
+
/** قالب مقدار در راهنما و برچسب @default ارقام فارسی با جداکننده */
|
|
27
|
+
format?: (n: number) => string;
|
|
28
|
+
/** برچسب مقدار روی بلندترین ستون @default true */
|
|
29
|
+
labelMax?: boolean;
|
|
30
|
+
height?: string;
|
|
31
|
+
className?: string;
|
|
32
|
+
}
|
|
33
|
+
/** نمودار ستونی؛ تکسری یا انباشته تا چهار سری. با موس یا Tab روی هر ستون مقدار دقیق نمایش داده میشود */
|
|
34
|
+
export declare function BarChart({ title, data, series, labels, format, labelMax, height, className }: BarChartProps): import("react").JSX.Element;
|
|
35
|
+
export interface BarListProps {
|
|
36
|
+
title: string;
|
|
37
|
+
/** به ترتیب دادهشده؛ معمولاً از بیشترین به کمترین */
|
|
38
|
+
data: {
|
|
39
|
+
label: string;
|
|
40
|
+
value: number;
|
|
41
|
+
href?: string;
|
|
42
|
+
}[];
|
|
43
|
+
format?: (n: number) => string;
|
|
44
|
+
className?: string;
|
|
45
|
+
}
|
|
46
|
+
/** میلههای افقی برای رتبهبندی با نامهای بلند، مثل پربازدیدترین صفحهها */
|
|
47
|
+
export declare function BarList({ title, data, format, className }: BarListProps): import("react").JSX.Element;
|
|
48
|
+
export interface ProportionBarProps {
|
|
49
|
+
title: string;
|
|
50
|
+
/** حداکثر چهار بخش؛ بیشتر را در «سایر» جمع کنید */
|
|
51
|
+
data: {
|
|
52
|
+
label: string;
|
|
53
|
+
value: number;
|
|
54
|
+
}[];
|
|
55
|
+
/** نمایش درصد کنار هر بخش در راهنما @default true */
|
|
56
|
+
percent?: boolean;
|
|
57
|
+
format?: (n: number) => string;
|
|
58
|
+
className?: string;
|
|
59
|
+
}
|
|
60
|
+
/** سهم هر بخش از کل در یک میلهٔ افقی، مثل منبع ورود کاربران؛ جایگزین خواناتر نمودار دایرهای */
|
|
61
|
+
export declare function ProportionBar({ title, data, percent, format, className }: ProportionBarProps): import("react").JSX.Element;
|
|
62
|
+
export interface PieChartProps {
|
|
63
|
+
title: string;
|
|
64
|
+
/** بیش از پنج بخش در «سایر» جمع میشود */
|
|
65
|
+
data: {
|
|
66
|
+
label: string;
|
|
67
|
+
value: number;
|
|
68
|
+
}[];
|
|
69
|
+
/** «donut» با جمع کل در وسط @default "donut" */
|
|
70
|
+
variant?: "donut" | "pie";
|
|
71
|
+
format?: (n: number) => string;
|
|
72
|
+
/** برچسب وسط دونات @default «جمع» */
|
|
73
|
+
totalLabel?: string;
|
|
74
|
+
/** اندازهٔ نمودار به پیکسل @default 176 */
|
|
75
|
+
size?: number;
|
|
76
|
+
className?: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* نمودار دایرهای یا دونات تا پنج بخش، با فاصلهٔ کوچک بین بخشها، راهنمای درصد و جدول همارز.
|
|
80
|
+
* برای مقایسهٔ دقیق بخشها ProportionBar خواناتر است.
|
|
81
|
+
*/
|
|
82
|
+
export declare function PieChart({ title, data, variant, format, totalLabel, size, className }: PieChartProps): import("react").JSX.Element;
|
|
83
|
+
export interface SparklineProps {
|
|
84
|
+
values: number[];
|
|
85
|
+
/** نام دسترسپذیر، مثل «روند فروش هفت روز اخیر: افزایشی» */
|
|
86
|
+
"aria-label"?: string;
|
|
87
|
+
/** رنگ خط @default برند */
|
|
88
|
+
tone?: "brand" | "success" | "error" | "neutral";
|
|
89
|
+
/** سطح کمرنگ زیر خط @default true */
|
|
90
|
+
area?: boolean;
|
|
91
|
+
className?: string;
|
|
92
|
+
}
|
|
93
|
+
/** خط روند کوچک برای کارت آمار و جدول؛ بدون محور، فقط شکل روند */
|
|
94
|
+
export declare function Sparkline({ values, "aria-label": ariaLabel, tone, area, className }: SparklineProps): import("react").JSX.Element;
|
|
95
|
+
export interface LineSeries {
|
|
96
|
+
name: string;
|
|
97
|
+
values: number[];
|
|
98
|
+
}
|
|
99
|
+
export interface LineChartProps {
|
|
100
|
+
title: string;
|
|
101
|
+
/** برچسب محور افقی، مثل ماهها */
|
|
102
|
+
labels: string[];
|
|
103
|
+
/** حداکثر چهار سری؛ رنگها به ترتیب ثابت */
|
|
104
|
+
series: LineSeries[];
|
|
105
|
+
format?: (n: number) => string;
|
|
106
|
+
/** سطح کمرنگ زیر خط (فقط برای یک سری) */
|
|
107
|
+
area?: boolean;
|
|
108
|
+
height?: string;
|
|
109
|
+
className?: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* نمودار خطی تا چهار سری: خط ۲ پیکسلی، برچسب نام در انتهای هر خط،
|
|
113
|
+
* راهنمای عمودی با موس و ←→ روی کیبورد، و جدول همارز برای صفحهخوان.
|
|
114
|
+
*/
|
|
115
|
+
export declare function LineChart({ title, labels, series, format, area, height, className }: LineChartProps): import("react").JSX.Element;
|
package/dist/Chart.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { formatNumber } from "@kashi-ui/persian";
|
|
4
|
+
import { cx } from "./cx.js";
|
|
5
|
+
const fmtDefault = (n) => formatNumber(Math.round(n));
|
|
6
|
+
/** عدد کوتاه برای محور: ۱٬۲۰۰٬۰۰۰ ← ۱٫۲ م */
|
|
7
|
+
const compact = (n) => {
|
|
8
|
+
const a = Math.abs(n);
|
|
9
|
+
const fa = (x, u) => `${formatNumber(Math.round(x * 10) / 10).replace(".", "٫")}${u}`;
|
|
10
|
+
if (a >= 1e9)
|
|
11
|
+
return fa(n / 1e9, " میلیارد");
|
|
12
|
+
if (a >= 1e6)
|
|
13
|
+
return fa(n / 1e6, " م");
|
|
14
|
+
if (a >= 1e3)
|
|
15
|
+
return fa(n / 1e3, " هزار");
|
|
16
|
+
return formatNumber(n);
|
|
17
|
+
};
|
|
18
|
+
/** گامهای گرد محور: ۰، ۲۵، ۵۰… */
|
|
19
|
+
function niceTicks(max, count = 4) {
|
|
20
|
+
if (max <= 0)
|
|
21
|
+
return [0];
|
|
22
|
+
const raw = max / count;
|
|
23
|
+
const mag = 10 ** Math.floor(Math.log10(raw));
|
|
24
|
+
const step = [1, 2, 2.5, 5, 10].map((m) => m * mag).find((s) => s >= raw);
|
|
25
|
+
return Array.from({ length: Math.ceil(max / step) + 1 }, (_, i) => i * step);
|
|
26
|
+
}
|
|
27
|
+
/** راهنمای شناور مشترک نمودارها؛ x و y نسبت به ظرف نمودار */
|
|
28
|
+
export function ChartTip({ x, y, title, rows, width }) {
|
|
29
|
+
// داخل ظرف بماند
|
|
30
|
+
const cx = width ? Math.min(Math.max(x, 80), width - 80) : x;
|
|
31
|
+
return (_jsxs("div", { className: "k-chart__tip", style: { left: cx, top: y }, "aria-hidden": "true", children: [title && _jsx("span", { className: "k-chart__tip-title", children: title }), rows.map((r, i) => (_jsxs("span", { className: "k-chart__tip-row", style: r.color ? { "--_c": r.color } : undefined, children: [r.name !== undefined && _jsxs("span", { children: [r.color && _jsx("i", {}), r.name] }), _jsx("strong", { children: r.value })] }, i)))] }));
|
|
32
|
+
}
|
|
33
|
+
/** موقعیت راهنما بالای یک عنصر، نسبت به ظرف */
|
|
34
|
+
const tipAt = (el, box, i, anchor) => {
|
|
35
|
+
if (!box)
|
|
36
|
+
return null;
|
|
37
|
+
const b = box.getBoundingClientRect();
|
|
38
|
+
const r = (anchor ?? el).getBoundingClientRect();
|
|
39
|
+
return { x: r.left + r.width / 2 - b.left, y: r.top - b.top, i };
|
|
40
|
+
};
|
|
41
|
+
/** جدول همارز برای صفحهخوان و چاپ */
|
|
42
|
+
function DataTable({ caption, labels, series, format }) {
|
|
43
|
+
return (_jsxs("table", { className: "k-sr-only", children: [_jsx("caption", { children: caption }), _jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { scope: "col", children: "\u062F\u0648\u0631\u0647" }), series.map((s) => _jsx("th", { scope: "col", children: s.name }, s.name))] }) }), _jsx("tbody", { children: labels.map((l, i) => _jsxs("tr", { children: [_jsx("th", { scope: "row", children: l }), series.map((s) => _jsx("td", { children: format(s.values[i]) }, s.name))] }, l)) })] }));
|
|
44
|
+
}
|
|
45
|
+
/** نمودار ستونی؛ تکسری یا انباشته تا چهار سری. با موس یا Tab روی هر ستون مقدار دقیق نمایش داده میشود */
|
|
46
|
+
export function BarChart({ title, data, series, labels, format = fmtDefault, labelMax = true, height, className }) {
|
|
47
|
+
const id = useId();
|
|
48
|
+
const stacked = series?.slice(0, 4);
|
|
49
|
+
const cats = stacked ? labels ?? [] : (data ?? []).map((d) => d.label);
|
|
50
|
+
const totals = cats.map((_, i) => (stacked ? stacked.reduce((n, s) => n + (s.values[i] ?? 0), 0) : data[i].value));
|
|
51
|
+
const max = Math.max(0, ...totals);
|
|
52
|
+
const ticks = niceTicks(max);
|
|
53
|
+
const top = ticks[ticks.length - 1] || 1;
|
|
54
|
+
const maxIndex = totals.indexOf(max);
|
|
55
|
+
const color = (i) => ({ "--_c": `var(--k-chart-${i + 1})` });
|
|
56
|
+
const describe = (i) => stacked ? `${cats[i]}: ${stacked.map((s) => `${s.name} ${format(s.values[i] ?? 0)}`).join("، ")}، جمع ${format(totals[i])}` : `${cats[i]}: ${format(totals[i])}`;
|
|
57
|
+
const box = useRef(null);
|
|
58
|
+
const [tip, setTip] = useState(null);
|
|
59
|
+
const show = (e, i) => setTip(tipAt(e.currentTarget, box.current, i, e.currentTarget.querySelector(".k-bars__bar, .k-bars__stack")));
|
|
60
|
+
return (_jsxs("figure", { className: cx("k-chart k-chart--bar", className), "aria-labelledby": `${id}-t`, style: { margin: 0, ...(height ? { "--k-chart-height": height } : {}) }, children: [_jsxs("figcaption", { className: "k-chart__head", children: [_jsx("p", { className: "k-chart__title", id: `${id}-t`, children: title }), stacked && stacked.length > 1 && (_jsx("ul", { className: "k-chart__legend", "aria-hidden": "true", children: stacked.map((s, i) => _jsxs("li", { style: color(i), children: [_jsx("span", { className: "k-chart__key k-chart__key--box" }), s.name] }, s.name)) }))] }), _jsxs("div", { className: "k-chart__plot", ref: box, children: [_jsx("div", { className: "k-bars__grid", "aria-hidden": "true", children: ticks.map((t) => _jsx("span", { "data-v": compact(t) }, t)) }), _jsx("div", { className: "k-bars", role: "list", "aria-label": title, children: cats.map((label, i) => (_jsxs("div", { className: "k-bars__col", role: "listitem", tabIndex: 0, "aria-label": describe(i), "data-active": tip?.i === i ? "" : undefined, style: { "--_d": `${i * 40}ms` }, onPointerEnter: (e) => show(e, i), onPointerLeave: () => setTip(null), onFocus: (e) => show(e, i), onBlur: () => setTip(null), children: [labelMax && i === maxIndex && _jsx("span", { className: "k-bars__value", "aria-hidden": "true", children: format(totals[i]) }), stacked ? (_jsx("span", { className: "k-bars__stack", style: { blockSize: `${(totals[i] / top) * 100}%` }, "aria-hidden": "true", children: stacked.map((s, si) => (_jsx("span", { className: "k-bars__seg", style: { ...color(si), flexGrow: s.values[i] ?? 0 } }, s.name))) })) : (_jsx("span", { className: "k-bars__bar", style: { blockSize: `${(totals[i] / top) * 100}%` }, "aria-hidden": "true" })), _jsx("span", { className: "k-bars__label", "aria-hidden": "true", children: label })] }, label))) }), tip && (_jsx(ChartTip, { x: tip.x, y: tip.y, width: box.current?.clientWidth, title: cats[tip.i], rows: stacked
|
|
61
|
+
? [...stacked.map((s, si) => ({ name: s.name, value: format(s.values[tip.i] ?? 0), color: `var(--k-chart-${si + 1})` })), { name: "جمع", value: format(totals[tip.i]) }]
|
|
62
|
+
: [{ value: format(totals[tip.i]) }] }))] })] }));
|
|
63
|
+
}
|
|
64
|
+
/** میلههای افقی برای رتبهبندی با نامهای بلند، مثل پربازدیدترین صفحهها */
|
|
65
|
+
export function BarList({ title, data, format = fmtDefault, className }) {
|
|
66
|
+
const id = useId();
|
|
67
|
+
const max = Math.max(1, ...data.map((d) => d.value));
|
|
68
|
+
const total = data.reduce((n, d) => n + d.value, 0) || 1;
|
|
69
|
+
const share = (v) => `${formatNumber(Math.round((v / total) * 1000) / 10).replace(".", "٫")}٪`;
|
|
70
|
+
const box = useRef(null);
|
|
71
|
+
const [tip, setTip] = useState(null);
|
|
72
|
+
return (_jsxs("figure", { className: cx("k-chart", className), "aria-labelledby": `${id}-t`, style: { margin: 0 }, children: [_jsx("figcaption", { className: "k-chart__head", children: _jsx("p", { className: "k-chart__title", id: `${id}-t`, children: title }) }), _jsxs("ol", { className: "k-barlist", ref: box, style: { position: "relative" }, children: [data.map((d, i) => (_jsxs("li", { className: "k-barlist__row", "data-active": tip?.i === i ? "" : undefined, style: { "--_d": `${i * 60}ms` }, onPointerEnter: (e) => setTip(tipAt(e.currentTarget, box.current, i, e.currentTarget.querySelector(".k-barlist__fill"))), onPointerLeave: () => setTip(null), children: [_jsxs("span", { className: "k-barlist__track", children: [_jsx("span", { className: "k-barlist__fill", style: { inlineSize: `${(d.value / max) * 100}%` }, "aria-hidden": "true" }), d.href ? _jsx("a", { className: "k-barlist__label", href: d.href, children: d.label }) : _jsx("span", { className: "k-barlist__label", children: d.label })] }), _jsxs("span", { className: "k-barlist__value", children: [format(d.value), " ", _jsx("span", { className: "k-barlist__share", children: share(d.value) })] })] }, d.label))), tip && _jsx(ChartTip, { x: tip.x, y: tip.y, width: box.current?.clientWidth, title: data[tip.i].label, rows: [{ name: "مقدار", value: format(data[tip.i].value) }, { name: "سهم از کل", value: share(data[tip.i].value) }] })] })] }));
|
|
73
|
+
}
|
|
74
|
+
/** سهم هر بخش از کل در یک میلهٔ افقی، مثل منبع ورود کاربران؛ جایگزین خواناتر نمودار دایرهای */
|
|
75
|
+
export function ProportionBar({ title, data, percent = true, format = fmtDefault, className }) {
|
|
76
|
+
const id = useId();
|
|
77
|
+
const parts = data.slice(0, 4);
|
|
78
|
+
const total = parts.reduce((n, d) => n + d.value, 0) || 1;
|
|
79
|
+
const pct = (v) => `${formatNumber(Math.round((v / total) * 1000) / 10).replace(".", "٫")}٪`;
|
|
80
|
+
const color = (i) => ({ "--_c": `var(--k-chart-${i + 1})` });
|
|
81
|
+
const box = useRef(null);
|
|
82
|
+
const [tip, setTip] = useState(null);
|
|
83
|
+
return (_jsxs("figure", { className: cx("k-chart", className), "aria-labelledby": `${id}-t`, style: { margin: 0 }, children: [_jsx("figcaption", { className: "k-chart__head", children: _jsx("p", { className: "k-chart__title", id: `${id}-t`, children: title }) }), _jsxs("div", { className: "k-proportion__wrap", ref: box, children: [_jsx("div", { className: "k-proportion", "aria-hidden": "true", children: parts.map((d, i) => (_jsx("span", { className: "k-proportion__seg", "data-active": tip?.i === i ? "" : undefined, style: { ...color(i), flexGrow: d.value }, onPointerEnter: (e) => setTip(tipAt(e.currentTarget, box.current, i)), onPointerLeave: () => setTip(null) }, d.label))) }), tip && _jsx(ChartTip, { x: tip.x, y: tip.y, width: box.current?.clientWidth, rows: [{ name: parts[tip.i].label, value: pct(parts[tip.i].value), color: `var(--k-chart-${tip.i + 1})` }, { name: "تعداد", value: format(parts[tip.i].value) }] })] }), _jsx("ul", { className: "k-proportion__legend", children: parts.map((d, i) => (_jsxs("li", { style: color(i), "data-active": tip?.i === i ? "" : undefined, children: [_jsx("span", { className: "k-chart__key k-chart__key--box", "aria-hidden": "true" }), _jsx("span", { children: d.label }), _jsx("strong", { children: percent ? pct(d.value) : format(d.value) }), percent && _jsx("span", { className: "k-proportion__abs", children: format(d.value) })] }, d.label))) })] }));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* نمودار دایرهای یا دونات تا پنج بخش، با فاصلهٔ کوچک بین بخشها، راهنمای درصد و جدول همارز.
|
|
87
|
+
* برای مقایسهٔ دقیق بخشها ProportionBar خواناتر است.
|
|
88
|
+
*/
|
|
89
|
+
export function PieChart({ title, data, variant = "donut", format = fmtDefault, totalLabel = "جمع", size = 176, className }) {
|
|
90
|
+
const id = useId();
|
|
91
|
+
const sorted = [...data].sort((a, b) => b.value - a.value);
|
|
92
|
+
const parts = sorted.length > 5 ? [...sorted.slice(0, 4), { label: "سایر", value: sorted.slice(4).reduce((n, d) => n + d.value, 0) }] : sorted;
|
|
93
|
+
const total = parts.reduce((n, d) => n + d.value, 0) || 1;
|
|
94
|
+
const pct = (v) => `${formatNumber(Math.round((v / total) * 1000) / 10).replace(".", "٫")}٪`;
|
|
95
|
+
const slot = (i) => (i < 4 ? `var(--k-chart-${i + 1})` : "var(--k-text-quaternary)");
|
|
96
|
+
const donut = variant === "donut";
|
|
97
|
+
// دایره با stroke-dasharray: هر بخش یک کمان؛ برای pie، شعاع نصف و ضخامت برابر قطر
|
|
98
|
+
const R = 50, r = donut ? 40 : 25, w = donut ? 16 : 50;
|
|
99
|
+
const C = 2 * Math.PI * r;
|
|
100
|
+
const gap = parts.length > 1 ? (donut ? 1.6 : 0.8) : 0;
|
|
101
|
+
const [active, setActive] = useState(null);
|
|
102
|
+
const box = useRef(null);
|
|
103
|
+
let acc = 0;
|
|
104
|
+
const arcs = parts.map((d) => {
|
|
105
|
+
const len = (d.value / total) * C;
|
|
106
|
+
const a = { start: acc, len };
|
|
107
|
+
acc += len;
|
|
108
|
+
return a;
|
|
109
|
+
});
|
|
110
|
+
const mid = (i) => {
|
|
111
|
+
const ang = ((arcs[i].start + arcs[i].len / 2) / C) * 2 * Math.PI - Math.PI / 2;
|
|
112
|
+
const rr = donut ? r : r * 1.3;
|
|
113
|
+
return { x: (R + rr * Math.cos(ang)) / (2 * R), y: (R + rr * Math.sin(ang)) / (2 * R) };
|
|
114
|
+
};
|
|
115
|
+
return (_jsxs("figure", { className: cx("k-chart k-pie", className), "aria-labelledby": `${id}-t`, style: { margin: 0 }, children: [_jsx("figcaption", { className: "k-chart__head", children: _jsx("p", { className: "k-chart__title", id: `${id}-t`, children: title }) }), _jsxs("div", { className: "k-pie__body", children: [_jsxs("div", { className: "k-pie__plot", ref: box, style: { inlineSize: size, blockSize: size }, onPointerLeave: () => setActive(null), children: [_jsxs("svg", { viewBox: "0 0 100 100", className: "k-pie__svg", "aria-hidden": "true", children: [donut && _jsx("circle", { cx: R, cy: R, r: r, fill: "none", stroke: "var(--k-bg-tertiary)", strokeWidth: w }), parts.map((d, i) => (_jsx("circle", { className: "k-pie__slice", "data-active": active === i ? "" : undefined, cx: R, cy: R, r: r, fill: "none", stroke: slot(i), strokeWidth: w, strokeDasharray: `${Math.max(0, arcs[i].len - gap)} ${C}`, strokeDashoffset: -arcs[i].start, transform: `rotate(-90 ${R} ${R})`, style: { "--_d": `${i * 80}ms` }, onPointerEnter: () => setActive(i) }, d.label)))] }), donut && (_jsxs("div", { className: "k-pie__center", "aria-hidden": "true", children: [_jsx("strong", { children: active === null ? format(total) : pct(parts[active].value) }), _jsx("span", { children: active === null ? totalLabel : parts[active].label })] })), active !== null && !donut && (_jsx(ChartTip, { x: mid(active).x * size, y: mid(active).y * size - 8, rows: [{ name: parts[active].label, value: pct(parts[active].value), color: slot(active) }, { name: "تعداد", value: format(parts[active].value) }] }))] }), _jsx("ul", { className: "k-pie__legend", children: parts.map((d, i) => (_jsxs("li", { style: { "--_c": slot(i) }, "data-active": active === i ? "" : undefined, onPointerEnter: () => setActive(i), onPointerLeave: () => setActive(null), children: [_jsx("span", { className: "k-chart__key k-chart__key--box", "aria-hidden": "true" }), _jsx("span", { className: "k-pie__name", children: d.label }), _jsx("strong", { children: pct(d.value) }), _jsx("span", { className: "k-pie__abs", children: format(d.value) })] }, d.label))) })] })] }));
|
|
116
|
+
}
|
|
117
|
+
/** خط روند کوچک برای کارت آمار و جدول؛ بدون محور، فقط شکل روند */
|
|
118
|
+
export function Sparkline({ values, "aria-label": ariaLabel, tone = "brand", area = true, className }) {
|
|
119
|
+
const hi = Math.max(...values), lo = Math.min(...values);
|
|
120
|
+
// حداقل دامنه ۲۵٪ مقدار تا تغییر کوچک، زیگزاگ بزرگ دیده نشود
|
|
121
|
+
const span = Math.max(hi - lo, Math.abs(hi) * 0.25) || 1;
|
|
122
|
+
const min = (hi + lo) / 2 - span / 2;
|
|
123
|
+
const pts = values.map((v, i) => [(i / Math.max(1, values.length - 1)) * 100, 28 - ((v - min) / span) * 24]);
|
|
124
|
+
const d = pts.map(([x, y], i) => `${i ? "L" : "M"}${x},${y}`).join(" ");
|
|
125
|
+
const c = tone === "neutral" ? "var(--k-text-tertiary)" : tone === "brand" ? "var(--k-text-brand)" : `var(--k-text-${tone})`;
|
|
126
|
+
return (_jsxs("svg", { className: cx("k-sparkline", className), viewBox: "0 0 100 30", preserveAspectRatio: "none", role: ariaLabel ? "img" : undefined, "aria-label": ariaLabel, "aria-hidden": ariaLabel ? undefined : true, style: { color: c }, children: [area && _jsx("path", { d: `${d} L100,30 L0,30 Z`, fill: "currentColor", opacity: 0.1 }), _jsx("path", { d: d, fill: "none", stroke: "currentColor", strokeWidth: 2, vectorEffect: "non-scaling-stroke", strokeLinejoin: "round", strokeLinecap: "round" })] }));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* نمودار خطی تا چهار سری: خط ۲ پیکسلی، برچسب نام در انتهای هر خط،
|
|
130
|
+
* راهنمای عمودی با موس و ←→ روی کیبورد، و جدول همارز برای صفحهخوان.
|
|
131
|
+
*/
|
|
132
|
+
export function LineChart({ title, labels, series, format = fmtDefault, area, height, className }) {
|
|
133
|
+
const id = useId();
|
|
134
|
+
const plot = useRef(null);
|
|
135
|
+
const [size, setSize] = useState({ w: 600, h: 220, rtl: true });
|
|
136
|
+
const [active, setActive] = useState(null);
|
|
137
|
+
const shown = series.slice(0, 4);
|
|
138
|
+
useLayoutEffect(() => {
|
|
139
|
+
const el = plot.current;
|
|
140
|
+
if (!el)
|
|
141
|
+
return;
|
|
142
|
+
const measure = () => setSize({ w: el.clientWidth, h: el.clientHeight, rtl: getComputedStyle(el).direction === "rtl" });
|
|
143
|
+
measure();
|
|
144
|
+
const ro = new ResizeObserver(measure);
|
|
145
|
+
ro.observe(el);
|
|
146
|
+
return () => ro.disconnect();
|
|
147
|
+
}, []);
|
|
148
|
+
const pad = { top: 12, bottom: 24, start: 52, end: shown.length > 1 ? 64 : 16 };
|
|
149
|
+
const max = Math.max(0, ...shown.flatMap((s) => s.values));
|
|
150
|
+
const ticks = niceTicks(max);
|
|
151
|
+
const top = ticks[ticks.length - 1] || 1;
|
|
152
|
+
const innerW = Math.max(1, size.w - pad.start - pad.end);
|
|
153
|
+
const innerH = Math.max(1, size.h - pad.top - pad.bottom);
|
|
154
|
+
// محور زمان در جهت خواندن صفحه: در RTL اولین نقطه سمت راست
|
|
155
|
+
const x = (i) => {
|
|
156
|
+
const t = labels.length > 1 ? i / (labels.length - 1) : 0.5;
|
|
157
|
+
return size.rtl ? size.w - pad.start - t * innerW : pad.start + t * innerW;
|
|
158
|
+
};
|
|
159
|
+
const y = (v) => pad.top + innerH - (v / top) * innerH;
|
|
160
|
+
// SVG با جهت ltr رسم میشود تا text-anchor فیزیکی باشد؛ اعداد محور بیرون از ناحیهٔ نمودار
|
|
161
|
+
const tickX = size.rtl ? size.w - pad.start + 8 : pad.start - 8;
|
|
162
|
+
const anchor = size.rtl ? "start" : "end";
|
|
163
|
+
// برچسب انتهای خطها روی هم نیفتند: دستکم ۱۴ پیکسل فاصلهٔ عمودی
|
|
164
|
+
const last = labels.length - 1;
|
|
165
|
+
const endY = (() => {
|
|
166
|
+
const order = shown.map((s, i) => ({ i, y: y(s.values[last]) })).sort((a, b) => a.y - b.y);
|
|
167
|
+
for (let k = 1; k < order.length; k++)
|
|
168
|
+
if (order[k].y - order[k - 1].y < 14)
|
|
169
|
+
order[k].y = order[k - 1].y + 14;
|
|
170
|
+
return Object.fromEntries(order.map((o) => [o.i, o.y]));
|
|
171
|
+
})();
|
|
172
|
+
const endX = (i) => x(i) + (size.rtl ? -8 : 8);
|
|
173
|
+
const color = (i) => ({ "--_c": `var(--k-chart-${i + 1})` });
|
|
174
|
+
const pick = (clientX) => {
|
|
175
|
+
const r = plot.current.getBoundingClientRect();
|
|
176
|
+
const px = clientX - r.left;
|
|
177
|
+
let best = 0;
|
|
178
|
+
labels.forEach((_, i) => { if (Math.abs(x(i) - px) < Math.abs(x(best) - px))
|
|
179
|
+
best = i; });
|
|
180
|
+
setActive(best);
|
|
181
|
+
};
|
|
182
|
+
const onKey = (e) => {
|
|
183
|
+
const next = { ArrowLeft: size.rtl ? 1 : -1, ArrowRight: size.rtl ? -1 : 1 }[e.key];
|
|
184
|
+
if (next) {
|
|
185
|
+
e.preventDefault();
|
|
186
|
+
setActive((a) => Math.min(labels.length - 1, Math.max(0, (a ?? (next > 0 ? -1 : labels.length)) + next)));
|
|
187
|
+
}
|
|
188
|
+
else if (e.key === "Home")
|
|
189
|
+
setActive(0);
|
|
190
|
+
else if (e.key === "End")
|
|
191
|
+
setActive(labels.length - 1);
|
|
192
|
+
else if (e.key === "Escape")
|
|
193
|
+
setActive(null);
|
|
194
|
+
};
|
|
195
|
+
const step = Math.max(1, Math.ceil(labels.length / Math.max(2, Math.floor(innerW / 64))));
|
|
196
|
+
return (_jsxs("figure", { className: cx("k-chart", className), "aria-labelledby": `${id}-t`, style: { margin: 0, ...(height ? { "--k-chart-height": height } : {}) }, children: [_jsxs("figcaption", { className: "k-chart__head", children: [_jsx("p", { className: "k-chart__title", id: `${id}-t`, children: title }), shown.length > 1 && (_jsx("ul", { className: "k-chart__legend", "aria-hidden": "true", children: shown.map((s, i) => _jsxs("li", { style: color(i), children: [_jsx("span", { className: "k-chart__key" }), s.name] }, s.name)) }))] }), _jsxs("div", { ref: plot, className: "k-chart__plot", tabIndex: 0, role: "img", "aria-label": `${title}. با کلیدهای جهت بین نقطهها حرکت کنید.`, "aria-describedby": `${id}-live`, onPointerMove: (e) => pick(e.clientX), onPointerLeave: () => setActive(null), onKeyDown: onKey, onBlur: () => setActive(null), children: [_jsxs("svg", { className: "k-chart__svg", "aria-hidden": "true", direction: "ltr", children: [_jsx("g", { className: "k-chart__grid", children: ticks.map((t) => _jsx("line", { x1: pad.start - (size.rtl ? 0 : 0), x2: size.w - pad.end, y1: y(t), y2: y(t), transform: size.rtl ? `translate(${pad.end - pad.start} 0)` : undefined }, t)) }), ticks.map((t) => _jsx("text", { className: "k-chart__tick", x: tickX, y: y(t) + 4, textAnchor: anchor, children: compact(t) }, t)), labels.map((l, i) => (i % step === 0 || i === labels.length - 1) && _jsx("text", { className: "k-chart__tick", x: x(i), y: size.h - 6, textAnchor: "middle", children: l }, l)), _jsx("defs", { children: _jsxs("linearGradient", { id: `${id}-fade`, x1: "0", y1: "0", x2: "0", y2: "1", children: [_jsx("stop", { offset: "0", style: { stopColor: "var(--k-chart-1)", stopOpacity: 0.28 } }), _jsx("stop", { offset: "1", style: { stopColor: "var(--k-chart-1)", stopOpacity: 0 } })] }) }), (area ?? shown.length === 1) && shown.length === 1 && (_jsx("path", { className: "k-chart__area", fill: `url(#${id}-fade)`, d: `M${x(0)},${y(0)} ${shown[0].values.map((v, i) => `L${x(i)},${y(v)}`).join(" ")} L${x(labels.length - 1)},${y(0)} Z` })), active !== null && _jsx("line", { className: "k-chart__cross", x1: x(active), x2: x(active), y1: pad.top, y2: pad.top + innerH }), shown.map((s, si) => (_jsxs("g", { style: color(si), children: [_jsx("path", { className: "k-chart__line", pathLength: 1, d: s.values.map((v, i) => `${i ? "L" : "M"}${x(i)},${y(v)}`).join(" ") }), _jsx("circle", { className: "k-chart__dot", cx: x(s.values.length - 1), cy: y(s.values[s.values.length - 1]), r: 4 }), shown.length > 1 && _jsx("text", { className: "k-chart__end", x: endX(last), y: endY[si] + 4, textAnchor: size.rtl ? "end" : "start", children: s.name }), active !== null && _jsx("circle", { className: "k-chart__dot", cx: x(active), cy: y(s.values[active]), r: 4.5 })] }, s.name)))] }), active !== null && (_jsx(ChartTip, { x: x(active), y: Math.min(...shown.map((s) => y(s.values[active]))) - 6, width: size.w, title: labels[active], rows: shown.map((s, si) => ({ name: s.name, value: format(s.values[active]), color: `var(--k-chart-${si + 1})` })) }))] }), _jsx("span", { className: "k-sr-only", "aria-live": "polite", id: `${id}-live`, children: active !== null ? `${labels[active]}: ${shown.map((s) => `${s.name} ${format(s.values[active])}`).join("، ")}` : "" }), _jsx(DataTable, { caption: title, labels: labels, series: shown, format: format })] }));
|
|
197
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface ColorPickerProps {
|
|
3
|
+
label?: ReactNode;
|
|
4
|
+
hint?: ReactNode;
|
|
5
|
+
/** رنگ hex، مثل "#0fa89b" */
|
|
6
|
+
value?: string;
|
|
7
|
+
defaultValue?: string;
|
|
8
|
+
onChange?: (hex: string) => void;
|
|
9
|
+
/** رنگهای پیشنهادی؛ [] برای حذف */
|
|
10
|
+
swatches?: {
|
|
11
|
+
name: string;
|
|
12
|
+
value: string;
|
|
13
|
+
}[];
|
|
14
|
+
/** نمایش کنتراست با متن سفید و مشکی @default true */
|
|
15
|
+
showContrast?: boolean;
|
|
16
|
+
name?: string;
|
|
17
|
+
id?: string;
|
|
18
|
+
className?: string;
|
|
19
|
+
}
|
|
20
|
+
/** نسبت کنتراست WCAG */
|
|
21
|
+
export declare const contrastRatio: (a: string, b: string) => number;
|
|
22
|
+
/** انتخاب رنگ برند یا برچسب: نمونهرنگها، کد hex و انتخابگر بومی، با کنتراست متن روی آن */
|
|
23
|
+
export declare function ColorPicker({ label, hint, value, defaultValue, onChange, swatches, showContrast, name, id, className }: ColorPickerProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useState } from "react";
|
|
3
|
+
import { toFaDigits } from "@kashi-ui/persian";
|
|
4
|
+
import { Field } from "./Field.js";
|
|
5
|
+
const DEFAULT_SWATCHES = [
|
|
6
|
+
{ name: "فیروزهای", value: "#0b8077" },
|
|
7
|
+
{ name: "لاجوردی", value: "#2740ed" },
|
|
8
|
+
{ name: "بنفش", value: "#7c3aed" },
|
|
9
|
+
{ name: "اناری", value: "#e51d2a" },
|
|
10
|
+
{ name: "زعفرانی", value: "#dd5e02" },
|
|
11
|
+
{ name: "پستهای", value: "#518b25" },
|
|
12
|
+
{ name: "شب", value: "#2a2f3d" },
|
|
13
|
+
];
|
|
14
|
+
const HEX = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
15
|
+
const norm = (v) => {
|
|
16
|
+
const m = v.trim().match(HEX);
|
|
17
|
+
if (!m)
|
|
18
|
+
return null;
|
|
19
|
+
const h = m[1].length === 3 ? [...m[1]].map((c) => c + c).join("") : m[1];
|
|
20
|
+
return `#${h.toLowerCase()}`;
|
|
21
|
+
};
|
|
22
|
+
const lum = (hex) => {
|
|
23
|
+
const [r, g, b] = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255).map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
|
|
24
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
25
|
+
};
|
|
26
|
+
/** نسبت کنتراست WCAG */
|
|
27
|
+
export const contrastRatio = (a, b) => {
|
|
28
|
+
a = norm(a) ?? a;
|
|
29
|
+
b = norm(b) ?? b;
|
|
30
|
+
const [x, y] = [lum(a), lum(b)].sort((m, n) => n - m);
|
|
31
|
+
return (x + 0.05) / (y + 0.05);
|
|
32
|
+
};
|
|
33
|
+
const Check = () => _jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 2.2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: _jsx("path", { d: "m5 10.5 3.2 3L15 7" }) });
|
|
34
|
+
/** انتخاب رنگ برند یا برچسب: نمونهرنگها، کد hex و انتخابگر بومی، با کنتراست متن روی آن */
|
|
35
|
+
export function ColorPicker({ label, hint, value, defaultValue = "#0b8077", onChange, swatches = DEFAULT_SWATCHES, showContrast = true, name, id, className }) {
|
|
36
|
+
const autoId = useId();
|
|
37
|
+
const fieldId = id ?? autoId;
|
|
38
|
+
const [inner, setInner] = useState(norm(defaultValue) ?? "#000000");
|
|
39
|
+
const current = norm(value ?? "") ?? inner;
|
|
40
|
+
const [text, setText] = useState(null);
|
|
41
|
+
const set = (hex) => {
|
|
42
|
+
if (value === undefined)
|
|
43
|
+
setInner(hex);
|
|
44
|
+
setText(null);
|
|
45
|
+
onChange?.(hex);
|
|
46
|
+
};
|
|
47
|
+
const white = contrastRatio(current, "#ffffff"), black = contrastRatio(current, "#000000");
|
|
48
|
+
const ink = white >= black ? "#ffffff" : "#000000";
|
|
49
|
+
const fmt = (r) => toFaDigits((Math.round(r * 10) / 10).toFixed(1)).replace(".", "٫");
|
|
50
|
+
const invalid = text !== null && !norm(text);
|
|
51
|
+
return (_jsx(Field, { id: `${fieldId}-hex`, label: label, hint: hint, error: invalid ? "کد رنگ معتبر نیست؛ مثل #0fa89b وارد کنید." : undefined, className: className, children: _jsxs("div", { className: "k-color", style: { "--_sw": current, "--_ink": ink }, children: [swatches.length > 0 && (_jsx("div", { className: "k-color__swatches", role: "radiogroup", "aria-label": "\u0631\u0646\u06AF\u200C\u0647\u0627\u06CC \u067E\u06CC\u0634\u0646\u0647\u0627\u062F\u06CC", children: swatches.map((s) => (_jsxs("label", { className: "k-color__swatch", style: { "--_sw": s.value, "--_ink": contrastRatio(s.value, "#ffffff") >= 3 ? "#fff" : "#000" }, title: s.name, children: [_jsx("input", { type: "radio", className: "k-sr-only", name: `${fieldId}-sw`, checked: norm(s.value) === current, "aria-label": s.name, onChange: () => set(norm(s.value)) }), _jsx(Check, {})] }, s.value))) })), _jsxs("div", { className: "k-color__row", children: [_jsxs("label", { className: "k-color__preview", title: "\u0627\u0646\u062A\u062E\u0627\u0628 \u0631\u0646\u06AF \u062F\u0644\u062E\u0648\u0627\u0647", children: [_jsx("span", { className: "k-sr-only", children: "\u0627\u0646\u062A\u062E\u0627\u0628 \u0631\u0646\u06AF \u062F\u0644\u062E\u0648\u0627\u0647" }), _jsx("input", { type: "color", value: current, onChange: (e) => set(e.target.value) })] }), _jsx("div", { className: "k-input", "data-state": invalid ? "invalid" : undefined, children: _jsx("input", { id: `${fieldId}-hex`, className: "k-input__control", dir: "ltr", spellCheck: false, autoComplete: "off", value: text ?? current, "aria-invalid": invalid || undefined, onChange: (e) => {
|
|
52
|
+
setText(e.target.value);
|
|
53
|
+
const h = norm(e.target.value);
|
|
54
|
+
if (h) {
|
|
55
|
+
if (value === undefined)
|
|
56
|
+
setInner(h);
|
|
57
|
+
onChange?.(h);
|
|
58
|
+
}
|
|
59
|
+
}, onBlur: () => !invalid && setText(null) }) })] }), showContrast && (_jsxs("p", { className: "k-color__contrast", "aria-live": "polite", children: [_jsx("span", { className: "k-color__sample", children: "\u0645\u062A\u0646 \u0646\u0645\u0648\u0646\u0647" }), _jsxs("span", { children: ["\u06A9\u0646\u062A\u0631\u0627\u0633\u062A \u0628\u0627 \u0645\u062A\u0646 ", ink === "#ffffff" ? "سفید" : "مشکی", ": ", fmt(Math.max(white, black)), " \u0628\u0647 \u06F1 ", Math.max(white, black) >= 4.5 ? "(AA)" : "(کمتر از AA)"] })] })), name && _jsx("input", { type: "hidden", name: name, value: current })] }) }));
|
|
60
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface ContextMenuProps {
|
|
3
|
+
/** ناحیهای که راستکلیک روی آن منو را باز میکند */
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
/** گزینهها: MenuItem، MenuSeparator و MenuLabel */
|
|
6
|
+
menu: ReactNode;
|
|
7
|
+
/** نام منو برای صفحهخوان @default «گزینهها» */
|
|
8
|
+
label?: string;
|
|
9
|
+
className?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* منوی راستکلیک؛ با کیبورد هم: کلید ContextMenu یا Shift+F10 روی ناحیهٔ فوکوسشده.
|
|
13
|
+
* همان گزینهها و پیمایش منوی کشویی را دارد.
|
|
14
|
+
*/
|
|
15
|
+
export declare function ContextMenu({ children, menu, label, className }: ContextMenuProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
3
|
+
import { MenuCtx, items, menuKeyDown } from "./DropdownMenu.js";
|
|
4
|
+
import { cx } from "./cx.js";
|
|
5
|
+
/**
|
|
6
|
+
* منوی راستکلیک؛ با کیبورد هم: کلید ContextMenu یا Shift+F10 روی ناحیهٔ فوکوسشده.
|
|
7
|
+
* همان گزینهها و پیمایش منوی کشویی را دارد.
|
|
8
|
+
*/
|
|
9
|
+
export function ContextMenu({ children, menu, label = "گزینهها", className }) {
|
|
10
|
+
const id = useId();
|
|
11
|
+
const [pos, setPos] = useState(null);
|
|
12
|
+
const area = useRef(null);
|
|
13
|
+
const list = useRef(null);
|
|
14
|
+
const returnTo = useRef(null);
|
|
15
|
+
const placed = useRef(false);
|
|
16
|
+
const close = (focusBack = true) => {
|
|
17
|
+
placed.current = false;
|
|
18
|
+
setPos(null);
|
|
19
|
+
if (focusBack)
|
|
20
|
+
returnTo.current?.focus();
|
|
21
|
+
};
|
|
22
|
+
const openAt = (x, y) => {
|
|
23
|
+
returnTo.current = document.activeElement ?? area.current;
|
|
24
|
+
setPos({ x, y });
|
|
25
|
+
};
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const el = list.current;
|
|
28
|
+
const pop = el;
|
|
29
|
+
if (!pos) {
|
|
30
|
+
if (el.matches?.(":popover-open"))
|
|
31
|
+
pop.hidePopover?.();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (pop.showPopover && !el.matches(":popover-open"))
|
|
35
|
+
pop.showPopover();
|
|
36
|
+
// یک بار جایگذاری: در RTL منو سمت چپ نشانگر باز میشود (مثل منوی بومی) و از صفحه بیرون نمیرود
|
|
37
|
+
if (!placed.current) {
|
|
38
|
+
placed.current = true;
|
|
39
|
+
const r = el.getBoundingClientRect();
|
|
40
|
+
const rtl = getComputedStyle(area.current).direction === "rtl";
|
|
41
|
+
let nx = rtl ? pos.x - r.width : pos.x;
|
|
42
|
+
if (nx < 8)
|
|
43
|
+
nx = rtl ? pos.x : 8;
|
|
44
|
+
nx = Math.max(8, Math.min(nx, innerWidth - r.width - 8));
|
|
45
|
+
const ny = Math.max(8, pos.y + r.height > innerHeight - 8 ? pos.y - r.height : pos.y);
|
|
46
|
+
if (nx !== pos.x || ny !== pos.y)
|
|
47
|
+
return setPos({ x: nx, y: Math.min(ny, innerHeight - r.height - 8) });
|
|
48
|
+
}
|
|
49
|
+
items(el)[0]?.focus({ preventScroll: true });
|
|
50
|
+
const onDown = (e) => !el.contains(e.target) && close(false);
|
|
51
|
+
const onScroll = () => close(false);
|
|
52
|
+
document.addEventListener("pointerdown", onDown);
|
|
53
|
+
// اسکرولی که همین حالا در جریان است منو را نبندد
|
|
54
|
+
const t = setTimeout(() => addEventListener("scroll", onScroll, { passive: true, once: true }), 150);
|
|
55
|
+
return () => {
|
|
56
|
+
clearTimeout(t);
|
|
57
|
+
document.removeEventListener("pointerdown", onDown);
|
|
58
|
+
removeEventListener("scroll", onScroll);
|
|
59
|
+
};
|
|
60
|
+
}, [pos?.x, pos?.y]);
|
|
61
|
+
return (_jsxs("div", { ref: area, className: cx("k-context-area", className), "aria-haspopup": "menu", "aria-describedby": `${id}-hint`, onContextMenu: (e) => {
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
openAt(e.clientX, e.clientY);
|
|
64
|
+
}, onKeyDown: (e) => {
|
|
65
|
+
if (e.key === "ContextMenu" || (e.shiftKey && e.key === "F10")) {
|
|
66
|
+
e.preventDefault();
|
|
67
|
+
const r = document.activeElement.getBoundingClientRect();
|
|
68
|
+
openAt(r.left + r.width / 2, r.top + r.height / 2);
|
|
69
|
+
}
|
|
70
|
+
}, children: [children, _jsx("span", { id: `${id}-hint`, className: "k-sr-only", children: "\u0628\u0631\u0627\u06CC \u06AF\u0632\u06CC\u0646\u0647\u200C\u0647\u0627 \u0631\u0627\u0633\u062A\u200C\u06A9\u0644\u06CC\u06A9 \u06A9\u0646\u06CC\u062F \u06CC\u0627 Shift+F10 \u0631\u0627 \u0628\u0632\u0646\u06CC\u062F." }), _jsx(MenuCtx.Provider, { value: { close }, children: _jsx("ul", { ref: list, ...{ popover: "manual" }, role: "menu", "aria-label": label, className: "k-menu k-menu--context", hidden: !pos, style: pos ? { "--_x": `${pos.x}px`, "--_y": `${pos.y}px` } : undefined, onKeyDown: (e) => menuKeyDown(e, list.current, () => close(), () => close(false)), children: menu }) })] }));
|
|
71
|
+
}
|
package/dist/DropdownMenu.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { type ReactElement, type ReactNode } from "react";
|
|
1
|
+
import { type KeyboardEvent, type ReactElement, type ReactNode } from "react";
|
|
2
|
+
export declare const MenuCtx: import("react").Context<{
|
|
3
|
+
close: (focusTrigger?: boolean) => void;
|
|
4
|
+
} | null>;
|
|
2
5
|
export interface DropdownMenuProps {
|
|
3
6
|
/** دکمهای که منو را باز میکند */
|
|
4
7
|
trigger: ReactElement;
|
|
@@ -8,12 +11,15 @@ export interface DropdownMenuProps {
|
|
|
8
11
|
align?: "start" | "end";
|
|
9
12
|
children: ReactNode;
|
|
10
13
|
}
|
|
14
|
+
export declare const items: (menu: HTMLElement | null) => HTMLElement[];
|
|
11
15
|
/**
|
|
12
16
|
* منوی کشویی با الگوی Menu Button در WAI-ARIA:
|
|
13
17
|
* Enter، Space یا ↓ باز میکند و اولین گزینه را فوکوس میکند، ↑ آخرین را؛
|
|
14
18
|
* ↑↓ و Home/End حرکت، حرف اول برای پرش، Esc بستن و برگشت به دکمه، Tab بستن.
|
|
15
19
|
*/
|
|
16
20
|
export declare function DropdownMenu({ trigger, label, align, children }: DropdownMenuProps): import("react").JSX.Element;
|
|
21
|
+
/** پیمایش کیبورد مشترک منوها: ↑↓، Home/End، حرف اول، Esc و Tab */
|
|
22
|
+
export declare function menuKeyDown(e: KeyboardEvent<HTMLElement>, menu: HTMLElement | null, close: () => void, dismiss: () => void): void;
|
|
17
23
|
export interface MenuItemProps {
|
|
18
24
|
onSelect?: () => void;
|
|
19
25
|
icon?: ReactNode;
|
package/dist/DropdownMenu.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { Children, cloneElement, createContext, isValidElement, useContext, useEffect, useId, useRef, useState, } from "react";
|
|
3
3
|
import { cx } from "./cx.js";
|
|
4
|
-
const MenuCtx = createContext(null);
|
|
5
|
-
const items = (menu) => [...(menu?.querySelectorAll('[role^="menuitem"]:not([aria-disabled="true"])') ?? [])];
|
|
4
|
+
export const MenuCtx = createContext(null);
|
|
5
|
+
export const items = (menu) => [...(menu?.querySelectorAll('[role^="menuitem"]:not([aria-disabled="true"])') ?? [])];
|
|
6
6
|
/**
|
|
7
7
|
* منوی کشویی با الگوی Menu Button در WAI-ARIA:
|
|
8
8
|
* Enter، Space یا ↓ باز میکند و اولین گزینه را فوکوس میکند، ↑ آخرین را؛
|
|
@@ -36,33 +36,7 @@ export function DropdownMenu({ trigger, label, align = "start", children }) {
|
|
|
36
36
|
focusOnOpen.current = where;
|
|
37
37
|
setOpen(true);
|
|
38
38
|
};
|
|
39
|
-
const onMenuKey = (e) =>
|
|
40
|
-
const list = items(menuRef.current);
|
|
41
|
-
const i = list.indexOf(document.activeElement);
|
|
42
|
-
const go = (j) => list[(j + list.length) % list.length]?.focus();
|
|
43
|
-
if (e.key === "ArrowDown")
|
|
44
|
-
go(i + 1);
|
|
45
|
-
else if (e.key === "ArrowUp")
|
|
46
|
-
go(i - 1);
|
|
47
|
-
else if (e.key === "Home")
|
|
48
|
-
go(0);
|
|
49
|
-
else if (e.key === "End")
|
|
50
|
-
go(list.length - 1);
|
|
51
|
-
else if (e.key === "Escape")
|
|
52
|
-
close();
|
|
53
|
-
else if (e.key === "Tab")
|
|
54
|
-
return setOpen(false);
|
|
55
|
-
else if (e.key.length === 1 && /\S/.test(e.key)) {
|
|
56
|
-
// پرش با حرف اول
|
|
57
|
-
const k = e.key.toLowerCase();
|
|
58
|
-
const start = i + 1;
|
|
59
|
-
const hit = [...list.slice(start), ...list.slice(0, start)].find((el) => el.textContent?.trim().toLowerCase().startsWith(k));
|
|
60
|
-
hit?.focus();
|
|
61
|
-
}
|
|
62
|
-
else
|
|
63
|
-
return;
|
|
64
|
-
e.preventDefault();
|
|
65
|
-
};
|
|
39
|
+
const onMenuKey = (e) => menuKeyDown(e, menuRef.current, close, () => setOpen(false));
|
|
66
40
|
const t = isValidElement(trigger)
|
|
67
41
|
? cloneElement(trigger, {
|
|
68
42
|
ref: triggerRef,
|
|
@@ -81,6 +55,34 @@ export function DropdownMenu({ trigger, label, align = "start", children }) {
|
|
|
81
55
|
: trigger;
|
|
82
56
|
return (_jsxs("span", { className: "k-menu-anchor", ref: anchorRef, children: [t, _jsx(MenuCtx.Provider, { value: { close }, children: _jsx("ul", { ref: menuRef, id: `${id}-menu`, role: "menu", "aria-label": label, "aria-labelledby": label ? undefined : `${id}-trigger`, className: cx("k-menu", align === "end" && "k-menu--end"), hidden: !open, onKeyDown: onMenuKey, children: Children.map(children, (c) => c) }) })] }));
|
|
83
57
|
}
|
|
58
|
+
/** پیمایش کیبورد مشترک منوها: ↑↓، Home/End، حرف اول، Esc و Tab */
|
|
59
|
+
export function menuKeyDown(e, menu, close, dismiss) {
|
|
60
|
+
const list = items(menu);
|
|
61
|
+
const i = list.indexOf(document.activeElement);
|
|
62
|
+
const go = (j) => list[(j + list.length) % list.length]?.focus();
|
|
63
|
+
if (e.key === "ArrowDown")
|
|
64
|
+
go(i + 1);
|
|
65
|
+
else if (e.key === "ArrowUp")
|
|
66
|
+
go(i - 1);
|
|
67
|
+
else if (e.key === "Home")
|
|
68
|
+
go(0);
|
|
69
|
+
else if (e.key === "End")
|
|
70
|
+
go(list.length - 1);
|
|
71
|
+
else if (e.key === "Escape")
|
|
72
|
+
close();
|
|
73
|
+
else if (e.key === "Tab")
|
|
74
|
+
return dismiss();
|
|
75
|
+
else if (e.key.length === 1 && /\S/.test(e.key)) {
|
|
76
|
+
// پرش با حرف اول
|
|
77
|
+
const k = e.key.toLowerCase();
|
|
78
|
+
const start = i + 1;
|
|
79
|
+
const hit = [...list.slice(start), ...list.slice(0, start)].find((el) => el.textContent?.trim().toLowerCase().startsWith(k));
|
|
80
|
+
hit?.focus();
|
|
81
|
+
}
|
|
82
|
+
else
|
|
83
|
+
return;
|
|
84
|
+
e.preventDefault();
|
|
85
|
+
}
|
|
84
86
|
export function MenuItem({ onSelect, icon, shortcut, tone = "default", disabled, href, checked, children }) {
|
|
85
87
|
const checkable = checked !== undefined;
|
|
86
88
|
const ctx = useContext(MenuCtx);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface InlineEditProps {
|
|
3
|
+
value?: string;
|
|
4
|
+
defaultValue?: string;
|
|
5
|
+
/** پس از ثبت؛ اگر Promise برگرداند تا پایانش منتظر میماند و در خطا متن قبلی برمیگردد */
|
|
6
|
+
onSave?: (value: string) => void | Promise<void>;
|
|
7
|
+
/** نام فیلد برای صفحهخوان، مثل «نام پروژه» (الزامی) */
|
|
8
|
+
label: string;
|
|
9
|
+
placeholder?: string;
|
|
10
|
+
/** اعتبارسنجی؛ پیام خطا یا undefined */
|
|
11
|
+
validate?: (value: string) => string | undefined;
|
|
12
|
+
/** نمایش متن، مثلاً بهشکل عنوان */
|
|
13
|
+
renderView?: (value: string) => ReactNode;
|
|
14
|
+
className?: string;
|
|
15
|
+
labels?: {
|
|
16
|
+
edit?: string;
|
|
17
|
+
save?: string;
|
|
18
|
+
cancel?: string;
|
|
19
|
+
empty?: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** ویرایش درجا: کلیک یا Enter برای ویرایش، Enter ثبت، Esc انصراف */
|
|
23
|
+
export declare function InlineEdit({ value, defaultValue, onSave, label, placeholder, validate, renderView, className, labels }: InlineEditProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
import { cx } from "./cx.js";
|
|
4
|
+
const Pencil = () => _jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 1.7, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: _jsx("path", { d: "m13.5 3.5 3 3L7 16H4v-3Z" }) });
|
|
5
|
+
const Check = () => _jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: _jsx("path", { d: "m5 10.5 3.2 3L15 7" }) });
|
|
6
|
+
const X = () => _jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 1.8, strokeLinecap: "round", "aria-hidden": "true", children: _jsx("path", { d: "m6 6 8 8M14 6l-8 8" }) });
|
|
7
|
+
/** ویرایش درجا: کلیک یا Enter برای ویرایش، Enter ثبت، Esc انصراف */
|
|
8
|
+
export function InlineEdit({ value, defaultValue = "", onSave, label, placeholder, validate, renderView, className, labels = {} }) {
|
|
9
|
+
const { edit = "ویرایش", save = "ثبت", cancel = "انصراف", empty = "خالی" } = labels;
|
|
10
|
+
const [inner, setInner] = useState(defaultValue);
|
|
11
|
+
const current = value ?? inner;
|
|
12
|
+
const [editing, setEditing] = useState(false);
|
|
13
|
+
const [draft, setDraft] = useState(current);
|
|
14
|
+
const [error, setError] = useState();
|
|
15
|
+
const [busy, setBusy] = useState(false);
|
|
16
|
+
const input = useRef(null);
|
|
17
|
+
const view = useRef(null);
|
|
18
|
+
const back = useRef(false);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (editing)
|
|
21
|
+
input.current?.select();
|
|
22
|
+
else if (back.current) {
|
|
23
|
+
back.current = false;
|
|
24
|
+
view.current?.focus();
|
|
25
|
+
}
|
|
26
|
+
}, [editing]);
|
|
27
|
+
const start = () => {
|
|
28
|
+
setDraft(current);
|
|
29
|
+
setError(undefined);
|
|
30
|
+
setEditing(true);
|
|
31
|
+
};
|
|
32
|
+
const stop = () => {
|
|
33
|
+
back.current = true;
|
|
34
|
+
setEditing(false);
|
|
35
|
+
};
|
|
36
|
+
const commit = async () => {
|
|
37
|
+
const v = draft.trim();
|
|
38
|
+
const err = validate?.(v);
|
|
39
|
+
if (err)
|
|
40
|
+
return setError(err);
|
|
41
|
+
if (v === current)
|
|
42
|
+
return stop();
|
|
43
|
+
const prev = current;
|
|
44
|
+
if (value === undefined)
|
|
45
|
+
setInner(v);
|
|
46
|
+
setBusy(true);
|
|
47
|
+
try {
|
|
48
|
+
await onSave?.(v);
|
|
49
|
+
stop();
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
if (value === undefined)
|
|
53
|
+
setInner(prev);
|
|
54
|
+
setError("ذخیره نشد؛ دوباره امتحان کنید.");
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
setBusy(false);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
if (!editing) {
|
|
61
|
+
return (_jsx("span", { className: cx("k-inline-edit", className), children: _jsxs("button", { ref: view, type: "button", className: "k-inline-edit__view", onClick: start, "aria-label": `${label}: ${current || empty}، ${edit}`, children: [current ? (renderView ? renderView(current) : current) : _jsx("span", { className: "k-inline-edit__placeholder", children: placeholder ?? empty }), _jsx(Pencil, {})] }) }));
|
|
62
|
+
}
|
|
63
|
+
const errId = `${label}-err`.replace(/\s/g, "-");
|
|
64
|
+
return (_jsxs("span", { className: cx("k-inline-edit", className), children: [_jsxs("span", { className: "k-field", style: { gap: 4 }, children: [_jsx("span", { className: "k-input", "data-state": error ? "invalid" : undefined, children: _jsx("input", { ref: input, className: "k-input__control", "aria-label": label, value: draft, placeholder: placeholder, "aria-invalid": error ? true : undefined, "aria-describedby": error ? errId : undefined, disabled: busy, onChange: (e) => {
|
|
65
|
+
setDraft(e.target.value);
|
|
66
|
+
setError(undefined);
|
|
67
|
+
}, onKeyDown: (e) => {
|
|
68
|
+
if (e.key === "Enter") {
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
commit();
|
|
71
|
+
}
|
|
72
|
+
else if (e.key === "Escape") {
|
|
73
|
+
e.preventDefault();
|
|
74
|
+
stop();
|
|
75
|
+
}
|
|
76
|
+
} }) }), error && _jsx("span", { className: "k-hint k-hint--error", id: errId, children: error })] }), _jsxs("span", { className: "k-inline-edit__actions", children: [_jsx("button", { type: "button", className: "k-btn k-btn--primary k-btn--icon k-btn--sm", "aria-label": save, "aria-busy": busy || undefined, onClick: commit, children: _jsx(Check, {}) }), _jsx("button", { type: "button", className: "k-btn k-btn--secondary k-btn--icon k-btn--sm", "aria-label": cancel, onClick: stop, children: _jsx(X, {}) })] })] }));
|
|
77
|
+
}
|
package/dist/Meter.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
export interface MeterProps {
|
|
3
|
+
label: ReactNode;
|
|
4
|
+
value: number;
|
|
5
|
+
/** @default 100 */
|
|
6
|
+
max?: number;
|
|
7
|
+
/** از این مقدار به بعد هشدار (زرد) @default ۸۰٪ max */
|
|
8
|
+
warning?: number;
|
|
9
|
+
/** از این مقدار به بعد بحرانی (قرمز) @default ۹۵٪ max */
|
|
10
|
+
critical?: number;
|
|
11
|
+
/** قالب مقدار، مثل «۴٫۲ گیگابایت» */
|
|
12
|
+
format?: (n: number) => string;
|
|
13
|
+
className?: string;
|
|
14
|
+
labels?: {
|
|
15
|
+
ok?: string;
|
|
16
|
+
warning?: string;
|
|
17
|
+
critical?: string;
|
|
18
|
+
of?: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** سنجهٔ یک مقدار در برابر سقف، مثل فضای ذخیره یا سهمیهٔ پیامک؛ وضعیت با متن و آیکون، نه فقط رنگ */
|
|
22
|
+
export declare function Meter({ label, value, max, warning, critical, format, className, labels }: MeterProps): import("react").JSX.Element;
|
package/dist/Meter.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { formatNumber, toFaDigits } from "@kashi-ui/persian";
|
|
3
|
+
import { cx } from "./cx.js";
|
|
4
|
+
const Icon = ({ d }) => _jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 1.8, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: _jsx("path", { d: d }) });
|
|
5
|
+
/** سنجهٔ یک مقدار در برابر سقف، مثل فضای ذخیره یا سهمیهٔ پیامک؛ وضعیت با متن و آیکون، نه فقط رنگ */
|
|
6
|
+
export function Meter({ label, value, max = 100, warning = max * 0.8, critical = max * 0.95, format = (n) => formatNumber(n), className, labels = {} }) {
|
|
7
|
+
const { ok = "در محدودهٔ مجاز", warning: warnText = "نزدیک به سقف", critical: critText = "سقف تقریباً پر شده", of = "از" } = labels;
|
|
8
|
+
const v = Math.min(max, Math.max(0, value));
|
|
9
|
+
const status = v >= critical ? "critical" : v >= warning ? "warning" : "ok";
|
|
10
|
+
const pct = Math.round((v / max) * 100);
|
|
11
|
+
const text = { ok, warning: warnText, critical: critText }[status];
|
|
12
|
+
return (_jsxs("div", { className: cx("k-meter", className), "data-status": status, children: [_jsxs("div", { className: "k-meter__head", children: [_jsx("span", { className: "k-meter__label", children: label }), _jsxs("span", { className: "k-meter__value", children: [format(v), " ", _jsxs("span", { children: [of, " ", format(max)] })] })] }), _jsxs("div", { className: "k-meter__track", role: "meter", "aria-valuenow": v, "aria-valuemin": 0, "aria-valuemax": max, "aria-valuetext": `${format(v)} ${of} ${format(max)}، ${toFaDigits(pct)}٪، ${text}`, "aria-label": typeof label === "string" ? label : undefined, children: [_jsx("span", { className: "k-meter__fill", style: { inlineSize: `${pct}%` } }), _jsx("span", { className: "k-meter__mark", style: { insetInlineStart: `${(warning / max) * 100}%` }, "aria-hidden": "true" })] }), _jsxs("p", { className: "k-meter__status", "aria-hidden": "true", children: [_jsx(Icon, { d: status === "ok" ? "m5 10.5 3.2 3L15 7" : "M10 3 2.5 16.5h15ZM10 8v4M10 14.5h.01" }), text, " \u00B7 ", toFaDigits(pct), "\u066A"] })] }));
|
|
13
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ComboboxProps } from "./Combobox.js";
|
|
2
|
+
export interface ProvinceSelectProps extends Omit<ComboboxProps, "options"> {
|
|
3
|
+
/** نمایش مرکز استان زیر نام @default true */
|
|
4
|
+
showCapital?: boolean;
|
|
5
|
+
}
|
|
6
|
+
/** انتخاب یکی از ۳۱ استان با جستجو؛ با نام مرکز استان هم پیدا میشود (مثلاً «مشهد») */
|
|
7
|
+
export declare const ProvinceSelect: import("react").ForwardRefExoticComponent<ProvinceSelectProps & import("react").RefAttributes<HTMLInputElement>>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef } from "react";
|
|
3
|
+
import { PROVINCES } from "@kashi-ui/persian";
|
|
4
|
+
import { Combobox } from "./Combobox.js";
|
|
5
|
+
const options = (capital) => PROVINCES.map((p) => ({ value: p.name, label: p.name, description: capital ? `مرکز: ${p.capital}` : undefined, keywords: [p.capital] }));
|
|
6
|
+
/** انتخاب یکی از ۳۱ استان با جستجو؛ با نام مرکز استان هم پیدا میشود (مثلاً «مشهد») */
|
|
7
|
+
export const ProvinceSelect = forwardRef(function ProvinceSelect({ showCapital = true, label = "استان", placeholder = "جستجوی استان یا شهر", ...rest }, ref) {
|
|
8
|
+
return _jsx(Combobox, { ref: ref, label: label, placeholder: placeholder, options: options(showCapital), ...rest });
|
|
9
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface SplitterProps {
|
|
3
|
+
/** دقیقاً دو فرزند: پنل ابتدا و پنل انتها */
|
|
4
|
+
children: [ReactNode, ReactNode];
|
|
5
|
+
/** «horizontal» کنار هم، «vertical» زیر هم @default "horizontal" */
|
|
6
|
+
orientation?: "horizontal" | "vertical";
|
|
7
|
+
/** اندازهٔ پنل اول به درصد */
|
|
8
|
+
size?: number;
|
|
9
|
+
defaultSize?: number;
|
|
10
|
+
onSizeChange?: (percent: number) => void;
|
|
11
|
+
/** @default 15 */
|
|
12
|
+
min?: number;
|
|
13
|
+
/** @default 85 */
|
|
14
|
+
max?: number;
|
|
15
|
+
/** نام دستگیره، مثل «اندازهٔ فهرست فایلها» @default «تغییر اندازهٔ پنلها» */
|
|
16
|
+
label?: string;
|
|
17
|
+
/** ارتفاع ظرف در حالت افقی */
|
|
18
|
+
height?: string;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
/** دو پنل با مرز قابلکشیدن؛ با کیبورد: ←→ (یا ↑↓) پنج درصد، Home/End کمینه و بیشینه */
|
|
22
|
+
export declare function Splitter({ children, orientation, size, defaultSize, onSizeChange, min, max, label, height, className }: SplitterProps): import("react").JSX.Element;
|
package/dist/Splitter.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState } from "react";
|
|
3
|
+
import { toFaDigits } from "@kashi-ui/persian";
|
|
4
|
+
import { cx } from "./cx.js";
|
|
5
|
+
/** دو پنل با مرز قابلکشیدن؛ با کیبورد: ←→ (یا ↑↓) پنج درصد، Home/End کمینه و بیشینه */
|
|
6
|
+
export function Splitter({ children, orientation = "horizontal", size, defaultSize = 40, onSizeChange, min = 15, max = 85, label = "تغییر اندازهٔ پنلها", height, className }) {
|
|
7
|
+
const [inner, setInner] = useState(defaultSize);
|
|
8
|
+
const current = size ?? inner;
|
|
9
|
+
const [dragging, setDragging] = useState(false);
|
|
10
|
+
const box = useRef(null);
|
|
11
|
+
const vertical = orientation === "vertical";
|
|
12
|
+
const set = (p) => {
|
|
13
|
+
const v = Math.round(Math.min(max, Math.max(min, p)) * 10) / 10;
|
|
14
|
+
if (size === undefined)
|
|
15
|
+
setInner(v);
|
|
16
|
+
onSizeChange?.(v);
|
|
17
|
+
};
|
|
18
|
+
const fromPointer = (e) => {
|
|
19
|
+
const r = box.current.getBoundingClientRect();
|
|
20
|
+
if (vertical)
|
|
21
|
+
return set(((e.clientY - r.top) / r.height) * 100);
|
|
22
|
+
// در RTL پنل اول سمت راست است
|
|
23
|
+
const rtl = getComputedStyle(box.current).direction === "rtl";
|
|
24
|
+
set(((rtl ? r.right - e.clientX : e.clientX - r.left) / r.width) * 100);
|
|
25
|
+
};
|
|
26
|
+
const onKey = (e) => {
|
|
27
|
+
const rtl = getComputedStyle(e.currentTarget).direction === "rtl";
|
|
28
|
+
const keys = vertical
|
|
29
|
+
? { ArrowUp: -5, ArrowDown: 5 }
|
|
30
|
+
: { ArrowLeft: rtl ? 5 : -5, ArrowRight: rtl ? -5 : 5 };
|
|
31
|
+
if (e.key in keys)
|
|
32
|
+
set(current + keys[e.key]);
|
|
33
|
+
else if (e.key === "Home")
|
|
34
|
+
set(min);
|
|
35
|
+
else if (e.key === "End")
|
|
36
|
+
set(max);
|
|
37
|
+
else
|
|
38
|
+
return;
|
|
39
|
+
e.preventDefault();
|
|
40
|
+
};
|
|
41
|
+
return (_jsxs("div", { ref: box, className: cx("k-splitter", vertical && "k-splitter--vertical", className), "data-dragging": dragging ? "" : undefined, style: { blockSize: height }, children: [_jsx("div", { className: "k-splitter__pane", style: vertical ? { blockSize: `${current}%` } : { inlineSize: `${current}%` }, children: children[0] }), _jsx("div", { className: "k-splitter__handle", role: "separator", tabIndex: 0, "aria-orientation": vertical ? "horizontal" : "vertical", "aria-valuenow": current, "aria-valuemin": min, "aria-valuemax": max, "aria-valuetext": `${toFaDigits(Math.round(current))}٪`, "aria-label": label, "data-dragging": dragging ? "" : undefined, onKeyDown: onKey, onPointerDown: (e) => {
|
|
42
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
43
|
+
setDragging(true);
|
|
44
|
+
}, onPointerMove: (e) => dragging && fromPointer(e), onPointerUp: () => setDragging(false), onPointerCancel: () => setDragging(false), onDoubleClick: () => set(defaultSize) }), _jsx("div", { className: "k-splitter__pane", children: children[1] })] }));
|
|
45
|
+
}
|
package/dist/Tour.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface TourStep {
|
|
3
|
+
/** CSS selector عنصری که این مرحله دربارهٔ آن است، مثل "#export-btn" */
|
|
4
|
+
target: string;
|
|
5
|
+
title: ReactNode;
|
|
6
|
+
content: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
export interface TourProps {
|
|
9
|
+
steps: TourStep[];
|
|
10
|
+
open: boolean;
|
|
11
|
+
onOpenChange: (open: boolean) => void;
|
|
12
|
+
/** پس از آخرین مرحله */
|
|
13
|
+
onFinish?: () => void;
|
|
14
|
+
labels?: {
|
|
15
|
+
next?: string;
|
|
16
|
+
prev?: string;
|
|
17
|
+
done?: string;
|
|
18
|
+
skip?: string;
|
|
19
|
+
step?: (n: number, total: number) => string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* راهنمای قدمبهقدم برای آشنایی با محصول: بخش هر مرحله روشن و بقیه کمرنگ میشود.
|
|
24
|
+
* Esc میبندد، ←→ بین مرحلهها و فوکوس پس از پایان به جای قبلی برمیگردد.
|
|
25
|
+
*/
|
|
26
|
+
export declare function Tour({ steps, open, onOpenChange, onFinish, labels }: TourProps): import("react").JSX.Element | null;
|
package/dist/Tour.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { toFaDigits } from "@kashi-ui/persian";
|
|
4
|
+
/**
|
|
5
|
+
* راهنمای قدمبهقدم برای آشنایی با محصول: بخش هر مرحله روشن و بقیه کمرنگ میشود.
|
|
6
|
+
* Esc میبندد، ←→ بین مرحلهها و فوکوس پس از پایان به جای قبلی برمیگردد.
|
|
7
|
+
*/
|
|
8
|
+
export function Tour({ steps, open, onOpenChange, onFinish, labels = {} }) {
|
|
9
|
+
const id = useId();
|
|
10
|
+
const { next = "بعدی", prev = "قبلی", done = "تمام", skip = "رد کردن", step: stepLabel = (n, t) => `مرحلهٔ ${toFaDigits(n)} از ${toFaDigits(t)}` } = labels;
|
|
11
|
+
const [i, setI] = useState(0);
|
|
12
|
+
const [rect, setRect] = useState(null);
|
|
13
|
+
const card = useRef(null);
|
|
14
|
+
const [cardPos, setCardPos] = useState({ top: 0, left: 0 });
|
|
15
|
+
const returnTo = useRef(null);
|
|
16
|
+
const s = steps[i];
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (open) {
|
|
19
|
+
returnTo.current = document.activeElement;
|
|
20
|
+
setI(0);
|
|
21
|
+
}
|
|
22
|
+
else
|
|
23
|
+
returnTo.current?.focus?.();
|
|
24
|
+
}, [open]);
|
|
25
|
+
// جای عنصر هدف را دنبال میکند
|
|
26
|
+
useLayoutEffect(() => {
|
|
27
|
+
if (!open || !s)
|
|
28
|
+
return;
|
|
29
|
+
const el = document.querySelector(s.target);
|
|
30
|
+
if (!el)
|
|
31
|
+
return setRect(null);
|
|
32
|
+
el.scrollIntoView({ block: "center", behavior: matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth" });
|
|
33
|
+
const update = () => setRect(el.getBoundingClientRect());
|
|
34
|
+
update();
|
|
35
|
+
const t = setTimeout(update, 400);
|
|
36
|
+
addEventListener("scroll", update, { passive: true });
|
|
37
|
+
addEventListener("resize", update);
|
|
38
|
+
return () => {
|
|
39
|
+
clearTimeout(t);
|
|
40
|
+
removeEventListener("scroll", update);
|
|
41
|
+
removeEventListener("resize", update);
|
|
42
|
+
};
|
|
43
|
+
}, [open, i, s?.target]);
|
|
44
|
+
// کارت زیر عنصر، یا بالای آن اگر جا نباشد
|
|
45
|
+
useLayoutEffect(() => {
|
|
46
|
+
if (!open || !card.current)
|
|
47
|
+
return;
|
|
48
|
+
const c = card.current.getBoundingClientRect();
|
|
49
|
+
if (!rect)
|
|
50
|
+
return setCardPos({ top: innerHeight / 2 - c.height / 2, left: innerWidth / 2 - c.width / 2 });
|
|
51
|
+
const below = rect.bottom + 12 + c.height < innerHeight;
|
|
52
|
+
const top = below ? rect.bottom + 12 : Math.max(12, rect.top - 12 - c.height);
|
|
53
|
+
const left = Math.min(innerWidth - c.width - 12, Math.max(12, rect.left + rect.width / 2 - c.width / 2));
|
|
54
|
+
setCardPos({ top, left });
|
|
55
|
+
card.current.focus({ preventScroll: true });
|
|
56
|
+
}, [rect, open, i]);
|
|
57
|
+
if (!open || !s)
|
|
58
|
+
return null;
|
|
59
|
+
const last = i === steps.length - 1;
|
|
60
|
+
const finish = () => {
|
|
61
|
+
onOpenChange(false);
|
|
62
|
+
onFinish?.();
|
|
63
|
+
};
|
|
64
|
+
return (_jsxs(_Fragment, { children: [rect && _jsx("div", { className: "k-tour__spot", "aria-hidden": "true", style: { top: rect.top - 6, left: rect.left - 6, inlineSize: rect.width + 12, blockSize: rect.height + 12 } }), _jsxs("div", { ref: card, className: "k-tour__card", role: "dialog", "aria-modal": "false", "aria-labelledby": `${id}-t`, "aria-describedby": `${id}-b`, tabIndex: -1, style: { top: cardPos.top, left: cardPos.left }, onKeyDown: (e) => {
|
|
65
|
+
if (e.key === "Escape")
|
|
66
|
+
onOpenChange(false);
|
|
67
|
+
const rtl = getComputedStyle(e.currentTarget).direction === "rtl";
|
|
68
|
+
if (e.key === (rtl ? "ArrowLeft" : "ArrowRight") && !last)
|
|
69
|
+
setI(i + 1);
|
|
70
|
+
if (e.key === (rtl ? "ArrowRight" : "ArrowLeft") && i > 0)
|
|
71
|
+
setI(i - 1);
|
|
72
|
+
}, children: [_jsx("span", { className: "k-tour__step", children: stepLabel(i + 1, steps.length) }), _jsx("p", { className: "k-tour__title", id: `${id}-t`, children: s.title }), _jsx("p", { className: "k-tour__body", id: `${id}-b`, children: s.content }), _jsxs("div", { className: "k-tour__actions", children: [_jsx("button", { type: "button", className: "k-btn k-btn--link k-tour__skip", onClick: () => onOpenChange(false), children: skip }), _jsx("span", { className: "k-tour__progress", "aria-hidden": "true", children: steps.map((_, k) => _jsx("span", { "data-on": k <= i ? "" : undefined }, k)) }), i > 0 && _jsx("button", { type: "button", className: "k-btn k-btn--secondary k-btn--sm", onClick: () => setI(i - 1), children: prev }), _jsx("button", { type: "button", className: "k-btn k-btn--primary k-btn--sm", onClick: () => (last ? finish() : setI(i + 1)), children: last ? done : next })] })] })] }));
|
|
73
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -112,3 +112,23 @@ export { MonthPicker } from "./MonthPicker.js";
|
|
|
112
112
|
export type { JalaliMonth, MonthPickerProps } from "./MonthPicker.js";
|
|
113
113
|
export { LandlineInput } from "./LandlineInput.js";
|
|
114
114
|
export type { LandlineInputProps } from "./LandlineInput.js";
|
|
115
|
+
export { BarChart, BarList, LineChart, PieChart, ProportionBar, Sparkline } from "./Chart.js";
|
|
116
|
+
export type { BarChartProps, BarListProps, LineChartProps, LineSeries, PieChartProps, ProportionBarProps, SparklineProps } from "./Chart.js";
|
|
117
|
+
export { ActivityHeatmap } from "./ActivityHeatmap.js";
|
|
118
|
+
export type { ActivityHeatmapProps } from "./ActivityHeatmap.js";
|
|
119
|
+
export { ContextMenu } from "./ContextMenu.js";
|
|
120
|
+
export type { ContextMenuProps } from "./ContextMenu.js";
|
|
121
|
+
export { InlineEdit } from "./InlineEdit.js";
|
|
122
|
+
export type { InlineEditProps } from "./InlineEdit.js";
|
|
123
|
+
export { ProvinceSelect } from "./ProvinceSelect.js";
|
|
124
|
+
export type { ProvinceSelectProps } from "./ProvinceSelect.js";
|
|
125
|
+
export { Carousel } from "./Carousel.js";
|
|
126
|
+
export type { CarouselProps } from "./Carousel.js";
|
|
127
|
+
export { ColorPicker, contrastRatio } from "./ColorPicker.js";
|
|
128
|
+
export type { ColorPickerProps } from "./ColorPicker.js";
|
|
129
|
+
export { Meter } from "./Meter.js";
|
|
130
|
+
export type { MeterProps } from "./Meter.js";
|
|
131
|
+
export { Splitter } from "./Splitter.js";
|
|
132
|
+
export type { SplitterProps } from "./Splitter.js";
|
|
133
|
+
export { Tour } from "./Tour.js";
|
|
134
|
+
export type { TourProps, TourStep } from "./Tour.js";
|
package/dist/index.js
CHANGED
|
@@ -56,3 +56,13 @@ export { PasswordStrength, defaultPasswordRules } from "./PasswordStrength.js";
|
|
|
56
56
|
export { Countdown } from "./Countdown.js";
|
|
57
57
|
export { MonthPicker } from "./MonthPicker.js";
|
|
58
58
|
export { LandlineInput } from "./LandlineInput.js";
|
|
59
|
+
export { BarChart, BarList, LineChart, PieChart, ProportionBar, Sparkline } from "./Chart.js";
|
|
60
|
+
export { ActivityHeatmap } from "./ActivityHeatmap.js";
|
|
61
|
+
export { ContextMenu } from "./ContextMenu.js";
|
|
62
|
+
export { InlineEdit } from "./InlineEdit.js";
|
|
63
|
+
export { ProvinceSelect } from "./ProvinceSelect.js";
|
|
64
|
+
export { Carousel } from "./Carousel.js";
|
|
65
|
+
export { ColorPicker, contrastRatio } from "./ColorPicker.js";
|
|
66
|
+
export { Meter } from "./Meter.js";
|
|
67
|
+
export { Splitter } from "./Splitter.js";
|
|
68
|
+
export { Tour } from "./Tour.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kashi-ui/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "کامپوننتهای React دیزاینسیستم کاشی",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"test": "node --test test/*.test.mjs"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@kashi-ui/css": "^0.
|
|
25
|
+
"@kashi-ui/css": "^0.13.0",
|
|
26
26
|
"react": ">=18"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
}
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@kashi-ui/bank-logos": "^0.
|
|
63
|
-
"@kashi-ui/persian": "^0.
|
|
62
|
+
"@kashi-ui/bank-logos": "^0.13.0",
|
|
63
|
+
"@kashi-ui/persian": "^0.13.0"
|
|
64
64
|
}
|
|
65
65
|
}
|