@overdoser/react-toolkit 0.6.0 → 0.8.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.
- package/AGENTS.md +6 -0
- package/CHANGELOG.md +82 -0
- package/components/ActionButton/ActionButton.d.ts +105 -0
- package/components/ActionButton/ActionButton.js +211 -0
- package/components/ActionButton/ActionButton.module.scss.js +47 -0
- package/components/ActionButton/CopyButton.d.ts +31 -0
- package/components/ActionButton/CopyButton.js +66 -0
- package/components/ActionButton/index.d.ts +4 -0
- package/components/Alert/Alert.js +82 -0
- package/components/Alert/Alert.module.scss.js +25 -0
- package/components/Avatar/Avatar.js +57 -0
- package/components/Avatar/Avatar.module.scss.js +43 -0
- package/components/Badge/Badge.js +38 -0
- package/components/Badge/Badge.module.scss.js +31 -0
- package/components/Breadcrumbs/Breadcrumbs.js +49 -0
- package/components/Breadcrumbs/Breadcrumbs.module.scss.js +17 -0
- package/components/Button/Button.js +57 -0
- package/components/Button/Button.module.scss.js +47 -0
- package/components/Chart/AreaChart.js +244 -0
- package/components/Chart/Axis.js +45 -0
- package/components/Chart/BarChart.js +353 -0
- package/components/Chart/Chart.module.scss.js +61 -0
- package/components/Chart/ChartContainer.js +41 -0
- package/components/Chart/ChartLegend.js +31 -0
- package/components/Chart/ChartTooltip.js +42 -0
- package/components/Chart/LineChart.js +236 -0
- package/components/Chart/PieChart.js +248 -0
- package/components/Chart/RadarChart.js +221 -0
- package/components/Chart/Sparkline.js +69 -0
- package/components/Chart/TradingChart.js +732 -0
- package/components/Chart/scales.js +39 -0
- package/components/Chart/trading/indicators.js +68 -0
- package/components/Chart/trading/period.js +71 -0
- package/components/Chart/useChartDimensions.js +23 -0
- package/components/Divider/Divider.js +36 -0
- package/components/Divider/Divider.module.scss.js +31 -0
- package/components/Draggable/Draggable.js +155 -0
- package/components/Draggable/Draggable.module.scss.js +17 -0
- package/components/Drawer/Drawer.js +126 -0
- package/components/Drawer/Drawer.module.scss.js +117 -0
- package/components/Dropdown/Dropdown.js +142 -0
- package/components/Dropdown/Dropdown.module.scss.js +35 -0
- package/components/Dropdown/DropdownItem.js +31 -0
- package/components/Dropzone/Dropzone.js +107 -0
- package/components/Dropzone/Dropzone.module.scss.js +21 -0
- package/components/Form/Form.js +36 -0
- package/components/Form/Form.module.scss.js +29 -0
- package/components/Form/FormField.js +76 -0
- package/components/Form/FormRow.js +9 -0
- package/components/Form/formFieldDefaults.js +5 -0
- package/components/Link/Link.js +59 -0
- package/components/Link/Link.module.scss.js +16 -0
- package/components/List/List.js +46 -0
- package/components/List/List.module.scss.js +21 -0
- package/components/Modal/Modal.js +109 -0
- package/components/Modal/Modal.module.scss.js +31 -0
- package/components/Pagination/Pagination.js +84 -0
- package/components/Pagination/Pagination.module.scss.js +23 -0
- package/components/Pagination/paginationRange.js +24 -0
- package/components/Popover/Popover.js +138 -0
- package/components/Popover/Popover.module.scss.js +21 -0
- package/components/Skeleton/Skeleton.js +50 -0
- package/components/Skeleton/Skeleton.module.scss.js +25 -0
- package/components/Spinner/Spinner.js +25 -0
- package/components/Spinner/Spinner.module.scss.js +20 -0
- package/components/Table/Table.d.ts +61 -6
- package/components/Table/Table.js +284 -0
- package/components/Table/Table.module.scss.js +59 -0
- package/components/Table/useTableSort.d.ts +7 -5
- package/components/Table/useTableSort.js +44 -0
- package/components/Tabs/Tabs.js +163 -0
- package/components/Tabs/Tabs.module.scss.js +53 -0
- package/components/Timer/Timer.js +159 -0
- package/components/Timer/Timer.module.scss.js +55 -0
- package/components/Toast/Toast.js +133 -0
- package/components/Toast/Toast.module.scss.js +62 -0
- package/components/Toast/toastStore.js +62 -0
- package/components/ToggleIconButton/ToggleIconButton.d.ts +62 -0
- package/components/ToggleIconButton/ToggleIconButton.js +58 -0
- package/components/ToggleIconButton/ToggleIconButton.module.scss.js +27 -0
- package/components/ToggleIconButton/index.d.ts +2 -0
- package/components/Tooltip/Tooltip.js +86 -0
- package/components/Tooltip/Tooltip.module.scss.js +30 -0
- package/components/Typography/Typography.js +37 -0
- package/components/Typography/Typography.module.scss.js +53 -0
- package/components/inputs/Checkbox/Checkbox.js +50 -0
- package/components/inputs/Checkbox/Checkbox.module.scss.js +15 -0
- package/components/inputs/Checkbox/CheckboxGroup.js +111 -0
- package/components/inputs/Checkbox/CheckboxGroup.module.scss.js +31 -0
- package/components/inputs/DatePicker/DatePicker.js +229 -0
- package/components/inputs/DatePicker/DatePicker.module.scss.js +45 -0
- package/components/inputs/DatePicker/dateUtils.js +41 -0
- package/components/inputs/Input/Input.js +50 -0
- package/components/inputs/Input/Input.module.scss.js +25 -0
- package/components/inputs/NumberInput/NumberInput.js +134 -0
- package/components/inputs/NumberInput/NumberInput.module.scss.js +25 -0
- package/components/inputs/Radio/Radio.js +68 -0
- package/components/inputs/Radio/Radio.module.scss.js +13 -0
- package/components/inputs/Select/Select.js +520 -0
- package/components/inputs/Select/Select.module.scss.js +69 -0
- package/components/inputs/Slider/Slider.js +128 -0
- package/components/inputs/Slider/Slider.module.scss.js +15 -0
- package/components/inputs/Textarea/Textarea.js +58 -0
- package/components/inputs/Textarea/Textarea.module.scss.js +25 -0
- package/hooks/useClickOutside.js +15 -0
- package/hooks/useFocusTrap.js +39 -0
- package/hooks/useKeyboard.js +19 -0
- package/hooks/usePortalPosition.js +20 -0
- package/index.css +1 -1
- package/index.d.ts +4 -0
- package/index.js +119 -6575
- package/index.layered.css +1 -1
- package/llms.txt +123 -5
- package/manifest.json +230 -2
- package/package.json +3 -2
- package/recipes/comparison-table.tsx +123 -0
package/llms.txt
CHANGED
|
@@ -60,6 +60,96 @@ Example:
|
|
|
60
60
|
</Button>
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
### ActionButton
|
|
64
|
+
Import: `import { ActionButton, type ActionButtonProps, type ActionButtonPhase } from '@overdoser/react-toolkit'`
|
|
65
|
+
Element: `<button>` (forwards ref, accepts all native button props except `children`).
|
|
66
|
+
|
|
67
|
+
Stateful action button with morphing icon/label layers: `idle → (armed) → (pending) → success → idle`. Sync `onAction` jumps straight to success; async `onAction` (returns a promise) shows a pending spinner until it resolves (rejection → back to idle + `onError`). Clicks are ignored while a sequence runs. Layers stack in one grid cell so the button width never jumps between states.
|
|
68
|
+
|
|
69
|
+
Props:
|
|
70
|
+
- `icon: ReactNode` — required. Idle-state icon (sized to `1em`).
|
|
71
|
+
- `label?: ReactNode` — idle label. Hidden when `iconOnly`.
|
|
72
|
+
- `pendingIcon?` / `pendingLabel?` — shown while the promise is in flight. Icon defaults to a built-in spinner arc; a missing label keeps the idle label visible.
|
|
73
|
+
- `successIcon?` / `successLabel?` — success state. Icon defaults to a built-in check mark.
|
|
74
|
+
- `confirmLabel?: ReactNode` — enables two-step confirm: first click arms (danger tint + icon shake + this label), second click within `confirmTimeout` runs the action.
|
|
75
|
+
- `confirmTimeout?: number` — default `2600` (ms, scaled by `speed`). Armed state auto-disarms after this.
|
|
76
|
+
- `onAction?: (context: ActionButtonContext) => void | Promise<unknown>` — the action. `context.setProgress(0–100)` during the promise switches pending from the indeterminate spinner to a determinate state: % label + accent fill bar (drive it from received bytes / `Content-Length` for downloads; `setProgress(null)` falls back to the spinner). `onError?: (error) => void` on throw/reject. `onPhaseChange?: (phase) => void`.
|
|
77
|
+
- `progressLabel?: (percent: number) => ReactNode` — default `` `${Math.round(percent)}%` ``.
|
|
78
|
+
- `resetAfter?: number` — default `1800` (ms, scaled by `speed`). Success hold before auto-reset; `0` stays in success.
|
|
79
|
+
- `speed?: number` — default `1`. Playback multiplier for every transition and timer (`2` halves, `0.5` doubles).
|
|
80
|
+
- `variant?: 'primary' | 'secondary' | 'danger' | 'ghost'` — default `'secondary'` (same scale as Button).
|
|
81
|
+
- `size?: 'sm' | 'md' | 'lg'` — default `'md'`
|
|
82
|
+
- `iconOnly?: boolean` — default `false`. Square icon-only button; pair with `aria-label`.
|
|
83
|
+
- `classes?: Partial<ActionButtonClasses>` where `ActionButtonClasses = { root, iconStack, iconLayer, labelStack, labelLayer, progressFill }`
|
|
84
|
+
|
|
85
|
+
Labels swap as a vertical carousel (played labels park above, upcoming wait below, entering layer starts a beat late) so transitions roll in one continuous direction.
|
|
86
|
+
|
|
87
|
+
Customization: the current phase is exposed as `data-phase` on the root for custom CSS. Colors/timings/transforms via CSS custom properties (set inline or on a parent): `--crk-action-fade` (200ms crossfade), `--crk-action-move` (340ms icon spring), `--crk-action-rise` (220ms label rise), `--crk-action-label-rise` (7px travel), `--crk-action-ease`, `--crk-action-spring`, `--crk-action-icon-exit` / `--crk-action-icon-enter` (transforms), `--crk-action-success-color/-bg/-border` (success recipe), `--crk-action-danger-color/-bg/-border` (armed recipe), `--crk-action-progress-color/-opacity` (fill bar), `--crk-action-press-scale`. Respects `prefers-reduced-motion` (transforms become plain crossfades).
|
|
88
|
+
|
|
89
|
+
Example (async send + two-step delete + download with progress):
|
|
90
|
+
```tsx
|
|
91
|
+
<ActionButton icon={<SendIcon />} label="Send" pendingLabel="Sending…" successLabel="Sent"
|
|
92
|
+
onAction={() => api.send(message)} onError={showToast} />
|
|
93
|
+
<ActionButton icon={<TrashIcon />} label="Delete" confirmLabel="Sure?" successLabel="Deleted"
|
|
94
|
+
onAction={() => api.remove(id)} />
|
|
95
|
+
<ActionButton icon={<DownloadIcon />} label="Download" pendingLabel="Preparing…" successLabel="Done"
|
|
96
|
+
onAction={async ({ setProgress }) => {
|
|
97
|
+
const res = await fetch(url);
|
|
98
|
+
const total = Number(res.headers.get('content-length'));
|
|
99
|
+
const reader = res.body!.getReader();
|
|
100
|
+
let received = 0;
|
|
101
|
+
for (;;) {
|
|
102
|
+
const { done, value } = await reader.read();
|
|
103
|
+
if (done) break;
|
|
104
|
+
received += value.length;
|
|
105
|
+
if (total) setProgress((received / total) * 100);
|
|
106
|
+
}
|
|
107
|
+
}} />
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### ToggleIconButton
|
|
111
|
+
Import: `import { ToggleIconButton, type ToggleIconButtonProps } from '@overdoser/react-toolkit'`
|
|
112
|
+
Element: `<button>` with `aria-pressed` (forwards ref, accepts all native button props except `children`).
|
|
113
|
+
|
|
114
|
+
Round icon-only toggle for like/save/bookmark-style optimistic actions: toggling on fills the icon with the active color, tints the surface, and pops the icon (scale 0.6 → 1.28 → 1), with an optional expanding burst ring. Pop/burst only animate on interaction, never when mounting already-pressed. Always pair with `aria-label`.
|
|
115
|
+
|
|
116
|
+
Props:
|
|
117
|
+
- `icon: ReactNode` — required. Outline SVG (`fill="none"`, `stroke="currentColor"`); the pressed fill is applied by the component.
|
|
118
|
+
- `pressed?: boolean` + `onPressedChange?: (pressed: boolean) => void` — controlled mode (optimistic: sync to the server in the background, flip back on failure).
|
|
119
|
+
- `defaultPressed?: boolean` — default `false`. Uncontrolled initial state.
|
|
120
|
+
- `activeColor?: string` — any CSS color for the pressed state. Default `var(--crk-color-primary)`.
|
|
121
|
+
- `burst?: boolean` — default `false`. Expanding ring on toggle-on (like/heart style).
|
|
122
|
+
- `variant?: 'secondary' | 'ghost'` — default `'secondary'` (off-state surface).
|
|
123
|
+
- `size?: 'sm' | 'md' | 'lg'` — default `'md'`. Square footprint matching Button's control scale.
|
|
124
|
+
- `speed?: number` — default `1`. Scales fill/pop/burst durations.
|
|
125
|
+
- `classes?: Partial<ToggleIconButtonClasses>` where `ToggleIconButtonClasses = { root, icon, burst }`
|
|
126
|
+
|
|
127
|
+
Customization: pressed state exposed as `data-pressed` on the root; `--crk-toggle-color/-bg/-border/-radius/-fill/-pop/-burst/-press-scale` custom properties. Respects `prefers-reduced-motion`.
|
|
128
|
+
|
|
129
|
+
Example:
|
|
130
|
+
```tsx
|
|
131
|
+
<ToggleIconButton icon={<HeartIcon />} burst activeColor="#ff5c7a"
|
|
132
|
+
pressed={liked} onPressedChange={setLiked} aria-label="Like" />
|
|
133
|
+
<ToggleIconButton icon={<BookmarkIcon />} defaultPressed aria-label="Save" />
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### CopyButton
|
|
137
|
+
Import: `import { CopyButton, type CopyButtonProps } from '@overdoser/react-toolkit'`
|
|
138
|
+
Element: `<button>` — a preconfigured `ActionButton`; all its props (`variant`, `size`, `speed`, `resetAfter`, `classes`, …) apply.
|
|
139
|
+
|
|
140
|
+
Copy-to-clipboard: clipboard icon morphs into a check, label swaps `Copy → Copied`, success tint, auto-reset. Success shows only after the clipboard write confirms; failures return to idle and call `onError`. Falls back to `execCommand('copy')` when the async Clipboard API is unavailable.
|
|
141
|
+
|
|
142
|
+
Props (on top of ActionButton's):
|
|
143
|
+
- `value: string | (() => string | Promise<string>)` — required. Text to copy, or a lazy (possibly async) getter.
|
|
144
|
+
- `label?: ReactNode` — default `'Copy'`; `successLabel?` — default `'Copied'`; `icon?` — default built-in clipboard icon.
|
|
145
|
+
- `onCopied?: (text: string) => void`
|
|
146
|
+
|
|
147
|
+
Example:
|
|
148
|
+
```tsx
|
|
149
|
+
<CopyButton value={shareUrl} /> // icon + text
|
|
150
|
+
<CopyButton value={shareUrl} iconOnly aria-label="Copy link" /> // icon only
|
|
151
|
+
```
|
|
152
|
+
|
|
63
153
|
### Link
|
|
64
154
|
Import: `import { Link } from '@overdoser/react-toolkit'`
|
|
65
155
|
Element: `<a>` (forwards ref, accepts all native anchor props).
|
|
@@ -117,7 +207,9 @@ Props:
|
|
|
117
207
|
- `columns: ColumnDef<T>[]` — see `ColumnDef` below.
|
|
118
208
|
- `sortConfig?: SortConfig[]` — controlled sort. Provide together with `onSort` for server-side sorting.
|
|
119
209
|
- `onSort?: (config: SortConfig[]) => void` — when provided, table is in **controlled** mode: it does NOT sort `data` internally and does NOT slice for pagination (server is assumed to handle both).
|
|
120
|
-
- `multiSort?: boolean` — default `true`. Ctrl/Cmd+click a sortable header to add/cycle a secondary sort.
|
|
210
|
+
- `multiSort?: boolean` — default `true`. Ctrl/Cmd/Shift+click a sortable header to add/cycle a secondary sort.
|
|
211
|
+
- `scrollable?: boolean` — default `false`. Wraps the table in a horizontal scroll container (`overflow-x: auto`). Required for `sticky` columns to have an effect.
|
|
212
|
+
- `dragScroll?: boolean` — default `false`. Drag anywhere on the table with the **middle or right mouse button** to scroll it horizontally. Implies `scrollable`. The left button keeps its native behavior (text selection, clicks); touch pans natively via the scroll container. After a right-button drag the context menu is suppressed; a direct right click (no drag within 200ms of the press) opens it normally.
|
|
121
213
|
- `striped?: boolean` — default `false`
|
|
122
214
|
- `hoverable?: boolean` — default `false`
|
|
123
215
|
- `compact?: boolean` — default `false`
|
|
@@ -130,11 +222,15 @@ Props:
|
|
|
130
222
|
|
|
131
223
|
`ColumnDef<T>`:
|
|
132
224
|
- `key: keyof T & string` — required.
|
|
133
|
-
- `header: ReactNode` — required.
|
|
225
|
+
- `header: ReactNode` — required. Interactive elements (e.g. a remove-column button) are allowed; inside a `sortable` column they must call `e.stopPropagation()` in their click handler so the click doesn't trigger a sort.
|
|
134
226
|
- `sortable?: boolean`
|
|
227
|
+
- `defaultSortDirection?: 'asc' | 'desc'` — default `'asc'`. Direction the sort cycle starts with: `none → defaultSortDirection → opposite → none`. Use `'desc'` for "highest first" columns (scores, amounts, %DV).
|
|
135
228
|
- `render?: (value: T[keyof T], row: T, index: number) => ReactNode`
|
|
136
229
|
- `width?: string | number`
|
|
137
230
|
- `align?: 'left' | 'center' | 'right'`
|
|
231
|
+
- `sticky?: boolean` — default `false`. Pins the column to the left edge while the table scrolls horizontally. Only meaningful with `scrollable`/`dragScroll`, and intended for the first column (multiple sticky columns all pin to `left: 0` and overlap). Sticky cells paint their own background, so a background set via `rowClassName`/`rowStyle` won't show through them — style sticky cells via `cellClassName` instead.
|
|
232
|
+
- `cellClassName?: string | ((value: T[keyof T], row: T, index: number) => string | undefined)` — per-cell class for this column. Callback form enables value-bucket shading (heatmap-style columns).
|
|
233
|
+
- `cellStyle?: CSSProperties | ((value: T[keyof T], row: T, index: number) => CSSProperties | undefined)` — per-cell inline style; merged over the column's `align`.
|
|
138
234
|
|
|
139
235
|
`SortConfig`:
|
|
140
236
|
- `key: string`
|
|
@@ -147,7 +243,7 @@ Props:
|
|
|
147
243
|
- `pageSizeOptions?: number[]` — default `[10, 25, 50, 100]`. Pass `[n]` to hide the page-size select.
|
|
148
244
|
- `onPageChange: (page: number, pageSize: number) => void`
|
|
149
245
|
|
|
150
|
-
Sort cycle on a sortable header click: `none → asc → desc → none
|
|
246
|
+
Sort cycle on a sortable header click: `none → asc → desc → none` (start at `desc` via `defaultSortDirection: 'desc'`).
|
|
151
247
|
|
|
152
248
|
Client-side example:
|
|
153
249
|
```tsx
|
|
@@ -194,14 +290,36 @@ Row-level styling (highlight rows of interest):
|
|
|
194
290
|
```
|
|
195
291
|
Both accept a static value or `(row, index) => …`. Returned `undefined` is treated as no class/style.
|
|
196
292
|
|
|
293
|
+
Wide comparison matrix (sticky first column, drag-to-scroll, per-cell bucket shading, removable columns):
|
|
294
|
+
```tsx
|
|
295
|
+
const columns: ColumnDef<Row>[] = [
|
|
296
|
+
{ key: 'metric', header: 'Metric', sticky: true, width: 160 },
|
|
297
|
+
...items.map((item) => ({
|
|
298
|
+
key: item.id,
|
|
299
|
+
header: (
|
|
300
|
+
<span>
|
|
301
|
+
{item.label}
|
|
302
|
+
{/* stopPropagation so the remove click doesn't trigger a sort */}
|
|
303
|
+
<button onClick={(e) => { e.stopPropagation(); removeItem(item.id); }}>✕</button>
|
|
304
|
+
</span>
|
|
305
|
+
),
|
|
306
|
+
sortable: true,
|
|
307
|
+
defaultSortDirection: 'desc' as const, // "highest first" on first click
|
|
308
|
+
cellClassName: (value) => ((value as number) >= 40 ? 'dv-high' : (value as number) >= 20 ? 'dv-mid' : undefined),
|
|
309
|
+
})),
|
|
310
|
+
];
|
|
311
|
+
<Table data={rows} columns={columns} rowKey="metricId" dragScroll />
|
|
312
|
+
```
|
|
313
|
+
Full copy-paste version: `recipes/comparison-table.tsx`.
|
|
314
|
+
|
|
197
315
|
### `useTableSort` (hook)
|
|
198
316
|
Import: `import { useTableSort } from '@overdoser/react-toolkit'`
|
|
199
317
|
Signature: `useTableSort<T>(data: T[], initialSort?: SortConfig[])`
|
|
200
318
|
Returns:
|
|
201
319
|
- `sortedData: T[]`
|
|
202
320
|
- `sortConfig: SortConfig[]`
|
|
203
|
-
- `requestSort(key): void` — single-column; cycles
|
|
204
|
-
- `requestMultiSort(key): void` — adds/toggles in multi-sort.
|
|
321
|
+
- `requestSort(key, startDirection?): void` — single-column; cycles startDirection → opposite → cleared (`startDirection` defaults to `'asc'`).
|
|
322
|
+
- `requestMultiSort(key, startDirection?): void` — adds/toggles in multi-sort.
|
|
205
323
|
- `resetSort(): void`
|
|
206
324
|
|
|
207
325
|
Use this only if you want to manage sort state outside `<Table>`; `<Table>` already does this internally when `onSort` is not supplied.
|
package/manifest.json
CHANGED
|
@@ -77,6 +77,201 @@
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
|
+
"ActionButton": {
|
|
81
|
+
"import": "import { ActionButton } from '@overdoser/react-toolkit'",
|
|
82
|
+
"element": "button",
|
|
83
|
+
"extendsNativeProps": "button (except children)",
|
|
84
|
+
"forwardsRef": true,
|
|
85
|
+
"notes": "Stateful action button with morphing icon/label layers: idle → (armed) → (pending) → success → idle. Async onAction shows a pending spinner; confirmLabel adds a two-step confirm. Phase exposed as data-phase on the root; transitions/colors themeable via --crk-action-* custom properties; respects prefers-reduced-motion.",
|
|
86
|
+
"props": {
|
|
87
|
+
"icon": {
|
|
88
|
+
"type": "ReactNode",
|
|
89
|
+
"required": true,
|
|
90
|
+
"notes": "Idle-state icon, sized to 1em."
|
|
91
|
+
},
|
|
92
|
+
"label": {
|
|
93
|
+
"type": "ReactNode",
|
|
94
|
+
"notes": "Idle label. Hidden when iconOnly."
|
|
95
|
+
},
|
|
96
|
+
"pendingIcon": {
|
|
97
|
+
"type": "ReactNode",
|
|
98
|
+
"notes": "Shown while onAction's promise is in flight. Default: built-in spinner arc."
|
|
99
|
+
},
|
|
100
|
+
"pendingLabel": {
|
|
101
|
+
"type": "ReactNode",
|
|
102
|
+
"notes": "Missing label keeps the idle label visible."
|
|
103
|
+
},
|
|
104
|
+
"successIcon": {
|
|
105
|
+
"type": "ReactNode",
|
|
106
|
+
"notes": "Default: built-in check mark."
|
|
107
|
+
},
|
|
108
|
+
"successLabel": {
|
|
109
|
+
"type": "ReactNode",
|
|
110
|
+
"notes": "e.g. 'Copied', 'Sent'. Missing label keeps the idle label visible."
|
|
111
|
+
},
|
|
112
|
+
"confirmLabel": {
|
|
113
|
+
"type": "ReactNode",
|
|
114
|
+
"notes": "Enables two-step confirm: first click arms (danger tint + icon shake), second click runs the action."
|
|
115
|
+
},
|
|
116
|
+
"confirmTimeout": {
|
|
117
|
+
"type": "number",
|
|
118
|
+
"default": 2600,
|
|
119
|
+
"notes": "ms, scaled by speed. Armed state auto-disarms after this."
|
|
120
|
+
},
|
|
121
|
+
"onAction": {
|
|
122
|
+
"type": "(context: { setProgress: (percent: number | null) => void }) => void | Promise<unknown>",
|
|
123
|
+
"notes": "Return a promise for a pending phase; rejection returns to idle and calls onError. Call context.setProgress(0–100) during the promise for a determinate pending state (% label + accent fill bar); null falls back to the spinner."
|
|
124
|
+
},
|
|
125
|
+
"progressLabel": {
|
|
126
|
+
"type": "(percent: number) => ReactNode",
|
|
127
|
+
"notes": "Label while determinate progress is shown. Default: Math.round(percent) + '%'."
|
|
128
|
+
},
|
|
129
|
+
"onError": {
|
|
130
|
+
"type": "(error: unknown) => void"
|
|
131
|
+
},
|
|
132
|
+
"onPhaseChange": {
|
|
133
|
+
"type": "(phase: 'idle' | 'armed' | 'pending' | 'success') => void"
|
|
134
|
+
},
|
|
135
|
+
"resetAfter": {
|
|
136
|
+
"type": "number",
|
|
137
|
+
"default": 1800,
|
|
138
|
+
"notes": "ms success hold before auto-reset, scaled by speed. 0 stays in success."
|
|
139
|
+
},
|
|
140
|
+
"speed": {
|
|
141
|
+
"type": "number",
|
|
142
|
+
"default": 1,
|
|
143
|
+
"notes": "Playback multiplier for every transition and timer: 2 halves, 0.5 doubles."
|
|
144
|
+
},
|
|
145
|
+
"variant": {
|
|
146
|
+
"type": "enum",
|
|
147
|
+
"values": [
|
|
148
|
+
"primary",
|
|
149
|
+
"secondary",
|
|
150
|
+
"danger",
|
|
151
|
+
"ghost"
|
|
152
|
+
],
|
|
153
|
+
"default": "secondary"
|
|
154
|
+
},
|
|
155
|
+
"size": {
|
|
156
|
+
"type": "enum",
|
|
157
|
+
"values": [
|
|
158
|
+
"sm",
|
|
159
|
+
"md",
|
|
160
|
+
"lg"
|
|
161
|
+
],
|
|
162
|
+
"default": "md"
|
|
163
|
+
},
|
|
164
|
+
"iconOnly": {
|
|
165
|
+
"type": "boolean",
|
|
166
|
+
"default": false,
|
|
167
|
+
"notes": "Square icon-only button, hides all labels. Pair with aria-label."
|
|
168
|
+
},
|
|
169
|
+
"classes": {
|
|
170
|
+
"type": "Partial<ActionButtonClasses>",
|
|
171
|
+
"shape": [
|
|
172
|
+
"root",
|
|
173
|
+
"iconStack",
|
|
174
|
+
"iconLayer",
|
|
175
|
+
"labelStack",
|
|
176
|
+
"labelLayer",
|
|
177
|
+
"progressFill"
|
|
178
|
+
]
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
"ToggleIconButton": {
|
|
183
|
+
"import": "import { ToggleIconButton } from '@overdoser/react-toolkit'",
|
|
184
|
+
"element": "button",
|
|
185
|
+
"extendsNativeProps": "button (except children)",
|
|
186
|
+
"forwardsRef": true,
|
|
187
|
+
"notes": "Round icon-only toggle (like/save/bookmark) with aria-pressed. Toggling on fills the icon, tints the surface, and pops the icon; optional burst ring. Pressed state exposed as data-pressed; themeable via --crk-toggle-* custom properties; respects prefers-reduced-motion. Always pair with aria-label.",
|
|
188
|
+
"props": {
|
|
189
|
+
"icon": {
|
|
190
|
+
"type": "ReactNode",
|
|
191
|
+
"required": true,
|
|
192
|
+
"notes": "Outline SVG (fill='none', stroke='currentColor'); the pressed fill is applied by the component."
|
|
193
|
+
},
|
|
194
|
+
"pressed": {
|
|
195
|
+
"type": "boolean",
|
|
196
|
+
"notes": "Controlled mode; pair with onPressedChange."
|
|
197
|
+
},
|
|
198
|
+
"defaultPressed": {
|
|
199
|
+
"type": "boolean",
|
|
200
|
+
"default": false
|
|
201
|
+
},
|
|
202
|
+
"onPressedChange": {
|
|
203
|
+
"type": "(pressed: boolean) => void"
|
|
204
|
+
},
|
|
205
|
+
"activeColor": {
|
|
206
|
+
"type": "string",
|
|
207
|
+
"notes": "Any CSS color for the pressed state. Default var(--crk-color-primary)."
|
|
208
|
+
},
|
|
209
|
+
"burst": {
|
|
210
|
+
"type": "boolean",
|
|
211
|
+
"default": false,
|
|
212
|
+
"notes": "Expanding ring animation on toggle-on (like/heart style)."
|
|
213
|
+
},
|
|
214
|
+
"variant": {
|
|
215
|
+
"type": "enum",
|
|
216
|
+
"values": [
|
|
217
|
+
"secondary",
|
|
218
|
+
"ghost"
|
|
219
|
+
],
|
|
220
|
+
"default": "secondary"
|
|
221
|
+
},
|
|
222
|
+
"size": {
|
|
223
|
+
"type": "enum",
|
|
224
|
+
"values": [
|
|
225
|
+
"sm",
|
|
226
|
+
"md",
|
|
227
|
+
"lg"
|
|
228
|
+
],
|
|
229
|
+
"default": "md"
|
|
230
|
+
},
|
|
231
|
+
"speed": {
|
|
232
|
+
"type": "number",
|
|
233
|
+
"default": 1,
|
|
234
|
+
"notes": "Scales fill/pop/burst durations: 2 halves, 0.5 doubles."
|
|
235
|
+
},
|
|
236
|
+
"classes": {
|
|
237
|
+
"type": "Partial<ToggleIconButtonClasses>",
|
|
238
|
+
"shape": [
|
|
239
|
+
"root",
|
|
240
|
+
"icon",
|
|
241
|
+
"burst"
|
|
242
|
+
]
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
"CopyButton": {
|
|
247
|
+
"import": "import { CopyButton } from '@overdoser/react-toolkit'",
|
|
248
|
+
"element": "button",
|
|
249
|
+
"extendsNativeProps": "button (except children)",
|
|
250
|
+
"forwardsRef": true,
|
|
251
|
+
"notes": "Preconfigured ActionButton for copy-to-clipboard: clipboard icon morphs into a check, label swaps Copy → Copied, success tint, auto-reset. All ActionButton props apply. Falls back to execCommand('copy') without the Clipboard API.",
|
|
252
|
+
"props": {
|
|
253
|
+
"value": {
|
|
254
|
+
"type": "string | (() => string | Promise<string>)",
|
|
255
|
+
"required": true,
|
|
256
|
+
"notes": "Text to copy, or a lazy (possibly async) getter."
|
|
257
|
+
},
|
|
258
|
+
"label": {
|
|
259
|
+
"type": "ReactNode",
|
|
260
|
+
"default": "Copy"
|
|
261
|
+
},
|
|
262
|
+
"successLabel": {
|
|
263
|
+
"type": "ReactNode",
|
|
264
|
+
"default": "Copied"
|
|
265
|
+
},
|
|
266
|
+
"icon": {
|
|
267
|
+
"type": "ReactNode",
|
|
268
|
+
"notes": "Default: built-in clipboard icon."
|
|
269
|
+
},
|
|
270
|
+
"onCopied": {
|
|
271
|
+
"type": "(text: string) => void"
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
},
|
|
80
275
|
"Link": {
|
|
81
276
|
"import": "import { Link } from '@overdoser/react-toolkit'",
|
|
82
277
|
"element": "a",
|
|
@@ -216,7 +411,17 @@
|
|
|
216
411
|
"multiSort": {
|
|
217
412
|
"type": "boolean",
|
|
218
413
|
"default": true,
|
|
219
|
-
"notes": "Ctrl/Cmd+click a sortable header to add a secondary sort."
|
|
414
|
+
"notes": "Ctrl/Cmd/Shift+click a sortable header to add a secondary sort."
|
|
415
|
+
},
|
|
416
|
+
"scrollable": {
|
|
417
|
+
"type": "boolean",
|
|
418
|
+
"default": false,
|
|
419
|
+
"notes": "Horizontal scroll container around the table. Required for sticky columns to have an effect."
|
|
420
|
+
},
|
|
421
|
+
"dragScroll": {
|
|
422
|
+
"type": "boolean",
|
|
423
|
+
"default": false,
|
|
424
|
+
"notes": "Middle/right-mouse-button drag scrolls the table horizontally. Implies scrollable. Left button keeps native behavior (selection, clicks); context menu suppressed only after a right-button drag."
|
|
220
425
|
},
|
|
221
426
|
"striped": {
|
|
222
427
|
"type": "boolean",
|
|
@@ -270,11 +475,21 @@
|
|
|
270
475
|
},
|
|
271
476
|
"header": {
|
|
272
477
|
"type": "ReactNode",
|
|
273
|
-
"required": true
|
|
478
|
+
"required": true,
|
|
479
|
+
"notes": "Interactive elements allowed; inside a sortable column they must call e.stopPropagation() in their click handler."
|
|
274
480
|
},
|
|
275
481
|
"sortable": {
|
|
276
482
|
"type": "boolean"
|
|
277
483
|
},
|
|
484
|
+
"defaultSortDirection": {
|
|
485
|
+
"type": "enum",
|
|
486
|
+
"values": [
|
|
487
|
+
"asc",
|
|
488
|
+
"desc"
|
|
489
|
+
],
|
|
490
|
+
"default": "asc",
|
|
491
|
+
"notes": "Direction the sort cycle starts with. Use desc for highest-first columns."
|
|
492
|
+
},
|
|
278
493
|
"render": {
|
|
279
494
|
"type": "(value, row, index) => ReactNode"
|
|
280
495
|
},
|
|
@@ -288,6 +503,19 @@
|
|
|
288
503
|
"center",
|
|
289
504
|
"right"
|
|
290
505
|
]
|
|
506
|
+
},
|
|
507
|
+
"sticky": {
|
|
508
|
+
"type": "boolean",
|
|
509
|
+
"default": false,
|
|
510
|
+
"notes": "Pin to the left edge while the table scrolls horizontally. Needs scrollable/dragScroll; intended for the first column."
|
|
511
|
+
},
|
|
512
|
+
"cellClassName": {
|
|
513
|
+
"type": "string | ((value, row, index) => string | undefined)",
|
|
514
|
+
"notes": "Per-cell class for this column, e.g. value-bucket shading."
|
|
515
|
+
},
|
|
516
|
+
"cellStyle": {
|
|
517
|
+
"type": "CSSProperties | ((value, row, index) => CSSProperties | undefined)",
|
|
518
|
+
"notes": "Per-cell inline style; merged over the column's align."
|
|
291
519
|
}
|
|
292
520
|
},
|
|
293
521
|
"SortConfig": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@overdoser/react-toolkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"react": "^18.0.0 || ^19.0.0",
|
|
24
|
-
"react-dom": "^18.0.0 || ^19.0.0"
|
|
24
|
+
"react-dom": "^18.0.0 || ^19.0.0",
|
|
25
|
+
"react-hook-form": "^7.0.0"
|
|
25
26
|
},
|
|
26
27
|
"peerDependenciesMeta": {
|
|
27
28
|
"react-hook-form": {
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { Table, type ColumnDef } from '@overdoser/react-toolkit';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Wide comparison matrix (e.g. compare products/foods side by side):
|
|
6
|
+
* - sticky first column that stays pinned while the matrix scrolls (`sticky`)
|
|
7
|
+
* - middle/right-mouse-button drag to scroll horizontally (`dragScroll` —
|
|
8
|
+
* implies `scrollable`; left button keeps text selection and clicks, and
|
|
9
|
+
* the context menu only opens on a direct right click, never after a drag)
|
|
10
|
+
* - per-cell value-bucket shading via `cellClassName` on the column def
|
|
11
|
+
* - remove buttons embedded in sortable headers (`e.stopPropagation()` keeps
|
|
12
|
+
* the button click from triggering a sort)
|
|
13
|
+
* - desc-first sort cycle (`defaultSortDirection: 'desc'`) — "highest value
|
|
14
|
+
* first" is the natural question for comparison metrics
|
|
15
|
+
* - multi-sort: Ctrl/Cmd/Shift+click adds a secondary sort (on by default)
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
interface ComparisonItem {
|
|
19
|
+
/** Column identity — must be unique (used as the column key). */
|
|
20
|
+
id: string;
|
|
21
|
+
label: string;
|
|
22
|
+
/** Metric values by row id, e.g. `{ protein: 42, fiber: 12 }` (%DV). */
|
|
23
|
+
values: Record<string, number>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface MetricRowBase {
|
|
27
|
+
metricId: string;
|
|
28
|
+
metric: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** One row per metric, one column per compared item. */
|
|
32
|
+
type MetricRow = MetricRowBase & Record<string, unknown>;
|
|
33
|
+
|
|
34
|
+
/** Shade cells by how much of the daily value they cover. */
|
|
35
|
+
function bucketClass(value: unknown): string | undefined {
|
|
36
|
+
const dv = value as number;
|
|
37
|
+
if (dv >= 40) return 'comparison-dv-high';
|
|
38
|
+
if (dv >= 20) return 'comparison-dv-mid';
|
|
39
|
+
if (dv > 0) return 'comparison-dv-low';
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function ComparisonTable({
|
|
44
|
+
metrics,
|
|
45
|
+
items: initialItems,
|
|
46
|
+
}: {
|
|
47
|
+
/** Row definitions, e.g. `[{ id: 'protein', label: 'Protein' }]`. */
|
|
48
|
+
metrics: { id: string; label: string }[];
|
|
49
|
+
items: ComparisonItem[];
|
|
50
|
+
}) {
|
|
51
|
+
const [items, setItems] = useState(initialItems);
|
|
52
|
+
|
|
53
|
+
const rows: MetricRow[] = metrics.map((m) => ({
|
|
54
|
+
metricId: m.id,
|
|
55
|
+
metric: m.label,
|
|
56
|
+
...Object.fromEntries(items.map((item) => [item.id, item.values[m.id] ?? 0])),
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
const columns: ColumnDef<MetricRow>[] = [
|
|
60
|
+
{ key: 'metric', header: 'Metric', sticky: true, width: 160 },
|
|
61
|
+
...items.map(
|
|
62
|
+
(item): ColumnDef<MetricRow> => ({
|
|
63
|
+
key: item.id,
|
|
64
|
+
header: (
|
|
65
|
+
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
|
|
66
|
+
{item.label}
|
|
67
|
+
<button
|
|
68
|
+
type="button"
|
|
69
|
+
className="comparison-remove"
|
|
70
|
+
aria-label={`Remove ${item.label}`}
|
|
71
|
+
onClick={(e) => {
|
|
72
|
+
e.stopPropagation(); // header is sortable — don't sort on remove
|
|
73
|
+
setItems((prev) => prev.filter((i) => i.id !== item.id));
|
|
74
|
+
}}
|
|
75
|
+
>
|
|
76
|
+
✕
|
|
77
|
+
</button>
|
|
78
|
+
</span>
|
|
79
|
+
),
|
|
80
|
+
sortable: true,
|
|
81
|
+
defaultSortDirection: 'desc',
|
|
82
|
+
align: 'right',
|
|
83
|
+
width: 120,
|
|
84
|
+
cellClassName: bucketClass,
|
|
85
|
+
render: (value) => `${value}%`,
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<>
|
|
92
|
+
{/* Bucket shading + remove-button hover — swap for your own palette or CSS-module classes. */}
|
|
93
|
+
<style>{`
|
|
94
|
+
.comparison-dv-low { background-color: rgba(34, 197, 94, 0.08); }
|
|
95
|
+
.comparison-dv-mid { background-color: rgba(34, 197, 94, 0.18); }
|
|
96
|
+
.comparison-dv-high { background-color: rgba(34, 197, 94, 0.32); }
|
|
97
|
+
.comparison-remove {
|
|
98
|
+
border: none;
|
|
99
|
+
background: none;
|
|
100
|
+
cursor: pointer;
|
|
101
|
+
padding: 0 4px;
|
|
102
|
+
border-radius: 4px;
|
|
103
|
+
font-size: inherit;
|
|
104
|
+
color: inherit;
|
|
105
|
+
line-height: 1.4;
|
|
106
|
+
transition: background-color 120ms ease, color 120ms ease;
|
|
107
|
+
}
|
|
108
|
+
.comparison-remove:hover {
|
|
109
|
+
background-color: rgba(239, 68, 68, 0.15);
|
|
110
|
+
color: rgb(220, 38, 38);
|
|
111
|
+
}
|
|
112
|
+
`}</style>
|
|
113
|
+
<Table<MetricRow>
|
|
114
|
+
data={rows}
|
|
115
|
+
columns={columns}
|
|
116
|
+
rowKey="metricId"
|
|
117
|
+
dragScroll
|
|
118
|
+
hoverable
|
|
119
|
+
emptyMessage="Nothing to compare yet."
|
|
120
|
+
/>
|
|
121
|
+
</>
|
|
122
|
+
);
|
|
123
|
+
}
|