@cocso-ui/react 1.10.1 → 1.12.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 (103) hide show
  1. package/dist/cjs/components/breadcrumb/breadcrumb.d.ts +24 -3
  2. package/dist/cjs/components/breadcrumb/breadcrumb.js +44 -2
  3. package/dist/cjs/components/breadcrumb/breadcrumb.module.css.js +2 -2
  4. package/dist/cjs/components/card/card.d.ts +49 -2
  5. package/dist/cjs/components/card/card.js +76 -10
  6. package/dist/cjs/components/card/card.module.css.js +2 -2
  7. package/dist/cjs/components/copy-button/copy-button.d.ts +29 -0
  8. package/dist/cjs/components/copy-button/copy-button.js +67 -0
  9. package/dist/cjs/components/date-time-picker/date-time-picker.js +99 -76
  10. package/dist/cjs/components/day-picker/day-picker.js +24 -137
  11. package/dist/cjs/components/day-picker/day-picker.module.css.js +2 -2
  12. package/dist/cjs/components/empty-state/empty-state.d.ts +30 -0
  13. package/dist/cjs/components/empty-state/empty-state.js +50 -0
  14. package/dist/cjs/components/empty-state/empty-state.module.css.js +9 -0
  15. package/dist/cjs/components/file-uploader/file-uploader.d.ts +80 -0
  16. package/dist/cjs/components/file-uploader/file-uploader.js +258 -0
  17. package/dist/cjs/components/file-uploader/file-uploader.module.css.js +9 -0
  18. package/dist/cjs/components/form-field/form-field-context.d.ts +31 -0
  19. package/dist/cjs/components/form-field/form-field-context.js +53 -0
  20. package/dist/cjs/components/form-field/form-field-parts.d.ts +52 -0
  21. package/dist/cjs/components/form-field/form-field-parts.js +109 -0
  22. package/dist/cjs/components/form-field/form-field.d.ts +44 -0
  23. package/dist/cjs/components/form-field/form-field.js +61 -0
  24. package/dist/cjs/components/form-field/form-field.module.css.js +9 -0
  25. package/dist/cjs/components/info-popover/info-popover.d.ts +43 -0
  26. package/dist/cjs/components/info-popover/info-popover.js +57 -0
  27. package/dist/cjs/components/info-popover/info-popover.module.css.js +9 -0
  28. package/dist/cjs/components/loading-overlay/loading-overlay.d.ts +33 -0
  29. package/dist/cjs/components/loading-overlay/loading-overlay.js +68 -0
  30. package/dist/cjs/components/loading-overlay/loading-overlay.module.css.js +9 -0
  31. package/dist/cjs/components/number-input/number-input.d.ts +39 -0
  32. package/dist/cjs/components/number-input/number-input.js +167 -0
  33. package/dist/cjs/components/picker/year-month-view.js +216 -0
  34. package/dist/cjs/components/picker/year-month-view.module.css.js +9 -0
  35. package/dist/cjs/components/section-header/section-header.d.ts +48 -0
  36. package/dist/cjs/components/section-header/section-header.js +210 -0
  37. package/dist/cjs/components/section-header/section-header.module.css.js +9 -0
  38. package/dist/cjs/components/segmented-control/segmented-control.d.ts +55 -0
  39. package/dist/cjs/components/segmented-control/segmented-control.js +119 -0
  40. package/dist/cjs/components/segmented-control/segmented-control.module.css.js +9 -0
  41. package/dist/cjs/components/selection-chip/selection-chip.d.ts +22 -0
  42. package/dist/cjs/components/selection-chip/selection-chip.js +32 -0
  43. package/dist/cjs/components/selection-chip/selection-chip.module.css.js +9 -0
  44. package/dist/cjs/components/skeleton/skeleton.d.ts +7 -1
  45. package/dist/cjs/components/skeleton/skeleton.js +20 -2
  46. package/dist/cjs/components/skeleton/skeleton.module.css.js +1 -1
  47. package/dist/cjs/components/summary-card/summary-card.d.ts +47 -0
  48. package/dist/cjs/components/summary-card/summary-card.js +89 -0
  49. package/dist/cjs/components/summary-card/summary-card.module.css.js +9 -0
  50. package/dist/cjs/index.d.ts +15 -2
  51. package/dist/cjs/index.js +24 -0
  52. package/dist/esm/components/breadcrumb/breadcrumb.d.mts +24 -3
  53. package/dist/esm/components/breadcrumb/breadcrumb.mjs +44 -2
  54. package/dist/esm/components/breadcrumb/breadcrumb.module.css.mjs +1 -1
  55. package/dist/esm/components/card/card.d.mts +49 -2
  56. package/dist/esm/components/card/card.mjs +76 -10
  57. package/dist/esm/components/card/card.module.css.mjs +1 -1
  58. package/dist/esm/components/copy-button/copy-button.d.mts +29 -0
  59. package/dist/esm/components/copy-button/copy-button.mjs +65 -0
  60. package/dist/esm/components/date-time-picker/date-time-picker.mjs +99 -76
  61. package/dist/esm/components/day-picker/day-picker.mjs +27 -140
  62. package/dist/esm/components/day-picker/day-picker.module.css.mjs +1 -1
  63. package/dist/esm/components/empty-state/empty-state.d.mts +30 -0
  64. package/dist/esm/components/empty-state/empty-state.mjs +48 -0
  65. package/dist/esm/components/empty-state/empty-state.module.css.mjs +3 -0
  66. package/dist/esm/components/file-uploader/file-uploader.d.mts +80 -0
  67. package/dist/esm/components/file-uploader/file-uploader.mjs +256 -0
  68. package/dist/esm/components/file-uploader/file-uploader.module.css.mjs +3 -0
  69. package/dist/esm/components/form-field/form-field-context.d.mts +31 -0
  70. package/dist/esm/components/form-field/form-field-context.mjs +49 -0
  71. package/dist/esm/components/form-field/form-field-parts.d.mts +52 -0
  72. package/dist/esm/components/form-field/form-field-parts.mjs +105 -0
  73. package/dist/esm/components/form-field/form-field.d.mts +44 -0
  74. package/dist/esm/components/form-field/form-field.mjs +59 -0
  75. package/dist/esm/components/form-field/form-field.module.css.mjs +3 -0
  76. package/dist/esm/components/info-popover/info-popover.d.mts +43 -0
  77. package/dist/esm/components/info-popover/info-popover.mjs +55 -0
  78. package/dist/esm/components/info-popover/info-popover.module.css.mjs +3 -0
  79. package/dist/esm/components/loading-overlay/loading-overlay.d.mts +33 -0
  80. package/dist/esm/components/loading-overlay/loading-overlay.mjs +66 -0
  81. package/dist/esm/components/loading-overlay/loading-overlay.module.css.mjs +3 -0
  82. package/dist/esm/components/number-input/number-input.d.mts +39 -0
  83. package/dist/esm/components/number-input/number-input.mjs +165 -0
  84. package/dist/esm/components/picker/year-month-view.mjs +212 -0
  85. package/dist/esm/components/picker/year-month-view.module.css.mjs +3 -0
  86. package/dist/esm/components/section-header/section-header.d.mts +48 -0
  87. package/dist/esm/components/section-header/section-header.mjs +208 -0
  88. package/dist/esm/components/section-header/section-header.module.css.mjs +3 -0
  89. package/dist/esm/components/segmented-control/segmented-control.d.mts +55 -0
  90. package/dist/esm/components/segmented-control/segmented-control.mjs +117 -0
  91. package/dist/esm/components/segmented-control/segmented-control.module.css.mjs +3 -0
  92. package/dist/esm/components/selection-chip/selection-chip.d.mts +22 -0
  93. package/dist/esm/components/selection-chip/selection-chip.mjs +30 -0
  94. package/dist/esm/components/selection-chip/selection-chip.module.css.mjs +3 -0
  95. package/dist/esm/components/skeleton/skeleton.d.mts +7 -1
  96. package/dist/esm/components/skeleton/skeleton.mjs +20 -2
  97. package/dist/esm/components/summary-card/summary-card.d.mts +47 -0
  98. package/dist/esm/components/summary-card/summary-card.mjs +87 -0
  99. package/dist/esm/components/summary-card/summary-card.module.css.mjs +3 -0
  100. package/dist/esm/index.css +2 -2
  101. package/dist/esm/index.d.mts +15 -2
  102. package/dist/esm/index.mjs +12 -0
  103. package/package.json +1 -1
