tekivex-ui 2.0.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.
Files changed (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/dist/index.cjs +70 -0
  4. package/dist/index.d.ts +89 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +11002 -0
  7. package/dist/src/a11y/index.d.ts +24 -0
  8. package/dist/src/a11y/index.d.ts.map +1 -0
  9. package/dist/src/components/TkxAccordion.d.ts +32 -0
  10. package/dist/src/components/TkxAccordion.d.ts.map +1 -0
  11. package/dist/src/components/TkxAlert.d.ts +14 -0
  12. package/dist/src/components/TkxAlert.d.ts.map +1 -0
  13. package/dist/src/components/TkxAvatar.d.ts +14 -0
  14. package/dist/src/components/TkxAvatar.d.ts.map +1 -0
  15. package/dist/src/components/TkxBadge.d.ts +12 -0
  16. package/dist/src/components/TkxBadge.d.ts.map +1 -0
  17. package/dist/src/components/TkxButton.d.ts +17 -0
  18. package/dist/src/components/TkxButton.d.ts.map +1 -0
  19. package/dist/src/components/TkxCard.d.ts +20 -0
  20. package/dist/src/components/TkxCard.d.ts.map +1 -0
  21. package/dist/src/components/TkxCarousel.d.ts +32 -0
  22. package/dist/src/components/TkxCarousel.d.ts.map +1 -0
  23. package/dist/src/components/TkxChat.d.ts +32 -0
  24. package/dist/src/components/TkxChat.d.ts.map +1 -0
  25. package/dist/src/components/TkxCheckbox.d.ts +13 -0
  26. package/dist/src/components/TkxCheckbox.d.ts.map +1 -0
  27. package/dist/src/components/TkxClock.d.ts +23 -0
  28. package/dist/src/components/TkxClock.d.ts.map +1 -0
  29. package/dist/src/components/TkxColorPicker.d.ts +18 -0
  30. package/dist/src/components/TkxColorPicker.d.ts.map +1 -0
  31. package/dist/src/components/TkxCommand.d.ts +34 -0
  32. package/dist/src/components/TkxCommand.d.ts.map +1 -0
  33. package/dist/src/components/TkxDatePicker.d.ts +47 -0
  34. package/dist/src/components/TkxDatePicker.d.ts.map +1 -0
  35. package/dist/src/components/TkxDivider.d.ts +8 -0
  36. package/dist/src/components/TkxDivider.d.ts.map +1 -0
  37. package/dist/src/components/TkxDrawer.d.ts +16 -0
  38. package/dist/src/components/TkxDrawer.d.ts.map +1 -0
  39. package/dist/src/components/TkxFileUpload.d.ts +16 -0
  40. package/dist/src/components/TkxFileUpload.d.ts.map +1 -0
  41. package/dist/src/components/TkxIcon.d.ts +16 -0
  42. package/dist/src/components/TkxIcon.d.ts.map +1 -0
  43. package/dist/src/components/TkxImage.d.ts +16 -0
  44. package/dist/src/components/TkxImage.d.ts.map +1 -0
  45. package/dist/src/components/TkxInput.d.ts +13 -0
  46. package/dist/src/components/TkxInput.d.ts.map +1 -0
  47. package/dist/src/components/TkxLogo.d.ts +21 -0
  48. package/dist/src/components/TkxLogo.d.ts.map +1 -0
  49. package/dist/src/components/TkxMenu.d.ts +64 -0
  50. package/dist/src/components/TkxMenu.d.ts.map +1 -0
  51. package/dist/src/components/TkxModal.d.ts +14 -0
  52. package/dist/src/components/TkxModal.d.ts.map +1 -0
  53. package/dist/src/components/TkxNumberInput.d.ts +32 -0
  54. package/dist/src/components/TkxNumberInput.d.ts.map +1 -0
  55. package/dist/src/components/TkxOTP.d.ts +24 -0
  56. package/dist/src/components/TkxOTP.d.ts.map +1 -0
  57. package/dist/src/components/TkxPagination.d.ts +19 -0
  58. package/dist/src/components/TkxPagination.d.ts.map +1 -0
  59. package/dist/src/components/TkxProgress.d.ts +13 -0
  60. package/dist/src/components/TkxProgress.d.ts.map +1 -0
  61. package/dist/src/components/TkxRadio.d.ts +22 -0
  62. package/dist/src/components/TkxRadio.d.ts.map +1 -0
  63. package/dist/src/components/TkxRating.d.ts +18 -0
  64. package/dist/src/components/TkxRating.d.ts.map +1 -0
  65. package/dist/src/components/TkxSelect.d.ts +37 -0
  66. package/dist/src/components/TkxSelect.d.ts.map +1 -0
  67. package/dist/src/components/TkxSkeleton.d.ts +12 -0
  68. package/dist/src/components/TkxSkeleton.d.ts.map +1 -0
  69. package/dist/src/components/TkxSlider.d.ts +33 -0
  70. package/dist/src/components/TkxSlider.d.ts.map +1 -0
  71. package/dist/src/components/TkxStepper.d.ts +31 -0
  72. package/dist/src/components/TkxStepper.d.ts.map +1 -0
  73. package/dist/src/components/TkxTable.d.ts +23 -0
  74. package/dist/src/components/TkxTable.d.ts.map +1 -0
  75. package/dist/src/components/TkxTabs.d.ts +32 -0
  76. package/dist/src/components/TkxTabs.d.ts.map +1 -0
  77. package/dist/src/components/TkxTag.d.ts +30 -0
  78. package/dist/src/components/TkxTag.d.ts.map +1 -0
  79. package/dist/src/components/TkxTimeline.d.ts +20 -0
  80. package/dist/src/components/TkxTimeline.d.ts.map +1 -0
  81. package/dist/src/components/TkxToast.d.ts +23 -0
  82. package/dist/src/components/TkxToast.d.ts.map +1 -0
  83. package/dist/src/components/TkxToggle.d.ts +11 -0
  84. package/dist/src/components/TkxToggle.d.ts.map +1 -0
  85. package/dist/src/components/TkxTooltip.d.ts +10 -0
  86. package/dist/src/components/TkxTooltip.d.ts.map +1 -0
  87. package/dist/src/components/TkxVideoPlayer.d.ts +43 -0
  88. package/dist/src/components/TkxVideoPlayer.d.ts.map +1 -0
  89. package/dist/src/components/index.d.ts +41 -0
  90. package/dist/src/components/index.d.ts.map +1 -0
  91. package/dist/src/engine/css.d.ts +24 -0
  92. package/dist/src/engine/css.d.ts.map +1 -0
  93. package/dist/src/engine/index.d.ts +10 -0
  94. package/dist/src/engine/index.d.ts.map +1 -0
  95. package/dist/src/engine/quantum.d.ts +47 -0
  96. package/dist/src/engine/quantum.d.ts.map +1 -0
  97. package/dist/src/engine/security.d.ts +54 -0
  98. package/dist/src/engine/security.d.ts.map +1 -0
  99. package/dist/src/engine/tkx.d.ts +29 -0
  100. package/dist/src/engine/tkx.d.ts.map +1 -0
  101. package/dist/src/engine/wcag.d.ts +34 -0
  102. package/dist/src/engine/wcag.d.ts.map +1 -0
  103. package/dist/src/hooks/index.d.ts +8 -0
  104. package/dist/src/hooks/index.d.ts.map +1 -0
  105. package/dist/src/themes/index.d.ts +26 -0
  106. package/dist/src/themes/index.d.ts.map +1 -0
  107. package/dist/style.css +1 -0
  108. package/dist/tests/TkxButton.test.d.ts +2 -0
  109. package/dist/tests/TkxButton.test.d.ts.map +1 -0
  110. package/dist/tests/a11y/components.test.d.ts +2 -0
  111. package/dist/tests/a11y/components.test.d.ts.map +1 -0
  112. package/dist/tests/quantum.test.d.ts +2 -0
  113. package/dist/tests/quantum.test.d.ts.map +1 -0
  114. package/dist/tests/security.test.d.ts +2 -0
  115. package/dist/tests/security.test.d.ts.map +1 -0
  116. package/dist/tests/setup.d.ts +2 -0
  117. package/dist/tests/setup.d.ts.map +1 -0
  118. package/dist/tests/tkx.test.d.ts +2 -0
  119. package/dist/tests/tkx.test.d.ts.map +1 -0
  120. package/dist/tests/wcag.test.d.ts +2 -0
  121. package/dist/tests/wcag.test.d.ts.map +1 -0
  122. package/package.json +78 -0
  123. package/src/a11y/index.tsx +87 -0
  124. package/src/components/TkxAccordion.tsx +588 -0
  125. package/src/components/TkxAlert.tsx +91 -0
  126. package/src/components/TkxAvatar.tsx +101 -0
  127. package/src/components/TkxBadge.tsx +73 -0
  128. package/src/components/TkxButton.tsx +153 -0
  129. package/src/components/TkxCard.tsx +92 -0
  130. package/src/components/TkxCarousel.tsx +552 -0
  131. package/src/components/TkxChat.tsx +354 -0
  132. package/src/components/TkxCheckbox.tsx +218 -0
  133. package/src/components/TkxClock.tsx +601 -0
  134. package/src/components/TkxColorPicker.tsx +633 -0
  135. package/src/components/TkxCommand.tsx +644 -0
  136. package/src/components/TkxDatePicker.tsx +1567 -0
  137. package/src/components/TkxDivider.tsx +49 -0
  138. package/src/components/TkxDrawer.tsx +234 -0
  139. package/src/components/TkxFileUpload.tsx +371 -0
  140. package/src/components/TkxIcon.tsx +683 -0
  141. package/src/components/TkxImage.tsx +277 -0
  142. package/src/components/TkxInput.tsx +101 -0
  143. package/src/components/TkxLogo.tsx +297 -0
  144. package/src/components/TkxMenu.tsx +1042 -0
  145. package/src/components/TkxModal.tsx +119 -0
  146. package/src/components/TkxNumberInput.tsx +408 -0
  147. package/src/components/TkxOTP.tsx +295 -0
  148. package/src/components/TkxPagination.tsx +388 -0
  149. package/src/components/TkxProgress.tsx +114 -0
  150. package/src/components/TkxRadio.tsx +284 -0
  151. package/src/components/TkxRating.tsx +286 -0
  152. package/src/components/TkxSelect.tsx +923 -0
  153. package/src/components/TkxSkeleton.tsx +105 -0
  154. package/src/components/TkxSlider.tsx +492 -0
  155. package/src/components/TkxStepper.tsx +485 -0
  156. package/src/components/TkxTable.tsx +148 -0
  157. package/src/components/TkxTabs.tsx +124 -0
  158. package/src/components/TkxTag.tsx +319 -0
  159. package/src/components/TkxTimeline.tsx +303 -0
  160. package/src/components/TkxToast.tsx +284 -0
  161. package/src/components/TkxToggle.tsx +93 -0
  162. package/src/components/TkxTooltip.tsx +69 -0
  163. package/src/components/TkxVideoPlayer.tsx +1013 -0
  164. package/src/components/index.ts +40 -0
  165. package/src/engine/css.ts +126 -0
  166. package/src/engine/index.ts +10 -0
  167. package/src/engine/quantum.ts +207 -0
  168. package/src/engine/security.ts +217 -0
  169. package/src/engine/tkx.ts +874 -0
  170. package/src/engine/wcag.ts +275 -0
  171. package/src/hooks/index.ts +120 -0
  172. package/src/styles/global.css +167 -0
  173. package/src/themes/index.ts +111 -0
@@ -0,0 +1,295 @@
1
+ import {
2
+ useState,
3
+ useRef,
4
+ useEffect,
5
+ useCallback,
6
+ useId,
7
+ type CSSProperties,
8
+ type ReactNode,
9
+ type ClipboardEvent,
10
+ type KeyboardEvent,
11
+ } from 'react';
12
+ import { useTheme } from '../themes';
13
+ import { tkx, cx } from '../engine/tkx';
14
+ import { sanitizeString } from '../engine/security';
15
+
16
+ // ── Types ─────────────────────────────────────────────────────────────────────
17
+
18
+ export interface TkxOTPProps {
19
+ length?: number;
20
+ value?: string;
21
+ onChange?: (value: string) => void;
22
+ onComplete?: (value: string) => void;
23
+ type?: 'number' | 'alphanumeric' | 'alpha';
24
+ mask?: boolean;
25
+ autoFocus?: boolean;
26
+ isDisabled?: boolean;
27
+ isInvalid?: boolean;
28
+ errorMessage?: string;
29
+ hint?: string;
30
+ size?: 'sm' | 'md' | 'lg';
31
+ separator?: ReactNode;
32
+ separatorPosition?: number;
33
+ className?: string;
34
+ style?: CSSProperties;
35
+ }
36
+
37
+ // ── Constants ─────────────────────────────────────────────────────────────────
38
+
39
+ const SIZE_MAP = {
40
+ sm: { box: '36px', fontSize: '1rem', gap: '6px' },
41
+ md: { box: '44px', fontSize: '1.25rem', gap: '8px' },
42
+ lg: { box: '52px', fontSize: '1.5rem', gap: '10px' },
43
+ };
44
+
45
+ const TYPE_PATTERN: Record<NonNullable<TkxOTPProps['type']>, RegExp> = {
46
+ number: /^\d$/,
47
+ alphanumeric: /^[a-zA-Z0-9]$/,
48
+ alpha: /^[a-zA-Z]$/,
49
+ };
50
+
51
+ // ── Helpers ───────────────────────────────────────────────────────────────────
52
+
53
+ function filterByType(char: string, type: NonNullable<TkxOTPProps['type']>): string {
54
+ return TYPE_PATTERN[type].test(char) ? char.toUpperCase() : '';
55
+ }
56
+
57
+ // ── Component ─────────────────────────────────────────────────────────────────
58
+
59
+ export function TkxOTP({
60
+ length = 6,
61
+ value,
62
+ onChange,
63
+ onComplete,
64
+ type = 'number',
65
+ mask = false,
66
+ autoFocus = false,
67
+ isDisabled = false,
68
+ isInvalid = false,
69
+ errorMessage,
70
+ hint,
71
+ size = 'md',
72
+ separator,
73
+ separatorPosition,
74
+ className,
75
+ style,
76
+ }: TkxOTPProps) {
77
+ const theme = useTheme();
78
+ const groupId = useId();
79
+ const hintId = `${groupId}-hint`;
80
+ const errorId = `${groupId}-error`;
81
+
82
+ const isControlled = value !== undefined;
83
+
84
+ const toArr = (v: string): string[] => {
85
+ const arr = v.split('').slice(0, length);
86
+ while (arr.length < length) arr.push('');
87
+ return arr;
88
+ };
89
+
90
+ const [internalDigits, setInternalDigits] = useState<string[]>(() =>
91
+ toArr(isControlled ? value! : ''),
92
+ );
93
+
94
+ const digits = isControlled ? toArr(value!) : internalDigits;
95
+
96
+ const inputRefs = useRef<(HTMLInputElement | null)[]>([]);
97
+
98
+ const sz = SIZE_MAP[size];
99
+ const hasError = isInvalid || !!errorMessage;
100
+ const isComplete = digits.every((d) => d !== '');
101
+
102
+ const safeHint = hint ? sanitizeString(hint) : undefined;
103
+ const safeError = errorMessage ? sanitizeString(errorMessage) : undefined;
104
+
105
+ // Compute success/error state colours
106
+ const boxBorder = (idx: number): string => {
107
+ if (hasError) return theme.danger;
108
+ if (isComplete) return theme.success;
109
+ return theme.border;
110
+ };
111
+
112
+ // Auto focus first input
113
+ useEffect(() => {
114
+ if (autoFocus) {
115
+ inputRefs.current[0]?.focus();
116
+ }
117
+ // eslint-disable-next-line react-hooks/exhaustive-deps
118
+ }, []);
119
+
120
+ const setDigits = useCallback((next: string[]) => {
121
+ if (!isControlled) setInternalDigits(next);
122
+ const joined = next.join('');
123
+ onChange?.(joined);
124
+ if (next.every((d) => d !== '')) {
125
+ onComplete?.(joined);
126
+ }
127
+ }, [isControlled, onChange, onComplete]);
128
+
129
+ const focusAt = (idx: number) => {
130
+ const clamped = Math.max(0, Math.min(length - 1, idx));
131
+ inputRefs.current[clamped]?.focus();
132
+ };
133
+
134
+ const handleKeyDown = (idx: number) => (e: KeyboardEvent<HTMLInputElement>) => {
135
+ if (isDisabled) return;
136
+
137
+ if (e.key === 'Backspace') {
138
+ e.preventDefault();
139
+ if (digits[idx] !== '') {
140
+ const next = [...digits];
141
+ next[idx] = '';
142
+ setDigits(next);
143
+ } else if (idx > 0) {
144
+ const next = [...digits];
145
+ next[idx - 1] = '';
146
+ setDigits(next);
147
+ focusAt(idx - 1);
148
+ }
149
+ return;
150
+ }
151
+
152
+ if (e.key === 'Delete') {
153
+ e.preventDefault();
154
+ const next = [...digits];
155
+ next[idx] = '';
156
+ setDigits(next);
157
+ return;
158
+ }
159
+
160
+ if (e.key === 'ArrowLeft') { e.preventDefault(); focusAt(idx - 1); return; }
161
+ if (e.key === 'ArrowRight') { e.preventDefault(); focusAt(idx + 1); return; }
162
+ if (e.key === 'Home') { e.preventDefault(); focusAt(0); return; }
163
+ if (e.key === 'End') { e.preventDefault(); focusAt(length - 1); return; }
164
+ };
165
+
166
+ const handleInput = (idx: number) => (e: React.ChangeEvent<HTMLInputElement>) => {
167
+ if (isDisabled) return;
168
+ // The value might be empty (deletion handled by keydown) or contain a char
169
+ const raw = e.target.value;
170
+ // Take only the last character typed (handles replacement)
171
+ const char = raw.slice(-1);
172
+ if (!char) return;
173
+
174
+ const filtered = filterByType(char, type);
175
+ if (!filtered) return;
176
+
177
+ const next = [...digits];
178
+ next[idx] = filtered;
179
+ setDigits(next);
180
+
181
+ // Advance focus
182
+ if (idx < length - 1) focusAt(idx + 1);
183
+ };
184
+
185
+ const handlePaste = (idx: number) => (e: ClipboardEvent<HTMLInputElement>) => {
186
+ e.preventDefault();
187
+ if (isDisabled) return;
188
+ const pasted = e.clipboardData.getData('text');
189
+ const chars = pasted.split('').map((c) => filterByType(c, type)).filter(Boolean);
190
+ if (!chars.length) return;
191
+
192
+ const next = [...digits];
193
+ let cursor = idx;
194
+ for (const ch of chars) {
195
+ if (cursor >= length) break;
196
+ next[cursor] = ch;
197
+ cursor++;
198
+ }
199
+ setDigits(next);
200
+ focusAt(Math.min(cursor, length - 1));
201
+ };
202
+
203
+ const describedBy = [safeHint && hintId, hasError && errorId].filter(Boolean).join(' ') || undefined;
204
+
205
+ return (
206
+ <div className={cx(tkx('flex flex-col gap-2 w-fit'), className)} style={style}>
207
+ <div
208
+ role="group"
209
+ aria-label="One-time password"
210
+ aria-describedby={describedBy}
211
+ className={tkx('flex items-center')}
212
+ style={{ gap: sz.gap }}
213
+ >
214
+ {Array.from({ length }, (_, idx) => {
215
+ const borderCol = boxBorder(idx);
216
+ const isFocusedEl = typeof document !== 'undefined' && document.activeElement === inputRefs.current[idx];
217
+
218
+ return (
219
+ <div key={idx} className={tkx('flex items-center')} style={{ gap: sz.gap }}>
220
+ {/* Separator before this index (when separatorPosition === idx) */}
221
+ {separator && separatorPosition === idx && idx !== 0 && (
222
+ <span
223
+ aria-hidden="true"
224
+ style={{ color: theme.textMuted, userSelect: 'none', flexShrink: 0 }}
225
+ >
226
+ {separator}
227
+ </span>
228
+ )}
229
+
230
+ <input
231
+ ref={(el) => { inputRefs.current[idx] = el; }}
232
+ type={mask ? 'password' : 'text'}
233
+ inputMode={type === 'number' ? 'numeric' : 'text'}
234
+ maxLength={1}
235
+ value={digits[idx]}
236
+ disabled={isDisabled}
237
+ aria-label={`Digit ${idx + 1} of ${length}`}
238
+ autoComplete="one-time-code"
239
+ spellCheck={false}
240
+ onChange={handleInput(idx)}
241
+ onKeyDown={handleKeyDown(idx)}
242
+ onPaste={handlePaste(idx)}
243
+ onFocus={(e) => e.target.select()}
244
+ style={{
245
+ width: sz.box,
246
+ height: sz.box,
247
+ textAlign: 'center',
248
+ fontSize: sz.fontSize,
249
+ fontFamily: 'monospace',
250
+ fontWeight: 600,
251
+ border: `1.5px solid ${borderCol}`,
252
+ borderRadius: '8px',
253
+ backgroundColor: isDisabled ? theme.surfaceAlt : theme.surface,
254
+ color: theme.text,
255
+ outline: 'none',
256
+ boxShadow: 'none',
257
+ cursor: isDisabled ? 'not-allowed' : 'text',
258
+ transition: 'border-color 150ms ease, box-shadow 150ms ease',
259
+ opacity: isDisabled ? 0.6 : 1,
260
+ // Focus ring applied via style since we can't rely on :focus in inline styles
261
+ }}
262
+ onFocusCapture={(e) => {
263
+ if (!isDisabled) {
264
+ (e.currentTarget as HTMLInputElement).style.borderColor = hasError ? theme.danger : theme.primary;
265
+ (e.currentTarget as HTMLInputElement).style.boxShadow = `0 0 0 3px ${hasError ? theme.danger : theme.primary}33`;
266
+ }
267
+ }}
268
+ onBlurCapture={(e) => {
269
+ (e.currentTarget as HTMLInputElement).style.borderColor = boxBorder(idx);
270
+ (e.currentTarget as HTMLInputElement).style.boxShadow = 'none';
271
+ }}
272
+ />
273
+ </div>
274
+ );
275
+ })}
276
+ </div>
277
+
278
+ {safeHint && !safeError && (
279
+ <span id={hintId} className={tkx('text-xs')} style={{ color: theme.textMuted }}>
280
+ {safeHint}
281
+ </span>
282
+ )}
283
+ {safeError && (
284
+ <span id={errorId} role="alert" className={tkx('text-xs flex items-center gap-1')} style={{ color: theme.danger }}>
285
+ <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
286
+ <path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
287
+ </svg>
288
+ {safeError}
289
+ </span>
290
+ )}
291
+ </div>
292
+ );
293
+ }
294
+
295
+ TkxOTP.displayName = 'TkxOTP';
@@ -0,0 +1,388 @@
1
+ import { useState, useId } from 'react';
2
+ import { useTheme } from '../themes';
3
+ import { tkx } from '../engine/tkx';
4
+ import { sanitizeString } from '../engine/security';
5
+
6
+ export interface TkxPaginationProps {
7
+ total: number;
8
+ pageSize?: number;
9
+ page?: number;
10
+ defaultPage?: number;
11
+ onChange?: (page: number) => void;
12
+ siblingCount?: number;
13
+ showEdges?: boolean;
14
+ showPageSize?: boolean;
15
+ onPageSizeChange?: (size: number) => void;
16
+ pageSizeOptions?: number[];
17
+ size?: 'sm' | 'md' | 'lg';
18
+ variant?: 'default' | 'outline' | 'ghost';
19
+ }
20
+
21
+ const SIZE_MAP = {
22
+ sm: { height: 28, minWidth: 28, fontSize: '0.75rem', px: 8 },
23
+ md: { height: 36, minWidth: 36, fontSize: '0.875rem', px: 10 },
24
+ lg: { height: 44, minWidth: 44, fontSize: '1rem', px: 12 },
25
+ };
26
+
27
+ const ELLIPSIS = '…';
28
+
29
+ function buildPageRange(
30
+ currentPage: number,
31
+ totalPages: number,
32
+ siblingCount: number,
33
+ showEdges: boolean,
34
+ ): (number | typeof ELLIPSIS)[] {
35
+ if (totalPages <= 1) return [1];
36
+
37
+ const range = (lo: number, hi: number): number[] =>
38
+ Array.from({ length: hi - lo + 1 }, (_, i) => lo + i);
39
+
40
+ const siblingStart = Math.max(currentPage - siblingCount, 1);
41
+ const siblingEnd = Math.min(currentPage + siblingCount, totalPages);
42
+
43
+ const showLeftEllipsis = siblingStart > 2;
44
+ const showRightEllipsis = siblingEnd < totalPages - 1;
45
+
46
+ if (!showEdges) {
47
+ const pages: (number | typeof ELLIPSIS)[] = [];
48
+ if (showLeftEllipsis) pages.push(ELLIPSIS);
49
+ pages.push(...range(siblingStart, siblingEnd));
50
+ if (showRightEllipsis) pages.push(ELLIPSIS);
51
+ return pages;
52
+ }
53
+
54
+ const pages: (number | typeof ELLIPSIS)[] = [1];
55
+
56
+ if (showLeftEllipsis) {
57
+ pages.push(ELLIPSIS);
58
+ } else {
59
+ for (let i = 2; i < siblingStart; i++) pages.push(i);
60
+ }
61
+
62
+ pages.push(...range(siblingStart, siblingEnd));
63
+
64
+ if (showRightEllipsis) {
65
+ pages.push(ELLIPSIS);
66
+ } else {
67
+ for (let i = siblingEnd + 1; i < totalPages; i++) pages.push(i);
68
+ }
69
+
70
+ if (totalPages > 1) pages.push(totalPages);
71
+
72
+ // deduplicate preserving order
73
+ const seen = new Set<number | typeof ELLIPSIS>();
74
+ const result: (number | typeof ELLIPSIS)[] = [];
75
+ for (const p of pages) {
76
+ const key = p === ELLIPSIS ? `${ELLIPSIS}${result.length}` : p;
77
+ if (!seen.has(p === ELLIPSIS ? (key as unknown as typeof ELLIPSIS) : p)) {
78
+ seen.add(p === ELLIPSIS ? (key as unknown as typeof ELLIPSIS) : p);
79
+ result.push(p);
80
+ }
81
+ }
82
+ return result;
83
+ }
84
+
85
+ function buildPages(
86
+ currentPage: number,
87
+ totalPages: number,
88
+ siblingCount: number,
89
+ showEdges: boolean,
90
+ ): (number | string)[] {
91
+ if (totalPages <= 1) return [1];
92
+
93
+ const range = (lo: number, hi: number): number[] =>
94
+ Array.from({ length: Math.max(0, hi - lo + 1) }, (_, i) => lo + i);
95
+
96
+ const siblingStart = Math.max(currentPage - siblingCount, 1);
97
+ const siblingEnd = Math.min(currentPage + siblingCount, totalPages);
98
+
99
+ if (!showEdges) {
100
+ const pages: (number | string)[] = [];
101
+ if (siblingStart > 1) pages.push('...');
102
+ pages.push(...range(siblingStart, siblingEnd));
103
+ if (siblingEnd < totalPages) pages.push('...');
104
+ return pages;
105
+ }
106
+
107
+ const leftBoundary = 1;
108
+ const rightBoundary = totalPages;
109
+
110
+ const showLeftEllipsis = siblingStart > leftBoundary + 2;
111
+ const showRightEllipsis = siblingEnd < rightBoundary - 2;
112
+
113
+ const result: (number | string)[] = [];
114
+ result.push(1);
115
+
116
+ if (showLeftEllipsis) {
117
+ result.push('left-ellipsis');
118
+ } else {
119
+ result.push(...range(2, siblingStart - 1));
120
+ }
121
+
122
+ result.push(...range(Math.max(siblingStart, 2), Math.min(siblingEnd, totalPages - 1)));
123
+
124
+ if (showRightEllipsis) {
125
+ result.push('right-ellipsis');
126
+ } else {
127
+ result.push(...range(siblingEnd + 1, totalPages - 1));
128
+ }
129
+
130
+ if (totalPages > 1) result.push(totalPages);
131
+
132
+ // deduplicate numbers
133
+ const seen = new Set<number>();
134
+ const deduped: (number | string)[] = [];
135
+ for (const p of result) {
136
+ if (typeof p === 'number') {
137
+ if (!seen.has(p)) { seen.add(p); deduped.push(p); }
138
+ } else {
139
+ deduped.push(p);
140
+ }
141
+ }
142
+ return deduped;
143
+ void buildPageRange;
144
+ }
145
+
146
+ export function TkxPagination({
147
+ total,
148
+ pageSize: pageSizeProp = 10,
149
+ page,
150
+ defaultPage = 1,
151
+ onChange,
152
+ siblingCount = 1,
153
+ showEdges = true,
154
+ showPageSize = false,
155
+ onPageSizeChange,
156
+ pageSizeOptions = [10, 25, 50, 100],
157
+ size = 'md',
158
+ variant = 'default',
159
+ }: TkxPaginationProps) {
160
+ const theme = useTheme();
161
+ const selectId = useId();
162
+
163
+ const isControlled = page !== undefined;
164
+ const [internalPage, setInternalPage] = useState(defaultPage);
165
+ const currentPage = isControlled ? page! : internalPage;
166
+
167
+ const [internalPageSize, setInternalPageSize] = useState(pageSizeProp);
168
+ const pageSize = pageSizeProp;
169
+
170
+ const totalPages = Math.max(1, Math.ceil(total / pageSize));
171
+
172
+ const goTo = (p: number) => {
173
+ const next = Math.min(Math.max(p, 1), totalPages);
174
+ if (next === currentPage) return;
175
+ if (!isControlled) setInternalPage(next);
176
+ onChange?.(next);
177
+ };
178
+
179
+ const handlePageSizeChange = (newSize: number) => {
180
+ setInternalPageSize(newSize);
181
+ onPageSizeChange?.(newSize);
182
+ goTo(1);
183
+ };
184
+
185
+ const pages = buildPages(currentPage, totalPages, siblingCount, showEdges);
186
+ const s = SIZE_MAP[size];
187
+
188
+ const itemStart = Math.min((currentPage - 1) * pageSize + 1, total);
189
+ const itemEnd = Math.min(currentPage * pageSize, total);
190
+
191
+ const getButtonStyle = (isActive: boolean, isDisabled: boolean) => {
192
+ const base: React.CSSProperties = {
193
+ height: s.height,
194
+ minWidth: s.minWidth,
195
+ paddingLeft: s.px,
196
+ paddingRight: s.px,
197
+ fontSize: s.fontSize,
198
+ borderRadius: '8px',
199
+ display: 'inline-flex',
200
+ alignItems: 'center',
201
+ justifyContent: 'center',
202
+ cursor: isDisabled ? 'not-allowed' : 'pointer',
203
+ border: 'none',
204
+ outline: 'none',
205
+ fontFamily: 'inherit',
206
+ transition: 'background 120ms ease, color 120ms ease',
207
+ opacity: isDisabled ? 0.4 : 1,
208
+ };
209
+
210
+ if (isActive) {
211
+ return {
212
+ ...base,
213
+ backgroundColor: theme.primary,
214
+ color: theme.bg,
215
+ fontWeight: 600,
216
+ boxShadow: `0 0 0 2px ${theme.primary}44`,
217
+ };
218
+ }
219
+
220
+ if (variant === 'default') {
221
+ return {
222
+ ...base,
223
+ backgroundColor: theme.surface,
224
+ color: theme.text,
225
+ border: `1px solid ${theme.border}`,
226
+ };
227
+ }
228
+ if (variant === 'outline') {
229
+ return {
230
+ ...base,
231
+ backgroundColor: 'transparent',
232
+ color: theme.text,
233
+ border: `1px solid ${theme.border}`,
234
+ };
235
+ }
236
+ // ghost
237
+ return {
238
+ ...base,
239
+ backgroundColor: 'transparent',
240
+ color: theme.text,
241
+ };
242
+ };
243
+
244
+ const NavIcon = ({ d }: { d: string }) => (
245
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" aria-hidden="true">
246
+ <polyline points={d} />
247
+ </svg>
248
+ );
249
+
250
+ return (
251
+ <nav aria-label="Pagination" className={tkx('flex flex-col gap-2')}>
252
+ {/* Showing X-Y of Z */}
253
+ {total > 0 && (
254
+ <p className={tkx('text-sm')} style={{ color: theme.textMuted, fontSize: s.fontSize }}>
255
+ {sanitizeString(`Showing ${itemStart}–${itemEnd} of ${total} items`)}
256
+ </p>
257
+ )}
258
+
259
+ <div className={tkx('flex items-center gap-1 flex-wrap')}>
260
+ {/* First */}
261
+ {showEdges && (
262
+ <button
263
+ type="button"
264
+ aria-label="First page"
265
+ disabled={currentPage === 1}
266
+ onClick={() => goTo(1)}
267
+ style={getButtonStyle(false, currentPage === 1)}
268
+ >
269
+ <NavIcon d="15 18 9 12 15 6" />
270
+ <NavIcon d="20 18 14 12 20 6" />
271
+ </button>
272
+ )}
273
+
274
+ {/* Prev */}
275
+ <button
276
+ type="button"
277
+ aria-label="Previous page"
278
+ disabled={currentPage === 1}
279
+ onClick={() => goTo(currentPage - 1)}
280
+ style={getButtonStyle(false, currentPage === 1)}
281
+ >
282
+ <NavIcon d="15 18 9 12 15 6" />
283
+ </button>
284
+
285
+ {/* Page numbers */}
286
+ {pages.map((p, idx) => {
287
+ const isEllipsis = typeof p === 'string';
288
+ if (isEllipsis) {
289
+ return (
290
+ <span
291
+ key={`${p}-${idx}`}
292
+ aria-hidden="true"
293
+ style={{
294
+ display: 'inline-flex',
295
+ alignItems: 'center',
296
+ justifyContent: 'center',
297
+ minWidth: s.minWidth,
298
+ height: s.height,
299
+ fontSize: s.fontSize,
300
+ color: theme.textMuted,
301
+ userSelect: 'none',
302
+ }}
303
+ >
304
+
305
+ </span>
306
+ );
307
+ }
308
+ const pageNum = p as number;
309
+ const isActive = pageNum === currentPage;
310
+ return (
311
+ <button
312
+ key={pageNum}
313
+ type="button"
314
+ aria-label={`Page ${pageNum}`}
315
+ aria-current={isActive ? 'page' : undefined}
316
+ onClick={() => goTo(pageNum)}
317
+ style={getButtonStyle(isActive, false)}
318
+ >
319
+ {pageNum}
320
+ </button>
321
+ );
322
+ })}
323
+
324
+ {/* Next */}
325
+ <button
326
+ type="button"
327
+ aria-label="Next page"
328
+ disabled={currentPage === totalPages}
329
+ onClick={() => goTo(currentPage + 1)}
330
+ style={getButtonStyle(false, currentPage === totalPages)}
331
+ >
332
+ <NavIcon d="9 18 15 12 9 6" />
333
+ </button>
334
+
335
+ {/* Last */}
336
+ {showEdges && (
337
+ <button
338
+ type="button"
339
+ aria-label="Last page"
340
+ disabled={currentPage === totalPages}
341
+ onClick={() => goTo(totalPages)}
342
+ style={getButtonStyle(false, currentPage === totalPages)}
343
+ >
344
+ <NavIcon d="4 18 10 12 4 6" />
345
+ <NavIcon d="14 18 20 12 14 6" />
346
+ </button>
347
+ )}
348
+
349
+ {/* Page size selector */}
350
+ {showPageSize && (
351
+ <div className={tkx('flex items-center gap-2 ml-2')}>
352
+ <label
353
+ htmlFor={selectId}
354
+ className={tkx('text-sm')}
355
+ style={{ color: theme.textMuted, fontSize: s.fontSize, whiteSpace: 'nowrap' }}
356
+ >
357
+ Rows per page
358
+ </label>
359
+ <select
360
+ id={selectId}
361
+ value={internalPageSize}
362
+ onChange={e => handlePageSizeChange(Number(e.target.value))}
363
+ style={{
364
+ height: s.height,
365
+ paddingLeft: s.px,
366
+ paddingRight: s.px,
367
+ fontSize: s.fontSize,
368
+ borderRadius: '8px',
369
+ border: `1px solid ${theme.border}`,
370
+ backgroundColor: theme.surface,
371
+ color: theme.text,
372
+ cursor: 'pointer',
373
+ outline: 'none',
374
+ fontFamily: 'inherit',
375
+ }}
376
+ >
377
+ {pageSizeOptions.map(opt => (
378
+ <option key={opt} value={opt}>{opt}</option>
379
+ ))}
380
+ </select>
381
+ </div>
382
+ )}
383
+ </div>
384
+ </nav>
385
+ );
386
+ }
387
+
388
+ TkxPagination.displayName = 'TkxPagination';