@kashi-ui/react 1.5.0 → 1.6.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/dist/Carousel.d.ts +23 -4
- package/dist/Carousel.js +143 -22
- package/dist/ContextMenu.js +18 -1
- package/dist/FileUpload.d.ts +33 -1
- package/dist/FileUpload.js +93 -9
- package/dist/Input.d.ts +6 -0
- package/dist/Input.js +2 -2
- package/dist/Radio.d.ts +4 -0
- package/dist/Radio.js +2 -2
- package/dist/Tooltip.d.ts +2 -0
- package/dist/Tooltip.js +22 -1
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +48 -0
- package/package.json +4 -4
package/dist/Carousel.d.ts
CHANGED
|
@@ -4,19 +4,38 @@ export interface CarouselProps {
|
|
|
4
4
|
children: ReactNode;
|
|
5
5
|
/** نام اسلایدر، مثل «محصولات پیشنهادی» (الزامی) */
|
|
6
6
|
label: string;
|
|
7
|
-
/** تعداد اسلاید در هر
|
|
7
|
+
/** تعداد اسلاید در هر نما؛ با effect="fade" همیشه ۱ @default 1 */
|
|
8
8
|
perView?: number;
|
|
9
9
|
/** نمایش نقطهها @default true */
|
|
10
10
|
dots?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* «slide» ریل اسکرول بومی؛ «fade» محو شدن اسلایدها روی هم با opacity، برای عکسهای بزرگ
|
|
13
|
+
* (Safari لایههای بزرگِ دارای transform را تکهتکه و تکراری رسم میکند). کشیدن انگشت در هر دو کار میکند. @default "slide"
|
|
14
|
+
*/
|
|
15
|
+
effect?: "slide" | "fade";
|
|
16
|
+
/**
|
|
17
|
+
* پخش خودکار؛ true یعنی هر ۵ ثانیه، عدد یعنی فاصله به میلیثانیه. با hover، فوکوس، پنهان شدن زبانه و نمای بزرگ مکث میکند،
|
|
18
|
+
* دکمهٔ توقف دارد (WCAG 2.2.2) و با prefers-reduced-motion متوقف شروع میشود.
|
|
19
|
+
*/
|
|
20
|
+
autoplay?: boolean | number;
|
|
21
|
+
/** نوار تصویرهای کوچک زیر اسلایدر؛ یکی برای هر اسلاید، معمولاً <img> کوچک */
|
|
22
|
+
thumbnails?: ReactNode[];
|
|
23
|
+
/** دکمهٔ «نمایش بزرگ» و باز شدن اسلاید با کلیک در پنجرهٔ تمامصفحه (Esc، کلیدهای جهت، فوکوس داخل پنجره) */
|
|
24
|
+
lightbox?: boolean;
|
|
25
|
+
/** بعد از عوض شدن اسلاید */
|
|
26
|
+
onIndexChange?: (index: number) => void;
|
|
11
27
|
className?: string;
|
|
12
28
|
labels?: {
|
|
13
29
|
prev?: string;
|
|
14
30
|
next?: string;
|
|
15
31
|
slide?: (n: number, total: number) => string;
|
|
32
|
+
play?: string;
|
|
33
|
+
pause?: string;
|
|
34
|
+
expand?: string;
|
|
16
35
|
};
|
|
17
36
|
}
|
|
18
37
|
/**
|
|
19
|
-
* اسلایدر روی scroll-snap
|
|
20
|
-
*
|
|
38
|
+
* اسلایدر روی scroll-snap بومی (یا محو شدن با effect="fade"): کشیدن، چرخ موس، دکمهها و کلیدهای جهت.
|
|
39
|
+
* اختیاری: تصویرهای کوچک، پخش خودکار با نوار پیشرفت و دکمهٔ توقف، و نمای بزرگ تمامصفحه.
|
|
21
40
|
*/
|
|
22
|
-
export declare function Carousel({ children, label, perView, dots, className, labels }: CarouselProps): import("react").JSX.Element;
|
|
41
|
+
export declare function Carousel({ children, label, perView: perViewProp, dots, effect, autoplay, thumbnails, lightbox, onIndexChange, className, labels, }: CarouselProps): import("react").JSX.Element;
|
package/dist/Carousel.js
CHANGED
|
@@ -1,25 +1,70 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { Children, useEffect, useId, useRef, useState } from "react";
|
|
4
4
|
import { cx } from "./cx.js";
|
|
5
5
|
import { useKashi } from "./KashiProvider.js";
|
|
6
6
|
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" }) }));
|
|
7
|
+
const PlayIcon = ({ playing }) => (_jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 1.8, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: playing ? _jsx("path", { d: "M7.5 5v10M12.5 5v10" }) : _jsx("path", { d: "M7 4.8v10.4a.6.6 0 0 0 .9.5l8-5.2a.6.6 0 0 0 0-1L7.9 4.3a.6.6 0 0 0-.9.5Z" }) }));
|
|
8
|
+
const ExpandIcon = () => (_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: "M12 3.5h4.5V8M8 16.5H3.5V12M16.5 3.5 11.5 8.5M3.5 16.5l5-5" }) }));
|
|
9
|
+
const CloseIcon = () => (_jsx("svg", { viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 1.8, strokeLinecap: "round", "aria-hidden": "true", children: _jsx("path", { d: "m5.5 5.5 9 9m0-9-9 9" }) }));
|
|
10
|
+
/** inert در React 18 و 19 یکسان نوشته نمیشود؛ مستقیم روی DOM */
|
|
11
|
+
const inert = (on) => (el) => {
|
|
12
|
+
if (el)
|
|
13
|
+
on ? el.setAttribute("inert", "") : el.removeAttribute("inert");
|
|
14
|
+
};
|
|
15
|
+
const isRtl = (el) => getComputedStyle(el).direction === "rtl";
|
|
16
|
+
/** کشیدن انگشت یا قلم: در RTL اسلاید بعدی سمت چپ است، پس کشیدن به راست یعنی «بعدی» */
|
|
17
|
+
function useSwipe(onStep) {
|
|
18
|
+
const start = useRef(null);
|
|
19
|
+
return {
|
|
20
|
+
onPointerDown: (e) => {
|
|
21
|
+
if (e.pointerType === "mouse")
|
|
22
|
+
return;
|
|
23
|
+
start.current = { x: e.clientX, y: e.clientY };
|
|
24
|
+
},
|
|
25
|
+
onPointerUp: (e) => {
|
|
26
|
+
const s = start.current;
|
|
27
|
+
start.current = null;
|
|
28
|
+
if (!s)
|
|
29
|
+
return;
|
|
30
|
+
const dx = e.clientX - s.x, dy = e.clientY - s.y;
|
|
31
|
+
if (Math.abs(dx) < 40 || Math.abs(dx) < Math.abs(dy) * 1.2)
|
|
32
|
+
return;
|
|
33
|
+
onStep((dx < 0) !== isRtl(e.currentTarget) ? 1 : -1);
|
|
34
|
+
},
|
|
35
|
+
onPointerCancel: () => {
|
|
36
|
+
start.current = null;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
7
40
|
/**
|
|
8
|
-
* اسلایدر روی scroll-snap
|
|
9
|
-
*
|
|
41
|
+
* اسلایدر روی scroll-snap بومی (یا محو شدن با effect="fade"): کشیدن، چرخ موس، دکمهها و کلیدهای جهت.
|
|
42
|
+
* اختیاری: تصویرهای کوچک، پخش خودکار با نوار پیشرفت و دکمهٔ توقف، و نمای بزرگ تمامصفحه.
|
|
10
43
|
*/
|
|
11
|
-
export function Carousel({ children, label, perView = 1, dots = true, className, labels = {} }) {
|
|
44
|
+
export function Carousel({ children, label, perView: perViewProp = 1, dots = true, effect = "slide", autoplay, thumbnails, lightbox, onIndexChange, className, labels = {}, }) {
|
|
12
45
|
const id = useId();
|
|
13
46
|
const { messages: M, n: N } = useKashi();
|
|
14
|
-
const { prev = M.common.prev, next = M.common.next, slide = (a, t) => M.carousel.slideOf(N(a), N(t)) } = labels;
|
|
47
|
+
const { prev = M.common.prev, next = M.common.next, slide = (a, t) => M.carousel.slideOf(N(a), N(t)), play = M.carousel.play, pause = M.carousel.pause, expand = M.carousel.expand, } = labels;
|
|
48
|
+
const fade = effect === "fade";
|
|
49
|
+
const perView = fade ? 1 : perViewProp;
|
|
15
50
|
const slides = Children.toArray(children);
|
|
16
51
|
const track = useRef(null);
|
|
17
|
-
const
|
|
52
|
+
const thumbs = useRef(null);
|
|
53
|
+
const dialog = useRef(null);
|
|
54
|
+
const [index, setIndexState] = useState(0);
|
|
55
|
+
const [boxIndex, setBoxIndex] = useState(null);
|
|
18
56
|
const pages = Math.max(1, slides.length - perView + 1);
|
|
19
|
-
|
|
57
|
+
const setIndex = (i) => {
|
|
58
|
+
setIndexState((old) => {
|
|
59
|
+
if (old !== i)
|
|
60
|
+
onIndexChange?.(i);
|
|
61
|
+
return i;
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
// اسلاید فعال از روی موقعیت اسکرول (فقط در حالت slide)
|
|
20
65
|
useEffect(() => {
|
|
21
66
|
const el = track.current;
|
|
22
|
-
if (!el)
|
|
67
|
+
if (!el || fade || typeof IntersectionObserver === "undefined")
|
|
23
68
|
return;
|
|
24
69
|
const items = [...el.children];
|
|
25
70
|
const io = new IntersectionObserver((entries) => {
|
|
@@ -29,21 +74,80 @@ export function Carousel({ children, label, perView = 1, dots = true, className,
|
|
|
29
74
|
}, { root: el, threshold: [0.6, 1] });
|
|
30
75
|
items.forEach((s) => io.observe(s));
|
|
31
76
|
return () => io.disconnect();
|
|
32
|
-
}, [slides.length]);
|
|
33
|
-
const go = (i) => {
|
|
34
|
-
const n = Math.max(0, Math.min(pages - 1, i));
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
77
|
+
}, [slides.length, fade]);
|
|
78
|
+
const go = (i, wrap = false) => {
|
|
79
|
+
const n = wrap ? (i + pages) % pages : Math.max(0, Math.min(pages - 1, i));
|
|
80
|
+
if (!fade) {
|
|
81
|
+
const t = track.current;
|
|
82
|
+
const el = t?.children[n];
|
|
83
|
+
if (!el || !t)
|
|
84
|
+
return;
|
|
85
|
+
// scrollIntoView صفحه را هم اسکرول میکند؛ فقط خود ریل را جابهجا میکنیم
|
|
86
|
+
const offset = isRtl(t) ? -(t.getBoundingClientRect().right - el.getBoundingClientRect().right) + t.scrollLeft : el.getBoundingClientRect().left - t.getBoundingClientRect().left + t.scrollLeft;
|
|
87
|
+
t.scrollTo?.({ left: offset });
|
|
88
|
+
}
|
|
43
89
|
setIndex(n);
|
|
44
90
|
};
|
|
45
|
-
|
|
46
|
-
|
|
91
|
+
// تصویر کوچک فعال در نوار خودش دیده شود (بدون اسکرول صفحه)
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
const strip = thumbs.current;
|
|
94
|
+
const b = strip?.children[index];
|
|
95
|
+
if (!strip || !b)
|
|
96
|
+
return;
|
|
97
|
+
const s = strip.getBoundingClientRect(), r = b.getBoundingClientRect();
|
|
98
|
+
if (r.left < s.left || r.right > s.right)
|
|
99
|
+
strip.scrollTo?.({ left: strip.scrollLeft + (r.left + r.width / 2) - (s.left + s.width / 2) });
|
|
100
|
+
}, [index]);
|
|
101
|
+
// ---- پخش خودکار
|
|
102
|
+
const interval = autoplay === true ? 5000 : typeof autoplay === "number" && autoplay > 0 ? autoplay : 0;
|
|
103
|
+
const [playing, setPlaying] = useState(interval > 0);
|
|
104
|
+
const [hold, setHold] = useState({ hover: false, focus: false, hidden: false });
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (!interval)
|
|
107
|
+
return;
|
|
108
|
+
if (matchMedia?.("(prefers-reduced-motion: reduce)").matches)
|
|
109
|
+
setPlaying(false);
|
|
110
|
+
const onVis = () => setHold((h) => ({ ...h, hidden: document.hidden }));
|
|
111
|
+
document.addEventListener("visibilitychange", onVis);
|
|
112
|
+
return () => document.removeEventListener("visibilitychange", onVis);
|
|
113
|
+
}, [interval]);
|
|
114
|
+
const running = interval > 0 && pages > 1 && playing && !hold.hover && !hold.focus && !hold.hidden && boxIndex === null;
|
|
115
|
+
const left = useRef(interval);
|
|
116
|
+
const startedAt = useRef(0);
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
left.current = interval;
|
|
119
|
+
}, [index, interval]);
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!running)
|
|
122
|
+
return;
|
|
123
|
+
startedAt.current = Date.now();
|
|
124
|
+
const t = setTimeout(() => go(index + 1, true), left.current);
|
|
125
|
+
return () => {
|
|
126
|
+
clearTimeout(t);
|
|
127
|
+
left.current = Math.max(0, left.current - (Date.now() - startedAt.current));
|
|
128
|
+
};
|
|
129
|
+
}, [running, index]);
|
|
130
|
+
// ---- نمای بزرگ
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
const d = dialog.current;
|
|
133
|
+
if (!d)
|
|
134
|
+
return;
|
|
135
|
+
if (boxIndex !== null && !d.open)
|
|
136
|
+
d.showModal?.();
|
|
137
|
+
if (boxIndex === null && d.open)
|
|
138
|
+
d.close();
|
|
139
|
+
}, [boxIndex !== null]);
|
|
140
|
+
const openBox = (i) => lightbox && setBoxIndex(i);
|
|
141
|
+
const boxGo = (i) => setBoxIndex((i + slides.length) % slides.length);
|
|
142
|
+
const closeBox = () => {
|
|
143
|
+
if (boxIndex !== null)
|
|
144
|
+
go(Math.min(boxIndex, pages - 1));
|
|
145
|
+
setBoxIndex(null);
|
|
146
|
+
};
|
|
147
|
+
const swipe = useSwipe((step) => go(index + step));
|
|
148
|
+
const boxSwipe = useSwipe((step) => boxIndex !== null && boxGo(boxIndex + step));
|
|
149
|
+
return (_jsxs("section", { className: cx("k-carousel", fade && "k-carousel--fade", className), "aria-roledescription": M.carousel.slider, "aria-label": label, "data-lightbox": lightbox ? "" : undefined, style: { "--k-carousel-per-view": perView }, onMouseEnter: () => setHold((h) => ({ ...h, hover: true })), onMouseLeave: () => setHold((h) => ({ ...h, hover: false })), onFocus: () => setHold((h) => ({ ...h, focus: true })), onBlur: (e) => !e.currentTarget.contains(e.relatedTarget) && setHold((h) => ({ ...h, focus: false })), children: [_jsx("div", { ref: track, className: "k-carousel__track", tabIndex: 0, "aria-live": running ? "off" : "polite", ...(fade ? swipe : {}), onKeyDown: (e) => {
|
|
150
|
+
const rtl = isRtl(e.currentTarget);
|
|
47
151
|
const step = { ArrowLeft: rtl ? 1 : -1, ArrowRight: rtl ? -1 : 1 }[e.key];
|
|
48
152
|
if (step) {
|
|
49
153
|
e.preventDefault();
|
|
@@ -53,5 +157,22 @@ export function Carousel({ children, label, perView = 1, dots = true, className,
|
|
|
53
157
|
go(0);
|
|
54
158
|
else if (e.key === "End")
|
|
55
159
|
go(pages - 1);
|
|
56
|
-
|
|
160
|
+
else if (lightbox && e.key === "Enter" && e.target === e.currentTarget)
|
|
161
|
+
openBox(index);
|
|
162
|
+
}, children: slides.map((s, i) => {
|
|
163
|
+
const off = fade && i !== index;
|
|
164
|
+
return (_jsx("div", { className: "k-carousel__slide", role: "group", "aria-roledescription": M.carousel.slide, "aria-label": slide(i + 1, slides.length), id: `${id}-s${i}`, "data-active": fade ? (i === index ? "" : undefined) : undefined, "aria-hidden": off || undefined, ref: inert(off), onClick: lightbox ? (e) => !e.target.closest("a, button, input, select, textarea, [tabindex]") && openBox(i) : undefined, children: s }, i));
|
|
165
|
+
}) }), interval > 0 && pages > 1 && (_jsx("span", { className: "k-carousel__progress", "aria-hidden": "true", children: _jsx("span", { className: "k-carousel__progress-bar", "data-paused": running ? undefined : "", style: { "--_d": `${interval}ms` } }, `${index}-${playing}`) })), thumbnails && thumbnails.length > 0 && (_jsx("div", { ref: thumbs, className: "k-carousel__thumbs", role: "group", "aria-label": M.carousel.thumbnails(label), children: thumbnails.map((t, i) => (_jsx("button", { type: "button", className: "k-carousel__thumb", "aria-label": slide(i + 1, slides.length), "aria-current": Math.min(i, pages - 1) === index && (perView === 1 || i === index), "aria-controls": `${id}-s${i}`, onClick: () => go(i), children: t }, i))) })), (pages > 1 || lightbox) && (_jsxs("div", { className: "k-carousel__bar", children: [dots && pages > 1 ? (_jsx("ul", { className: "k-carousel__dots", "aria-label": M.carousel.slides(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: [interval > 0 && pages > 1 && (_jsx("button", { type: "button", className: "k-btn k-btn--tertiary k-btn--icon k-btn--sm", "aria-label": playing ? pause : play, "aria-pressed": !playing, onClick: () => setPlaying((p) => !p), children: _jsx(PlayIcon, { playing: playing }) })), lightbox && (_jsx("button", { type: "button", className: "k-btn k-btn--tertiary k-btn--icon k-btn--sm", "aria-label": expand, "aria-haspopup": "dialog", onClick: () => openBox(index), children: _jsx(ExpandIcon, {}) })), pages > 1 && (_jsxs(_Fragment, { 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 }) })] }))] })] })), lightbox && (_jsx("dialog", { ref: dialog, className: "k-carousel__lightbox", "aria-label": M.carousel.lightbox(label), onClose: closeBox, onCancel: (e) => {
|
|
166
|
+
e.preventDefault();
|
|
167
|
+
closeBox();
|
|
168
|
+
}, onClick: (e) => e.target === e.currentTarget && closeBox(), onKeyDown: (e) => {
|
|
169
|
+
if (boxIndex === null)
|
|
170
|
+
return;
|
|
171
|
+
const rtl = isRtl(e.currentTarget);
|
|
172
|
+
const step = { ArrowLeft: rtl ? 1 : -1, ArrowRight: rtl ? -1 : 1 }[e.key];
|
|
173
|
+
if (step) {
|
|
174
|
+
e.preventDefault();
|
|
175
|
+
boxGo(boxIndex + step);
|
|
176
|
+
}
|
|
177
|
+
}, children: boxIndex !== null && (_jsxs(_Fragment, { children: [_jsxs("div", { className: "k-carousel__lightbox-head", children: [_jsx("span", { className: "k-carousel__lightbox-count", "aria-live": "polite", children: slide(boxIndex + 1, slides.length) }), _jsx("button", { type: "button", className: "k-btn k-btn--tertiary k-btn--icon", "aria-label": M.common.close, onClick: closeBox, autoFocus: true, children: _jsx(CloseIcon, {}) })] }), _jsx("div", { className: "k-carousel__lightbox-stage", ...boxSwipe, children: slides.map((s, i) => (_jsx("div", { className: "k-carousel__lightbox-slide", "data-active": i === boxIndex ? "" : undefined, "aria-hidden": i !== boxIndex || undefined, ref: inert(i !== boxIndex), children: s }, i))) }), slides.length > 1 && (_jsxs("div", { className: "k-carousel__lightbox-nav", children: [_jsx("button", { type: "button", className: "k-btn k-btn--secondary k-btn--icon", "aria-label": prev, onClick: () => boxGo(boxIndex - 1), children: _jsx(Arrow, {}) }), _jsx("button", { type: "button", className: "k-btn k-btn--secondary k-btn--icon", "aria-label": next, onClick: () => boxGo(boxIndex + 1), children: _jsx(Arrow, { next: true }) })] }))] })) }))] }));
|
|
57
178
|
}
|
package/dist/ContextMenu.js
CHANGED
|
@@ -17,12 +17,20 @@ export function ContextMenu({ children, menu, label, className }) {
|
|
|
17
17
|
const list = useRef(null);
|
|
18
18
|
const returnTo = useRef(null);
|
|
19
19
|
const placed = useRef(false);
|
|
20
|
+
// نگهداشتن انگشت روی صفحهٔ لمسی؛ Safari در iOS رویداد contextmenu نمیفرستد
|
|
21
|
+
const press = useRef(null);
|
|
22
|
+
const cancelPress = () => {
|
|
23
|
+
if (press.current)
|
|
24
|
+
clearTimeout(press.current.t);
|
|
25
|
+
press.current = null;
|
|
26
|
+
};
|
|
20
27
|
const close = (focusBack = true) => {
|
|
21
28
|
placed.current = false;
|
|
22
29
|
setPos(null);
|
|
23
30
|
if (focusBack)
|
|
24
31
|
returnTo.current?.focus();
|
|
25
32
|
};
|
|
33
|
+
useEffect(() => cancelPress, []);
|
|
26
34
|
const openAt = (x, y) => {
|
|
27
35
|
returnTo.current = document.activeElement ?? area.current;
|
|
28
36
|
setPos({ x, y });
|
|
@@ -65,7 +73,16 @@ export function ContextMenu({ children, menu, label, className }) {
|
|
|
65
73
|
return (_jsxs("div", { ref: area, className: cx("k-context-area", className), "aria-haspopup": "menu", "aria-describedby": `${id}-hint`, onContextMenu: (e) => {
|
|
66
74
|
e.preventDefault();
|
|
67
75
|
openAt(e.clientX, e.clientY);
|
|
68
|
-
},
|
|
76
|
+
}, onPointerDown: (e) => {
|
|
77
|
+
if (e.pointerType !== "touch")
|
|
78
|
+
return;
|
|
79
|
+
cancelPress();
|
|
80
|
+
const { clientX: x, clientY: y } = e;
|
|
81
|
+
press.current = { x, y, t: setTimeout(() => { press.current = null; openAt(x, y); }, 500) };
|
|
82
|
+
}, onPointerMove: (e) => {
|
|
83
|
+
if (press.current && Math.hypot(e.clientX - press.current.x, e.clientY - press.current.y) > 10)
|
|
84
|
+
cancelPress();
|
|
85
|
+
}, onPointerUp: cancelPress, onPointerCancel: cancelPress, onKeyDown: (e) => {
|
|
69
86
|
if (e.key === "ContextMenu" || (e.shiftKey && e.key === "F10")) {
|
|
70
87
|
e.preventDefault();
|
|
71
88
|
const r = document.activeElement.getBoundingClientRect();
|
package/dist/FileUpload.d.ts
CHANGED
|
@@ -3,13 +3,35 @@ import { type KashiLocale } from "@kashi-ui/persian";
|
|
|
3
3
|
export type FileStatus = "uploading" | "done" | "error";
|
|
4
4
|
export interface FileItem {
|
|
5
5
|
id: string;
|
|
6
|
+
/** فایلی که ارسال میشود؛ اگر resizeImages کوچکش کرده باشد، همان نسخهٔ کوچک */
|
|
6
7
|
file: File;
|
|
7
8
|
status: FileStatus;
|
|
8
9
|
progress: number;
|
|
9
10
|
error?: string;
|
|
11
|
+
/** نشانی پیشنمایش (object URL) برای تصویرها */
|
|
12
|
+
previewUrl?: string;
|
|
13
|
+
/** توضیح کوتاه، مثل «کوچک شد به ۲۰۴۸×۱۵۳۶» */
|
|
14
|
+
note?: string;
|
|
10
15
|
}
|
|
11
16
|
/** حجم فایل: ۱٫۲ مگابایت؛ با locale="en" ← «1.2 MB» */
|
|
12
17
|
export declare function formatBytes(bytes: number, locale?: KashiLocale): string;
|
|
18
|
+
export interface ImageResizeOptions {
|
|
19
|
+
/** بلندترین ضلع تصویر بعد از کوچک شدن، به پیکسل */
|
|
20
|
+
maxDimension: number;
|
|
21
|
+
/** کیفیت JPEG/WebP بین ۰ و ۱ @default 0.85 */
|
|
22
|
+
quality?: number;
|
|
23
|
+
/** نوع خروجی؛ پیشفرض همان نوع فایل */
|
|
24
|
+
type?: "image/jpeg" | "image/webp" | "image/png";
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* عکس بزرگ را پیش از ارسال در مرورگر کوچک میکند (JPEG، PNG و WebP؛ GIF و SVG دست نمیخورند).
|
|
28
|
+
* اگر عکس از maxDimension کوچکتر باشد یا خروجی سبکتر نشود، null برمیگرداند و همان فایل اصلی میماند.
|
|
29
|
+
*/
|
|
30
|
+
export declare function shrinkImage(file: File, { maxDimension, quality, type }: ImageResizeOptions): Promise<{
|
|
31
|
+
file: File;
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
} | null>;
|
|
13
35
|
export interface FileUploadProps {
|
|
14
36
|
/** مثل "image/*,.pdf" */
|
|
15
37
|
accept?: string;
|
|
@@ -23,7 +45,17 @@ export interface FileUploadProps {
|
|
|
23
45
|
/** متن راهنما، مثل «PNG یا PDF، حداکثر ۵ مگابایت» */
|
|
24
46
|
hint?: ReactNode;
|
|
25
47
|
disabled?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* نام فیلد در فرم: فایلهای پذیرفتهشده (بعد از کوچک شدن) در یک <input type="file" name> واقعی میمانند و با ارسال
|
|
50
|
+
* معمولی فرم، FormData و Server Action فرستاده میشوند؛ uploader لازم نیست.
|
|
51
|
+
*/
|
|
26
52
|
name?: string;
|
|
53
|
+
/** با name: فرم بدون فایل ارسال نمیشود */
|
|
54
|
+
required?: boolean;
|
|
55
|
+
/** پیشنمایش کوچک برای تصویرها به جای پسوند فایل @default true */
|
|
56
|
+
preview?: boolean;
|
|
57
|
+
/** کوچک کردن عکسهای بزرگ در مرورگر پیش از بررسی حجم و ارسال، مثل { maxDimension: 2048, quality: 0.85 } */
|
|
58
|
+
resizeImages?: ImageResizeOptions;
|
|
27
59
|
className?: string;
|
|
28
60
|
labels?: {
|
|
29
61
|
browse?: string;
|
|
@@ -37,4 +69,4 @@ export interface FileUploadProps {
|
|
|
37
69
|
};
|
|
38
70
|
}
|
|
39
71
|
/** بارگذاری فایل با کشیدن و رها کردن یا دکمهٔ انتخاب؛ هر فایل وضعیت و پیشرفت خودش را دارد */
|
|
40
|
-
export declare function FileUpload({ accept, multiple, maxSize, maxFiles, uploader, onFilesChange, hint, disabled, name, className, labels }: FileUploadProps): import("react").JSX.Element;
|
|
72
|
+
export declare function FileUpload({ accept, multiple, maxSize, maxFiles, uploader, onFilesChange, hint, disabled, name, required, preview, resizeImages, className, labels, }: FileUploadProps): import("react").JSX.Element;
|
package/dist/FileUpload.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { useId, useRef, useState } from "react";
|
|
3
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
4
4
|
import { getMessages, toFaDigits } from "@kashi-ui/persian";
|
|
5
5
|
import { cx } from "./cx.js";
|
|
6
6
|
import { Progress } from "./Progress.js";
|
|
@@ -18,6 +18,54 @@ export function formatBytes(bytes, locale = "fa") {
|
|
|
18
18
|
const n = i === 0 ? String(v) : v.toFixed(v < 10 ? 1 : 0).replace(/\.0$/, "");
|
|
19
19
|
return `${en ? n : toFaDigits(n).replace(".", "٫")} ${units[i]}`;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* عکس بزرگ را پیش از ارسال در مرورگر کوچک میکند (JPEG، PNG و WebP؛ GIF و SVG دست نمیخورند).
|
|
23
|
+
* اگر عکس از maxDimension کوچکتر باشد یا خروجی سبکتر نشود، null برمیگرداند و همان فایل اصلی میماند.
|
|
24
|
+
*/
|
|
25
|
+
export async function shrinkImage(file, { maxDimension, quality = 0.85, type }) {
|
|
26
|
+
if (!/^image\/(jpeg|png|webp)$/.test(file.type) || typeof createImageBitmap !== "function")
|
|
27
|
+
return null;
|
|
28
|
+
let bmp;
|
|
29
|
+
try {
|
|
30
|
+
bmp = await createImageBitmap(file, { imageOrientation: "from-image" });
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const scale = Math.min(1, maxDimension / Math.max(bmp.width, bmp.height));
|
|
36
|
+
if (scale >= 1) {
|
|
37
|
+
bmp.close?.();
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const width = Math.round(bmp.width * scale), height = Math.round(bmp.height * scale);
|
|
41
|
+
const out = type ?? file.type;
|
|
42
|
+
let blob = null;
|
|
43
|
+
try {
|
|
44
|
+
if (typeof OffscreenCanvas !== "undefined") {
|
|
45
|
+
const c = new OffscreenCanvas(width, height);
|
|
46
|
+
const ctx = c.getContext("2d");
|
|
47
|
+
ctx.imageSmoothingQuality = "high";
|
|
48
|
+
ctx.drawImage(bmp, 0, 0, width, height);
|
|
49
|
+
blob = await c.convertToBlob({ type: out, quality });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
const c = document.createElement("canvas");
|
|
53
|
+
c.width = width;
|
|
54
|
+
c.height = height;
|
|
55
|
+
const ctx = c.getContext("2d");
|
|
56
|
+
ctx.imageSmoothingQuality = "high";
|
|
57
|
+
ctx.drawImage(bmp, 0, 0, width, height);
|
|
58
|
+
blob = await new Promise((r) => c.toBlob(r, out, quality));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
bmp.close?.();
|
|
63
|
+
}
|
|
64
|
+
if (!blob || blob.size >= file.size)
|
|
65
|
+
return null;
|
|
66
|
+
const name = out === file.type ? file.name : `${file.name.replace(/\.[^.]+$/, "")}.${out === "image/jpeg" ? "jpg" : out.split("/")[1]}`;
|
|
67
|
+
return { file: new File([blob], name, { type: out, lastModified: file.lastModified }), width, height };
|
|
68
|
+
}
|
|
21
69
|
const ext = (name) => (name.split(".").pop() ?? "").slice(0, 4).toUpperCase();
|
|
22
70
|
function accepts(file, accept) {
|
|
23
71
|
if (!accept)
|
|
@@ -27,10 +75,13 @@ function accepts(file, accept) {
|
|
|
27
75
|
const UploadIcon = () => (_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: "M10 13V3.5m-4 4 4-4 4 4M4 13v2.5A1.5 1.5 0 0 0 5.5 17h9a1.5 1.5 0 0 0 1.5-1.5V13" }) }));
|
|
28
76
|
const TrashIcon = () => (_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: "M3.5 5.5h13M8 5.5V4h4v1.5M5 5.5l.8 10.2a1.5 1.5 0 0 0 1.5 1.3h5.4a1.5 1.5 0 0 0 1.5-1.3L15 5.5" }) }));
|
|
29
77
|
/** بارگذاری فایل با کشیدن و رها کردن یا دکمهٔ انتخاب؛ هر فایل وضعیت و پیشرفت خودش را دارد */
|
|
30
|
-
export function FileUpload({ accept, multiple = true, maxSize, maxFiles, uploader, onFilesChange, hint, disabled, name, className, labels = {} }) {
|
|
78
|
+
export function FileUpload({ accept, multiple = true, maxSize, maxFiles, uploader, onFilesChange, hint, disabled, name, required, preview = true, resizeImages, className, labels = {}, }) {
|
|
31
79
|
const id = useId();
|
|
32
80
|
const inputRef = useRef(null);
|
|
81
|
+
const formRef = useRef(null);
|
|
33
82
|
const [items, setItems] = useState([]);
|
|
83
|
+
const itemsRef = useRef(items);
|
|
84
|
+
itemsRef.current = items;
|
|
34
85
|
const [dragging, setDragging] = useState(false);
|
|
35
86
|
const [announce, setAnnounce] = useState("");
|
|
36
87
|
const { messages: M, n: N, locale } = useKashi();
|
|
@@ -41,19 +92,50 @@ export function FileUpload({ accept, multiple = true, maxSize, maxFiles, uploade
|
|
|
41
92
|
onFilesChange?.(next);
|
|
42
93
|
return next;
|
|
43
94
|
});
|
|
95
|
+
// فیلد file واقعی فرم همیشه همان فایلهای پذیرفتهشدهٔ فهرست را دارد
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
const el = formRef.current;
|
|
98
|
+
if (!el)
|
|
99
|
+
return;
|
|
100
|
+
try {
|
|
101
|
+
const dt = new DataTransfer();
|
|
102
|
+
for (const it of items)
|
|
103
|
+
if (it.status !== "error")
|
|
104
|
+
dt.items.add(it.file);
|
|
105
|
+
el.files = dt.files;
|
|
106
|
+
if (dt.files.length)
|
|
107
|
+
inputRef.current?.closest(".k-dropzone")?.removeAttribute("data-invalid");
|
|
108
|
+
}
|
|
109
|
+
catch { /* مرورگر بدون DataTransfer */ }
|
|
110
|
+
}, [items]);
|
|
111
|
+
// نشانیهای پیشنمایش با بسته شدن کامپوننت آزاد میشوند
|
|
112
|
+
useEffect(() => () => itemsRef.current.forEach((it) => it.previewUrl && URL.revokeObjectURL(it.previewUrl)), []);
|
|
44
113
|
const add = (files) => {
|
|
45
|
-
const
|
|
46
|
-
|
|
114
|
+
const picked = Array.from(files).slice(0, multiple ? undefined : 1);
|
|
115
|
+
if (!resizeImages)
|
|
116
|
+
return accept_(picked.map((file) => ({ file, note: undefined })));
|
|
117
|
+
// کوچک کردن عکسها ناهمگام است؛ بدون resizeImages همه چیز همزمان میماند
|
|
118
|
+
Promise.all(picked.map(async (file) => {
|
|
119
|
+
const small = accepts(file, accept) ? await shrinkImage(file, resizeImages) : null;
|
|
120
|
+
return small ? { file: small.file, note: M.fileUpload.resized(N(small.width), N(small.height)) } : { file, note: undefined };
|
|
121
|
+
})).then(accept_);
|
|
122
|
+
};
|
|
123
|
+
const accept_ = (list) => {
|
|
124
|
+
const kept = multiple ? itemsRef.current.filter((x) => x.status !== "error").length : 0;
|
|
125
|
+
const added = list.map(({ file, note }, i) => {
|
|
47
126
|
const fid = `${Date.now()}-${i}-${file.name}`;
|
|
48
127
|
let err;
|
|
49
128
|
if (!accepts(file, accept))
|
|
50
129
|
err = wrongType;
|
|
51
130
|
else if (maxSize && file.size > maxSize)
|
|
52
131
|
err = tooBig(bytes(maxSize));
|
|
53
|
-
else if (maxFiles &&
|
|
132
|
+
else if (maxFiles && kept + i >= maxFiles)
|
|
54
133
|
err = tooMany(maxFiles);
|
|
55
|
-
|
|
134
|
+
const previewUrl = preview && !err && file.type.startsWith("image/") && typeof URL.createObjectURL === "function" ? URL.createObjectURL(file) : undefined;
|
|
135
|
+
return { id: fid, file, status: err ? "error" : uploader ? "uploading" : "done", progress: err ? 0 : uploader ? 0 : 100, error: err, previewUrl, note };
|
|
56
136
|
});
|
|
137
|
+
if (!multiple)
|
|
138
|
+
itemsRef.current.forEach((it) => it.previewUrl && URL.revokeObjectURL(it.previewUrl));
|
|
57
139
|
update((prev) => (multiple ? [...prev, ...added] : added));
|
|
58
140
|
setAnnounce(added.map((a) => `${a.file.name}: ${a.error ?? (uploader ? uploading : done)}`).join(M.common.list));
|
|
59
141
|
for (const it of added) {
|
|
@@ -84,12 +166,14 @@ export function FileUpload({ accept, multiple = true, maxSize, maxFiles, uploade
|
|
|
84
166
|
}, onDragLeave: (e) => {
|
|
85
167
|
if (!e.currentTarget.contains(e.relatedTarget))
|
|
86
168
|
setDragging(false);
|
|
87
|
-
}, onDrop: onDrop, children: [_jsx("span", { className: "k-dropzone__icon", "aria-hidden": "true", children: _jsx(UploadIcon, {}) }), _jsxs("p", { className: "k-dropzone__title", style: { margin: 0 }, children: [_jsx("button", { type: "button", className: "k-dropzone__browse", disabled: disabled, "aria-describedby": hint ? `${id}-hint` : undefined, onClick: () => inputRef.current?.click(), children: browse }), " ", _jsx("span", { children: drop })] }), hint && _jsx("p", { className: "k-dropzone__hint", id: `${id}-hint`, style: { margin: 0 }, children: hint }), _jsx("input", { ref: inputRef, type: "file", accept: accept, multiple: multiple,
|
|
169
|
+
}, onDrop: onDrop, children: [_jsx("span", { className: "k-dropzone__icon", "aria-hidden": "true", children: _jsx(UploadIcon, {}) }), _jsxs("p", { className: "k-dropzone__title", style: { margin: 0 }, children: [_jsx("button", { type: "button", className: "k-dropzone__browse", disabled: disabled, "aria-describedby": hint ? `${id}-hint` : undefined, onClick: () => inputRef.current?.click(), children: browse }), " ", _jsx("span", { children: drop })] }), hint && _jsx("p", { className: "k-dropzone__hint", id: `${id}-hint`, style: { margin: 0 }, children: hint }), _jsx("input", { ref: inputRef, type: "file", accept: accept, multiple: multiple, tabIndex: -1, "aria-hidden": "true", onChange: (e) => {
|
|
88
170
|
if (e.target.files?.length)
|
|
89
171
|
add(e.target.files);
|
|
90
172
|
e.target.value = "";
|
|
91
|
-
} })] }), items.length > 0 && (_jsx("ul", { className: "k-file-list", role: "list", children: items.map((it) => (_jsxs("li", { className: "k-file", "data-status": it.status, children: [_jsx("span", { className: "k-file__icon", "aria-hidden": "true", children: ext(it.file.name) }), _jsx("span", { className: "k-file__name", title: it.file.name, children: it.file.name }), _jsx("button", { type: "button", className: cx("k-btn k-btn--tertiary k-btn--icon k-btn--sm k-file__remove"), "aria-label": remove(it.file.name), onClick: () => {
|
|
173
|
+
} }), name && (_jsx("input", { ref: formRef, type: "file", className: "k-dropzone__form", name: name, multiple: multiple, required: required, tabIndex: -1, "aria-hidden": "true", onInvalid: () => inputRef.current?.closest(".k-dropzone")?.setAttribute("data-invalid", ""), onChange: (e) => e.preventDefault() }))] }), items.length > 0 && (_jsx("ul", { className: "k-file-list", role: "list", children: items.map((it) => (_jsxs("li", { className: "k-file", "data-status": it.status, children: [it.previewUrl ? (_jsx("img", { className: "k-file__thumb", src: it.previewUrl, alt: "" })) : (_jsx("span", { className: "k-file__icon", "aria-hidden": "true", children: ext(it.file.name) })), _jsx("span", { className: "k-file__name", title: it.file.name, children: it.file.name }), _jsx("button", { type: "button", className: cx("k-btn k-btn--tertiary k-btn--icon k-btn--sm k-file__remove"), "aria-label": remove(it.file.name), onClick: () => {
|
|
174
|
+
if (it.previewUrl)
|
|
175
|
+
URL.revokeObjectURL(it.previewUrl);
|
|
92
176
|
update((l) => l.filter((x) => x.id !== it.id));
|
|
93
177
|
setAnnounce(M.common.removed(it.file.name));
|
|
94
|
-
}, children: _jsx(TrashIcon, {}) }), _jsx("span", { className: "k-file__meta", children: it.error ?? `${bytes(it.file.size)}${it.status === "uploading" ? ` · ${M.common.percent(N(Math.round(it.progress)))}` : it.status === "done" ? ` · ${done}` : ""}` }), it.status === "uploading" && _jsx(Progress, { value: it.progress, size: "sm", "aria-label": `${uploading} ${it.file.name}` })] }, it.id))) })), _jsx("span", { className: "k-sr-only", "aria-live": "polite", children: announce })] }));
|
|
178
|
+
}, children: _jsx(TrashIcon, {}) }), _jsx("span", { className: "k-file__meta", children: it.error ?? `${bytes(it.file.size)}${it.status === "uploading" ? ` · ${M.common.percent(N(Math.round(it.progress)))}` : it.status === "done" ? ` · ${done}` : ""}${it.note ? ` · ${it.note}` : ""}` }), it.status === "uploading" && _jsx(Progress, { value: it.progress, size: "sm", "aria-label": `${uploading} ${it.file.name}` })] }, it.id))) })), _jsx("span", { className: "k-sr-only", "aria-live": "polite", children: announce })] }));
|
|
95
179
|
}
|
package/dist/Input.d.ts
CHANGED
|
@@ -41,6 +41,12 @@ export interface InputProps extends FieldOwnProps, Omit<InputHTMLAttributes<HTML
|
|
|
41
41
|
revealable?: boolean;
|
|
42
42
|
/** اسپینر در انتهای فیلد، مثلاً هنگام بررسی نام کاربری */
|
|
43
43
|
loading?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* «code» برای کد تخفیف، کلید لایسنس، شناسه و مقدارهای شبیه شبا: متن تایپشده تکفاصله، چپبهراست و جدا از متن فارسی
|
|
46
|
+
* (unicode-bidi: isolate) با فاصلهٔ حروف بیشتر، ولی متن placeholder با همان فونت فارسی میماند. غلطگیر و حروف بزرگ خودکار خاموشاند.
|
|
47
|
+
* @default "default"
|
|
48
|
+
*/
|
|
49
|
+
variant?: "default" | "code";
|
|
44
50
|
labels?: {
|
|
45
51
|
clear?: string;
|
|
46
52
|
show?: string;
|
package/dist/Input.js
CHANGED
|
@@ -23,7 +23,7 @@ function setNativeValue(el, value) {
|
|
|
23
23
|
function Counter({ id, length, max }) {
|
|
24
24
|
return (_jsx("span", { className: "k-counter", id: id, "data-over": max !== undefined && length > max ? "" : undefined, children: max !== undefined ? `${toFaDigits(length)} / ${toFaDigits(max)}` : toFaDigits(length) }));
|
|
25
25
|
}
|
|
26
|
-
export const Input = forwardRef(function Input({ label, hint, error, optional, labelEnd, showCount, size = "md", addonStart, addonEnd, clearable, onClear, revealable = true, loading, labels = {}, required, id, type = "text", className, fieldClassName, classNames = {}, "aria-describedby": ariaDescribedBy, onChange, value, defaultValue, maxLength, disabled, readOnly, ...rest }, ref) {
|
|
26
|
+
export const Input = forwardRef(function Input({ label, hint, error, optional, labelEnd, showCount, size = "md", addonStart, addonEnd, clearable, onClear, revealable = true, loading, variant = "default", labels = {}, required, id, type = "text", className, fieldClassName, classNames = {}, "aria-describedby": ariaDescribedBy, onChange, value, defaultValue, maxLength, disabled, readOnly, ...rest }, ref) {
|
|
27
27
|
const autoId = useId();
|
|
28
28
|
const inputId = id ?? autoId;
|
|
29
29
|
const inner = useRef(null);
|
|
@@ -40,7 +40,7 @@ export const Input = forwardRef(function Input({ label, hint, error, optional, l
|
|
|
40
40
|
inner.current?.focus();
|
|
41
41
|
onClear?.();
|
|
42
42
|
}, children: _jsx(X, {}) })), isPassword && revealable && (_jsx("button", { type: "button", className: "k-input__action", "aria-label": revealed ? hide : show, "aria-pressed": revealed, onClick: () => setRevealed((r) => !r), children: _jsx(Eye, { off: revealed }) })), addonEnd && _jsx("span", { className: cx("k-input__addon", classNames.addon), children: addonEnd })] }));
|
|
43
|
-
return (_jsx(Field, { id: inputId, label: label, hint: hint, error: error, required: required, optional: optional, labelEnd: labelEnd, hintEnd: showCount ? _jsx(Counter, { id: counterId, length: length, max: maxLength }) : undefined, className: fieldClassName, classNames: classNames, children: _jsxs("div", { className: cx("k-input", size !== "md" && `k-input--${size}`, className, classNames.box), "data-size": size, "data-state": disabled ? "disabled" : error ? "invalid" : undefined, children: [addonStart && _jsx("span", { className: cx("k-input__addon", classNames.addon), children: addonStart }), _jsx("input", { ref: mergeRefs(ref, inner), id: inputId, className: cx("k-input__control", classNames.control), type: isPassword && revealed ? "text" : type, required: required, value: value, defaultValue: defaultValue, maxLength: maxLength, disabled: disabled, readOnly: readOnly, "aria-invalid": error ? true : undefined, "aria-describedby": describedBy(inputId, hint, error, [ariaDescribedBy, showCount ? counterId : undefined].filter(Boolean).join(" ") || undefined), onChange: (e) => {
|
|
43
|
+
return (_jsx(Field, { id: inputId, label: label, hint: hint, error: error, required: required, optional: optional, labelEnd: labelEnd, hintEnd: showCount ? _jsx(Counter, { id: counterId, length: length, max: maxLength }) : undefined, className: fieldClassName, classNames: classNames, children: _jsxs("div", { className: cx("k-input", size !== "md" && `k-input--${size}`, variant === "code" && "k-input--code", className, classNames.box), "data-size": size, "data-state": disabled ? "disabled" : error ? "invalid" : undefined, children: [addonStart && _jsx("span", { className: cx("k-input__addon", classNames.addon), children: addonStart }), _jsx("input", { ref: mergeRefs(ref, inner), id: inputId, className: cx("k-input__control", classNames.control), type: isPassword && revealed ? "text" : type, required: required, value: value, defaultValue: defaultValue, maxLength: maxLength, disabled: disabled, readOnly: readOnly, "aria-invalid": error ? true : undefined, "aria-describedby": describedBy(inputId, hint, error, [ariaDescribedBy, showCount ? counterId : undefined].filter(Boolean).join(" ") || undefined), ...(variant === "code" ? { spellCheck: false, autoCapitalize: "off", autoCorrect: "off", autoComplete: "off" } : {}), onChange: (e) => {
|
|
44
44
|
trackLength(e);
|
|
45
45
|
onChange?.(e);
|
|
46
46
|
}, ...rest }), end] }) }));
|
package/dist/Radio.d.ts
CHANGED
|
@@ -22,5 +22,9 @@ export interface RadioProps extends Omit<InputHTMLAttributes<HTMLInputElement>,
|
|
|
22
22
|
value: string;
|
|
23
23
|
label: ReactNode;
|
|
24
24
|
description?: ReactNode;
|
|
25
|
+
/** نشان کنار عنوان، مثل «پیشنهادی»؛ متن ساده به نشان برند تبدیل میشود. بیشتر برای variant="cards" */
|
|
26
|
+
badge?: ReactNode;
|
|
27
|
+
/** قیمت یا اطلاعات کوتاه در انتهای کارت، مثل «۴۹۰ هزار تومان / سال». بیشتر برای variant="cards" */
|
|
28
|
+
meta?: ReactNode;
|
|
25
29
|
}
|
|
26
30
|
export declare const Radio: import("react").ForwardRefExoticComponent<RadioProps & import("react").RefAttributes<HTMLInputElement>>;
|
package/dist/Radio.js
CHANGED
|
@@ -22,7 +22,7 @@ export function RadioGroup({ label, name, value, defaultValue, onChange, variant
|
|
|
22
22
|
},
|
|
23
23
|
}, children: children }), error && _jsx("p", { className: "k-hint k-hint--error", id: `${id}-error`, children: error }), hint && _jsx("p", { className: "k-hint", id: `${id}-hint`, children: hint })] }));
|
|
24
24
|
}
|
|
25
|
-
export const Radio = forwardRef(function Radio({ value, label, description, id, className, disabled, name, ...rest }, ref) {
|
|
25
|
+
export const Radio = forwardRef(function Radio({ value, label, description, badge, meta, id, className, disabled, name, ...rest }, ref) {
|
|
26
26
|
const ctx = useContext(RadioCtx);
|
|
27
27
|
// بدون name مشترک، مرورگر رادیوها را یک گروه نمیداند و چند گزینه همزمان انتخاب میشود
|
|
28
28
|
if (!ctx && !name && isDev()) {
|
|
@@ -30,5 +30,5 @@ export const Radio = forwardRef(function Radio({ value, label, description, id,
|
|
|
30
30
|
}
|
|
31
31
|
const autoId = useId();
|
|
32
32
|
const inputId = id ?? autoId;
|
|
33
|
-
return (_jsxs("label", { className: cx("k-check", className), htmlFor: inputId, children: [_jsx("input", { ref: ref, type: "radio", className: "k-radio", id: inputId, name: ctx?.name ?? name, value: value, checked: ctx ? ctx.value === value : undefined, onChange: () => ctx?.setValue(value), disabled: disabled, "aria-describedby": description ? `${inputId}-desc` : undefined, ...rest }), _jsxs("span", { className: "k-check__text", children: [_jsx("span", { className: "k-check__label", children: label }), description && _jsx("span", { className: "k-check__description", id: `${inputId}-desc`, children: description })] })] }));
|
|
33
|
+
return (_jsxs("label", { className: cx("k-check", className), htmlFor: inputId, children: [_jsx("input", { ref: ref, type: "radio", className: "k-radio", id: inputId, name: ctx?.name ?? name, value: value, checked: ctx ? ctx.value === value : undefined, onChange: () => ctx?.setValue(value), disabled: disabled, "aria-describedby": description ? `${inputId}-desc` : undefined, ...rest }), _jsxs("span", { className: "k-check__text", children: [badge != null && badge !== false ? (_jsxs("span", { className: "k-check__head", children: [_jsx("span", { className: "k-check__label", children: label }), typeof badge === "string" ? _jsx("span", { className: "k-badge k-badge--brand k-badge--sm", children: badge }) : badge] })) : (_jsx("span", { className: "k-check__label", children: label })), description && _jsx("span", { className: "k-check__description", id: `${inputId}-desc`, children: description })] }), meta != null && meta !== false && _jsx("span", { className: "k-check__meta", children: meta })] }));
|
|
34
34
|
});
|
package/dist/Tooltip.d.ts
CHANGED
|
@@ -12,5 +12,7 @@ export interface TooltipProps {
|
|
|
12
12
|
/**
|
|
13
13
|
* راهنمای کوتاه طبق WCAG 1.4.13: با hover و فوکوس نمایش داده میشود، روی خودش هم hover پذیر است
|
|
14
14
|
* و با Esc بسته میشود. با aria-describedby به عنصر متصل است.
|
|
15
|
+
* در لایهٔ بالایی مرورگر نمایش داده میشود، پس overflow: hidden والدها (مثل کارت) آن را نمیبرد،
|
|
16
|
+
* و اگر سمت خواستهشده جا نداشته باشد به سمت مقابل میرود.
|
|
15
17
|
*/
|
|
16
18
|
export declare function Tooltip({ content, children, placement, delay }: TooltipProps): import("react").JSX.Element;
|
package/dist/Tooltip.js
CHANGED
|
@@ -2,14 +2,19 @@
|
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { cloneElement, isValidElement, useEffect, useId, useRef, useState } from "react";
|
|
4
4
|
import { cx } from "./cx.js";
|
|
5
|
+
import { placeTip, unplaceTip, useIsomorphicLayoutEffect } from "./utils.js";
|
|
5
6
|
/**
|
|
6
7
|
* راهنمای کوتاه طبق WCAG 1.4.13: با hover و فوکوس نمایش داده میشود، روی خودش هم hover پذیر است
|
|
7
8
|
* و با Esc بسته میشود. با aria-describedby به عنصر متصل است.
|
|
9
|
+
* در لایهٔ بالایی مرورگر نمایش داده میشود، پس overflow: hidden والدها (مثل کارت) آن را نمیبرد،
|
|
10
|
+
* و اگر سمت خواستهشده جا نداشته باشد به سمت مقابل میرود.
|
|
8
11
|
*/
|
|
9
12
|
export function Tooltip({ content, children, placement = "top", delay = 300 }) {
|
|
10
13
|
const id = useId();
|
|
11
14
|
const [open, setOpen] = useState(false);
|
|
12
15
|
const timer = useRef(undefined);
|
|
16
|
+
const anchor = useRef(null);
|
|
17
|
+
const tip = useRef(null);
|
|
13
18
|
const show = (wait) => {
|
|
14
19
|
clearTimeout(timer.current);
|
|
15
20
|
timer.current = setTimeout(() => setOpen(true), wait);
|
|
@@ -26,10 +31,26 @@ export function Tooltip({ content, children, placement = "top", delay = 300 }) {
|
|
|
26
31
|
return () => document.removeEventListener("keydown", onKey);
|
|
27
32
|
}, [open]);
|
|
28
33
|
useEffect(() => () => clearTimeout(timer.current), []);
|
|
34
|
+
useIsomorphicLayoutEffect(() => {
|
|
35
|
+
const a = anchor.current, t = tip.current;
|
|
36
|
+
if (!a || !t)
|
|
37
|
+
return;
|
|
38
|
+
if (!open)
|
|
39
|
+
return unplaceTip(t);
|
|
40
|
+
const place = () => placeTip(a, t, placement);
|
|
41
|
+
place();
|
|
42
|
+
addEventListener("scroll", place, { capture: true, passive: true });
|
|
43
|
+
addEventListener("resize", place);
|
|
44
|
+
return () => {
|
|
45
|
+
removeEventListener("scroll", place, { capture: true });
|
|
46
|
+
removeEventListener("resize", place);
|
|
47
|
+
unplaceTip(t);
|
|
48
|
+
};
|
|
49
|
+
}, [open, placement, content]);
|
|
29
50
|
const child = isValidElement(children)
|
|
30
51
|
? cloneElement(children, {
|
|
31
52
|
"aria-describedby": [children.props["aria-describedby"], id].filter(Boolean).join(" "),
|
|
32
53
|
})
|
|
33
54
|
: children;
|
|
34
|
-
return (_jsxs("span", { className: "k-tooltip-anchor", onMouseEnter: () => show(delay), onMouseLeave: hide, onFocus: () => show(0), onBlur: hide, children: [child, _jsx("span", { role: "tooltip", id: id, className: cx("k-tooltip", placement !== "top" && `k-tooltip--${placement}`), hidden: !open, children: content })] }));
|
|
55
|
+
return (_jsxs("span", { ref: anchor, className: "k-tooltip-anchor", onMouseEnter: () => show(delay), onMouseLeave: hide, onFocus: () => show(0), onBlur: hide, children: [child, _jsx("span", { ref: tip, ...{ popover: "manual" }, role: "tooltip", id: id, className: cx("k-tooltip", placement !== "top" && `k-tooltip--${placement}`), hidden: !open, children: content })] }));
|
|
35
56
|
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -10,3 +10,11 @@ export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
|
|
|
10
10
|
* deprecate("Button.kind", "prop kind منسوخ است؛ از variant استفاده کنید. در ۲.۰ حذف میشود.")
|
|
11
11
|
*/
|
|
12
12
|
export declare function deprecate(id: string, message: string): void;
|
|
13
|
+
export type TipPlacement = "top" | "bottom" | "start" | "end";
|
|
14
|
+
/**
|
|
15
|
+
* راهنما را در لایهٔ بالایی مرورگر (Popover API) و با position: fixed کنار عنصر میگذارد تا overflow: hidden
|
|
16
|
+
* هیچ والدی آن را نبرد. اگر سمت خواستهشده جا نداشته باشد، به سمت مقابل میرود و داخل صفحه میماند.
|
|
17
|
+
* در RTL «start» سمت راست است. مرورگر بدون Popover API همان position: fixed را میگیرد.
|
|
18
|
+
*/
|
|
19
|
+
export declare function placeTip(anchor: HTMLElement, tip: HTMLElement, placement?: TipPlacement): void;
|
|
20
|
+
export declare function unplaceTip(tip: HTMLElement): void;
|
package/dist/utils.js
CHANGED
|
@@ -27,3 +27,51 @@ export function deprecate(id, message) {
|
|
|
27
27
|
deprecations.add(id);
|
|
28
28
|
console.warn(`[kashi] منسوخ: ${message}`);
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* راهنما را در لایهٔ بالایی مرورگر (Popover API) و با position: fixed کنار عنصر میگذارد تا overflow: hidden
|
|
32
|
+
* هیچ والدی آن را نبرد. اگر سمت خواستهشده جا نداشته باشد، به سمت مقابل میرود و داخل صفحه میماند.
|
|
33
|
+
* در RTL «start» سمت راست است. مرورگر بدون Popover API همان position: fixed را میگیرد.
|
|
34
|
+
*/
|
|
35
|
+
export function placeTip(anchor, tip, placement = "top") {
|
|
36
|
+
const pop = tip;
|
|
37
|
+
tip.setAttribute("data-placed", "");
|
|
38
|
+
try {
|
|
39
|
+
if (pop.showPopover && !tip.matches(":popover-open"))
|
|
40
|
+
pop.showPopover();
|
|
41
|
+
}
|
|
42
|
+
catch { /* بدون پشتیبانی کامل Popover */ }
|
|
43
|
+
const a = anchor.getBoundingClientRect();
|
|
44
|
+
const t = tip.getBoundingClientRect();
|
|
45
|
+
const rtl = getComputedStyle(anchor).direction === "rtl";
|
|
46
|
+
const gap = 8, m = 8, W = document.documentElement.clientWidth || innerWidth, H = innerHeight;
|
|
47
|
+
let side = placement === "top" || placement === "bottom" ? placement : (placement === "start") !== rtl ? "left" : "right";
|
|
48
|
+
const room = { top: a.top - gap - m, bottom: H - a.bottom - gap - m, left: a.left - gap - m, right: W - a.right - gap - m };
|
|
49
|
+
const opposite = { top: "bottom", bottom: "top", left: "right", right: "left" };
|
|
50
|
+
const need = side === "top" || side === "bottom" ? t.height : t.width;
|
|
51
|
+
if (room[side] < need && room[opposite[side]] > room[side])
|
|
52
|
+
side = opposite[side];
|
|
53
|
+
let x, y;
|
|
54
|
+
if (side === "top" || side === "bottom") {
|
|
55
|
+
x = Math.min(Math.max(a.left + a.width / 2 - t.width / 2, m), W - t.width - m);
|
|
56
|
+
y = side === "top" ? a.top - gap - t.height : a.bottom + gap;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
y = Math.min(Math.max(a.top + a.height / 2 - t.height / 2, m), H - t.height - m);
|
|
60
|
+
x = side === "left" ? a.left - gap - t.width : a.right + gap;
|
|
61
|
+
}
|
|
62
|
+
tip.dataset.side = side;
|
|
63
|
+
const px = (v) => `${Math.round(v)}px`;
|
|
64
|
+
tip.style.setProperty("--_x", px(x));
|
|
65
|
+
tip.style.setProperty("--_y", px(y));
|
|
66
|
+
// پیکان رو به وسط عنصر، حتی وقتی راهنما برای ماندن در صفحه جابهجا شده است
|
|
67
|
+
tip.style.setProperty("--_ax", px(Math.min(Math.max(a.left + a.width / 2 - x, 10), t.width - 10)));
|
|
68
|
+
tip.style.setProperty("--_ay", px(Math.min(Math.max(a.top + a.height / 2 - y, 10), t.height - 10)));
|
|
69
|
+
}
|
|
70
|
+
export function unplaceTip(tip) {
|
|
71
|
+
const pop = tip;
|
|
72
|
+
try {
|
|
73
|
+
if (pop.hidePopover && tip.matches(":popover-open"))
|
|
74
|
+
pop.hidePopover();
|
|
75
|
+
}
|
|
76
|
+
catch { /* */ }
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kashi-ui/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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": "^1.
|
|
25
|
+
"@kashi-ui/css": "^1.6.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": "^1.
|
|
63
|
-
"@kashi-ui/persian": "^1.
|
|
62
|
+
"@kashi-ui/bank-logos": "^1.6.0",
|
|
63
|
+
"@kashi-ui/persian": "^1.6.0"
|
|
64
64
|
}
|
|
65
65
|
}
|