@codefast/ui 0.3.16-canary.2 → 0.3.16-canary.3

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 (223) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +1 -1
  3. package/dist/components/accordion.mjs +2 -2
  4. package/dist/components/alert-dialog.d.mts +1 -1
  5. package/dist/components/alert-dialog.mjs +4 -4
  6. package/dist/components/alert.d.mts +3 -13
  7. package/dist/components/alert.mjs +3 -23
  8. package/dist/components/badge.d.mts +3 -15
  9. package/dist/components/badge.mjs +2 -44
  10. package/dist/components/breadcrumb.mjs +1 -1
  11. package/dist/components/button-group.d.mts +3 -13
  12. package/dist/components/button-group.mjs +3 -24
  13. package/dist/components/button.d.mts +3 -25
  14. package/dist/components/button.mjs +2 -72
  15. package/dist/components/calendar.mjs +2 -1
  16. package/dist/components/carousel.d.mts +1 -2
  17. package/dist/components/chart.d.mts +2 -4
  18. package/dist/components/checkbox.mjs +2 -2
  19. package/dist/components/context-menu.mjs +2 -2
  20. package/dist/components/dialog.d.mts +1 -1
  21. package/dist/components/dialog.mjs +4 -4
  22. package/dist/components/drawer.d.mts +1 -1
  23. package/dist/components/drawer.mjs +2 -2
  24. package/dist/components/dropdown-menu.mjs +2 -2
  25. package/dist/components/empty.d.mts +3 -13
  26. package/dist/components/empty.mjs +3 -18
  27. package/dist/components/field.d.mts +3 -14
  28. package/dist/components/field.mjs +3 -32
  29. package/dist/components/form.d.mts +2 -4
  30. package/dist/components/hover-card.mjs +1 -1
  31. package/dist/components/input-group.d.mts +4 -31
  32. package/dist/components/input-group.mjs +3 -90
  33. package/dist/components/input-number.mjs +4 -4
  34. package/dist/components/input-otp.mjs +2 -2
  35. package/dist/components/input.mjs +1 -1
  36. package/dist/components/item.d.mts +4 -29
  37. package/dist/components/item.mjs +3 -56
  38. package/dist/components/menubar.mjs +2 -2
  39. package/dist/components/native-select.mjs +1 -1
  40. package/dist/components/navigation-menu.d.mts +1 -6
  41. package/dist/components/navigation-menu.mjs +8 -15
  42. package/dist/components/pagination.d.mts +1 -1
  43. package/dist/components/pagination.mjs +1 -1
  44. package/dist/components/popover.mjs +1 -1
  45. package/dist/components/progress-circle.d.mts +3 -47
  46. package/dist/components/progress-circle.mjs +2 -47
  47. package/dist/components/progress.mjs +1 -1
  48. package/dist/components/radio-group.mjs +1 -1
  49. package/dist/components/radio.mjs +1 -1
  50. package/dist/components/scroll-area.d.mts +3 -19
  51. package/dist/components/scroll-area.mjs +4 -61
  52. package/dist/components/select.d.mts +1 -1
  53. package/dist/components/select.mjs +3 -3
  54. package/dist/components/separator.d.mts +3 -18
  55. package/dist/components/separator.mjs +3 -23
  56. package/dist/components/sheet.d.mts +6 -18
  57. package/dist/components/sheet.mjs +6 -49
  58. package/dist/components/sidebar.d.mts +4 -19
  59. package/dist/components/sidebar.mjs +10 -46
  60. package/dist/components/skeleton.mjs +1 -1
  61. package/dist/components/slider.mjs +1 -1
  62. package/dist/components/spinner.mjs +1 -1
  63. package/dist/components/switch.mjs +2 -2
  64. package/dist/components/table.mjs +1 -1
  65. package/dist/components/tabs.mjs +1 -1
  66. package/dist/components/textarea.mjs +1 -1
  67. package/dist/components/toggle-group.d.mts +3 -2
  68. package/dist/components/toggle-group.mjs +1 -1
  69. package/dist/components/toggle.d.mts +2 -21
  70. package/dist/components/toggle.mjs +2 -39
  71. package/dist/components/tooltip.mjs +1 -1
  72. package/dist/index.d.mts +31 -16
  73. package/dist/index.mjs +30 -15
  74. package/dist/lib/utils.d.mts +1 -12
  75. package/dist/lib/utils.mjs +1 -9
  76. package/dist/primitives/checkbox-group.d.mts +1 -2
  77. package/dist/primitives/input-number.d.mts +1 -2
  78. package/dist/primitives/input.d.mts +1 -2
  79. package/dist/primitives/progress-circle.d.mts +1 -2
  80. package/dist/variants/alert.d.mts +18 -0
  81. package/dist/variants/alert.mjs +25 -0
  82. package/dist/variants/badge.d.mts +20 -0
  83. package/dist/variants/badge.mjs +46 -0
  84. package/dist/variants/button-group.d.mts +18 -0
  85. package/dist/variants/button-group.mjs +26 -0
  86. package/dist/variants/button.d.mts +30 -0
  87. package/dist/variants/button.mjs +76 -0
  88. package/dist/variants/empty.d.mts +18 -0
  89. package/dist/variants/empty.mjs +20 -0
  90. package/dist/variants/field.d.mts +19 -0
  91. package/dist/variants/field.mjs +34 -0
  92. package/dist/variants/input-group.d.mts +43 -0
  93. package/dist/variants/input-group.mjs +93 -0
  94. package/dist/variants/item.d.mts +37 -0
  95. package/dist/variants/item.mjs +60 -0
  96. package/dist/variants/navigation-menu.d.mts +13 -0
  97. package/dist/variants/navigation-menu.mjs +12 -0
  98. package/dist/variants/progress-circle.d.mts +52 -0
  99. package/dist/variants/progress-circle.mjs +49 -0
  100. package/dist/variants/scroll-area.d.mts +24 -0
  101. package/dist/variants/scroll-area.mjs +63 -0
  102. package/dist/variants/separator.d.mts +23 -0
  103. package/dist/variants/separator.mjs +25 -0
  104. package/dist/variants/sheet.d.mts +20 -0
  105. package/dist/variants/sheet.mjs +50 -0
  106. package/dist/variants/sidebar.d.mts +23 -0
  107. package/dist/variants/sidebar.mjs +42 -0
  108. package/dist/variants/toggle.d.mts +23 -0
  109. package/dist/variants/toggle.mjs +43 -0
  110. package/package.json +168 -20
  111. package/src/components/accordion.tsx +156 -0
  112. package/src/components/alert-dialog.tsx +314 -0
  113. package/src/components/alert.tsx +86 -0
  114. package/src/components/aspect-ratio.tsx +28 -0
  115. package/src/components/avatar.tsx +84 -0
  116. package/src/components/badge.tsx +38 -0
  117. package/src/components/breadcrumb.tsx +197 -0
  118. package/src/components/button-group.tsx +107 -0
  119. package/src/components/button.tsx +66 -0
  120. package/src/components/calendar.tsx +277 -0
  121. package/src/components/card.tsx +175 -0
  122. package/src/components/carousel.tsx +367 -0
  123. package/src/components/chart.tsx +587 -0
  124. package/src/components/checkbox-cards.tsx +92 -0
  125. package/src/components/checkbox-group.tsx +83 -0
  126. package/src/components/checkbox.tsx +65 -0
  127. package/src/components/collapsible.tsx +60 -0
  128. package/src/components/command.tsx +311 -0
  129. package/src/components/context-menu.tsx +489 -0
  130. package/src/components/dialog.tsx +295 -0
  131. package/src/components/drawer.tsx +271 -0
  132. package/src/components/dropdown-menu.tsx +498 -0
  133. package/src/components/empty.tsx +169 -0
  134. package/src/components/field.tsx +362 -0
  135. package/src/components/form.tsx +300 -0
  136. package/src/components/hover-card.tsx +116 -0
  137. package/src/components/input-group.tsx +224 -0
  138. package/src/components/input-number.tsx +161 -0
  139. package/src/components/input-otp.tsx +151 -0
  140. package/src/components/input-password.tsx +74 -0
  141. package/src/components/input-search.tsx +98 -0
  142. package/src/components/input.tsx +52 -0
  143. package/src/components/item.tsx +280 -0
  144. package/src/components/kbd.tsx +59 -0
  145. package/src/components/label.tsx +44 -0
  146. package/src/components/menubar.tsx +531 -0
  147. package/src/components/native-select.tsx +96 -0
  148. package/src/components/navigation-menu.tsx +295 -0
  149. package/src/components/pagination.tsx +204 -0
  150. package/src/components/popover.tsx +139 -0
  151. package/src/components/progress-circle.tsx +203 -0
  152. package/src/components/progress.tsx +54 -0
  153. package/src/components/radio-cards.tsx +85 -0
  154. package/src/components/radio-group.tsx +79 -0
  155. package/src/components/radio.tsx +61 -0
  156. package/src/components/resizable.tsx +99 -0
  157. package/src/components/scroll-area.tsx +115 -0
  158. package/src/components/select.tsx +319 -0
  159. package/src/components/separator.tsx +74 -0
  160. package/src/components/sheet.tsx +278 -0
  161. package/src/components/sidebar.tsx +1056 -0
  162. package/src/components/skeleton.tsx +37 -0
  163. package/src/components/slider.tsx +95 -0
  164. package/src/components/sonner.tsx +47 -0
  165. package/src/components/spinner.tsx +75 -0
  166. package/src/components/switch.tsx +66 -0
  167. package/src/components/table.tsx +200 -0
  168. package/src/components/tabs.tsx +128 -0
  169. package/src/components/textarea.tsx +49 -0
  170. package/src/components/toggle-group.tsx +141 -0
  171. package/src/components/toggle.tsx +39 -0
  172. package/src/components/tooltip.tsx +141 -0
  173. package/src/css/amber.css +59 -22
  174. package/src/css/blue.css +59 -22
  175. package/src/css/cyan.css +59 -22
  176. package/src/css/emerald.css +59 -22
  177. package/src/css/fuchsia.css +59 -22
  178. package/src/css/gray.css +59 -22
  179. package/src/css/green.css +59 -22
  180. package/src/css/indigo.css +59 -22
  181. package/src/css/lime.css +59 -22
  182. package/src/css/neutral.css +59 -22
  183. package/src/css/orange.css +59 -22
  184. package/src/css/pink.css +59 -22
  185. package/src/css/preset.css +32 -13
  186. package/src/css/purple.css +59 -22
  187. package/src/css/red.css +59 -22
  188. package/src/css/rose.css +59 -22
  189. package/src/css/sky.css +59 -22
  190. package/src/css/slate.css +59 -22
  191. package/src/css/stone.css +59 -22
  192. package/src/css/teal.css +59 -22
  193. package/src/css/violet.css +59 -22
  194. package/src/css/yellow.css +59 -22
  195. package/src/css/zinc.css +59 -22
  196. package/src/hooks/use-animated-value.ts +97 -0
  197. package/src/hooks/use-copy-to-clipboard.ts +63 -0
  198. package/src/hooks/use-is-mobile.ts +27 -0
  199. package/src/hooks/use-media-query.ts +71 -0
  200. package/src/hooks/use-mutation-observer.ts +54 -0
  201. package/src/hooks/use-pagination.ts +166 -0
  202. package/src/index.ts +720 -0
  203. package/src/lib/utils.ts +5 -0
  204. package/src/primitives/checkbox-group.tsx +360 -0
  205. package/src/primitives/input-number.tsx +1013 -0
  206. package/src/primitives/input.tsx +243 -0
  207. package/src/primitives/progress-circle.tsx +537 -0
  208. package/src/variants/alert.ts +45 -0
  209. package/src/variants/badge.ts +66 -0
  210. package/src/variants/button-group.ts +49 -0
  211. package/src/variants/button.ts +93 -0
  212. package/src/variants/empty.ts +43 -0
  213. package/src/variants/field.ts +50 -0
  214. package/src/variants/input-group.ts +132 -0
  215. package/src/variants/item.ts +90 -0
  216. package/src/variants/navigation-menu.ts +32 -0
  217. package/src/variants/progress-circle.ts +47 -0
  218. package/src/variants/scroll-area.ts +79 -0
  219. package/src/variants/separator.ts +41 -0
  220. package/src/variants/sheet.ts +70 -0
  221. package/src/variants/sidebar.ts +61 -0
  222. package/src/variants/toggle.ts +59 -0
  223. package/dist/node_modules/.pnpm/clsx@2.1.1/node_modules/clsx/clsx.d.mts +0 -6
