@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
@@ -0,0 +1,165 @@
1
+ "use client";
2
+ import { useState, useRef, useEffect } from 'react';
3
+ import { jsx } from 'react/jsx-runtime';
4
+ import { Input } from '../input/input.mjs';
5
+
6
+ const NON_NUMERIC = /[^0-9.]/g;
7
+ const ALL_POINTS = /\./g;
8
+ const LEADING_ZEROS = /^0+(?=\d)/;
9
+ const THOUSANDS = /\B(?=(\d{3})+(?!\d))/g;
10
+ const GROUP_SEPARATORS = /,/g;
11
+ const NUMERIC_CHAR = /[0-9.]/;
12
+
13
+ /**
14
+ * Keeps digits and the decimal point, plus a leading `-`.
15
+ *
16
+ * With `maxFractionDigits` 0 the point is dropped; otherwise only the first
17
+ * point survives and the fraction is cut to length. Leading zeros on the
18
+ * integer part go too, so typing after a prefilled `0` gives `5`, not `05`.
19
+ */
20
+ function sanitizeNumeric(raw, maxFractionDigits) {
21
+ const sign = raw.trimStart().startsWith("-") ? "-" : "";
22
+ let cleaned = raw.replace(NON_NUMERIC, "");
23
+ if (maxFractionDigits === 0) {
24
+ cleaned = cleaned.replace(ALL_POINTS, "");
25
+ } else {
26
+ const dot = cleaned.indexOf(".");
27
+ if (dot !== -1) {
28
+ const integer = cleaned.slice(0, dot);
29
+ const fraction = cleaned.slice(dot + 1).replace(ALL_POINTS, "").slice(0, maxFractionDigits);
30
+ cleaned = `${integer}.${fraction}`;
31
+ }
32
+ }
33
+ // "0123" becomes "123"; the zero in "0" and "0.5" stays.
34
+ cleaned = cleaned.replace(LEADING_ZEROS, "");
35
+ return `${sign}${cleaned}`;
36
+ }
37
+
38
+ /** Groups the integer part in thousands. The sign and fraction are kept as they are. */
39
+ function formatWithGrouping(numeric) {
40
+ const sign = numeric.startsWith("-") ? "-" : "";
41
+ const [integer = "", fraction] = numeric.slice(sign.length).split(".");
42
+ const grouped = integer.replace(THOUSANDS, ",");
43
+ return fraction === undefined ? `${sign}${grouped}` : `${sign}${grouped}.${fraction}`;
44
+ }
45
+
46
+ /** Display text to a number; `null` when it is empty or not yet a number. */
47
+ function parseNumeric(display) {
48
+ const numeric = display.replace(GROUP_SEPARATORS, "");
49
+ if (numeric === "" || numeric === "." || numeric === "-") {
50
+ return null;
51
+ }
52
+ const parsed = Number(numeric);
53
+ return Number.isNaN(parsed) ? null : parsed;
54
+ }
55
+ function toDisplay(value) {
56
+ return value == null ? "" : formatWithGrouping(String(value));
57
+ }
58
+ /**
59
+ * An `Input` for amounts, grouped in thousands as the user types.
60
+ *
61
+ * It is a text field with `inputMode`, not `type="number"`: a native number
62
+ * input cannot display a separator. The caret is put back after the same digit
63
+ * when a comma is inserted or removed in front of it.
64
+ *
65
+ * Defaults to the `small` size and the full width of its container, the shape
66
+ * the field had where it came from; both can be overridden.
67
+ *
68
+ * @example
69
+ * <Controller
70
+ * control={control}
71
+ * name="amount"
72
+ * render={({ field }) => (
73
+ * <NumberInput
74
+ * aria-label="금액"
75
+ * onBlur={field.onBlur}
76
+ * onValueChange={field.onChange}
77
+ * value={field.value}
78
+ * />
79
+ * )}
80
+ * />
81
+ */
82
+ function NumberInput({
83
+ value,
84
+ onValueChange,
85
+ maxFractionDigits = 0,
86
+ onBlur,
87
+ size = "small",
88
+ stretch = true,
89
+ ...props
90
+ }) {
91
+ // An external value is formatted as given; the fraction limit applies to
92
+ // what is typed, not to what the caller supplies.
93
+ const [draft, setDraft] = useState(() => toDisplay(value));
94
+ // What this field last reported, to tell its own edits from a caller's reset.
95
+ const lastEmitted = useRef(value ?? null);
96
+ const frame = useRef(null);
97
+
98
+ // Only a value that did not come from this field rewrites the text, so an
99
+ // intermediate state like "1." survives the re-render its own edit causes.
100
+ useEffect(() => {
101
+ const next = value ?? null;
102
+ if (next === lastEmitted.current) {
103
+ return;
104
+ }
105
+ lastEmitted.current = next;
106
+ setDraft(toDisplay(next));
107
+ }, [value]);
108
+ useEffect(() => () => {
109
+ if (frame.current !== null) {
110
+ cancelAnimationFrame(frame.current);
111
+ }
112
+ }, []);
113
+ const handleChange = event => {
114
+ const el = event.currentTarget;
115
+ const caret = el.selectionStart ?? el.value.length;
116
+ // Count the digits before the caret, then put it back after the same digit
117
+ // once the commas have moved.
118
+ const digitsBeforeCaret = el.value.slice(0, caret).replace(NON_NUMERIC, "").length;
119
+ const numeric = sanitizeNumeric(el.value, maxFractionDigits);
120
+ const formatted = formatWithGrouping(numeric);
121
+ setDraft(formatted);
122
+ const parsed = parseNumeric(numeric);
123
+ lastEmitted.current = parsed;
124
+ onValueChange(parsed);
125
+ if (frame.current !== null) {
126
+ cancelAnimationFrame(frame.current);
127
+ }
128
+ frame.current = requestAnimationFrame(() => {
129
+ frame.current = null;
130
+ let position = formatted.startsWith("-") ? 1 : 0;
131
+ let count = 0;
132
+ while (position < formatted.length && count < digitsBeforeCaret) {
133
+ if (NUMERIC_CHAR.test(formatted.charAt(position))) {
134
+ count += 1;
135
+ }
136
+ position += 1;
137
+ }
138
+ if (document.activeElement === el) {
139
+ el.setSelectionRange(position, position);
140
+ }
141
+ });
142
+ };
143
+ const handleBlur = event => {
144
+ // Tidy what typing leaves behind: a trailing point, a lone sign.
145
+ setDraft(previous => {
146
+ const trimmed = previous.endsWith(".") ? previous.slice(0, -1) : previous;
147
+ return trimmed === "-" ? "" : trimmed;
148
+ });
149
+ onBlur?.(event);
150
+ };
151
+ return /*#__PURE__*/jsx(Input, {
152
+ autoComplete: "off",
153
+ "data-cocso-component": "number-input",
154
+ inputMode: maxFractionDigits > 0 ? "decimal" : "numeric",
155
+ size: size,
156
+ stretch: stretch,
157
+ ...props,
158
+ onBlur: handleBlur,
159
+ onChange: handleChange,
160
+ type: "text",
161
+ value: draft
162
+ });
163
+ }
164
+
165
+ export { NumberInput };
@@ -0,0 +1,212 @@
1
+ "use client";
2
+ import { ArrowDropUpIcon, ArrowIOSBackwardIcon, ArrowIOSForwardIcon, ArrowDropDownIcon } from '@cocso-ui/react-icons';
3
+ import { format, startOfMonth, endOfMonth } from 'date-fns';
4
+ import { useState, useRef, useCallback, useEffect } from 'react';
5
+ import styles from './year-month-view.module.css.mjs';
6
+ import { jsxs, jsx } from 'react/jsx-runtime';
7
+ import { Typography } from '../typography/typography.mjs';
8
+ import { Button } from '../button/button.mjs';
9
+
10
+ const MONTHS = Array.from({
11
+ length: 12
12
+ }, (_, month) => month);
13
+ /**
14
+ * The state behind a calendar header that opens a year-and-month grid, shared
15
+ * by DayPicker and DateTimePicker so a date years away is a few clicks in
16
+ * either of them instead of one click per month.
17
+ */
18
+ function useYearMonthView(initial) {
19
+ const [view, setView] = useState("day");
20
+ const [viewDate, setViewDate] = useState(initial);
21
+ // Remounts the calendar after a jump. A live calendar follows its keyboard
22
+ // cursor before `openToDate`, so a fresh mount is the reliable way to land
23
+ // on the picked month.
24
+ const [calendarKey, setCalendarKey] = useState(0);
25
+ // The day grid's height, held by the month grid so the panel does not
26
+ // shrink under the pointer — a panel opened above its trigger would jump.
27
+ const [dayViewHeight, setDayViewHeight] = useState();
28
+ const focusAfterViewChange = useRef(false);
29
+ const titleElement = useRef(null);
30
+ // Set when the day view should take focus but its header is not mounted yet.
31
+ // A remounted calendar draws its header in a later pass when it carries a
32
+ // time list (DateTimePicker), after the view effect has already run.
33
+ const titleFocusPending = useRef(false);
34
+ const currentMonthRef = useRef(null);
35
+ const titleRef = useCallback(element => {
36
+ titleElement.current = element;
37
+ if (element && titleFocusPending.current) {
38
+ titleFocusPending.current = false;
39
+ element.focus();
40
+ }
41
+ }, []);
42
+
43
+ // A view change unmounts the control that had focus. Hand it to the
44
+ // matching control in the new view instead of dropping it on the body.
45
+ useEffect(() => {
46
+ if (!focusAfterViewChange.current) {
47
+ return;
48
+ }
49
+ focusAfterViewChange.current = false;
50
+ if (view === "month") {
51
+ currentMonthRef.current?.focus();
52
+ return;
53
+ }
54
+ const title = titleElement.current;
55
+ if (title?.isConnected) {
56
+ title.focus();
57
+ } else {
58
+ titleFocusPending.current = true;
59
+ }
60
+ }, [view]);
61
+ const changeView = next => {
62
+ focusAfterViewChange.current = true;
63
+ setView(next);
64
+ };
65
+
66
+ /** Back to the day grid on `date`'s month, as when the panel opens. */
67
+ const reset = date => {
68
+ setView("day");
69
+ setViewDate(date);
70
+ setCalendarKey(key => key + 1);
71
+ };
72
+ const openMonths = (date, calendar) => {
73
+ setDayViewHeight(calendar?.getBoundingClientRect().height);
74
+ setViewDate(date);
75
+ changeView("month");
76
+ };
77
+ const jumpTo = (year, month) => {
78
+ setViewDate(new Date(year, month, 1));
79
+ setCalendarKey(key => key + 1);
80
+ changeView("day");
81
+ };
82
+ return {
83
+ calendarKey,
84
+ changeView,
85
+ currentMonthRef,
86
+ dayViewHeight,
87
+ jumpTo,
88
+ openMonths,
89
+ reset,
90
+ setViewDate,
91
+ titleRef,
92
+ view,
93
+ viewDate
94
+ };
95
+ }
96
+ /** The day grid's "2026년 10월 ▾", pressed to open the month grid. */
97
+ function CalendarTitle({
98
+ date,
99
+ locale,
100
+ onPress,
101
+ ref
102
+ }) {
103
+ return /*#__PURE__*/jsxs("button", {
104
+ "aria-label": "Choose year and month",
105
+ className: styles.title,
106
+ onClick: event => onPress(event.currentTarget.closest(".react-datepicker")),
107
+ ref: ref,
108
+ type: "button",
109
+ children: [/*#__PURE__*/jsx(Typography, {
110
+ size: "small",
111
+ type: "body",
112
+ weight: "semibold",
113
+ children: date.toLocaleDateString(locale.code ?? "ko-KR", {
114
+ year: "numeric",
115
+ month: "long"
116
+ })
117
+ }), /*#__PURE__*/jsx(ArrowDropDownIcon, {
118
+ "aria-hidden": true,
119
+ size: 16
120
+ })]
121
+ });
122
+ }
123
+ /** The year's twelve months, with the year paged by the header's arrows. */
124
+ function YearMonthView({
125
+ currentMonthRef,
126
+ hidden,
127
+ locale,
128
+ maxDate,
129
+ minDate,
130
+ minHeight,
131
+ onBack,
132
+ onPick,
133
+ onYearChange,
134
+ selected,
135
+ viewDate
136
+ }) {
137
+ const year = viewDate.getFullYear();
138
+ const monthDisabled = month => {
139
+ const first = startOfMonth(new Date(year, month, 1));
140
+ return minDate !== undefined && endOfMonth(first) < minDate || maxDate !== undefined && first > maxDate;
141
+ };
142
+ return /*#__PURE__*/jsxs("div", {
143
+ className: styles.monthView,
144
+ hidden: hidden,
145
+ style: {
146
+ minHeight
147
+ },
148
+ children: [/*#__PURE__*/jsxs("div", {
149
+ className: styles.monthHeader,
150
+ children: [/*#__PURE__*/jsxs("button", {
151
+ "aria-label": "Back to days",
152
+ className: styles.title,
153
+ onClick: onBack,
154
+ type: "button",
155
+ children: [/*#__PURE__*/jsx(Typography, {
156
+ size: "small",
157
+ type: "body",
158
+ weight: "semibold",
159
+ children: viewDate.toLocaleDateString(locale.code ?? "ko-KR", {
160
+ year: "numeric"
161
+ })
162
+ }), /*#__PURE__*/jsx(ArrowDropUpIcon, {
163
+ "aria-hidden": true,
164
+ size: 16
165
+ })]
166
+ }), /*#__PURE__*/jsxs("div", {
167
+ className: styles.menu,
168
+ children: [/*#__PURE__*/jsx(Button, {
169
+ "aria-label": "Previous year",
170
+ className: styles.arrow,
171
+ disabled: minDate !== undefined && year - 1 < minDate.getFullYear(),
172
+ onClick: () => onYearChange(new Date(year - 1, 0, 1)),
173
+ size: "x-small",
174
+ svgOnly: true,
175
+ type: "button",
176
+ variant: "outline",
177
+ children: /*#__PURE__*/jsx(ArrowIOSBackwardIcon, {})
178
+ }), /*#__PURE__*/jsx(Button, {
179
+ "aria-label": "Next year",
180
+ className: styles.arrow,
181
+ disabled: maxDate !== undefined && year + 1 > maxDate.getFullYear(),
182
+ onClick: () => onYearChange(new Date(year + 1, 0, 1)),
183
+ size: "x-small",
184
+ svgOnly: true,
185
+ type: "button",
186
+ variant: "outline",
187
+ children: /*#__PURE__*/jsx(ArrowIOSForwardIcon, {})
188
+ })]
189
+ })]
190
+ }), /*#__PURE__*/jsx("div", {
191
+ className: styles.monthGrid,
192
+ children: MONTHS.map(month => {
193
+ const current = selected != null && selected.getFullYear() === year && selected.getMonth() === month;
194
+ const isViewed = viewDate.getMonth() === month;
195
+ return /*#__PURE__*/jsx("button", {
196
+ "aria-current": current ? "date" : undefined,
197
+ className: styles.monthCell,
198
+ "data-selected": current || undefined,
199
+ disabled: monthDisabled(month),
200
+ onClick: () => onPick(year, month),
201
+ ref: isViewed ? currentMonthRef : undefined,
202
+ type: "button",
203
+ children: format(new Date(year, month, 1), "LLL", {
204
+ locale
205
+ })
206
+ }, month);
207
+ })
208
+ })]
209
+ });
210
+ }
211
+
212
+ export { CalendarTitle, YearMonthView, useYearMonthView };
@@ -0,0 +1,3 @@
1
+ var styles = {"title":"year-month-view-module_title__xCQjI","monthCell":"year-month-view-module_monthCell__Tju2y","menu":"year-month-view-module_menu__WInY3","arrow":"year-month-view-module_arrow__oIH1T","monthView":"year-month-view-module_monthView__6rbV7","monthHeader":"year-month-view-module_monthHeader__3ZcdS","monthGrid":"year-month-view-module_monthGrid__jGMsB"};
2
+
3
+ export { styles as default };
@@ -0,0 +1,48 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ComponentProps, ReactNode } from 'react';
3
+
4
+ type SectionHeaderLevel = 1 | 2 | 3 | 4 | 5 | 6;
5
+ interface SectionHeaderProps extends Omit<ComponentProps<"div">, "title"> {
6
+ /**
7
+ * Trailing controls — buttons, a menu, a search box. When the header is too
8
+ * narrow to keep `MIN_TITLE_WIDTH` for the title they collapse behind one
9
+ * toggle button.
10
+ */
11
+ actions?: ReactNode;
12
+ /**
13
+ * Collapses `actions` behind a toggle when they would crowd the title.
14
+ * Defaults to `true`.
15
+ */
16
+ collapsible?: boolean;
17
+ /**
18
+ * Opens the collapsed panel on mount. Only meaningful while collapsed; mostly
19
+ * for showing the panel in documentation.
20
+ */
21
+ defaultActionsOpen?: boolean;
22
+ /** Secondary text under the title. */
23
+ description?: ReactNode;
24
+ /** Heading level of the title. Defaults to `2`. */
25
+ level?: SectionHeaderLevel;
26
+ /** Text of the toggle the actions collapse into. Defaults to `"More"`. */
27
+ moreLabel?: string;
28
+ title: ReactNode;
29
+ /** Class for the title element, e.g. to truncate a long title. */
30
+ titleClassName?: string;
31
+ }
32
+ /**
33
+ * The heading row of a section or a card: a title, its actions on the same
34
+ * line, and an optional description under them.
35
+ *
36
+ * When the row gets too narrow for the actions to sit beside the title, they
37
+ * collapse behind a single toggle and open in a panel under it, so the title
38
+ * is never squeezed to nothing.
39
+ *
40
+ * @example
41
+ * <Card>
42
+ * <SectionHeader actions={<Button>Add</Button>} title="Medicines" />
43
+ * </Card>
44
+ */
45
+ declare function SectionHeader({ className, title, titleClassName, description, actions, level, moreLabel, collapsible, defaultActionsOpen, ...props }: SectionHeaderProps): react_jsx_runtime.JSX.Element;
46
+
47
+ export { SectionHeader };
48
+ export type { SectionHeaderLevel, SectionHeaderProps };
@@ -0,0 +1,208 @@
1
+ "use client";
2
+ import { ChevronDownIcon } from '@cocso-ui/react-icons';
3
+ import { useId, useRef, useState, useLayoutEffect, useEffect } from 'react';
4
+ import { cn } from '../../cn.mjs';
5
+ import styles from './section-header.module.css.mjs';
6
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
7
+ import { Typography } from '../typography/typography.mjs';
8
+ import { Button } from '../button/button.mjs';
9
+
10
+ const MIN_TITLE_WIDTH = 120;
11
+ /** The gap between the title and the actions — `--cocso-spacing-5`. */
12
+ const HEADER_GAP = 8;
13
+ /** The least room left between the open panel and the viewport edge. */
14
+ const PANEL_MARGIN = 16;
15
+ /** The gap between the toggle and the panel under it. */
16
+ const PANEL_OFFSET = 4;
17
+
18
+ /**
19
+ * Floating layers an action may have opened from inside the panel — a menu, a
20
+ * dialog. Interacting with one does not close the panel, and Escape belongs
21
+ * to it first.
22
+ */
23
+ const FLOATING_SELECTOR = '[role="menu"],[role="dialog"],[role="alertdialog"],[role="listbox"]';
24
+ function measurePanel(toggle) {
25
+ const rect = toggle.getBoundingClientRect();
26
+ const viewport = document.documentElement.clientWidth;
27
+ const rtl = getComputedStyle(toggle).direction === "rtl";
28
+ return {
29
+ top: rect.bottom + PANEL_OFFSET,
30
+ insetInlineEnd: rtl ? rect.left : viewport - rect.right,
31
+ maxWidth: (rtl ? viewport - rect.left : rect.right) - PANEL_MARGIN
32
+ };
33
+ }
34
+
35
+ /**
36
+ * The actions slot. When it runs out of room it hands its content to a panel
37
+ * that opens under a toggle.
38
+ *
39
+ * The actions are hidden, never unmounted, while the panel is closed, and the
40
+ * panel is the same element as the inline slot. An action that owns a dialog
41
+ * keeps its state through a collapse, and a dialog opened from the panel
42
+ * survives the panel closing. That is also why this is not built on
43
+ * `Popover`: a portal moves the actions to another tree and remounts them.
44
+ * The panel is a disclosure — the toggle carries `aria-expanded` and
45
+ * `aria-controls` — not a dialog.
46
+ */
47
+ function CollapsibleActions({
48
+ actions,
49
+ collapsible,
50
+ defaultOpen,
51
+ moreLabel
52
+ }) {
53
+ const panelId = useId();
54
+ const actionsRef = useRef(null);
55
+ const toggleRef = useRef(null);
56
+ const [collapsed, setCollapsed] = useState(false);
57
+ const [open, setOpen] = useState(defaultOpen);
58
+ const [position, setPosition] = useState(null);
59
+
60
+ // Once collapsed the slot no longer has an inline width to measure, so the
61
+ // width it had while expanded is kept.
62
+ const expandedWidthRef = useRef(0);
63
+ const collapsedRef = useRef(false);
64
+ useLayoutEffect(() => {
65
+ const element = actionsRef.current;
66
+ const header = element?.parentElement;
67
+ if (!(collapsible && header && element)) {
68
+ collapsedRef.current = false;
69
+ setCollapsed(false);
70
+ return;
71
+ }
72
+ const measure = () => {
73
+ if (!collapsedRef.current) {
74
+ expandedWidthRef.current = element.offsetWidth;
75
+ }
76
+ const next = header.clientWidth < expandedWidthRef.current + HEADER_GAP + MIN_TITLE_WIDTH;
77
+ collapsedRef.current = next;
78
+ setCollapsed(next);
79
+ // Widening closes the panel, so narrowing again later does not bring
80
+ // it back by itself.
81
+ if (!next) {
82
+ setOpen(false);
83
+ }
84
+ };
85
+ measure();
86
+ const observer = new ResizeObserver(measure);
87
+ observer.observe(header);
88
+ observer.observe(element);
89
+ return () => observer.disconnect();
90
+ }, [collapsible]);
91
+ const expanded = collapsed && open;
92
+ useLayoutEffect(() => {
93
+ if (expanded && toggleRef.current) {
94
+ setPosition(measurePanel(toggleRef.current));
95
+ } else {
96
+ setPosition(null);
97
+ }
98
+ }, [expanded]);
99
+ useEffect(() => {
100
+ if (!expanded) {
101
+ return;
102
+ }
103
+ const isInside = target => target instanceof Element && (Boolean(actionsRef.current?.contains(target)) || Boolean(toggleRef.current?.contains(target)) || Boolean(target.closest(FLOATING_SELECTOR)));
104
+ const close = event => {
105
+ if (!isInside(event.target)) {
106
+ setOpen(false);
107
+ }
108
+ };
109
+ const closeOnEscape = event => {
110
+ if (event.key === "Escape" && !document.querySelector(FLOATING_SELECTOR)) {
111
+ setOpen(false);
112
+ toggleRef.current?.focus();
113
+ }
114
+ };
115
+ document.addEventListener("click", close);
116
+ document.addEventListener("keydown", closeOnEscape);
117
+ window.addEventListener("scroll", close, true);
118
+ return () => {
119
+ document.removeEventListener("click", close);
120
+ document.removeEventListener("keydown", closeOnEscape);
121
+ window.removeEventListener("scroll", close, true);
122
+ };
123
+ }, [expanded]);
124
+ const style = expanded && position ? position : undefined;
125
+ let state;
126
+ if (collapsed) {
127
+ state = open ? "open" : "closed";
128
+ }
129
+ return /*#__PURE__*/jsxs(Fragment, {
130
+ children: [collapsed && /*#__PURE__*/jsx(Button, {
131
+ "aria-controls": panelId,
132
+ "aria-expanded": open,
133
+ className: styles.toggle,
134
+ onClick: () => setOpen(current => !current),
135
+ ref: toggleRef,
136
+ size: "x-small",
137
+ suffix: /*#__PURE__*/jsx(ChevronDownIcon, {
138
+ size: 14
139
+ }),
140
+ type: "button",
141
+ variant: "outline",
142
+ children: moreLabel
143
+ }), /*#__PURE__*/jsx("div", {
144
+ className: cn(styles.actions, collapsed && styles.panel),
145
+ "data-cocso-component": "section-header-actions",
146
+ "data-state": state,
147
+ hidden: collapsed && !open,
148
+ id: panelId,
149
+ ref: actionsRef,
150
+ style: style,
151
+ children: actions
152
+ })]
153
+ });
154
+ }
155
+
156
+ /**
157
+ * The heading row of a section or a card: a title, its actions on the same
158
+ * line, and an optional description under them.
159
+ *
160
+ * When the row gets too narrow for the actions to sit beside the title, they
161
+ * collapse behind a single toggle and open in a panel under it, so the title
162
+ * is never squeezed to nothing.
163
+ *
164
+ * @example
165
+ * <Card>
166
+ * <SectionHeader actions={<Button>Add</Button>} title="Medicines" />
167
+ * </Card>
168
+ */
169
+ function SectionHeader({
170
+ className,
171
+ title,
172
+ titleClassName,
173
+ description,
174
+ actions,
175
+ level = 2,
176
+ moreLabel = "More",
177
+ collapsible = true,
178
+ defaultActionsOpen = false,
179
+ ...props
180
+ }) {
181
+ const Heading = `h${level}`;
182
+ return /*#__PURE__*/jsxs("div", {
183
+ className: cn(styles.root, className),
184
+ "data-cocso-component": "section-header",
185
+ ...props,
186
+ children: [/*#__PURE__*/jsxs("div", {
187
+ className: styles.row,
188
+ children: [/*#__PURE__*/jsx(Typography, {
189
+ className: cn(styles.title, titleClassName),
190
+ render: /*#__PURE__*/jsx(Heading, {}),
191
+ size: "small",
192
+ type: "heading",
193
+ weight: "bold",
194
+ children: title
195
+ }), actions ? /*#__PURE__*/jsx(CollapsibleActions, {
196
+ actions: actions,
197
+ collapsible: collapsible,
198
+ defaultOpen: defaultActionsOpen,
199
+ moreLabel: moreLabel
200
+ }) : null]
201
+ }), description ? /*#__PURE__*/jsx("p", {
202
+ className: styles.description,
203
+ children: description
204
+ }) : null]
205
+ });
206
+ }
207
+
208
+ export { SectionHeader };
@@ -0,0 +1,3 @@
1
+ var styles = {"root":"section-header-module_root__sOgb3","row":"section-header-module_row__xsVIe","title":"section-header-module_title__7ddbh","description":"section-header-module_description__T8tck","toggle":"section-header-module_toggle__bVZ9N","actions":"section-header-module_actions__kS-uK","panel":"section-header-module_panel__1Zc9C"};
2
+
3
+ export { styles as default };