@@ -1,19 +1,15 @@
1
1
  "use client";
2
- import { ArrowDropUpIcon, ArrowIOSBackwardIcon, ArrowIOSForwardIcon, ArrowDropDownIcon } from '@cocso-ui/react-icons';
3
- import { format, startOfMonth, endOfMonth } from 'date-fns';
2
+ import { ArrowIOSBackwardIcon, ArrowIOSForwardIcon } from '@cocso-ui/react-icons';
3
+ import { format } from 'date-fns';
4
4
  import { ko } from 'date-fns/locale';
5
- import { useState, useRef, useEffect } from 'react';
5
+ import { useState } from 'react';
6
6
  import DatePicker from 'react-datepicker';
7
7
  import { cn } from '../../cn.mjs';
8
+ import { useYearMonthView, YearMonthView, CalendarTitle } from '../picker/year-month-view.mjs';
8
9
  import styles from './day-picker.module.css.mjs';
9
10
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
10
11
  import { Popover } from '../popover/popover.mjs';
11
12
  import { Button } from '../button/button.mjs';
12
- import { Typography } from '../typography/typography.mjs';
13
-
14
- const MONTHS = Array.from({
15
- length: 12
16
- }, (_, month) => month);
17
13
 
18
14
  // Where the calendar opens: the value's month, else today's, pulled inside