@@ -0,0 +1,1013 @@
1
+ import type { Scope } from "@radix-ui/react-context";
2
+ import type {
3
+ ComponentProps,
4
+ FocusEventHandler,
5
+ JSX,
6
+ KeyboardEvent,
7
+ KeyboardEventHandler,
8
+ MouseEventHandler,
9
+ PointerEventHandler,
10
+ RefObject,
11
+ } from "react";
12
+
13
+ import { composeEventHandlers } from "@radix-ui/primitive";
14
+ import { createContextScope } from "@radix-ui/react-context";
15
+ import { useControllableState } from "@radix-ui/react-use-controllable-state";
16
+ import { useCallback, useEffect, useMemo, useRef } from "react";
17
+
18
+ import * as InputPrimitive from "#/primitives/input";
19
+ import { createInputScope } from "#/primitives/input";
20
+
21
+ /* -----------------------------------------------------------------------------
22
+ * Context: InputNumber
23
+ * -------------------------------------------------------------------------- */
24
+
25
+ /**
26
+ * The name of the InputNumber component constant.
27
+ */
28
+ const NUMBER_INPUT_NAME = "InputNumber";
29
+
30
+ /**
31
+ * Props that include an optional scope for the InputNumber component.
32
+ */
33
+ type ScopedProps<P> = P & {
34
+ /**
35
+ * Optional scope for the InputNumber component context
36
+ */
37
+ __scopeInputNumber?: Scope;
38
+ };
39
+
40
+ const [createInputNumberContext, createInputNumberScope] = createContextScope(NUMBER_INPUT_NAME, [
41
+ createInputScope,
42
+ ]);
43
+ const useInputScope = createInputScope();
44
+
45
+ /**
46
+ * Context value for the InputNumber component.
47
+ */
48
+ interface InputNumberContextValue {
49
+ /**
50
+ * Formatting options for displaying the number value
51
+ */
52
+ formatOptions: Intl.NumberFormatOptions;
53
+
54
+ /**
55
+ * Function to format a number value as a string
56
+ */
57
+ formatValue: (value?: number) => string;
58
+
59
+ /**
60
+ * Reference to the input element
61
+ */
62
+ inputRef: RefObject<HTMLInputElement | null>;
63
+
64
+ /**
65
+ * Handler for when the value changes
66
+ */
67
+ onChange: (value?: number) => void;
68
+
69
+ /**
70
+ * Handler to decrement the value
71
+ */
72
+ onDecrement: () => void;
73
+
74
+ /**
75
+ * Handler to decrement the value to the minimum allowed
76
+ */
77
+ onDecrementToMin: () => void;
78
+
79
+ /**
80
+ * Handler to increment the value
81
+ */
82
+ onIncrement: () => void;
83
+
84
+ /**
85
+ * Handler to increment the value to the maximum allowed
86
+ */
87
+ onIncrementToMax: () => void;
88
+
89
+ /**
90
+ * Function to parse a value into a number
91
+ */
92
+ parseValue: (value: number | ReadonlyArray<string> | string | undefined) => number;
93
+
94
+ /**
95
+ * Accessible label for the decrement button
96
+ */
97
+ ariaDecrementLabel?: string;
98
+
99
+ /**
100
+ * Accessible label for the increment button
101
+ */
102
+ ariaIncrementLabel?: string;
103
+
104
+ /**
105
+ * Initial value used when the input is uncontrolled
106
+ */
107
+ defaultValue?: number;
108
+
109
+ /**
110
+ * Whether the input is disabled
111
+ */
112
+ disabled?: boolean;
113
+
114
+ /**
115
+ * Unique identifier for the input
116
+ */
117
+ id?: string;
118
+
119
+ /**
120
+ * Maximum allowed value
121
+ */
122
+ max?: number;
123
+
124
+ /**
125
+ * Minimum allowed value
126
+ */
127
+ min?: number;
128
+
129
+ /**
130
+ * Whether the input is read-only
131
+ */
132
+ readOnly?: boolean;
133
+
134
+ /**
135
+ * Step value for increments/decrements
136
+ */
137
+ step?: number;
138
+
139
+ /**
140
+ * Current value of the input
141
+ */
142
+ value?: number;
143
+ }
144
+
145
+ const [InputNumberContextProvider, useInputNumberContext] =
146
+ createInputNumberContext<InputNumberContextValue>(NUMBER_INPUT_NAME);
147
+
148
+ /* -----------------------------------------------------------------------------
149
+ * Component: InputNumber
150
+ * -------------------------------------------------------------------------- */
151
+
152
+ /**
153
+ * Props for the main InputNumber component.
154
+ *
155
+ * @since 0.3.16-canary.0
156
+ */
157
+ interface InputNumberProps extends ComponentProps<typeof InputPrimitive.Root> {
158
+ /**
159
+ * Accessible label for the decrement button
160
+ */
161
+ ariaDecrementLabel?: string;
162
+
163
+ /**
164
+ * Accessible label for the increment button
165
+ */
166
+ ariaIncrementLabel?: string;
167
+
168
+ /**
169
+ * Initial value when uncontrolled
170
+ */
171
+ defaultValue?: number;
172
+
173
+ /**
174
+ * Options for number formatting
175
+ */
176
+ formatOptions?: Intl.NumberFormatOptions;
177
+
178
+ /**
179
+ * Unique identifier for the input
180
+ */
181
+ id?: string;
182
+
183
+ /**
184
+ * Locale used for number formatting
185
+ */
186
+ locale?: string;
187
+
188
+ /**
189
+ * Maximum allowed value
190
+ */
191
+ max?: number;
192
+
193
+ /**
194
+ * Minimum allowed value
195
+ */
196
+ min?: number;
197
+
198
+ /**
199
+ * Handler called when the value changes
200
+ */
201
+ onChange?: (value?: number) => void;
202
+
203
+ /**
204
+ * Step value for increments/decrements
205
+ */
206
+ step?: number;
207
+
208
+ /**
209
+ * Current value when controlled
210
+ */
211
+ value?: number;
212
+ }
213
+
214
+ /**
215
+ * @since 0.3.16-canary.0
216
+ */
217
+ function InputNumber(numberInputProps: ScopedProps<InputNumberProps>): JSX.Element {
218
+ const {
219
+ __scopeInputNumber,
220
+ ariaDecrementLabel,
221
+ ariaIncrementLabel,
222
+ defaultValue,
223
+ formatOptions = { minimumFractionDigits: 0, style: "decimal" },
224
+ id,
225
+ locale,
226
+ max,
227
+ min,
228
+ onChange,
229
+ step = 1,
230
+ value: valueProperty,
231
+ ...props
232
+ } = numberInputProps;
233
+
234
+ /**
235
+ * Scope for the input component
236
+ */
237
+ const inputScope = useInputScope(__scopeInputNumber);
238
+
239
+ /**
240
+ * Reference to the input element
241
+ */
242
+ const inputRef = useRef<HTMLInputElement>(null);
243
+
244
+ /**
245
+ * Controlled or uncontrolled value state
246
+ */
247
+ const [value, setValue] = useControllableState<number | undefined>({
248
+ defaultProp: defaultValue,
249
+ onChange,
250
+ prop: valueProperty,
251
+ });
252
+
253
+ /**
254
+ * Separators used for number formatting based on locale
255
+ */
256
+ const { decimalSeparator, thousandSeparator } = useMemo(
257
+ () => getNumberFormatSeparators(locale),
258
+ [locale],
259
+ );
260
+
261
+ /**
262
+ * Formats a number value into a string representation
263
+ * @param inputValue - The number to format
264
+ * @returns A formatted string representation of the number
265
+ */
266
+ const formatValue = useCallback(
267
+ (inputValue?: number): string => {
268
+ if (inputValue === undefined || Number.isNaN(inputValue)) {
269
+ return "";
270
+ }
271
+
272
+ return new Intl.NumberFormat(locale, formatOptions).format(inputValue);
273
+ },
274
+ [formatOptions, locale],
275
+ );
276
+
277
+ /**
278
+ * Parses a string or number input into a normalized number value
279
+ * @param inputValue - The value to parse
280
+ * @returns The parsed number value, clamped between min and max
281
+ */
282
+ const parseValue = useCallback(
283
+ (inputValue: number | ReadonlyArray<string> | string | undefined): number => {
284
+ if (typeof inputValue === "number") {
285
+ return clamp(inputValue, min, max);
286
+ }
287
+
288
+ if (typeof inputValue !== "string") {
289
+ return Number.NaN;
290
+ }
291
+
292
+ const cleanedValue = inputValue.trim().replaceAll(/[^\d.,\-()]/g, "");
293
+
294
+ if (cleanedValue === "") {
295
+ return Number.NaN;
296
+ }
297
+
298
+ const normalizedValue = normalizeInputValue(
299
+ cleanedValue,
300
+ thousandSeparator,
301
+ decimalSeparator,
302
+ );
303
+ let parsedValue = Number.parseFloat(normalizedValue);
304
+
305
+ if (formatOptions.style === "percent") {
306
+ parsedValue /= 100;
307
+ }
308
+
309
+ return Number.isNaN(parsedValue) ? 0 : clamp(parsedValue, min, max);
310
+ },
311
+ [decimalSeparator, formatOptions.style, max, min, thousandSeparator],
312
+ );
313
+
314
+ /**
315
+ * Changes the current value based on a provided operation
316
+ * @param operation - Function that takes the current value and returns a new value
317
+ */
318
+ const changeNumberValue = useCallback(
319
+ (operation: (number: number) => number) => {
320
+ const inputElement = inputRef.current;
321
+
322
+ if (!inputElement || props.disabled || props.readOnly) {
323
+ return;
324
+ }
325
+
326
+ const currentValue = parseValue(inputElement.value) || 0;
327
+ const newValue = clamp(operation(currentValue), min, max);
328
+
329
+ inputElement.value = formatValue(newValue);
330
+ setValue(newValue);
331
+ },
332
+ [props.disabled, formatValue, max, min, parseValue, props.readOnly, setValue],
333
+ );
334
+
335
+ /**
336
+ * Increments the current value by the step amount
337
+ */
338
+ const handleIncrement = useCallback(() => {
339
+ changeNumberValue((number) => number + step);
340
+ }, [changeNumberValue, step]);
341
+
342
+ /**
343
+ * Decrements the current value by the step amount
344
+ */
345
+ const handleDecrement = useCallback(() => {
346
+ changeNumberValue((number) => number - step);
347
+ }, [changeNumberValue, step]);
348
+
349
+ /**
350
+ * Sets the value to the maximum allowed
351
+ */
352
+ const handleIncrementToMax = useCallback(() => {
353
+ changeNumberValue((number) => max ?? number + step);
354
+ }, [changeNumberValue, max, step]);
355
+
356
+ /**
357
+ * Sets the value to the minimum allowed
358
+ */
359
+ const handleDecrementToMin = useCallback(() => {
360
+ changeNumberValue((number) => min ?? number - step);
361
+ }, [changeNumberValue, min, step]);
362
+
363
+ return (
364
+ <InputNumberContextProvider
365
+ ariaDecrementLabel={ariaDecrementLabel}
366
+ ariaIncrementLabel={ariaIncrementLabel}
367
+ defaultValue={defaultValue}
368
+ disabled={props.disabled}
369
+ formatOptions={formatOptions}
370
+ formatValue={formatValue}
371
+ id={id}
372
+ inputRef={inputRef}
373
+ max={max}
374
+ min={min}
375
+ parseValue={parseValue}
376
+ readOnly={props.readOnly}
377
+ scope={__scopeInputNumber}
378
+ value={value}
379
+ onChange={setValue}
380
+ onDecrement={handleDecrement}
381
+ onDecrementToMin={handleDecrementToMin}
382
+ onIncrement={handleIncrement}
383
+ onIncrementToMax={handleIncrementToMax}
384
+ >
385
+ <InputPrimitive.Root {...inputScope} {...props} />
386
+ </InputNumberContextProvider>
387
+ );
388
+ }
389
+
390
+ /* -----------------------------------------------------------------------------
391
+ * Component: InputNumberField
392
+ * -------------------------------------------------------------------------- */
393
+
394
+ /**
395
+ * The name of the InputNumberField component constant.
396
+ */
397
+ const NUMBER_INPUT_FIELD_NAME = "InputNumberField";
398
+
399
+ /**
400
+ * Defines the props for the `InputNumberField` component.
401
+ *
402
+ * @since 0.3.16-canary.0
403
+ */
404
+ type InputNumberFieldProps = Omit<
405
+ ComponentProps<typeof InputPrimitive.Field>,
406
+ | "defaultValue"
407
+ | "disabled"
408
+ | "id"
409
+ | "max"
410
+ | "min"
411
+ | "onChange"
412
+ | "prefix"
413
+ | "readOnly"
414
+ | "step"
415
+ | "value"
416
+ >;
417
+
418
+ /**
419
+ * @since 0.3.16-canary.0
420
+ */
421
+ function InputNumberField({
422
+ __scopeInputNumber,
423
+ onBlur,
424
+ onKeyDown,
425
+ ...props
426
+ }: ScopedProps<InputNumberFieldProps>): JSX.Element {
427
+ // Retrieve input number context and input scope
428
+ const inputScope = useInputScope(__scopeInputNumber);
429
+ const {
430
+ defaultValue,
431
+ disabled,
432
+ formatValue,
433
+ id,
434
+ inputRef,
435
+ max,
436
+ min,
437
+ onChange,
438
+ onDecrement,
439
+ onDecrementToMin,
440
+ onIncrement,
441
+ onIncrementToMax,
442
+ parseValue,
443
+ readOnly,
444
+ step,
445
+ value,
446
+ } = useInputNumberContext(NUMBER_INPUT_FIELD_NAME, __scopeInputNumber);
447
+
448
+ /**
449
+ * Handles the blur event to format the value of the input.
450
+ *
451
+ * @param event - The blur event triggered when the input loses focus.
452
+ */
453
+ const handleBlur = useCallback<FocusEventHandler<HTMLInputElement>>(
454
+ (event) => {
455
+ const numericValue = parseValue(event.target.value);
456
+ const formattedValue = formatValue(numericValue);
457
+
458
+ if (formattedValue !== event.target.value) {
459
+ event.target.value = formattedValue;
460
+ }
461
+
462
+ onChange(numericValue);
463
+ },
464
+ [formatValue, onChange, parseValue],
465
+ );
466
+
467
+ /**
468
+ * Handles keydown events to increment, decrement, or perform other actions.
469
+ *
470
+ * @param event - The keyboard event triggered by key presses.
471
+ */
472
+ const handleKeyDown = useCallback<KeyboardEventHandler<HTMLInputElement>>(
473
+ (event) => {
474
+ switch (event.key) {
475
+ case "ArrowUp": {
476
+ onIncrement();
477
+ event.preventDefault();
478
+ break;
479
+ }
480
+
481
+ case "PageUp": {
482
+ onIncrementToMax();
483
+ event.preventDefault();
484
+ break;
485
+ }
486
+
487
+ case "ArrowDown": {
488
+ onDecrement();
489
+ event.preventDefault();
490
+ break;
491
+ }
492
+
493
+ case "PageDown": {
494
+ onDecrementToMin();
495
+ event.preventDefault();
496
+ break;
497
+ }
498
+
499
+ default: {
500
+ break;
501
+ }
502
+ }
503
+ },
504
+ [onIncrement, onIncrementToMax, onDecrement, onDecrementToMin],
505
+ );
506
+
507
+ /**
508
+ * Prevents invalid keyboard input for the numeric input field.
509
+ *
510
+ * @param event - The keyboard event to handle.
511
+ */
512
+ const handleKeyDownPrevent = useCallback<KeyboardEventHandler<HTMLInputElement>>((event) => {
513
+ switch (event.key) {
514
+ case "ArrowUp":
515
+
516
+ case "ArrowDown":
517
+
518
+ case "ArrowLeft":
519
+
520
+ case "ArrowRight":
521
+
522
+ case "PageUp":
523
+
524
+ case "PageDown":
525
+
526
+ case "Tab":
527
+
528
+ case "Escape":
529
+
530
+ case "Enter":
531
+
532
+ case "Backspace":
533
+
534
+ case "Delete":
535
+
536
+ case "Home":
537
+
538
+ case "End":
539
+
540
+ case ".":
541
+
542
+ case ",":
543
+
544
+ case "-":
545
+
546
+ case "%": {
547
+ return;
548
+ }
549
+
550
+ default: {
551
+ if (isNumberKey(event.key) || isModifierKey(event) || isFunctionKey(event.key)) {
552
+ return;
553
+ }
554
+
555
+ event.preventDefault();
556
+ }
557
+ }
558
+ }, []);
559
+
560
+ /**
561
+ * Handles the Enter key to format the value of the input.
562
+ *
563
+ * @param event - The keyboard event triggered by pressing Enter.
564
+ */
565
+ const handleKeyDownEnter = useCallback<KeyboardEventHandler<HTMLInputElement>>(
566
+ (event) => {
567
+ const inputElement = inputRef.current;
568
+
569
+ if (event.key !== "Enter" || !inputElement) {
570
+ return;
571
+ }
572
+
573
+ const numericValue = parseValue(inputElement.value);
574
+ const formattedValue = formatValue(numericValue);
575
+
576
+ if (formattedValue !== inputElement.value) {
577
+ inputElement.value = formattedValue;
578
+ }
579
+
580
+ onChange(numericValue);
581
+ },
582
+ [formatValue, inputRef, onChange, parseValue],
583
+ );
584
+
585
+ /**
586
+ * Creates a combined keydown event handler that processes keyboard events in sequence.
587
+ *
588
+ * The handler chain executes in the following order:
589
+ * 1. User-provided onKeyDown handler (if any)
590
+ * 2. handleKeyDownPrevent - Prevents non-numeric input
591
+ * 3. handleKeyDown - Handles arrow keys and page up/down for value adjustments
592
+ * 4. handleKeyDownEnter - Handles Enter key press to format and update the value
593
+ *
594
+ * @returns A composed event handler function for the onKeyDown event
595
+ */
596
+ const combinedKeyDownHandler = useCallback<KeyboardEventHandler<HTMLInputElement>>(
597
+ (event) => {
598
+ composeEventHandlers(
599
+ onKeyDown,
600
+ chain(handleKeyDownPrevent, handleKeyDown, handleKeyDownEnter),
601
+ )(event);
602
+ },
603
+ [onKeyDown, handleKeyDown, handleKeyDownEnter, handleKeyDownPrevent],
604
+ );
605
+
606
+ /**
607
+ * Adds a listener to handle wheel events for incrementing or decrementing the value.
608
+ */
609
+ useEffect(() => {
610
+ const handleWheel = (event: WheelEvent): void => {
611
+ const inputElement = inputRef.current;
612
+
613
+ if (!inputElement || disabled || readOnly || document.activeElement !== inputElement) {
614
+ return;
615
+ }
616
+
617
+ event.preventDefault();
618
+
619
+ if (event.deltaY > 0) {
620
+ onIncrement();
621
+ } else {
622
+ onDecrement();
623
+ }
624
+ };
625
+
626
+ const inputElement = inputRef.current;
627
+
628
+ inputElement?.addEventListener("wheel", handleWheel);
629
+
630
+ return (): void => {
631
+ inputElement?.removeEventListener("wheel", handleWheel);
632
+ };
633
+ }, [onIncrement, onDecrement, inputRef, disabled, readOnly]);
634
+
635
+ /**
636
+ * Updates the input field's value when it changes in the context.
637
+ */
638
+ useEffect(() => {
639
+ const inputElement = inputRef.current;
640
+
641
+ if (inputElement && inputElement !== document.activeElement) {
642
+ inputElement.value = formatValue(value);
643
+ }
644
+ }, [formatValue, inputRef, value]);
645
+
646
+ /**
647
+ * Adds a listener to handle form reset events by clearing the input value.
648
+ */
649
+ useEffect(() => {
650
+ const inputElement = inputRef.current;
651
+
652
+ if (!inputElement) {
653
+ return;
654
+ }
655
+
656
+ const handleReset = (): void => {
657
+ onChange(parseValue(defaultValue));
658
+ };
659
+
660
+ const form = inputElement.form;
661
+
662
+ form?.addEventListener("reset", handleReset);
663
+
664
+ return (): void => {
665
+ form?.removeEventListener("reset", handleReset);
666
+ };
667
+ }, [defaultValue, inputRef, onChange, parseValue]);
668
+
669
+ return (
670
+ <InputPrimitive.Field
671
+ ref={inputRef}
672
+ defaultValue={formatValue(value)}
673
+ disabled={disabled}
674
+ id={id}
675
+ inputMode="decimal"
676
+ max={max}
677
+ min={min}
678
+ readOnly={readOnly}
679
+ step={step}
680
+ onBlur={composeEventHandlers(onBlur, handleBlur)}
681
+ onKeyDown={combinedKeyDownHandler}
682
+ {...inputScope}
683
+ {...props}
684
+ />
685
+ );
686
+ }
687
+
688
+ /* -----------------------------------------------------------------------------
689
+ * Component: NumberStepperButton
690
+ * -------------------------------------------------------------------------- */
691
+
692
+ /**
693
+ * The name of the NumberStepperButton component constant.
694
+ */
695
+ const NUMBER_STEPPER_BUTTON_NAME = "NumberStepperButton";
696
+
697
+ /**
698
+ * Props for the NumberStepperButton component.
699
+ */
700
+ interface NumberStepperButtonProps extends ComponentProps<"button"> {
701
+ /**
702
+ * The operation to perform when the button is pressed.
703
+ * - `'increment'`: Increases the value.
704
+ * - `'decrement'`: Decreases the value.
705
+ */
706
+ operation: "decrement" | "increment";
707
+ }
708
+
709
+ function NumberStepperButton({
710
+ __scopeInputNumber,
711
+ operation,
712
+ ...props
713
+ }: ScopedProps<NumberStepperButtonProps>): JSX.Element {
714
+ // Destructures relevant context values for the button functionality.
715
+ const {
716
+ ariaDecrementLabel,
717
+ ariaIncrementLabel,
718
+ disabled,
719
+ id,
720
+ max,
721
+ min,
722
+ onDecrement,
723
+ onIncrement,
724
+ value,
725
+ } = useInputNumberContext(NUMBER_STEPPER_BUTTON_NAME, __scopeInputNumber);
726
+
727
+ const isDisabled = useMemo(() => {
728
+ const atMin = min !== undefined && value !== undefined && value <= min;
729
+ const atMax = max !== undefined && value !== undefined && value >= max;
730
+
731
+ return (disabled ?? atMin) || atMax;
732
+ }, [min, max, value, disabled]);
733
+
734
+ /**
735
+ * Ref to store a timeout ID for managing repeated button actions.
736
+ */
737
+ const timeoutIdRef = useRef<ReturnType<typeof setTimeout>>(null);
738
+
739
+ /**
740
+ * Starts a repeated action at a regular interval.
741
+ * The action begins immediately and then continues with a delay.
742
+ *
743
+ * @param callback - The callback function to execute repeatedly.
744
+ */
745
+ const startActionInterval = useCallback((callback: () => void) => {
746
+ // Time between repeated actions (in milliseconds).
747
+ const interval = 100;
748
+
749
+ // Function to perform the action and set the next interval.
750
+ const repeatAction = (): void => {
751
+ callback();
752
+ timeoutIdRef.current = setTimeout(repeatAction, interval);
753
+ };
754
+
755
+ callback();
756
+ timeoutIdRef.current = setTimeout(repeatAction, interval * 2);
757
+ }, []);
758
+
759
+ /**
760
+ * Clears any ongoing action intervals.
761
+ */
762
+ const clearActionInterval = useCallback(() => {
763
+ if (timeoutIdRef.current) {
764
+ clearTimeout(timeoutIdRef.current);
765
+ timeoutIdRef.current = null;
766
+ }
767
+ }, []);
768
+
769
+ /**
770
+ * Handles pointer down events and triggers the appropriate action
771
+ * (`increment` or `decrement`).
772
+ */
773
+ const handlePointerDown = useCallback<PointerEventHandler<HTMLButtonElement>>(() => {
774
+ const action = operation === "increment" ? onIncrement : onDecrement;
775
+
776
+ startActionInterval(action);
777
+ }, [onDecrement, onIncrement, operation, startActionInterval]);
778
+
779
+ /**
780
+ * Prevents the context menu from displaying when the button is right-clicked.
781
+ *
782
+ * @param event - The mouse event triggered by the right-click.
783
+ */
784
+ const handleContextMenu = useCallback<MouseEventHandler<HTMLButtonElement>>((event) => {
785
+ event.preventDefault();
786
+ }, []);
787
+
788
+ /**
789
+ * Handles keyboard events to support activation of the button using
790
+ * keyboard navigation (Enter or Space).
791
+ *
792
+ * @param event - The keyboard event with the triggered key.
793
+ */
794
+ const handleKeyDown = useCallback<KeyboardEventHandler<HTMLButtonElement>>(
795
+ (event) => {
796
+ if (event.key === "Enter" || event.key === " ") {
797
+ event.preventDefault();
798
+
799
+ const action = operation === "increment" ? onIncrement : onDecrement;
800
+
801
+ action();
802
+ }
803
+ },
804
+ [onDecrement, onIncrement, operation],
805
+ );
806
+
807
+ return (
808
+ <button
809
+ aria-controls={id}
810
+ aria-label={operation === "increment" ? ariaIncrementLabel : ariaDecrementLabel}
811
+ aria-live="polite"
812
+ disabled={isDisabled}
813
+ type="button"
814
+ onContextMenu={handleContextMenu}
815
+ onKeyDown={handleKeyDown}
816
+ onPointerCancel={clearActionInterval}
817
+ onPointerDown={handlePointerDown}
818
+ onPointerLeave={clearActionInterval}
819
+ onPointerUp={clearActionInterval}
820
+ {...props}
821
+ />
822
+ );
823
+ }
824
+
825
+ /* -----------------------------------------------------------------------------
826
+ * Component: InputNumberIncrementButton
827
+ * -------------------------------------------------------------------------- */
828
+
829
+ /**
830
+ * @since 0.3.16-canary.0
831
+ */
832
+ type InputNumberIncrementButtonProps = Omit<
833
+ ComponentProps<typeof NumberStepperButton>,
834
+ "operation"
835
+ >;
836
+
837
+ /**
838
+ * @since 0.3.16-canary.0
839
+ */
840
+ function InputNumberIncrementButton(props: InputNumberIncrementButtonProps): JSX.Element {
841
+ return <NumberStepperButton operation="increment" {...props} />;
842
+ }
843
+
844
+ /* -----------------------------------------------------------------------------
845
+ * Component: InputNumberDecrementButton
846
+ * -------------------------------------------------------------------------- */
847
+
848
+ /**
849
+ * @since 0.3.16-canary.0
850
+ */
851
+ type InputNumberDecrementButtonProps = Omit<
852
+ ComponentProps<typeof NumberStepperButton>,
853
+ "operation"
854
+ >;
855
+
856
+ /**
857
+ * @since 0.3.16-canary.0
858
+ */
859
+ function InputNumberDecrementButton(props: InputNumberDecrementButtonProps): JSX.Element {
860
+ return <NumberStepperButton operation="decrement" {...props} />;
861
+ }
862
+
863
+ /* -----------------------------------------------------------------------------
864
+ * Utility Functions
865
+ * -------------------------------------------------------------------------- */
866
+
867
+ /**
868
+ * Chains multiple callbacks into a single function
869
+ *
870
+ * @param callbacks - Array of callback functions that will be executed in order
871
+ * @returns A single function that executes all callbacks
872
+ */
873
+ function chain<T extends Array<unknown>>(
874
+ ...callbacks: Array<(...args: T) => void>
875
+ ): (...args: T) => void {
876
+ return (...args: T) => {
877
+ for (const callback of callbacks) {
878
+ callback(...args);
879
+ }
880
+ };
881
+ }
882
+
883
+ /**
884
+ * Interface for number formatting separators
885
+ */
886
+ interface NumberFormatSeparators {
887
+ /**
888
+ * The character used to separate decimal part (e.g., "." or ",")
889
+ */
890
+ decimalSeparator: string;
891
+ /**
892
+ * The character used to separate thousands (e.g., "," or ".")
893
+ */
894
+ thousandSeparator: string;
895
+ }
896
+
897
+ /**
898
+ * Extracts decimal and a thousand separators from a given locale's number format
899
+ *
900
+ * @param locale - The locale string to use for number formatting (e.g., 'en-US', 'de-DE')
901
+ * @returns Object containing decimal and a thousand separators
902
+ */
903
+ function getNumberFormatSeparators(locale?: string): NumberFormatSeparators {
904
+ const numberFormat = new Intl.NumberFormat(locale);
905
+ const parts = numberFormat.formatToParts(12_345.6);
906
+ let thousandSeparator = "";
907
+ let decimalSeparator = "";
908
+
909
+ for (const part of parts) {
910
+ if (part.type === "group") {
911
+ thousandSeparator = part.value;
912
+ }
913
+
914
+ if (part.type === "decimal") {
915
+ decimalSeparator = part.value;
916
+ }
917
+
918
+ // Stop early if you've found enough.
919
+ if (thousandSeparator && decimalSeparator) {
920
+ break;
921
+ }
922
+ }
923
+
924
+ return { decimalSeparator, thousandSeparator };
925
+ }
926
+
927
+ /**
928
+ * Normalizes an input value by removing formatting characters
929
+ *
930
+ * @param value - The input string to normalize
931
+ * @param thousandSeparator - The thousand-separator character to remove
932
+ * @param decimalSeparator - The decimal separator to convert to standard dot notation
933
+ * @returns Normalized string value ready for numeric conversion
934
+ */
935
+ function normalizeInputValue(
936
+ value: string,
937
+ thousandSeparator: string,
938
+ decimalSeparator: string,
939
+ ): string {
940
+ return value
941
+ .replaceAll(new RegExp(`\\${thousandSeparator}`, "g"), "")
942
+ .replace(new RegExp(`\\${decimalSeparator}`), ".")
943
+ .replaceAll(/[()]/g, "-");
944
+ }
945
+
946
+ /**
947
+ * Checks if a keyboard event includes modifier keys (Ctrl, Alt, Meta, Shift)
948
+ *
949
+ * @param event - The keyboard event to check
950
+ * @returns True if any modifier key is pressed
951
+ */
952
+ function isModifierKey(event: KeyboardEvent<HTMLInputElement>): boolean {
953
+ return event.ctrlKey || event.altKey || event.metaKey || event.shiftKey;
954
+ }
955
+
956
+ /**
957
+ * Determines if a key is a function key (F1-F12)
958
+ *
959
+ * @param key - The key name to check
960
+ * @returns True if the key is a function key
961
+ */
962
+ function isFunctionKey(key: string): boolean {
963
+ return key.startsWith("F") && key.length > 1;
964
+ }
965
+
966
+ /**
967
+ * Checks if a key represents a number (0-9)
968
+ *
969
+ * @param key - The key name to check
970
+ * @returns True if the key represents a number
971
+ */
972
+ function isNumberKey(key: string): boolean {
973
+ return !Number.isNaN(Number(key));
974
+ }
975
+
976
+ /**
977
+ * Clamps a numeric value between a minimum and maximum
978
+ *
979
+ * @param value - The value to clamp
980
+ * @param min - The minimum allowed value (defaults to \-Infinity)
981
+ * @param max - The maximum allowed value (defaults to Infinity)
982
+ * @returns The clamped value
983
+ */
984
+ function clamp(
985
+ value: number,
986
+ min = Number.NEGATIVE_INFINITY,
987
+ max = Number.POSITIVE_INFINITY,
988
+ ): number {
989
+ return Math.min(Math.max(value, min), max);
990
+ }
991
+
992
+ /* -----------------------------------------------------------------------------
993
+ * Exports
994
+ * -------------------------------------------------------------------------- */
995
+
996
+ export {
997
+ createInputNumberScope,
998
+ InputNumberDecrementButton as DecrementButton,
999
+ InputNumberField as Field,
1000
+ InputNumberIncrementButton as IncrementButton,
1001
+ InputNumber,
1002
+ InputNumberDecrementButton,
1003
+ InputNumberField,
1004
+ InputNumberIncrementButton,
1005
+ InputNumber as Root,
1006
+ };
1007
+
1008
+ export type {
1009
+ InputNumberDecrementButtonProps,
1010
+ InputNumberFieldProps,
1011
+ InputNumberIncrementButtonProps,
1012
+ InputNumberProps,
1013
+ };