@cocso-ui/react 1.11.0 → 1.12.1

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 (93) 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/empty-state/empty-state.d.ts +30 -0
  10. package/dist/cjs/components/empty-state/empty-state.js +50 -0
  11. package/dist/cjs/components/empty-state/empty-state.module.css.js +9 -0
  12. package/dist/cjs/components/file-uploader/file-uploader.d.ts +80 -0
  13. package/dist/cjs/components/file-uploader/file-uploader.js +258 -0
  14. package/dist/cjs/components/file-uploader/file-uploader.module.css.js +9 -0
  15. package/dist/cjs/components/form-field/form-field-context.d.ts +31 -0
  16. package/dist/cjs/components/form-field/form-field-context.js +53 -0
  17. package/dist/cjs/components/form-field/form-field-parts.d.ts +52 -0
  18. package/dist/cjs/components/form-field/form-field-parts.js +109 -0
  19. package/dist/cjs/components/form-field/form-field.d.ts +44 -0
  20. package/dist/cjs/components/form-field/form-field.js +61 -0
  21. package/dist/cjs/components/form-field/form-field.module.css.js +9 -0
  22. package/dist/cjs/components/info-popover/info-popover.d.ts +43 -0
  23. package/dist/cjs/components/info-popover/info-popover.js +57 -0
  24. package/dist/cjs/components/info-popover/info-popover.module.css.js +9 -0
  25. package/dist/cjs/components/loading-overlay/loading-overlay.d.ts +33 -0
  26. package/dist/cjs/components/loading-overlay/loading-overlay.js +68 -0
  27. package/dist/cjs/components/loading-overlay/loading-overlay.module.css.js +9 -0
  28. package/dist/cjs/components/number-input/number-input.d.ts +39 -0
  29. package/dist/cjs/components/number-input/number-input.js +167 -0
  30. package/dist/cjs/components/section-header/section-header.d.ts +48 -0
  31. package/dist/cjs/components/section-header/section-header.js +210 -0
  32. package/dist/cjs/components/section-header/section-header.module.css.js +9 -0
  33. package/dist/cjs/components/segmented-control/segmented-control.d.ts +55 -0
  34. package/dist/cjs/components/segmented-control/segmented-control.js +119 -0
  35. package/dist/cjs/components/segmented-control/segmented-control.module.css.js +9 -0
  36. package/dist/cjs/components/selection-chip/selection-chip.d.ts +22 -0
  37. package/dist/cjs/components/selection-chip/selection-chip.js +32 -0
  38. package/dist/cjs/components/selection-chip/selection-chip.module.css.js +9 -0
  39. package/dist/cjs/components/skeleton/skeleton.d.ts +7 -1
  40. package/dist/cjs/components/skeleton/skeleton.js +20 -2
  41. package/dist/cjs/components/skeleton/skeleton.module.css.js +1 -1
  42. package/dist/cjs/components/summary-card/summary-card.d.ts +47 -0
  43. package/dist/cjs/components/summary-card/summary-card.js +89 -0
  44. package/dist/cjs/components/summary-card/summary-card.module.css.js +9 -0
  45. package/dist/cjs/index.d.ts +15 -2
  46. package/dist/cjs/index.js +24 -0
  47. package/dist/esm/components/breadcrumb/breadcrumb.d.mts +24 -3
  48. package/dist/esm/components/breadcrumb/breadcrumb.mjs +44 -2
  49. package/dist/esm/components/breadcrumb/breadcrumb.module.css.mjs +1 -1
  50. package/dist/esm/components/card/card.d.mts +49 -2
  51. package/dist/esm/components/card/card.mjs +76 -10
  52. package/dist/esm/components/card/card.module.css.mjs +1 -1
  53. package/dist/esm/components/copy-button/copy-button.d.mts +29 -0
  54. package/dist/esm/components/copy-button/copy-button.mjs +65 -0
  55. package/dist/esm/components/empty-state/empty-state.d.mts +30 -0
  56. package/dist/esm/components/empty-state/empty-state.mjs +48 -0
  57. package/dist/esm/components/empty-state/empty-state.module.css.mjs +3 -0
  58. package/dist/esm/components/file-uploader/file-uploader.d.mts +80 -0
  59. package/dist/esm/components/file-uploader/file-uploader.mjs +256 -0
  60. package/dist/esm/components/file-uploader/file-uploader.module.css.mjs +3 -0
  61. package/dist/esm/components/form-field/form-field-context.d.mts +31 -0
  62. package/dist/esm/components/form-field/form-field-context.mjs +49 -0
  63. package/dist/esm/components/form-field/form-field-parts.d.mts +52 -0
  64. package/dist/esm/components/form-field/form-field-parts.mjs +105 -0
  65. package/dist/esm/components/form-field/form-field.d.mts +44 -0
  66. package/dist/esm/components/form-field/form-field.mjs +59 -0
  67. package/dist/esm/components/form-field/form-field.module.css.mjs +3 -0
  68. package/dist/esm/components/info-popover/info-popover.d.mts +43 -0
  69. package/dist/esm/components/info-popover/info-popover.mjs +55 -0
  70. package/dist/esm/components/info-popover/info-popover.module.css.mjs +3 -0
  71. package/dist/esm/components/loading-overlay/loading-overlay.d.mts +33 -0
  72. package/dist/esm/components/loading-overlay/loading-overlay.mjs +66 -0
  73. package/dist/esm/components/loading-overlay/loading-overlay.module.css.mjs +3 -0
  74. package/dist/esm/components/number-input/number-input.d.mts +39 -0
  75. package/dist/esm/components/number-input/number-input.mjs +165 -0
  76. package/dist/esm/components/section-header/section-header.d.mts +48 -0
  77. package/dist/esm/components/section-header/section-header.mjs +208 -0
  78. package/dist/esm/components/section-header/section-header.module.css.mjs +3 -0
  79. package/dist/esm/components/segmented-control/segmented-control.d.mts +55 -0
  80. package/dist/esm/components/segmented-control/segmented-control.mjs +117 -0
  81. package/dist/esm/components/segmented-control/segmented-control.module.css.mjs +3 -0
  82. package/dist/esm/components/selection-chip/selection-chip.d.mts +22 -0
  83. package/dist/esm/components/selection-chip/selection-chip.mjs +30 -0
  84. package/dist/esm/components/selection-chip/selection-chip.module.css.mjs +3 -0
  85. package/dist/esm/components/skeleton/skeleton.d.mts +7 -1
  86. package/dist/esm/components/skeleton/skeleton.mjs +20 -2
  87. package/dist/esm/components/summary-card/summary-card.d.mts +47 -0
  88. package/dist/esm/components/summary-card/summary-card.mjs +87 -0
  89. package/dist/esm/components/summary-card/summary-card.module.css.mjs +3 -0
  90. package/dist/esm/index.css +2 -2
  91. package/dist/esm/index.d.mts +15 -2
  92. package/dist/esm/index.mjs +12 -0
  93. package/package.json +1 -1
