@edux-design/buttons 0.1.2 → 0.1.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.
package/dist/index.js DELETED
@@ -1,803 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
-
29
- // src/index.js
30
- var index_exports = {};
31
- __export(index_exports, {
32
- Button: () => Button,
33
- ButtonCounter: () => ButtonCounter,
34
- ButtonGroup: () => ButtonGroup,
35
- ButtonGroupItem: () => ButtonGroupItem,
36
- IconButton: () => IconButton,
37
- MenuButton: () => MenuButton
38
- });
39
- module.exports = __toCommonJS(index_exports);
40
-
41
- // src/elements/IconButton.jsx
42
- var import_react = __toESM(require("react"));
43
- var import_utils = require("@edux-design/utils");
44
- var import_jsx_runtime = require("react/jsx-runtime");
45
- var IconButton = (0, import_react.forwardRef)(
46
- ({
47
- icon,
48
- size = "medium",
49
- isSecondary,
50
- isDanger,
51
- isBare,
52
- disabled,
53
- className,
54
- ...props
55
- }, ref) => {
56
- let variant = "primary";
57
- if (isBare) variant = "bare";
58
- if (isSecondary) variant = "secondary";
59
- if (isDanger) variant = "danger";
60
- if (disabled) variant = "disabled";
61
- if (process.env.NODE_ENV !== "production") {
62
- const count = [isSecondary, isDanger, isBare].filter(Boolean).length;
63
- if (count > 1) {
64
- console.warn(
65
- "[IconButton]: Multiple variant props passed. Using the highest priority only."
66
- );
67
- }
68
- }
69
- const defaults = "flex justify-center items-center focus:shadow-focus focus-visible:shadow-focus outline-none transition-all duration-300 ease-in-out rounded-full cursor-pointer";
70
- const sizes = {
71
- small: "h-[36px] w-[36px] text-sm",
72
- medium: "h-[44px] w-[44px] text-base",
73
- large: "h-[56px] w-[56px] text-lg"
74
- };
75
- const variants = {
76
- primary: "border-2 border-border-primary-emphasis bg-bg-primary-base text-white hover:bg-border-focus hover:text-fg-base hover:border-border-emphasis hover:shadow-md active:bg-border-focus active:bg-bg-focus-overlay active:text-fg-base",
77
- secondary: "border-2 border-border-primary-base bg-bg-base text-fg-primary-emphasis hover:shadow-md hover:bg-bg-primary-subtle active:bg-bg-primary-subtle-90",
78
- bare: "bg-bg-base text-fg-primary-emphasis hover:bg-bg-minimal active:bg-bg-minimal-5",
79
- danger: "bg-bg-danger-base border-2 border-border-danger-emphasis text-white hover:bg-bg-danger-base-5 active:bg-bg-danger-base-10",
80
- disabled: "border-2 border-border-subtle bg-bg-inactive text-fg-inactive hover:no-underline cursor-not-allowed"
81
- };
82
- const STYLES = (0, import_utils.cx)(defaults, sizes[size], variants[variant], className);
83
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
84
- "button",
85
- {
86
- role: "button",
87
- type: "button",
88
- "aria-disabled": disabled,
89
- disabled,
90
- className: STYLES,
91
- ref,
92
- ...props,
93
- children: icon
94
- }
95
- );
96
- }
97
- );
98
-
99
- // src/elements/Button.jsx
100
- var import_react2 = __toESM(require("react"));
101
- var import_utils2 = require("@edux-design/utils");
102
- var import_jsx_runtime2 = require("react/jsx-runtime");
103
- var Button = (0, import_react2.forwardRef)(
104
- ({
105
- children,
106
- startIcon,
107
- endIcon,
108
- size = "medium",
109
- isSecondary,
110
- isDanger,
111
- isBare,
112
- isStretched,
113
- disabled,
114
- className,
115
- ...props
116
- }, ref) => {
117
- let variant = "primary";
118
- if (disabled) {
119
- variant = "disabled";
120
- } else if (isDanger && isBare) {
121
- variant = "dangerBare";
122
- } else if (isDanger) {
123
- variant = "danger";
124
- } else if (isSecondary) {
125
- variant = "secondary";
126
- } else if (isBare) {
127
- variant = "bare";
128
- }
129
- if (process.env.NODE_ENV !== "production") {
130
- const count = [isSecondary, isDanger, isBare].filter(Boolean).length;
131
- const hasAllowedCombo = isDanger && isBare && count === 2;
132
- if (count > 1 && !hasAllowedCombo) {
133
- console.warn(
134
- "[Button]: Multiple variant props passed. Using the highest priority only."
135
- );
136
- }
137
- }
138
- const defaults = "flex justify-center items-center gap-8 focus:shadow-focus focus-visible:shadow-focus outline-none transition-all duration-300 ease-in-out cursor-pointer";
139
- const fonts = "text-bold tracking-normal hover:underline hover:underline-offset-(--underline-offset-btn) active:underline active:underline-offset-(--underline-offset-btn)";
140
- const sizes = {
141
- small: "h-[36px] py-8 px-16 rounded-lg text-sm",
142
- medium: "h-[44px] py-8 px-16 rounded-lg text-base",
143
- large: "h-[56px] py-8 px-16 rounded-lg text-lg"
144
- };
145
- const width = isStretched ? "w-full" : "w-fit-content";
146
- const variants = {
147
- primary: "border-2 border-border-primary-emphasis bg-bg-primary-base text-white hover:bg-border-focus hover:text-fg-base hover:border-border-emphasis hover:shadow-md active:bg-border-focus active:bg-bg-focus-overlay active:text-fg-base",
148
- secondary: "border-2 border-border-primary-base bg-bg-base text-fg-primary-emphasis hover:shadow-md hover:bg-bg-primary-subtle active:bg-bg-primary-subtle-90",
149
- bare: "bg-bg-base text-fg-primary-emphasis hover:bg-bg-minimal active:bg-bg-minimal-5",
150
- dangerBare: "bg-bg-base text-fg-danger-emphasis hover:bg-bg-danger-subtle active:bg-bg-danger-subtle",
151
- danger: "bg-bg-danger-base border-2 border-border-danger-emphasis text-white hover:bg-bg-danger-base-5 active:bg-bg-danger-base-10",
152
- disabled: "border-2 border-border-subtle bg-bg-inactive text-fg-inactive hover:no-underline"
153
- };
154
- const STYLES = (0, import_utils2.cx)(defaults, sizes[size], width, fonts, variants[variant]);
155
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
156
- "button",
157
- {
158
- role: "button",
159
- type: "button",
160
- "aria-disabled": disabled,
161
- disabled,
162
- className: STYLES,
163
- ref,
164
- ...props,
165
- children: [
166
- startIcon,
167
- children ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children }) : null,
168
- endIcon
169
- ]
170
- }
171
- );
172
- }
173
- );
174
-
175
- // src/elements/MenuButton.jsx
176
- var import_react4 = __toESM(require("react"));
177
-
178
- // src/util/MenuButtonContext.jsx
179
- var import_react3 = __toESM(require("react"));
180
- var MenuButtonContext = (0, import_react3.createContext)();
181
-
182
- // src/elements/MenuButton.jsx
183
- var import_utils3 = require("@edux-design/utils");
184
-
185
- // ../icons/dist/index.mjs
186
- var import_jsx_runtime3 = require("react/jsx-runtime");
187
- var SvgChevron = (props) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
188
- "svg",
189
- {
190
- xmlns: "http://www.w3.org/2000/svg",
191
- width: "1em",
192
- height: "1em",
193
- fill: "none",
194
- viewBox: "0 0 24 24",
195
- ...props,
196
- children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
197
- "path",
198
- {
199
- fill: "currentColor",
200
- fillRule: "evenodd",
201
- d: "M13.586 12 9.293 7.707a1 1 0 0 1 1.414-1.414l5 5a1 1 0 0 1 0 1.414l-5 5a1 1 0 0 1-1.414-1.414z",
202
- clipRule: "evenodd"
203
- }
204
- )
205
- }
206
- );
207
- var Chevron_default = SvgChevron;
208
-
209
- // src/elements/MenuButton.jsx
210
- var import_jsx_runtime4 = require("react/jsx-runtime");
211
- function MenuButton({ children, size }) {
212
- const SPLIT_BUTTON_STYLES = (0, import_utils3.cx)(
213
- "w-[36px] border-l-[1px] border-l-black flex flex-row justify-center items-center",
214
- {
215
- "h-[32px]": size === "small",
216
- "h-[40px] py-2": size === "medium" || size === void 0
217
- }
218
- );
219
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(MenuButtonContext.Provider, { value: true, children: [
220
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: SPLIT_BUTTON_STYLES, children }),
221
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Chevron_default, {})
222
- ] });
223
- }
224
-
225
- // src/elements/ButtonGroup.jsx
226
- var import_react5 = __toESM(require("react"));
227
- var import_utils4 = require("@edux-design/utils");
228
- var import_jsx_runtime5 = require("react/jsx-runtime");
229
- var SIZE_STYLES = {
230
- small: {
231
- button: "text-xs px-12 py-6 rounded-md "
232
- },
233
- medium: {
234
- button: "text-sm px-12 py-6 rounded-md "
235
- },
236
- large: {
237
- button: "text-base px-12 py-6 rounded-md"
238
- }
239
- };
240
- var isBrowser = typeof window !== "undefined";
241
- var useIsomorphicLayoutEffect = isBrowser ? import_react5.useLayoutEffect : import_react5.useEffect;
242
- var ButtonGroup = (0, import_react5.forwardRef)(function ButtonGroup2({
243
- value,
244
- defaultValue,
245
- onChange,
246
- size = "medium",
247
- isStretched = false,
248
- className,
249
- children,
250
- ...rest
251
- }, forwardedRef) {
252
- const normalizedOptions = (0, import_react5.useMemo)(() => {
253
- const childItems = (0, import_utils4.filterChildrenByDisplayName)(children, "ButtonGroupItem");
254
- return childItems.map((child, index) => {
255
- const props = child.props || {};
256
- const hasValue = props.value !== void 0;
257
- const safeValue = hasValue && props.value !== null ? props.value : index;
258
- return {
259
- ...props,
260
- label: props.label ?? props.children ?? `Option ${index + 1}`,
261
- value: safeValue
262
- };
263
- });
264
- }, [children]);
265
- const sizeStyles = SIZE_STYLES[size] ?? SIZE_STYLES.medium;
266
- const initialValue = value ?? defaultValue ?? (normalizedOptions.length ? normalizedOptions[0].value : void 0);
267
- const [internalValue, setInternalValue] = (0, import_react5.useState)(initialValue);
268
- const isControlled = value !== void 0;
269
- const selectedValue = isControlled ? value : internalValue;
270
- (0, import_react5.useEffect)(() => {
271
- if (!isControlled && defaultValue !== void 0) {
272
- setInternalValue(defaultValue);
273
- }
274
- }, [defaultValue, isControlled]);
275
- (0, import_react5.useEffect)(() => {
276
- if (isControlled) return;
277
- if (normalizedOptions.length && !normalizedOptions.some((option) => option.value === internalValue)) {
278
- setInternalValue(normalizedOptions[0].value);
279
- }
280
- }, [internalValue, isControlled, normalizedOptions]);
281
- const buttonRefs = (0, import_react5.useRef)(/* @__PURE__ */ new Map());
282
- const containerRef = (0, import_react5.useRef)(null);
283
- const indicatorRef = (0, import_react5.useRef)({ width: 0, height: 0, x: 0, y: 0 });
284
- const [indicator, setIndicator] = (0, import_react5.useState)({
285
- width: 0,
286
- height: 0,
287
- x: 0,
288
- y: 0
289
- });
290
- const setContainerRef = (0, import_react5.useCallback)(
291
- (node) => {
292
- containerRef.current = node;
293
- if (typeof forwardedRef === "function") {
294
- forwardedRef(node);
295
- } else if (forwardedRef) {
296
- forwardedRef.current = node;
297
- }
298
- },
299
- [forwardedRef]
300
- );
301
- const registerButtonRef = (0, import_react5.useCallback)((optionValue) => {
302
- return (node) => {
303
- if (!buttonRefs.current) return;
304
- if (node) {
305
- buttonRefs.current.set(optionValue, node);
306
- } else {
307
- buttonRefs.current.delete(optionValue);
308
- }
309
- };
310
- }, []);
311
- const updateIndicator = (0, import_react5.useCallback)(() => {
312
- const container = containerRef.current;
313
- const target = buttonRefs.current.get(selectedValue);
314
- if (!container || !target) {
315
- if (indicatorRef.current.width !== 0) {
316
- indicatorRef.current = { width: 0, height: 0, x: 0, y: 0 };
317
- setIndicator(indicatorRef.current);
318
- }
319
- return;
320
- }
321
- const containerRect = container.getBoundingClientRect();
322
- const targetRect = target.getBoundingClientRect();
323
- const next = {
324
- width: targetRect.width,
325
- height: targetRect.height,
326
- x: targetRect.left - containerRect.left,
327
- y: targetRect.top - containerRect.top
328
- };
329
- const prev = indicatorRef.current;
330
- if (prev.width !== next.width || prev.height !== next.height || prev.x !== next.x || prev.y !== next.y) {
331
- indicatorRef.current = next;
332
- setIndicator(next);
333
- }
334
- }, [selectedValue]);
335
- useIsomorphicLayoutEffect(() => {
336
- updateIndicator();
337
- }, [updateIndicator, normalizedOptions.length, size]);
338
- (0, import_react5.useEffect)(() => {
339
- if (!isBrowser) return;
340
- const handleResize = () => updateIndicator();
341
- window.addEventListener("resize", handleResize);
342
- return () => window.removeEventListener("resize", handleResize);
343
- }, [updateIndicator]);
344
- (0, import_react5.useEffect)(() => {
345
- if (!isBrowser) return;
346
- const node = containerRef.current;
347
- if (!node || typeof ResizeObserver === "undefined") return;
348
- const observer = new ResizeObserver(() => {
349
- updateIndicator();
350
- });
351
- observer.observe(node);
352
- return () => observer.disconnect();
353
- }, [updateIndicator]);
354
- const focusOption = (0, import_react5.useCallback)((option) => {
355
- const node = buttonRefs.current.get(option.value);
356
- if (node) {
357
- node.focus();
358
- }
359
- }, []);
360
- const handleSelect = (0, import_react5.useCallback)(
361
- (option) => {
362
- if (option.disabled || option.value === selectedValue) {
363
- return;
364
- }
365
- if (!isControlled) {
366
- setInternalValue(option.value);
367
- }
368
- if (typeof onChange === "function") {
369
- onChange(option.value, option);
370
- }
371
- },
372
- [isControlled, onChange, selectedValue]
373
- );
374
- const getNextEnabledIndex = (0, import_react5.useCallback)(
375
- (startIndex, direction) => {
376
- if (!normalizedOptions.length) return -1;
377
- let nextIndex = startIndex;
378
- for (let i = 0; i < normalizedOptions.length; i += 1) {
379
- nextIndex = (nextIndex + direction + normalizedOptions.length) % normalizedOptions.length;
380
- if (!normalizedOptions[nextIndex].disabled) {
381
- return nextIndex;
382
- }
383
- }
384
- return startIndex;
385
- },
386
- [normalizedOptions]
387
- );
388
- const findFirstEnabledIndex = (0, import_react5.useCallback)(() => {
389
- return normalizedOptions.findIndex((option) => !option.disabled);
390
- }, [normalizedOptions]);
391
- const findLastEnabledIndex = (0, import_react5.useCallback)(() => {
392
- for (let i = normalizedOptions.length - 1; i >= 0; i -= 1) {
393
- if (!normalizedOptions[i].disabled) {
394
- return i;
395
- }
396
- }
397
- return -1;
398
- }, [normalizedOptions]);
399
- const indicatorVisible = indicator.width > 0 && indicator.height > 0;
400
- const rootClasses = (0, import_utils4.cx)(
401
- "inline-flex h-[44px] rounded-lg border-2 border-border-subtle bg-bg-base/90 p-2 shadow-inner",
402
- isStretched ? "w-full" : "w-auto",
403
- className
404
- );
405
- const trackClasses = (0, import_utils4.cx)(
406
- "relative isolate flex w-full items-center gap-8",
407
- !isStretched && "w-auto"
408
- );
409
- const buttonBaseClasses = (0, import_utils4.cx)(
410
- "relative z-10 h-[36px] inline-flex items-center justify-center gap-2 whitespace-nowrap font-semibold tracking-tight transition-colors duration-200 ease-out focus-visible:outline-none focus-visible:shadow-focus motion-reduce:duration-0",
411
- sizeStyles.button,
412
- isStretched ? "flex-1" : "w-auto"
413
- );
414
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
415
- "div",
416
- {
417
- ...rest,
418
- ref: setContainerRef,
419
- role: "radiogroup",
420
- className: rootClasses,
421
- children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: trackClasses, children: [
422
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
423
- "span",
424
- {
425
- "aria-hidden": "true",
426
- className: (0, import_utils4.cx)(
427
- "pointer-events-none h-[44px] absolute rounded-md border-2 border-border-primary-emphasis bg-bg-base shadow-[0_8px_20px_rgb(22_92_131/0.18)] transition-[transform,width,height] duration-300 ease-out motion-reduce:duration-0",
428
- !indicatorVisible && "opacity-0"
429
- ),
430
- style: {
431
- width: indicator.width || void 0,
432
- height: indicator.height || void 0,
433
- transform: `translate(${indicator.x - 4}px`
434
- }
435
- }
436
- ),
437
- normalizedOptions.map((option, index) => {
438
- const isActive = option.value === selectedValue;
439
- const handleKeyDown = (event) => {
440
- if (option.disabled) return;
441
- if (event.key === "ArrowRight" || event.key === "ArrowDown") {
442
- event.preventDefault();
443
- const nextIndex = getNextEnabledIndex(index, 1);
444
- const nextOption = normalizedOptions[nextIndex];
445
- if (nextOption && nextOption !== option) {
446
- focusOption(nextOption);
447
- handleSelect(nextOption);
448
- }
449
- } else if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
450
- event.preventDefault();
451
- const prevIndex = getNextEnabledIndex(index, -1);
452
- const prevOption = normalizedOptions[prevIndex];
453
- if (prevOption && prevOption !== option) {
454
- focusOption(prevOption);
455
- handleSelect(prevOption);
456
- }
457
- } else if (event.key === "Home") {
458
- event.preventDefault();
459
- const firstIndex = findFirstEnabledIndex();
460
- const firstOption = firstIndex >= 0 ? normalizedOptions[firstIndex] : void 0;
461
- if (firstOption && firstOption !== option) {
462
- focusOption(firstOption);
463
- handleSelect(firstOption);
464
- }
465
- } else if (event.key === "End") {
466
- event.preventDefault();
467
- const lastIndex = findLastEnabledIndex();
468
- const lastOption = lastIndex >= 0 ? normalizedOptions[lastIndex] : void 0;
469
- if (lastOption && lastOption !== option) {
470
- focusOption(lastOption);
471
- handleSelect(lastOption);
472
- }
473
- }
474
- };
475
- const buttonClasses = (0, import_utils4.cx)(
476
- buttonBaseClasses,
477
- option.disabled ? "cursor-not-allowed text-fg-inactive opacity-60" : "cursor-pointer text-fg-subtle hover:text-fg-base",
478
- isActive && "text-fg-primary-emphasis",
479
- !isActive && "hover:bg-bg-minimal-5 active:bg-bg-inactive"
480
- );
481
- const hasStartAdornment = option.startAdornment !== void 0 && option.startAdornment !== null;
482
- const endAdornment = option.endAdornment ?? option.endIcon;
483
- const hasEndAdornment = endAdornment !== void 0 && endAdornment !== null;
484
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
485
- "button",
486
- {
487
- ref: registerButtonRef(option.value),
488
- type: "button",
489
- role: "radio",
490
- "aria-checked": isActive,
491
- disabled: option.disabled,
492
- className: buttonClasses,
493
- onClick: () => handleSelect(option),
494
- onKeyDown: handleKeyDown,
495
- "data-state": isActive ? "active" : "inactive",
496
- children: [
497
- hasStartAdornment && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
498
- "span",
499
- {
500
- "aria-hidden": "true",
501
- className: "flex items-center text-current",
502
- children: option.startAdornment
503
- }
504
- ),
505
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "flex items-center gap-1 leading-none text-current", children: option.label ?? `Option ${index + 1}` }),
506
- hasEndAdornment && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
507
- "span",
508
- {
509
- "aria-hidden": "true",
510
- className: "flex items-center text-current",
511
- children: endAdornment
512
- }
513
- )
514
- ]
515
- },
516
- String(option.value)
517
- );
518
- })
519
- ] })
520
- }
521
- );
522
- });
523
-
524
- // src/elements/ButtonGroupItem.jsx
525
- var import_react6 = __toESM(require("react"));
526
- function ButtonGroupItem(_props) {
527
- return null;
528
- }
529
- ButtonGroupItem.displayName = "ButtonGroupItem";
530
-
531
- // src/elements/ButtonCounter.jsx
532
- var import_react7 = __toESM(require("react"));
533
- var import_utils5 = require("@edux-design/utils");
534
- var import_jsx_runtime6 = require("react/jsx-runtime");
535
- var ButtonCounter = (0, import_react7.forwardRef)(function ButtonCounter2({
536
- value,
537
- defaultValue = 1,
538
- onChange,
539
- min = Number.NEGATIVE_INFINITY,
540
- max = Number.POSITIVE_INFINITY,
541
- step = 1,
542
- children,
543
- size = "medium",
544
- isSecondary,
545
- isDanger,
546
- isBare,
547
- isStretched,
548
- disabled,
549
- isEditable = false,
550
- inputProps,
551
- decrementIcon,
552
- incrementIcon,
553
- startIcon,
554
- endIcon,
555
- decrementButtonProps,
556
- incrementButtonProps,
557
- className,
558
- ...rest
559
- }, ref) {
560
- const isControlled = value !== void 0;
561
- const [internalValue, setInternalValue] = (0, import_react7.useState)(defaultValue);
562
- const currentValue = isControlled ? value : internalValue;
563
- const clampedValue = (0, import_react7.useMemo)(() => {
564
- const safe = Number.isFinite(currentValue) ? currentValue : defaultValue;
565
- return Math.min(max, Math.max(min, safe));
566
- }, [currentValue, defaultValue, max, min]);
567
- const commitValue = (0, import_react7.useCallback)(
568
- (next) => {
569
- const clamped = Math.min(max, Math.max(min, next));
570
- if (!isControlled) setInternalValue(clamped);
571
- onChange == null ? void 0 : onChange(clamped);
572
- },
573
- [isControlled, max, min, onChange]
574
- );
575
- const decrementDisabled = !!disabled || !Number.isFinite(clampedValue) || clampedValue - step < min;
576
- const incrementDisabled = !!disabled || !Number.isFinite(clampedValue) || clampedValue + step > max;
577
- let variant = "primary";
578
- if (disabled) {
579
- variant = "disabled";
580
- } else if (isDanger && isBare) {
581
- variant = "dangerBare";
582
- } else if (isDanger) {
583
- variant = "danger";
584
- } else if (isSecondary) {
585
- variant = "secondary";
586
- } else if (isBare) {
587
- variant = "bare";
588
- }
589
- if (process.env.NODE_ENV !== "production") {
590
- const count = [isSecondary, isDanger, isBare].filter(Boolean).length;
591
- const hasAllowedCombo = isDanger && isBare && count === 2;
592
- if (count > 1 && !hasAllowedCombo) {
593
- console.warn(
594
- "[ButtonCounter]: Multiple variant props passed. Using the highest priority only."
595
- );
596
- }
597
- if (step <= 0) {
598
- console.warn("[ButtonCounter]: step should be > 0.");
599
- }
600
- }
601
- const CONTROL_SIZES = {
602
- small: {
603
- button: "h-[36px] w-[36px] text-sm",
604
- display: "h-[36px] min-w-[48px] px-12 text-sm"
605
- },
606
- medium: {
607
- button: "h-[44px] w-[44px] text-base",
608
- display: "h-[44px] min-w-[56px] px-12 text-base"
609
- },
610
- large: {
611
- button: "h-[56px] w-[56px] text-lg",
612
- display: "h-[56px] min-w-[72px] px-16 text-lg"
613
- }
614
- };
615
- const defaultsButton = "flex justify-center items-center focus:shadow-focus focus-visible:shadow-focus outline-none transition-all duration-300 ease-in-out cursor-pointer";
616
- const defaultsDisplay = "flex items-center justify-center text-bold tracking-normal";
617
- const width = isStretched ? "w-full" : "w-fit-content";
618
- const variants = {
619
- primary: {
620
- button: "border-2 border-border-primary-emphasis bg-bg-primary-base text-white hover:bg-border-focus hover:text-fg-base hover:border-border-emphasis hover:shadow-md active:bg-border-focus active:bg-bg-focus-overlay active:text-fg-base",
621
- display: "border-y-2 border-x-2 border-border-primary-emphasis bg-bg-primary-base text-white"
622
- },
623
- secondary: {
624
- button: "border-2 border-border-primary-base bg-bg-base text-fg-primary-emphasis hover:shadow-md hover:bg-bg-primary-subtle active:bg-bg-primary-subtle-90",
625
- display: "border-y-2 border-x-2 border-border-primary-base bg-bg-base text-fg-primary-emphasis"
626
- },
627
- bare: {
628
- button: "bg-bg-base text-fg-primary-emphasis hover:bg-bg-minimal active:bg-bg-minimal-5",
629
- display: "bg-bg-base text-fg-primary-emphasis"
630
- },
631
- dangerBare: {
632
- button: "bg-bg-base text-fg-danger-emphasis hover:bg-bg-danger-subtle active:bg-bg-danger-subtle",
633
- display: "bg-bg-base text-fg-danger-emphasis"
634
- },
635
- danger: {
636
- button: "bg-bg-danger-base border-2 border-border-danger-emphasis text-white hover:bg-bg-danger-base-5 active:bg-bg-danger-base-10",
637
- display: "border-y-2 border-x-2 border-border-danger-emphasis bg-bg-danger-base text-white"
638
- },
639
- disabled: {
640
- button: "border-2 border-border-subtle bg-bg-inactive text-fg-inactive hover:no-underline cursor-not-allowed",
641
- display: "border-y-2 border-x-2 border-border-subtle bg-bg-inactive text-fg-inactive"
642
- }
643
- };
644
- const sizeStyles = CONTROL_SIZES[size] ?? CONTROL_SIZES.medium;
645
- const variantStyles = variants[variant] ?? variants.primary;
646
- const leftButtonStyles = (0, import_utils5.cx)(
647
- defaultsButton,
648
- sizeStyles.button,
649
- "rounded-l-lg",
650
- "relative z-10",
651
- variantStyles.button,
652
- variant !== "bare" && variant !== "dangerBare" ? "border-r-0" : null
653
- );
654
- const rightButtonStyles = (0, import_utils5.cx)(
655
- defaultsButton,
656
- sizeStyles.button,
657
- "rounded-r-lg",
658
- variantStyles.button,
659
- variant !== "bare" && variant !== "dangerBare" ? "border-l-0" : null
660
- );
661
- const displayStyles = (0, import_utils5.cx)(
662
- defaultsDisplay,
663
- sizeStyles.display,
664
- "relative z-0",
665
- variantStyles.display,
666
- isStretched ? "flex-1" : null
667
- );
668
- const effectiveDecrementIcon = decrementIcon ?? startIcon ?? "-";
669
- const effectiveIncrementIcon = incrementIcon ?? endIcon ?? "+";
670
- const [draftValue, setDraftValue] = (0, import_react7.useState)(String(clampedValue));
671
- const [isEditing, setIsEditing] = (0, import_react7.useState)(false);
672
- const inputRef = (0, import_react7.useRef)(null);
673
- (0, import_react7.useEffect)(() => {
674
- if (isEditing) return;
675
- setDraftValue(String(clampedValue));
676
- }, [clampedValue, isEditing]);
677
- const commitDraft = (0, import_react7.useCallback)(() => {
678
- const trimmed = String(draftValue).trim();
679
- if (trimmed === "") {
680
- setDraftValue(String(clampedValue));
681
- return;
682
- }
683
- const next = Number(trimmed);
684
- if (!Number.isFinite(next)) {
685
- setDraftValue(String(clampedValue));
686
- return;
687
- }
688
- commitValue(next);
689
- }, [clampedValue, commitValue, draftValue]);
690
- return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
691
- "div",
692
- {
693
- ref,
694
- className: (0, import_utils5.cx)("inline-flex items-stretch", width, className),
695
- ...rest,
696
- children: [
697
- /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
698
- "button",
699
- {
700
- ...decrementButtonProps,
701
- type: "button",
702
- "aria-label": (decrementButtonProps == null ? void 0 : decrementButtonProps["aria-label"]) ?? "Decrease",
703
- "aria-disabled": decrementDisabled,
704
- disabled: decrementDisabled,
705
- className: (0, import_utils5.cx)(
706
- leftButtonStyles,
707
- decrementDisabled && variant !== "disabled" ? variants.disabled.button : null,
708
- decrementButtonProps == null ? void 0 : decrementButtonProps.className
709
- ),
710
- onClick: (event) => {
711
- var _a;
712
- (_a = decrementButtonProps == null ? void 0 : decrementButtonProps.onClick) == null ? void 0 : _a.call(decrementButtonProps, event);
713
- if (event.defaultPrevented) return;
714
- commitValue(clampedValue - step);
715
- },
716
- children: effectiveDecrementIcon
717
- }
718
- ),
719
- isEditable && children == null ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
720
- "input",
721
- {
722
- ...inputProps,
723
- ref: inputRef,
724
- type: "number",
725
- inputMode: "numeric",
726
- disabled: !!disabled,
727
- "aria-label": (inputProps == null ? void 0 : inputProps["aria-label"]) ?? "Value",
728
- className: (0, import_utils5.cx)(
729
- displayStyles,
730
- "w-[44px] text-center outline-none bg-transparent text-fg-base",
731
- "[appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:m-0 [&::-webkit-inner-spin-button]:m-0",
732
- "focus:shadow-focus focus-visible:shadow-focus focus:z-15 focus-within:z-15",
733
- inputProps == null ? void 0 : inputProps.className
734
- ),
735
- value: draftValue,
736
- onChange: (event) => {
737
- var _a;
738
- (_a = inputProps == null ? void 0 : inputProps.onChange) == null ? void 0 : _a.call(inputProps, event);
739
- if (event.defaultPrevented) return;
740
- setDraftValue(event.target.value);
741
- },
742
- onFocus: (event) => {
743
- var _a;
744
- (_a = inputProps == null ? void 0 : inputProps.onFocus) == null ? void 0 : _a.call(inputProps, event);
745
- if (event.defaultPrevented) return;
746
- setIsEditing(true);
747
- },
748
- onBlur: (event) => {
749
- var _a;
750
- (_a = inputProps == null ? void 0 : inputProps.onBlur) == null ? void 0 : _a.call(inputProps, event);
751
- if (event.defaultPrevented) return;
752
- setIsEditing(false);
753
- commitDraft();
754
- },
755
- onKeyDown: (event) => {
756
- var _a;
757
- (_a = inputProps == null ? void 0 : inputProps.onKeyDown) == null ? void 0 : _a.call(inputProps, event);
758
- if (event.defaultPrevented) return;
759
- if (event.key === "Enter") {
760
- event.currentTarget.blur();
761
- }
762
- if (event.key === "Escape") {
763
- setDraftValue(String(clampedValue));
764
- event.currentTarget.blur();
765
- }
766
- }
767
- }
768
- ) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: (0, import_utils5.cx)(displayStyles, "select-none"), children: children ?? clampedValue }),
769
- /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
770
- "button",
771
- {
772
- ...incrementButtonProps,
773
- type: "button",
774
- "aria-label": (incrementButtonProps == null ? void 0 : incrementButtonProps["aria-label"]) ?? "Increase",
775
- "aria-disabled": incrementDisabled,
776
- disabled: incrementDisabled,
777
- className: (0, import_utils5.cx)(
778
- rightButtonStyles,
779
- incrementDisabled && variant !== "disabled" ? variants.disabled.button : null,
780
- incrementButtonProps == null ? void 0 : incrementButtonProps.className
781
- ),
782
- onClick: (event) => {
783
- var _a;
784
- (_a = incrementButtonProps == null ? void 0 : incrementButtonProps.onClick) == null ? void 0 : _a.call(incrementButtonProps, event);
785
- if (event.defaultPrevented) return;
786
- commitValue(clampedValue + step);
787
- },
788
- children: effectiveIncrementIcon
789
- }
790
- )
791
- ]
792
- }
793
- );
794
- });
795
- // Annotate the CommonJS export names for ESM import in node:
796
- 0 && (module.exports = {
797
- Button,
798
- ButtonCounter,
799
- ButtonGroup,
800
- ButtonGroupItem,
801
- IconButton,
802
- MenuButton
803
- });