19
15
  // the bounds so an empty picker with a future `minDate` does not open on a
@@ -47,54 +43,17 @@ function DayPicker({
47
43
  const [open, setOpen] = useState(false);
48
44
  // The header's month opens a year-and-month grid in the same panel, so a
49
45
  // date years away is a few clicks instead of one click per month.
50
- const [view, setView] = useState("day");
51
- const [viewDate, setViewDate] = useState(() => initialViewDate(value, minDate, maxDate));
52
- // Remounts the calendar after a jump. A live calendar follows its keyboard
53
- // cursor before `openToDate`, so a fresh mount is the reliable way to land
54
- // on the picked month.
55
- const [calendarKey, setCalendarKey] = useState(0);
56
- // The day grid's height, held by the month grid so the panel does not
57
- // shrink under the pointer — a panel opened above its trigger would jump.
58
- const [dayViewHeight, setDayViewHeight] = useState();
59
- const focusAfterViewChange = useRef(false);
60
- const titleRef = useRef(null);
61
- const currentMonthRef = useRef(null);
62
-
63
- // A view change unmounts the control that had focus. Hand it to the
64
- // matching control in the new view instead of dropping it on the body.
65
- useEffect(() => {
66
- if (!focusAfterViewChange.current) {
67
- return;
68
- }
69
- focusAfterViewChange.current = false;
70
- (view === "month" ? currentMonthRef : titleRef).current?.focus();
71
- }, [view]);
46
+ const calendar = useYearMonthView(() => initialViewDate(value, minDate, maxDate));
72
47
  const handleOpenChange = next => {
73
48
  setOpen(next);
74
49
  if (next) {
75
- setView("day");
76
- setViewDate(initialViewDate(value, minDate, maxDate));
77
- setCalendarKey(key => key + 1);
50
+ calendar.reset(initialViewDate(value, minDate, maxDate));
78
51
  }
79
52
  };
80
53
  const handleChange = date => {
81
54
  onValueChange?.(date);
82
55
  setOpen(false);
83
56
  };
84
- const changeView = next => {
85
- focusAfterViewChange.current = true;
86
- setView(next);
87
- };
88
- const jumpTo = (year, month) => {
89
- setViewDate(new Date(year, month, 1));
90
- setCalendarKey(key => key + 1);
91
- changeView("day");
92
- };
93
- const year = viewDate.getFullYear();
94
- const monthDisabled = month => {
95
- const first = startOfMonth(new Date(year, month, 1));
96
- return minDate !== undefined && endOfMonth(first) < minDate || maxDate !== undefined && first > maxDate;
97
- };
98
57
  return /*#__PURE__*/jsx("div", {
99
58
  className: cn(styles.root, className),
100
59
  ref: ref,
@@ -114,75 +73,20 @@ function DayPicker({
114
73
  }), /*#__PURE__*/jsxs(Popover.Content, {
115
74
  "aria-label": "Select date",
116
75
  className: styles.content,
117
- children: [/*#__PURE__*/jsxs("div", {
118
- className: styles.monthView,
119
- hidden: view !== "month",
120
- style: {
121
- minHeight: dayViewHeight
122
- },
123
- children: [/*#__PURE__*/jsxs("div", {
124
- className: styles.monthHeader,
125
- children: [/*#__PURE__*/jsxs("button", {
126
- "aria-label": "Back to days",
127
- className: styles.title,
128
- onClick: () => changeView("day"),
129
- type: "button",
130
- children: [/*#__PURE__*/jsx(Typography, {
131
- size: "small",
132
- type: "body",
133
- weight: "semibold",
134
- children: viewDate.toLocaleDateString(locale.code ?? "ko-KR", {
135
- year: "numeric"
136
- })
137
- }), /*#__PURE__*/jsx(ArrowDropUpIcon, {
138
- "aria-hidden": true,
139
- size: 16
140
- })]
141
- }), /*#__PURE__*/jsxs("div", {
142
- className: styles.menu,
143
- children: [/*#__PURE__*/jsx(Button, {
144
- "aria-label": "Previous year",
145
- className: styles.arrow,
146
- disabled: minDate !== undefined && year - 1 < minDate.getFullYear(),
147
- onClick: () => setViewDate(new Date(year - 1, 0, 1)),
148
- size: "x-small",
149
- svgOnly: true,
150
- type: "button",
151
- variant: "outline",
152
- children: /*#__PURE__*/jsx(ArrowIOSBackwardIcon, {})
153
- }), /*#__PURE__*/jsx(Button, {
154
- "aria-label": "Next year",
155
- className: styles.arrow,
156
- disabled: maxDate !== undefined && year + 1 > maxDate.getFullYear(),
157
- onClick: () => setViewDate(new Date(year + 1, 0, 1)),
158
- size: "x-small",
159
- svgOnly: true,
160
- type: "button",
161
- variant: "outline",
162
- children: /*#__PURE__*/jsx(ArrowIOSForwardIcon, {})
163
- })]
164
- })]
165
- }), /*#__PURE__*/jsx("div", {
166
- className: styles.monthGrid,
167
- children: MONTHS.map(month => {
168
- const current = value !== undefined && value.getFullYear() === year && value.getMonth() === month;
169
- const isViewed = viewDate.getMonth() === month;
170
- return /*#__PURE__*/jsx("button", {
171
- "aria-current": current ? "date" : undefined,
172
- className: styles.monthCell,
173
- "data-selected": current || undefined,
174
- disabled: monthDisabled(month),
175
- onClick: () => jumpTo(year, month),
176
- ref: isViewed ? currentMonthRef : undefined,
177
- type: "button",
178
- children: format(new Date(year, month, 1), "LLL", {
179
- locale
180
- })
181
- }, month);
182
- })
183
- })]
76
+ children: [/*#__PURE__*/jsx(YearMonthView, {
77
+ currentMonthRef: calendar.currentMonthRef,
78
+ hidden: calendar.view !== "month",
79
+ locale: locale,
80
+ maxDate: maxDate,
81
+ minDate: minDate,
82
+ minHeight: calendar.dayViewHeight,
83
+ onBack: () => calendar.changeView("day"),
84
+ onPick: calendar.jumpTo,
85
+ onYearChange: calendar.setViewDate,
86
+ selected: value,
87
+ viewDate: calendar.viewDate
184
88
  }), /*#__PURE__*/jsx("div", {
185
- hidden: view !== "day",
89
+ hidden: calendar.view !== "day",
186
90
  children: /*#__PURE__*/jsx(DatePicker, {
187
91
  dateFormat: dateFormat,
188
92
  dayClassName: date => {
@@ -207,8 +111,8 @@ function DayPicker({
207
111
  maxDate: maxDate,
208
112
  minDate: minDate,
209
113
  onChange: handleChange,
210
- onMonthChange: setViewDate,
211
- openToDate: viewDate,
114
+ onMonthChange: calendar.setViewDate,
115
+ openToDate: calendar.viewDate,
212
116
  renderCustomHeader: ({
213
117
  date,
214
118
  decreaseMonth,
@@ -216,28 +120,11 @@ function DayPicker({
216
120
  prevMonthButtonDisabled,
217
121
  nextMonthButtonDisabled
218
122
  }) => /*#__PURE__*/jsxs(Fragment, {
219
- children: [/*#__PURE__*/jsxs("button", {
220
- "aria-label": "Choose year and month",
221
- className: styles.title,
222
- onClick: event => {
223
- setDayViewHeight(event.currentTarget.closest(".react-datepicker")?.getBoundingClientRect().height);
224
- setViewDate(date);
225
- changeView("month");
226
- },
227
- ref: titleRef,
228
- type: "button",
229
- children: [/*#__PURE__*/jsx(Typography, {
230
- size: "small",
231
- type: "body",
232
- weight: "semibold",
233
- children: date.toLocaleDateString(locale.code ?? "ko-KR", {
234
- year: "numeric",
235
- month: "long"
236
- })
237
- }), /*#__PURE__*/jsx(ArrowDropDownIcon, {
238
- "aria-hidden": true,
239
- size: 16
240
- })]
123
+ children: [/*#__PURE__*/jsx(CalendarTitle, {
124
+ date: date,
125
+ locale: locale,
126
+ onPress: panel => calendar.openMonths(date, panel),
127
+ ref: calendar.titleRef
241
128
  }), /*#__PURE__*/jsxs("div", {
242
129
  className: styles.menu,
243
130
  children: [/*#__PURE__*/jsx(Button, {
@@ -265,7 +152,7 @@ function DayPicker({
265
152
  }),
266
153
  selected: value,
267
154
  showPopperArrow: false
268
- }, calendarKey)
155
+ }, calendar.calendarKey)
269
156
  })]
270
157
  })]
271
158
  })
@@ -1,3 +1,3 @@
1
- var styles = {"root":"day-picker-module_root__QVrbb","content":"day-picker-module_content__34qUu","menu":"day-picker-module_menu__2eMt0","arrow":"day-picker-module_arrow__p9QLq","title":"day-picker-module_title__UokL8","monthCell":"day-picker-module_monthCell__lbR2S","monthView":"day-picker-module_monthView__tPMlr","monthHeader":"day-picker-module_monthHeader__mAlm5","monthGrid":"day-picker-module_monthGrid__14EcZ","sunday":"day-picker-module_sunday__nVavs","saturday":"day-picker-module_saturday__ZYoB9"};
1
+ var styles = {"root":"day-picker-module_root__QVrbb","content":"day-picker-module_content__34qUu","menu":"day-picker-module_menu__2eMt0","arrow":"day-picker-module_arrow__p9QLq","sunday":"day-picker-module_sunday__nVavs","saturday":"day-picker-module_saturday__ZYoB9"};
2
2
 
3
3
  export { styles as default };
@@ -0,0 +1,30 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ComponentProps, ReactNode } from 'react';
3
+
4
+ interface EmptyStateProps extends Omit<ComponentProps<"div">, "title"> {
5
+ /** A button placed under the text. Usually a single "add" action. */
6
+ action?: ReactNode;
7
+ /**
8
+ * Rendered above the title, in the same frame — a `Spinner` while loading,
9
+ * or an illustration. Loading, failure and empty share one frame so the
10
+ * three states stand in the same place at the same size.
11
+ */
12
+ children?: ReactNode;
13
+ /** Why being empty is expected here, or what to do next. */
14
+ description?: ReactNode;
15
+ /** One bold line that says what is missing. */
16
+ title?: ReactNode;
17
+ }
18
+ /**
19
+ * The empty state of a region that is not a table.
20
+ *
21
+ * Inside a table, use the table's own empty row — it has to span the columns.
22
+ * Outside one (a list inside a card, a section of a popup), bare text floating
23
+ * in the layout reads as "something went wrong". The dashed frame marks out
24
+ * the place the content would occupy, so the screen says first that being
25
+ * empty is normal.
26
+ */
27
+ declare function EmptyState({ ref, className, title, description, action, children, ...props }: EmptyStateProps): react_jsx_runtime.JSX.Element;
28
+
29
+ export { EmptyState };
30
+ export type { EmptyStateProps };
@@ -0,0 +1,48 @@
1
+ import { cn } from '../../cn.mjs';
2
+ import styles from './empty-state.module.css.mjs';
3
+ import { jsxs, jsx } from 'react/jsx-runtime';
4
+ import { Typography } from '../typography/typography.mjs';
5
+ import { colors } from '../../token/color.mjs';
6
+
7
+ /**
8
+ * The empty state of a region that is not a table.
9
+ *
10
+ * Inside a table, use the table's own empty row — it has to span the columns.
11
+ * Outside one (a list inside a card, a section of a popup), bare text floating
12
+ * in the layout reads as "something went wrong". The dashed frame marks out
13
+ * the place the content would occupy, so the screen says first that being
14
+ * empty is normal.
15
+ */
16
+ function EmptyState({
17
+ ref,
18
+ className,
19
+ title,
20
+ description,
21
+ action,
22
+ children,
23
+ ...props
24
+ }) {
25
+ return /*#__PURE__*/jsxs("div", {
26
+ className: cn(styles.root, className),
27
+ "data-cocso-component": "empty-state",
28
+ ref: ref,
29
+ ...props,
30
+ children: [children, title && /*#__PURE__*/jsx(Typography, {
31
+ size: "small",
32
+ type: "body",
33
+ weight: "medium",
34
+ children: title
35
+ }), description && /*#__PURE__*/jsx(Typography, {
36
+ className: styles.description,
37
+ color: colors.textSecondary,
38
+ size: "x-small",
39
+ type: "body",
40
+ children: description
41
+ }), action && /*#__PURE__*/jsx("div", {
42
+ className: styles.action,
43
+ children: action
44
+ })]
45
+ });
46
+ }
47
+
48
+ export { EmptyState };
@@ -0,0 +1,3 @@
1
+ var styles = {"root":"empty-state-module_root__UNTr-","description":"empty-state-module_description__muQc4","action":"empty-state-module_action__OEm3v"};
2
+
3
+ export { styles as default };
@@ -0,0 +1,80 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ComponentProps, ReactNode, Ref } from 'react';
3
+
4
+ /** One chosen file. The `File` is kept so the caller can upload it later. */
5
+ interface FileItem {
6
+ file: File;
7
+ name: string;
8
+ size: number;
9
+ }
10
+ /** Why a file was turned away. */
11
+ type FileUploaderRejectionReason = "type" | "size" | "count";
12
+ interface FileUploaderRejection {
13
+ /** The file that was turned away. */
14
+ file: File;
15
+ /** The message that describes it, already resolved from `messages`. */
16
+ message: string;
17
+ reason: FileUploaderRejectionReason;
18
+ }
19
+ /** Every word the component says. Each one is optional; the rest keep the default. */
20
+ interface FileUploaderMessages {
21
+ /** The prompt in the one-line (`compact`) drop zone. */
22
+ compactPrompt?: ReactNode;
23
+ /** The `(max N MB)` hint shown when no `sizeHint` is given. */
24
+ maxSizeHint?: (maxSizeMb: number) => ReactNode;
25
+ /** The prompt in the default drop zone. */
26
+ prompt?: ReactNode;
27
+ /** A file arrived after `maxFiles` was reached. */
28
+ rejectCount?: (maxFiles: number) => string;
29
+ /** A file is larger than `maxSize`. */
30
+ rejectSize?: (file: File, maxSizeMb: number) => string;
31
+ /** A file does not match `accept`. */
32
+ rejectType?: (file: File) => string;
33
+ /** Accessible name of the remove button on each row. */
34
+ remove?: (fileName: string) => string;
35
+ }
36
+ interface FileUploaderProps extends Omit<ComponentProps<"input">, "type" | "size" | "ref"> {
37
+ /**
38
+ * One-line drop zone. A table row that holds an uploader grows by the full
39
+ * zone's height otherwise; this lays the prompt out on a single line.
40
+ */
41
+ compact?: boolean;
42
+ /** The chosen files (controlled). */
43
+ files: FileItem[];
44
+ /** Most files that may be chosen in total. Default 2. */
45
+ maxFiles?: number;
46
+ /** Largest single file in bytes. Default 3 MB. */
47
+ maxSize?: number;
48
+ /** Override any of the default (Korean) words. */
49
+ messages?: FileUploaderMessages;
50
+ /** Called with the next list whenever files are added or removed. */
51
+ onFilesChange: (files: FileItem[]) => void;
52
+ /**
53
+ * Called with every file turned away by one pick or drop. When given, the
54
+ * default error toasts are not shown — the caller reports them instead.
55
+ */
56
+ onReject?: (rejections: FileUploaderRejection[]) => void;
57
+ /** Called after a row's remove button takes a file out. */
58
+ onRemove?: (fileName: string) => void;
59
+ /** Forwarded to the underlying `<input type="file">`. */
60
+ ref?: Ref<HTMLInputElement>;
61
+ /** Replaces the `(max N MB)` hint. Still shown while disabled. */
62
+ sizeHint?: ReactNode;
63
+ }
64
+ /**
65
+ * Drag-and-drop or pick files, listed underneath as `FileRow`s.
66
+ *
67
+ * The drop zone is a `<label>` around a real, visually hidden
68
+ * `<input type="file">`, so Tab reaches the input, Enter and Space open the
69
+ * picker natively, and the zone's prompt is the input's accessible name. The
70
+ * zone is drawn focused while the input is. It disappears once `maxFiles` is
71
+ * reached (or one file is chosen when `multiple` is false).
72
+ *
73
+ * Files that fail `accept`, `maxSize` or `maxFiles` are turned away and
74
+ * reported — through `onReject` when given, otherwise as error toasts (which
75
+ * need a `Toaster` mounted).
76
+ */
77
+ declare function FileUploader({ ref, className, style, files, onFilesChange, onRemove, onReject, maxFiles, maxSize, sizeHint, compact, messages: messagesProp, multiple, accept, disabled, onChange, ...props }: FileUploaderProps): react_jsx_runtime.JSX.Element;
78
+
79
+ export { FileUploader };
80
+ export type { FileItem, FileUploaderMessages, FileUploaderProps, FileUploaderRejection, FileUploaderRejectionReason };
@@ -0,0 +1,256 @@
1
+ "use client";
2
+ import { PlusIcon, CloseIcon } from '@cocso-ui/react-icons';
3
+ import { useState, useRef } from 'react';
4
+ import { cn } from '../../cn.mjs';
5
+ import styles from './file-uploader.module.css.mjs';
6
+ import { jsxs, jsx } from 'react/jsx-runtime';
7
+ import { Typography } from '../typography/typography.mjs';
8
+ import { FileRow } from '../file-row/file-row.mjs';
9
+ import { Button } from '../button/button.mjs';
10
+ import { toast } from 'sonner';
11
+
12
+ const DEFAULT_MESSAGES = {
13
+ compactPrompt: "끌어다 놓거나 클릭해 등록",
14
+ prompt: "파일을 여기에 끌어다 놓거나, 클릭하여 파일을 선택해주세요",
15
+ maxSizeHint: maxSizeMb => `(최대 ${maxSizeMb}MB)`,
16
+ remove: fileName => `${fileName} 삭제`,
17
+ rejectCount: maxFiles => `최대 ${maxFiles}개의 파일만 업로드할 수 있습니다.`,
18
+ rejectSize: (file, maxSizeMb) => `${file.name}은(는) ${maxSizeMb}MB를 초과합니다.`,
19
+ rejectType: file => `${file.name}은(는) 허용되지 않는 파일 타입입니다.`
20
+ };
21
+ const BYTES_PER_MB = 1024 * 1024;
22
+ const DEFAULT_MAX_SIZE = 3 * BYTES_PER_MB;
23
+ function matchesAccept(file, accept) {
24
+ if (!accept) {
25
+ return true;
26
+ }
27
+ return accept.split(",").map(type => type.trim()).filter(Boolean).some(type => {
28
+ if (type.endsWith("/*")) {
29
+ return file.type.startsWith(type.slice(0, -1));
30
+ }
31
+ return file.type === type || file.name.toLowerCase().endsWith(type.toLowerCase());
32
+ });
33
+ }
34
+
35
+ /**
36
+ * Drag-and-drop or pick files, listed underneath as `FileRow`s.
37
+ *
38
+ * The drop zone is a `<label>` around a real, visually hidden
39
+ * `<input type="file">`, so Tab reaches the input, Enter and Space open the
40
+ * picker natively, and the zone's prompt is the input's accessible name. The
41
+ * zone is drawn focused while the input is. It disappears once `maxFiles` is
42
+ * reached (or one file is chosen when `multiple` is false).
43
+ *
44
+ * Files that fail `accept`, `maxSize` or `maxFiles` are turned away and
45
+ * reported — through `onReject` when given, otherwise as error toasts (which
46
+ * need a `Toaster` mounted).
47
+ */
48
+ function FileUploader({
49
+ ref,
50
+ className,
51
+ style,
52
+ files,
53
+ onFilesChange,
54
+ onRemove,
55
+ onReject,
56
+ maxFiles = 2,
57
+ maxSize = DEFAULT_MAX_SIZE,
58
+ sizeHint,
59
+ compact = false,
60
+ messages: messagesProp,
61
+ multiple = true,
62
+ accept,
63
+ disabled = false,
64
+ onChange,
65
+ ...props
66
+ }) {
67
+ const messages = {
68
+ ...DEFAULT_MESSAGES,
69
+ ...messagesProp
70
+ };
71
+ const [isDragActive, setIsDragActive] = useState(false);
72
+ const zoneRef = useRef(null);
73
+ const maxSizeMb = Math.round(maxSize / BYTES_PER_MB);
74
+
75
+ // Names and sizes can repeat, so each `File` gets an id the first time it
76
+ // is seen. The caller keeps the same `File` across renders, so the id holds.
77
+ const keys = useRef(new WeakMap());
78
+ const nextKey = useRef(0);
79
+ const keyFor = file => {
80
+ let key = keys.current.get(file);
81
+ if (key === undefined) {
82
+ key = nextKey.current++;
83
+ keys.current.set(file, key);
84
+ }
85
+ return key;
86
+ };
87
+ const addFiles = incoming => {
88
+ const rejections = [];
89
+ const accepted = [];
90
+ for (const file of Array.from(incoming)) {
91
+ if (!matchesAccept(file, accept)) {
92
+ rejections.push({
93
+ file,
94
+ reason: "type",
95
+ message: messages.rejectType(file)
96
+ });
97
+ } else if (file.size > maxSize) {
98
+ rejections.push({
99
+ file,
100
+ reason: "size",
101
+ message: messages.rejectSize(file, maxSizeMb)
102
+ });
103
+ } else {
104
+ accepted.push(file);
105
+ }
106
+ }
107
+ const remaining = Math.max(0, maxFiles - files.length);
108
+ const added = accepted.slice(0, remaining);
109
+ for (const file of accepted.slice(remaining)) {
110
+ rejections.push({
111
+ file,
112
+ reason: "count",
113
+ message: messages.rejectCount(maxFiles)
114
+ });
115
+ }
116
+ if (rejections.length > 0) {
117
+ if (onReject) {
118
+ onReject(rejections);
119
+ } else {
120
+ // One toast per distinct message: ten files over the count limit
121
+ // say so once, not ten times.
122
+ for (const message of new Set(rejections.map(r => r.message))) {
123
+ toast.error(message);
124
+ }
125
+ }
126
+ }
127
+ if (added.length > 0) {
128
+ onFilesChange([...files, ...added.map(file => ({
129
+ file,
130
+ name: file.name,
131
+ size: file.size
132
+ }))]);
133
+ }
134
+ };
135
+ const handleDragEnter = event => {
136
+ event.preventDefault();
137
+ event.stopPropagation();
138
+ if (!disabled) {
139
+ setIsDragActive(true);
140
+ }
141
+ };
142
+ const handleDragOver = event => {
143
+ event.preventDefault();
144
+ event.stopPropagation();
145
+ };
146
+ const handleDragLeave = event => {
147
+ event.preventDefault();
148
+ event.stopPropagation();
149
+ const next = event.relatedTarget;
150
+ if (!(next && zoneRef.current?.contains(next))) {
151
+ setIsDragActive(false);
152
+ }
153
+ };
154
+ const handleDrop = event => {
155
+ event.preventDefault();
156
+ event.stopPropagation();
157
+ setIsDragActive(false);
158
+ if (disabled) {
159
+ return;
160
+ }
161
+ if (event.dataTransfer?.files) {
162
+ addFiles(event.dataTransfer.files);
163
+ }
164
+ };
165
+ const handleRemove = index => {
166
+ const removed = files[index];
167
+ onFilesChange(files.filter((_, i) => i !== index));
168
+ if (removed) {
169
+ onRemove?.(removed.name);
170
+ }
171
+ };
172
+ const hideZone = files.length >= maxFiles || !multiple && files.length >= 1;
173
+ const hint = sizeHint ?? messages.maxSizeHint(maxSizeMb);
174
+ const prompt = compact ? messages.compactPrompt : messages.prompt;
175
+ return /*#__PURE__*/jsxs("div", {
176
+ className: cn(styles.uploader, className),
177
+ "data-cocso-component": "file-uploader",
178
+ style: style,
179
+ children: [!hideZone &&
180
+ /*#__PURE__*/
181
+ // biome-ignore lint/a11y/noNoninteractiveElementInteractions: drag and drop has no keyboard equivalent; the wrapped input is the keyboard path
182
+ jsxs("label", {
183
+ className: cn(styles.zone, compact && styles.compact),
184
+ "data-cocso-component": "file-uploader-dropzone",
185
+ "data-compact": compact || undefined,
186
+ "data-disabled": disabled || undefined,
187
+ "data-drag-active": isDragActive || undefined,
188
+ onDragEnter: handleDragEnter,
189
+ onDragLeave: handleDragLeave,
190
+ onDragOver: handleDragOver,
191
+ onDrop: handleDrop,
192
+ ref: zoneRef,
193
+ children: [/*#__PURE__*/jsx("input", {
194
+ accept: accept,
195
+ className: styles.input,
196
+ disabled: disabled,
197
+ multiple: multiple,
198
+ ref: ref,
199
+ type: "file",
200
+ ...props,
201
+ onChange: event => {
202
+ onChange?.(event);
203
+ if (event.target.files) {
204
+ addFiles(event.target.files);
205
+ }
206
+ event.target.value = "";
207
+ }
208
+ }), compact && !disabled && /*#__PURE__*/jsx(PlusIcon, {
209
+ "aria-hidden": true,
210
+ className: styles.icon,
211
+ size: 20
212
+ }), /*#__PURE__*/jsxs(Typography, {
213
+ className: styles.text,
214
+ render: /*#__PURE__*/jsx("span", {}),
215
+ size: "x-small",
216
+ type: "body",
217
+ children: [!disabled && /*#__PURE__*/jsx("span", {
218
+ className: styles.prompt,
219
+ children: prompt
220
+ }), !disabled && (compact ? " · " : " "), /*#__PURE__*/jsx("span", {
221
+ className: styles.hint,
222
+ children: hint
223
+ })]
224
+ }), !(compact || disabled) && /*#__PURE__*/jsx(PlusIcon, {
225
+ "aria-hidden": true,
226
+ className: styles.icon,
227
+ size: 20
228
+ })]
229
+ }), files.length > 0 && /*#__PURE__*/jsx("ul", {
230
+ className: styles.list,
231
+ "data-cocso-component": "file-uploader-list",
232
+ children: files.map((item, index) => /*#__PURE__*/jsx("li", {
233
+ className: styles.item,
234
+ children: /*#__PURE__*/jsx(FileRow, {
235
+ actions: /*#__PURE__*/jsx(Button, {
236
+ "aria-label": messages.remove(item.name),
237
+ disabled: disabled,
238
+ onClick: () => handleRemove(index),
239
+ size: "x-small",
240
+ svgOnly: true,
241
+ type: "button",
242
+ variant: "ghost",
243
+ children: /*#__PURE__*/jsx(CloseIcon, {
244
+ size: 14
245
+ })
246
+ }),
247
+ disabled: disabled,
248
+ name: item.name,
249
+ stretch: true
250
+ })
251
+ }, keyFor(item.file)))
252
+ })]
253
+ });
254
+ }
255
+
256
+ export { FileUploader };
@@ -0,0 +1,3 @@
1
+ var styles = {"uploader":"file-uploader-module_uploader__H6Mi3","zone":"file-uploader-module_zone__-lnp-","input":"file-uploader-module_input__wft8r","compact":"file-uploader-module_compact__s-Ynq","text":"file-uploader-module_text__hTQbR","prompt":"file-uploader-module_prompt__bvvKg","icon":"file-uploader-module_icon__7Pt-y","list":"file-uploader-module_list__neAsl","item":"file-uploader-module_item__p8Jns"};
2
+
3
+ export { styles as default };