@@ -0,0 +1,55 @@
1
+ "use client";
2
+ import { InfoIcon } from '@cocso-ui/react-icons';
3
+ import { cn } from '../../cn.mjs';
4
+ import styles from './info-popover.module.css.mjs';
5
+ import { jsxs, jsx } from 'react/jsx-runtime';
6
+ import { Popover } from '../popover/popover.mjs';
7
+
8
+ /**
9
+ * An info icon that folds away an explanation that never changes — how a
10
+ * total is summed, why a duplicate is refused — so the card it explains stays
11
+ * short.
12
+ *
13
+ * Guidance that changes with the value or the state should stay on the page
14
+ * instead: folding it away hides exactly when it matters.
15
+ *
16
+ * Built on `Popover`, so the panel is a labelled dialog on the popover layer
17
+ * and keeps Popover's `popover-content` / `popover-positioner` hooks, as the
18
+ * pickers do. The icon button is `info-popover-trigger`.
19
+ */
20
+ function InfoPopover({
21
+ children,
22
+ className,
23
+ iconSize = 14,
24
+ label = "More information",
25
+ triggerClassName,
26
+ width = 300,
27
+ side,
28
+ align,
29
+ ...rootProps
30
+ }) {
31
+ const style = {
32
+ "--cocso-info-popover-width": typeof width === "number" ? `${width}px` : width
33
+ };
34
+ return /*#__PURE__*/jsxs(Popover, {
35
+ ...rootProps,
36
+ children: [/*#__PURE__*/jsx(Popover.Trigger, {
37
+ "aria-label": label,
38
+ className: cn(styles.trigger, triggerClassName),
39
+ "data-cocso-component": "info-popover-trigger",
40
+ children: /*#__PURE__*/jsx(InfoIcon, {
41
+ "aria-hidden": "true",
42
+ size: iconSize
43
+ })
44
+ }), /*#__PURE__*/jsx(Popover.Content, {
45
+ align: align,
46
+ "aria-label": label,
47
+ className: cn(styles.content, className),
48
+ side: side,
49
+ style: style,
50
+ children: children
51
+ })]
52
+ });
53
+ }
54
+
55
+ export { InfoPopover };
@@ -0,0 +1,3 @@
1
+ var styles = {"trigger":"info-popover-module_trigger__-FJSm","content":"info-popover-module_content__A4QVA"};
2
+
3
+ export { styles as default };
@@ -0,0 +1,33 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ComponentProps } from 'react';
3
+
4
+ interface LoadingOverlayProps extends Omit<ComponentProps<"div">, "children"> {
5
+ /** Secondary guidance under the message — how long it takes, not to close the window. */
6
+ description?: string;
7
+ /** The main line under the spinner. Also the spinner's accessible name. */
8
+ message?: string;
9
+ /** Renders the overlay only while true; renders nothing otherwise. */
10
+ open: boolean;
11
+ }
12
+ /**
13
+ * A layer that covers the whole viewport while a long task runs.
14
+ *
15
+ * For work that takes several seconds and must not be interrupted — an
16
+ * upload, say. It is `position: fixed`, so in a popup window opened with
17
+ * `window.open` it covers that whole window.
18
+ *
19
+ * It blocks the pointer, not the keyboard: content underneath stays in the
20
+ * tab order. Mark it `inert` yourself if the task must not be re-triggered
21
+ * from the keyboard.
22
+ *
23
+ * It sits on `--cocso-z-index-overlay`, the dialog backdrop layer, so it is
24
+ * drawn under an open `Dialog` panel. Override the z-index through
25
+ * `[data-cocso-component="loading-overlay"]` when it has to cover one.
26
+ *
27
+ * @example
28
+ * <LoadingOverlay open={isPending} message="정산 데이터를 업로드하고 있습니다" />
29
+ */
30
+ declare function LoadingOverlay({ ref, className, open, message, description, ...props }: LoadingOverlayProps): react_jsx_runtime.JSX.Element | null;
31
+
32
+ export { LoadingOverlay };
33
+ export type { LoadingOverlayProps };
@@ -0,0 +1,66 @@
1
+ "use client";
2
+ import { cn } from '../../cn.mjs';
3
+ import styles from './loading-overlay.module.css.mjs';
4
+ import { jsxs, jsx } from 'react/jsx-runtime';
5
+ import { Spinner } from '../spinner/spinner.mjs';
6
+ import { Typography } from '../typography/typography.mjs';
7
+ import { colors } from '../../token/color.mjs';
8
+
9
+ /**
10
+ * A layer that covers the whole viewport while a long task runs.
11
+ *
12
+ * For work that takes several seconds and must not be interrupted — an
13
+ * upload, say. It is `position: fixed`, so in a popup window opened with
14
+ * `window.open` it covers that whole window.
15
+ *
16
+ * It blocks the pointer, not the keyboard: content underneath stays in the
17
+ * tab order. Mark it `inert` yourself if the task must not be re-triggered
18
+ * from the keyboard.
19
+ *
20
+ * It sits on `--cocso-z-index-overlay`, the dialog backdrop layer, so it is
21
+ * drawn under an open `Dialog` panel. Override the z-index through
22
+ * `[data-cocso-component="loading-overlay"]` when it has to cover one.
23
+ *
24
+ * @example
25
+ * <LoadingOverlay open={isPending} message="정산 데이터를 업로드하고 있습니다" />
26
+ */
27
+ function LoadingOverlay({
28
+ ref,
29
+ className,
30
+ open,
31
+ message = "처리 중입니다",
32
+ description,
33
+ ...props
34
+ }) {
35
+ if (!open) {
36
+ return null;
37
+ }
38
+ return /*#__PURE__*/jsxs("div", {
39
+ "aria-busy": "true",
40
+ className: cn(styles.overlay, className),
41
+ "data-cocso-component": "loading-overlay",
42
+ ref: ref,
43
+ ...props,
44
+ children: [/*#__PURE__*/jsx(Spinner, {
45
+ label: message,
46
+ size: "large",
47
+ variant: "primary"
48
+ }), /*#__PURE__*/jsxs("div", {
49
+ className: styles.text,
50
+ children: [/*#__PURE__*/jsx(Typography, {
51
+ color: colors.textPrimary,
52
+ type: "body",
53
+ weight: "medium",
54
+ children: message
55
+ }), description && /*#__PURE__*/jsx(Typography, {
56
+ className: styles.description,
57
+ color: colors.textSecondary,
58
+ size: "small",
59
+ type: "body",
60
+ children: description
61
+ })]
62
+ })]
63
+ });
64
+ }
65
+
66
+ export { LoadingOverlay };
@@ -0,0 +1,3 @@
1
+ var styles = {"overlay":"loading-overlay-module_overlay__Xnl3P","text":"loading-overlay-module_text__1581R","description":"loading-overlay-module_description__o0pJf"};
2
+
3
+ export { styles as default };
@@ -0,0 +1,39 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { InputProps } from '../input/input.mjs';
3
+
4
+ interface NumberInputProps extends Omit<InputProps, "type" | "value" | "defaultValue" | "onChange" | "passwordToggle"> {
5
+ /** Decimal places a user may type. `0`, the default, is a whole amount. */
6
+ maxFractionDigits?: number;
7
+ /** The parsed number on every edit, or `null` when the field is empty. */
8
+ onValueChange: (value: number | null) => void;
9
+ /** The current number. `null` or `undefined` shows an empty field. */
10
+ value: number | null | undefined;
11
+ }
12
+ /**
13
+ * An `Input` for amounts, grouped in thousands as the user types.
14
+ *
15
+ * It is a text field with `inputMode`, not `type="number"`: a native number
16
+ * input cannot display a separator. The caret is put back after the same digit
17
+ * when a comma is inserted or removed in front of it.
18
+ *
19
+ * Defaults to the `small` size and the full width of its container, the shape
20
+ * the field had where it came from; both can be overridden.
21
+ *
22
+ * @example
23
+ * <Controller
24
+ * control={control}
25
+ * name="amount"
26
+ * render={({ field }) => (
27
+ * <NumberInput
28
+ * aria-label="금액"
29
+ * onBlur={field.onBlur}
30
+ * onValueChange={field.onChange}
31
+ * value={field.value}
32
+ * />
33
+ * )}
34
+ * />
35
+ */
36
+ declare function NumberInput({ value, onValueChange, maxFractionDigits, onBlur, size, stretch, ...props }: NumberInputProps): react_jsx_runtime.JSX.Element;
37
+
38
+ export { NumberInput };
39
+ export type { NumberInputProps };
@@ -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,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 };
@@ -0,0 +1,55 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ type SegmentedControlSize = "medium" | "small";
5
+ interface SegmentedControlOption<T extends string> {
6
+ /**
7
+ * One line under the label. When any option has one, every segment grows to
8
+ * two lines so the control keeps a single height.
9
+ */
10
+ description?: string;
11
+ label: string;
12
+ value: T;
13
+ }
14
+ interface SegmentedControlProps<T extends string> {
15
+ /** Names the group. One of this or `aria-labelledby` is required for a screen reader to say what is being chosen. */
16
+ "aria-label"?: string;
17
+ "aria-labelledby"?: string;
18
+ /**
19
+ * Applied to the outermost element: the wrapper when `description` is set,
20
+ * the track otherwise.
21
+ */
22
+ className?: string;
23
+ /** A line under the control that applies to every option. Linked with `aria-describedby`. */
24
+ description?: ReactNode;
25
+ disabled?: boolean;
26
+ /**
27
+ * Sizes each segment to its label instead of splitting the track evenly. The
28
+ * selection indicator is measured and follows the selected segment.
29
+ */
30
+ fitContent?: boolean;
31
+ onChange: (value: T) => void;
32
+ options: readonly SegmentedControlOption<T>[];
33
+ /**
34
+ * `medium` is 32px, the height of a `small` `Input`; `small` is 28px, the
35
+ * height of an `x-small` one.
36
+ */
37
+ size?: SegmentedControlSize;
38
+ /**
39
+ * The selected value. Typed `string` rather than `T` so a caller holding a
40
+ * wider value (`String(field.value)`) does not need a cast; a value matching
41
+ * no option selects nothing.
42
+ */
43
+ value: string;
44
+ }
45
+ /**
46
+ * A single choice among two or three options, drawn as one pill-shaped track.
47
+ *
48
+ * Built on Base UI's radio group, so it is a `radiogroup` of `radio`s: Tab
49
+ * reaches the selected segment and the arrow keys move the selection, the way
50
+ * a set of radio buttons behaves.
51
+ */
52
+ declare function SegmentedControl<T extends string>({ value, options, onChange, description, className, disabled, fitContent, size, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, }: SegmentedControlProps<T>): react_jsx_runtime.JSX.Element;
53
+
54
+ export { SegmentedControl };
55
+ export type { SegmentedControlOption, SegmentedControlProps, SegmentedControlSize };