@rise-x/apps-sdk 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/README.md +192 -10
- package/build/connectors/agents.js +1 -1
- package/build/connectors/assets.d.ts +9 -3
- package/build/connectors/assets.js +32 -1
- package/build/connectors/index.d.ts +3 -2
- package/build/connectors/index.js +4 -1
- package/build/connectors/search.d.ts +34 -0
- package/build/connectors/search.js +63 -0
- package/build/connectors/types.d.ts +51 -0
- package/build/connectors/types.js +71 -0
- package/build/connectors/work.d.ts +7 -1
- package/build/connectors/work.js +13 -0
- package/build/index.d.ts +1 -1
- package/build/mockShell.d.ts +11 -0
- package/build/mockShell.js +159 -2
- package/build/query/assets.d.ts +19 -6
- package/build/query/assets.js +35 -3
- package/build/query/context.d.ts +14 -0
- package/build/query/context.js +16 -0
- package/build/query/index.d.ts +4 -3
- package/build/query/index.js +14 -2
- package/build/query/keys.d.ts +4 -0
- package/build/query/keys.js +7 -0
- package/build/query/layer.d.ts +25 -3
- package/build/query/work.d.ts +14 -1
- package/build/query/work.js +32 -1
- package/build/shellBridge.d.ts +1 -1
- package/build/ui/components/accordion.d.ts +7 -0
- package/build/ui/components/aspect-ratio.d.ts +4 -0
- package/build/ui/components/button-group.d.ts +7 -0
- package/build/ui/components/carousel.d.ts +18 -0
- package/build/ui/components/chart.d.ts +45 -94
- package/build/ui/components/collapsible.d.ts +6 -0
- package/build/ui/components/combobox.d.ts +19 -0
- package/build/ui/components/command.d.ts +18 -0
- package/build/ui/components/context-menu.d.ts +29 -0
- package/build/ui/components/data-table.d.ts +13 -2
- package/build/ui/components/date-picker.d.ts +16 -0
- package/build/ui/components/direction.d.ts +6 -0
- package/build/ui/components/field.d.ts +25 -0
- package/build/ui/components/hover-card.d.ts +6 -0
- package/build/ui/components/input-group.d.ts +13 -0
- package/build/ui/components/input-otp.d.ts +34 -0
- package/build/ui/components/input.d.ts +1 -3
- package/build/ui/components/item.d.ts +23 -0
- package/build/ui/components/kbd.d.ts +4 -0
- package/build/ui/components/menubar.d.ts +26 -0
- package/build/ui/components/message-scroller.d.ts +15 -0
- package/build/ui/components/navigation-menu.d.ts +13 -0
- package/build/ui/components/page-header.d.ts +7 -1
- package/build/ui/components/table.d.ts +7 -1
- package/build/ui/index.d.ts +19 -0
- package/build/ui/reference/components/accordion.tsx +85 -0
- package/build/ui/reference/components/alert-dialog.tsx +7 -2
- package/build/ui/reference/components/aspect-ratio.tsx +28 -0
- package/build/ui/reference/components/button-group.tsx +77 -0
- package/build/ui/reference/components/button.tsx +8 -1
- package/build/ui/reference/components/carousel.tsx +248 -0
- package/build/ui/reference/components/chart.tsx +441 -1131
- package/build/ui/reference/components/collapsible.tsx +43 -0
- package/build/ui/reference/components/combobox.tsx +141 -0
- package/build/ui/reference/components/command.tsx +181 -0
- package/build/ui/reference/components/context-menu.tsx +243 -0
- package/build/ui/reference/components/data-table.tsx +199 -29
- package/build/ui/reference/components/date-picker.tsx +218 -0
- package/build/ui/reference/components/dialog.tsx +2 -2
- package/build/ui/reference/components/direction.tsx +22 -0
- package/build/ui/reference/components/dropdown-menu.tsx +2 -2
- package/build/ui/reference/components/field.tsx +252 -0
- package/build/ui/reference/components/hover-card.tsx +64 -0
- package/build/ui/reference/components/input-group.tsx +157 -0
- package/build/ui/reference/components/input-otp.tsx +106 -0
- package/build/ui/reference/components/input.tsx +4 -32
- package/build/ui/reference/components/item.tsx +188 -0
- package/build/ui/reference/components/kbd.tsx +43 -0
- package/build/ui/reference/components/label.tsx +1 -1
- package/build/ui/reference/components/menubar.tsx +275 -0
- package/build/ui/reference/components/message-scroller.tsx +141 -0
- package/build/ui/reference/components/nav.tsx +9 -0
- package/build/ui/reference/components/navigation-menu.tsx +191 -0
- package/build/ui/reference/components/page-header.tsx +32 -0
- package/build/ui/reference/components/popover.tsx +2 -2
- package/build/ui/reference/components/select.tsx +11 -8
- package/build/ui/reference/components/sheet.tsx +14 -3
- package/build/ui/reference/components/table.tsx +15 -2
- package/build/ui/reference/components/timeline.tsx +7 -0
- package/build/ui/reference/components/toggle.tsx +1 -1
- package/build/ui/reference/index.ts +19 -0
- package/build/ui/standalone.js +167 -28
- package/build/ui/styles.css +1 -1
- package/package.json +15 -9
- package/template/README.md +6 -0
- package/template/src/App.tsx +10 -1
|
@@ -17,7 +17,16 @@ import {
|
|
|
17
17
|
type Table as TanstackTable,
|
|
18
18
|
type VisibilityState,
|
|
19
19
|
} from '@tanstack/react-table';
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
ArrowDown,
|
|
22
|
+
ArrowUp,
|
|
23
|
+
CheckIcon,
|
|
24
|
+
ChevronsUpDown,
|
|
25
|
+
Plus,
|
|
26
|
+
Search,
|
|
27
|
+
Settings2,
|
|
28
|
+
X,
|
|
29
|
+
} from 'lucide-react';
|
|
21
30
|
|
|
22
31
|
import { cn } from '@rise-x/ui/lib/utils';
|
|
23
32
|
import { Badge } from '@rise-x/ui/components/badge';
|
|
@@ -28,10 +37,10 @@ import {
|
|
|
28
37
|
DropdownMenuCheckboxItem,
|
|
29
38
|
DropdownMenuContent,
|
|
30
39
|
DropdownMenuLabel,
|
|
31
|
-
DropdownMenuSeparator,
|
|
32
40
|
DropdownMenuTrigger,
|
|
33
41
|
} from '@rise-x/ui/components/dropdown-menu';
|
|
34
42
|
import { Input } from '@rise-x/ui/components/input';
|
|
43
|
+
import { Popover, PopoverContent, PopoverTrigger } from '@rise-x/ui/components/popover';
|
|
35
44
|
import { Separator } from '@rise-x/ui/components/separator';
|
|
36
45
|
import {
|
|
37
46
|
Table,
|
|
@@ -66,14 +75,26 @@ export interface FacetedFilterProps {
|
|
|
66
75
|
selected: Iterable<string>;
|
|
67
76
|
onSelect: (value: string, checked: boolean) => void;
|
|
68
77
|
onClear: () => void;
|
|
78
|
+
/** Search box above the list. Defaults to on past SEARCH_THRESHOLD options. */
|
|
79
|
+
searchable?: boolean;
|
|
80
|
+
/** Drop options whose `count` is 0 — inert when counts aren't provided.
|
|
81
|
+
* Applied values stay listed so they can be unticked. */
|
|
82
|
+
hideEmpty?: boolean;
|
|
69
83
|
className?: string;
|
|
70
84
|
}
|
|
71
85
|
|
|
86
|
+
// Long option lists are unusable without a filter; short ones don't need one.
|
|
87
|
+
const SEARCH_THRESHOLD = 10;
|
|
88
|
+
|
|
72
89
|
export interface DataTableFilter {
|
|
73
90
|
/** Column id (accessorKey) the filter applies to. */
|
|
74
91
|
columnId: string;
|
|
75
92
|
title: string;
|
|
76
93
|
options: DataTableFilterOption[];
|
|
94
|
+
/** Search box above the options. Defaults to on past SEARCH_THRESHOLD. */
|
|
95
|
+
searchable?: boolean;
|
|
96
|
+
/** Drop options no visible row carries. */
|
|
97
|
+
hideEmpty?: boolean;
|
|
77
98
|
}
|
|
78
99
|
|
|
79
100
|
export interface DataTableProps<TData, TValue> {
|
|
@@ -140,8 +161,13 @@ export function FacetedFilter({
|
|
|
140
161
|
selected: selectedValues,
|
|
141
162
|
onSelect,
|
|
142
163
|
onClear,
|
|
164
|
+
searchable,
|
|
165
|
+
hideEmpty,
|
|
143
166
|
className,
|
|
144
167
|
}: FacetedFilterProps) {
|
|
168
|
+
const [open, setOpen] = React.useState(false);
|
|
169
|
+
const [query, setQuery] = React.useState('');
|
|
170
|
+
const listRef = React.useRef<HTMLDivElement>(null);
|
|
145
171
|
const selected = new Set(selectedValues);
|
|
146
172
|
const selectedOptions = options.filter((option) => selected.has(option.value));
|
|
147
173
|
|
|
@@ -152,9 +178,62 @@ export function FacetedFilter({
|
|
|
152
178
|
* an × appears to clear THIS filter without touching the others. */
|
|
153
179
|
const hasSelection = selectedOptions.length > 0;
|
|
154
180
|
|
|
181
|
+
const visible = hideEmpty
|
|
182
|
+
? options.filter((option) => option.count !== 0 || selected.has(option.value))
|
|
183
|
+
: options;
|
|
184
|
+
const showSearch = searchable ?? visible.length > SEARCH_THRESHOLD;
|
|
185
|
+
const matches = query
|
|
186
|
+
? visible.filter((option) => option.label.toLowerCase().includes(query.toLowerCase()))
|
|
187
|
+
: visible;
|
|
188
|
+
|
|
189
|
+
/* Roving focus: one tab stop for the whole list, arrows move within it —
|
|
190
|
+
* matching the DropdownMenu this replaced, where a long list would otherwise
|
|
191
|
+
* be one tab stop per option. `active` carries the tab stop with the focus so
|
|
192
|
+
* tabbing away and back returns to where the user left off. */
|
|
193
|
+
const [active, setActive] = React.useState(0);
|
|
194
|
+
const focusOption = (index: number) => {
|
|
195
|
+
const items = listRef.current?.querySelectorAll<HTMLElement>('[role="option"]');
|
|
196
|
+
if (!items?.length) return;
|
|
197
|
+
const next = Math.min(Math.max(index === -1 ? items.length - 1 : index, 0), items.length - 1);
|
|
198
|
+
setActive(next);
|
|
199
|
+
items[next].focus();
|
|
200
|
+
};
|
|
201
|
+
const onListKeyDown = (event: React.KeyboardEvent) => {
|
|
202
|
+
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
|
|
203
|
+
// Home/End belong to the caret while the search box has focus. This handler
|
|
204
|
+
// sits on the popover container, so without this it would swallow them and
|
|
205
|
+
// jump the list instead of moving through what the user typed.
|
|
206
|
+
if (
|
|
207
|
+
document.activeElement instanceof HTMLInputElement &&
|
|
208
|
+
(event.key === 'Home' || event.key === 'End')
|
|
209
|
+
) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
event.preventDefault();
|
|
213
|
+
if (event.key === 'Home') return focusOption(0);
|
|
214
|
+
if (event.key === 'End') return focusOption(-1);
|
|
215
|
+
// Radix focuses the popover container on open and the search box is not an
|
|
216
|
+
// option either, so the first arrow has to ENTER the list rather than step
|
|
217
|
+
// from a position nothing is sitting on.
|
|
218
|
+
const items = listRef.current?.querySelectorAll<HTMLElement>('[role="option"]');
|
|
219
|
+
const inList =
|
|
220
|
+
!!items?.length && Array.from(items).includes(document.activeElement as HTMLElement);
|
|
221
|
+
if (!inList) return focusOption(event.key === 'ArrowDown' ? 0 : -1);
|
|
222
|
+
focusOption(active + (event.key === 'ArrowDown' ? 1 : -1));
|
|
223
|
+
};
|
|
224
|
+
|
|
155
225
|
return (
|
|
156
|
-
<
|
|
157
|
-
|
|
226
|
+
<Popover
|
|
227
|
+
open={open}
|
|
228
|
+
onOpenChange={(next) => {
|
|
229
|
+
setOpen(next);
|
|
230
|
+
if (!next) {
|
|
231
|
+
setQuery('');
|
|
232
|
+
setActive(0);
|
|
233
|
+
}
|
|
234
|
+
}}
|
|
235
|
+
>
|
|
236
|
+
<PopoverTrigger asChild>
|
|
158
237
|
<Button variant="dashed" size="sm" aria-pressed={hasSelection} className={className}>
|
|
159
238
|
<Plus />
|
|
160
239
|
{title}
|
|
@@ -177,8 +256,14 @@ export function FacetedFilter({
|
|
|
177
256
|
role="button"
|
|
178
257
|
tabIndex={-1}
|
|
179
258
|
aria-label={`Clear ${title} filter`}
|
|
259
|
+
// Both handlers: pointerdown suppresses the focus shift, and
|
|
260
|
+
// PopoverTrigger toggles on click, so the click must be
|
|
261
|
+
// swallowed here or clearing would also open the list.
|
|
180
262
|
onPointerDown={(event) => {
|
|
181
|
-
|
|
263
|
+
event.preventDefault();
|
|
264
|
+
event.stopPropagation();
|
|
265
|
+
}}
|
|
266
|
+
onClick={(event) => {
|
|
182
267
|
event.preventDefault();
|
|
183
268
|
event.stopPropagation();
|
|
184
269
|
onClear();
|
|
@@ -190,33 +275,89 @@ export function FacetedFilter({
|
|
|
190
275
|
</>
|
|
191
276
|
)}
|
|
192
277
|
</Button>
|
|
193
|
-
</
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
278
|
+
</PopoverTrigger>
|
|
279
|
+
{/* Width follows the content (nowrap rows, scroll past the cap) so long
|
|
280
|
+
* values read on one line instead of wrapping. */}
|
|
281
|
+
{/* gap-0 resets PopoverContent's own `flex flex-col gap-4`, which would
|
|
282
|
+
* otherwise put 16px between the title, search box, list and clear row.
|
|
283
|
+
* onKeyDown sits here, not on the listbox: Radix focuses the content
|
|
284
|
+
* container on open, so a handler on a child never sees the first
|
|
285
|
+
* arrow. */}
|
|
286
|
+
<PopoverContent
|
|
287
|
+
align="start"
|
|
288
|
+
onKeyDown={onListKeyDown}
|
|
289
|
+
className="w-auto max-w-96 min-w-48 gap-0 p-1"
|
|
290
|
+
>
|
|
291
|
+
<p className="px-2 py-1.5 text-xs font-medium text-muted-foreground">{title}</p>
|
|
292
|
+
{showSearch && (
|
|
293
|
+
<Input
|
|
294
|
+
value={query}
|
|
295
|
+
onChange={(event) => {
|
|
296
|
+
setQuery(event.target.value);
|
|
297
|
+
setActive(0);
|
|
298
|
+
}}
|
|
299
|
+
placeholder={`Filter ${title.toLowerCase()}…`}
|
|
300
|
+
aria-label={`Filter ${title} options`}
|
|
301
|
+
className="mb-1 h-control-sm"
|
|
302
|
+
/>
|
|
303
|
+
)}
|
|
304
|
+
<div ref={listRef} role="listbox" aria-multiselectable className="max-h-72 overflow-auto">
|
|
305
|
+
{matches.map((option, index) => {
|
|
306
|
+
const checked = selected.has(option.value);
|
|
307
|
+
return (
|
|
308
|
+
<button
|
|
309
|
+
key={option.value}
|
|
310
|
+
type="button"
|
|
311
|
+
role="option"
|
|
312
|
+
aria-selected={checked}
|
|
313
|
+
// Clamped: filtering the list can shrink it past `active`, and
|
|
314
|
+
// an out-of-range index would leave no tab stop at all.
|
|
315
|
+
tabIndex={index === Math.min(active, matches.length - 1) ? 0 : -1}
|
|
316
|
+
onFocus={() => setActive(index)}
|
|
317
|
+
onClick={() => onSelect(option.value, !checked)}
|
|
318
|
+
className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-ui whitespace-nowrap outline-none hover:bg-fill-2 focus-visible:bg-fill-2"
|
|
319
|
+
>
|
|
320
|
+
{/* Not the Checkbox component: that renders a <button>, and
|
|
321
|
+
* nesting it in the option button is invalid HTML. */}
|
|
322
|
+
<span
|
|
323
|
+
aria-hidden="true"
|
|
324
|
+
className={cn(
|
|
325
|
+
'grid size-[15px] shrink-0 place-content-center rounded-[4px] border transition-[background-color,border-color] duration-100',
|
|
326
|
+
checked
|
|
327
|
+
? 'border-primary bg-primary text-primary-foreground'
|
|
328
|
+
: 'border-input bg-card',
|
|
329
|
+
)}
|
|
330
|
+
>
|
|
331
|
+
{checked && <CheckIcon className="size-2.5 stroke-[3.5]" />}
|
|
332
|
+
</span>
|
|
333
|
+
{option.icon && <option.icon className="size-4 text-muted-foreground" />}
|
|
334
|
+
{option.label}
|
|
335
|
+
{option.count !== undefined && (
|
|
336
|
+
<span className="ms-auto ps-3 text-xs text-muted-foreground">{option.count}</span>
|
|
337
|
+
)}
|
|
338
|
+
</button>
|
|
339
|
+
);
|
|
340
|
+
})}
|
|
341
|
+
</div>
|
|
342
|
+
{matches.length === 0 && (
|
|
343
|
+
<p className="px-2 py-3 text-ui text-muted-foreground">No matches</p>
|
|
344
|
+
)}
|
|
345
|
+
{/* selected, not hasSelection: a value applied but absent from
|
|
346
|
+
* `options` renders no chip, and this row is then the only way out. */}
|
|
210
347
|
{selected.size > 0 && (
|
|
211
348
|
<>
|
|
212
|
-
<
|
|
213
|
-
<
|
|
349
|
+
<Separator className="my-1" />
|
|
350
|
+
<button
|
|
351
|
+
type="button"
|
|
352
|
+
onClick={() => onClear()}
|
|
353
|
+
className="w-full rounded-sm px-2 py-1.5 text-ui outline-none hover:bg-fill-2 focus-visible:bg-fill-2"
|
|
354
|
+
>
|
|
214
355
|
Clear filter
|
|
215
|
-
</
|
|
356
|
+
</button>
|
|
216
357
|
</>
|
|
217
358
|
)}
|
|
218
|
-
</
|
|
219
|
-
</
|
|
359
|
+
</PopoverContent>
|
|
360
|
+
</Popover>
|
|
220
361
|
);
|
|
221
362
|
}
|
|
222
363
|
|
|
@@ -226,19 +367,44 @@ export function DataTableFacetedFilter<TData>({
|
|
|
226
367
|
column,
|
|
227
368
|
title,
|
|
228
369
|
options,
|
|
370
|
+
searchable,
|
|
371
|
+
hideEmpty,
|
|
229
372
|
}: {
|
|
230
373
|
column: Column<TData, unknown>;
|
|
231
374
|
title: string;
|
|
232
375
|
options: DataTableFilterOption[];
|
|
376
|
+
searchable?: boolean;
|
|
377
|
+
hideEmpty?: boolean;
|
|
233
378
|
}) {
|
|
234
|
-
const facets = column.getFacetedUniqueValues();
|
|
235
379
|
const selected = new Set((column.getFilterValue() as string[] | undefined) ?? []);
|
|
236
380
|
|
|
381
|
+
/* Counted here rather than via getFacetedUniqueValues(): faceted columns hold
|
|
382
|
+
* an array per row so `arrIncludesSome` can match, and that helper keys the
|
|
383
|
+
* facet map on the whole array — ['Piping','Structural'] — so a lookup of a
|
|
384
|
+
* single value never hits and every count renders 0. The faceted row model
|
|
385
|
+
* also excludes this column's own filter, which keeps counts stable while
|
|
386
|
+
* values are being ticked. */
|
|
387
|
+
const facetedRows = column.getFacetedRowModel().flatRows;
|
|
388
|
+
const counts = React.useMemo(() => {
|
|
389
|
+
const tally = new Map<string, number>();
|
|
390
|
+
for (const row of facetedRows) {
|
|
391
|
+
const value = row.getValue(column.id);
|
|
392
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
393
|
+
if (item == null || item === '') continue;
|
|
394
|
+
const key = String(item);
|
|
395
|
+
tally.set(key, (tally.get(key) ?? 0) + 1);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return tally;
|
|
399
|
+
}, [facetedRows, column.id]);
|
|
400
|
+
|
|
237
401
|
return (
|
|
238
402
|
<FacetedFilter
|
|
239
403
|
title={title}
|
|
240
|
-
options={options.map((option) => ({ ...option, count:
|
|
404
|
+
options={options.map((option) => ({ ...option, count: counts.get(option.value) ?? 0 }))}
|
|
241
405
|
selected={selected}
|
|
406
|
+
searchable={searchable}
|
|
407
|
+
hideEmpty={hideEmpty}
|
|
242
408
|
onSelect={(value, checked) => {
|
|
243
409
|
const next = new Set(selected);
|
|
244
410
|
if (checked) {
|
|
@@ -363,6 +529,8 @@ export function DataTable<TData, TValue>({
|
|
|
363
529
|
getFilteredRowModel: getFilteredRowModel(),
|
|
364
530
|
getSortedRowModel: getSortedRowModel(),
|
|
365
531
|
getFacetedRowModel: getFacetedRowModel(),
|
|
532
|
+
// Kept for consumers even though the faceted filter counts from the row
|
|
533
|
+
// model instead (this helper keys array cells on the whole array).
|
|
366
534
|
getFacetedUniqueValues: getFacetedUniqueValues(),
|
|
367
535
|
});
|
|
368
536
|
|
|
@@ -391,6 +559,8 @@ export function DataTable<TData, TValue>({
|
|
|
391
559
|
column={column}
|
|
392
560
|
title={filter.title}
|
|
393
561
|
options={filter.options}
|
|
562
|
+
searchable={filter.searchable}
|
|
563
|
+
hideEmpty={filter.hideEmpty}
|
|
394
564
|
/>
|
|
395
565
|
);
|
|
396
566
|
})}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { CalendarIcon, ChevronDownIcon } from 'lucide-react';
|
|
5
|
+
|
|
6
|
+
import { cn } from '@rise-x/ui/lib/utils';
|
|
7
|
+
import { Calendar, type CalendarDay } from '@rise-x/ui/components/calendar';
|
|
8
|
+
import { Popover, PopoverContent, PopoverTrigger } from '@rise-x/ui/components/popover';
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* Calendar plus Popover plus a field trigger. Calendar is deliberately dumb — it
|
|
12
|
+
* renders whatever days it is handed — so the month arithmetic lives here, once,
|
|
13
|
+
* instead of in every app that needs a date.
|
|
14
|
+
*
|
|
15
|
+
* DATES ARE BUILT FROM PARTS, NEVER PARSED FROM A DATE-ONLY STRING.
|
|
16
|
+
* `new Date("2027-08-31")` is parsed as UTC midnight, so anywhere west of London
|
|
17
|
+
* it renders as the 30th. Every Date in this file is constructed with
|
|
18
|
+
* `new Date(y, m, d)` (local midnight) and compared on its local parts. If you
|
|
19
|
+
* add a formatter, keep it on the parts too.
|
|
20
|
+
*
|
|
21
|
+
* The trigger is a FIELD, so it takes the brand focus ring like Input and Select —
|
|
22
|
+
* a control you commit data into, not one you merely operate.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const MONTHS = [
|
|
26
|
+
'January',
|
|
27
|
+
'February',
|
|
28
|
+
'March',
|
|
29
|
+
'April',
|
|
30
|
+
'May',
|
|
31
|
+
'June',
|
|
32
|
+
'July',
|
|
33
|
+
'August',
|
|
34
|
+
'September',
|
|
35
|
+
'October',
|
|
36
|
+
'November',
|
|
37
|
+
'December',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/** Monday-first, matching Calendar's own day-of-week header. */
|
|
41
|
+
function startOffset(year: number, month: number) {
|
|
42
|
+
const jsDay = new Date(year, month, 1).getDay(); // 0 = Sunday
|
|
43
|
+
return (jsDay + 6) % 7;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function daysInMonth(year: number, month: number) {
|
|
47
|
+
// day 0 of the next month is the last day of this one
|
|
48
|
+
return new Date(year, month + 1, 0).getDate();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* Normalise the parts before comparing: buildDays pushes outside days with
|
|
52
|
+
* unnormalised indexes (month - 1 is -1 in January, month + 1 is 12 in
|
|
53
|
+
* December) and the year of the VIEW, while getMonth() only returns 0-11. Raw
|
|
54
|
+
* comparison therefore misses the selection and today ring on the adjacent
|
|
55
|
+
* month's cells at a year boundary — the same class as the trailing-day
|
|
56
|
+
* numbering bug. new Date(y, m, d) rolls both into range, which is why the
|
|
57
|
+
* range highlight (via dayValue) was never affected. */
|
|
58
|
+
function sameDay(a: Date | undefined, y: number, m: number, d: number) {
|
|
59
|
+
if (!a) return false;
|
|
60
|
+
const b = new Date(y, m, d);
|
|
61
|
+
return (
|
|
62
|
+
a.getFullYear() === b.getFullYear() &&
|
|
63
|
+
a.getMonth() === b.getMonth() &&
|
|
64
|
+
a.getDate() === b.getDate()
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Local-midnight comparison, so a time component can never flip the answer. */
|
|
69
|
+
function dayValue(y: number, m: number, d: number) {
|
|
70
|
+
return new Date(y, m, d).setHours(0, 0, 0, 0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface DatePickerRange {
|
|
74
|
+
from?: Date;
|
|
75
|
+
to?: Date;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function buildDays(
|
|
79
|
+
year: number,
|
|
80
|
+
month: number,
|
|
81
|
+
selected: Date | undefined,
|
|
82
|
+
range: DatePickerRange | undefined,
|
|
83
|
+
): Array<CalendarDay & { y: number; m: number; d: number }> {
|
|
84
|
+
const out: Array<CalendarDay & { y: number; m: number; d: number }> = [];
|
|
85
|
+
const lead = startOffset(year, month);
|
|
86
|
+
const prevCount = daysInMonth(year, month - 1);
|
|
87
|
+
const today = new Date();
|
|
88
|
+
|
|
89
|
+
const push = (y: number, m: number, d: number, outside: boolean) => {
|
|
90
|
+
const v = dayValue(y, m, d);
|
|
91
|
+
let position: CalendarDay['range'];
|
|
92
|
+
if (range?.from && range?.to) {
|
|
93
|
+
const from = dayValue(range.from.getFullYear(), range.from.getMonth(), range.from.getDate());
|
|
94
|
+
const to = dayValue(range.to.getFullYear(), range.to.getMonth(), range.to.getDate());
|
|
95
|
+
if (v === from) position = 'start';
|
|
96
|
+
else if (v === to) position = 'end';
|
|
97
|
+
else if (v > from && v < to) position = 'in';
|
|
98
|
+
} else if (
|
|
99
|
+
range?.from &&
|
|
100
|
+
dayValue(range.from.getFullYear(), range.from.getMonth(), range.from.getDate()) === v
|
|
101
|
+
) {
|
|
102
|
+
position = 'start';
|
|
103
|
+
} else if (sameDay(selected, y, m, d)) {
|
|
104
|
+
// a single selected date is drawn as a one-day span, so selection looks the
|
|
105
|
+
// same whether the picker is in single or range mode
|
|
106
|
+
position = 'start';
|
|
107
|
+
}
|
|
108
|
+
out.push({
|
|
109
|
+
label: d,
|
|
110
|
+
outside: outside || undefined,
|
|
111
|
+
today: sameDay(today, y, m, d) || undefined,
|
|
112
|
+
range: position,
|
|
113
|
+
y,
|
|
114
|
+
m,
|
|
115
|
+
d,
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
for (let i = lead; i > 0; i--) push(year, month - 1, prevCount - i + 1, true);
|
|
120
|
+
for (let d = 1; d <= daysInMonth(year, month); d++) push(year, month, d, false);
|
|
121
|
+
// fill to a whole number of weeks so the grid never reflows between months.
|
|
122
|
+
// The trailing days count from 1, so they need their own counter — deriving
|
|
123
|
+
// the number from out.length numbered them by grid position instead.
|
|
124
|
+
for (let d = 1; out.length % 7 !== 0; d++) push(year, month + 1, d, true);
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const DatePicker = React.forwardRef<
|
|
129
|
+
HTMLButtonElement,
|
|
130
|
+
Omit<React.ComponentProps<'button'>, 'value' | 'onChange'> & {
|
|
131
|
+
value?: Date;
|
|
132
|
+
onValueChange?: (date: Date) => void;
|
|
133
|
+
/** Renders a range span instead of a single day. Selection stays the caller's. */
|
|
134
|
+
range?: DatePickerRange;
|
|
135
|
+
placeholder?: string;
|
|
136
|
+
/** Formats the trigger label. Keep it on the date's local parts. */
|
|
137
|
+
format?: (date: Date) => string;
|
|
138
|
+
footer?: React.ReactNode;
|
|
139
|
+
}
|
|
140
|
+
>(function DatePicker(
|
|
141
|
+
{ className, value, onValueChange, range, placeholder = 'Pick a date', format, footer, ...props },
|
|
142
|
+
ref,
|
|
143
|
+
) {
|
|
144
|
+
const [open, setOpen] = React.useState(false);
|
|
145
|
+
const anchor = value ?? range?.from ?? new Date();
|
|
146
|
+
const [view, setView] = React.useState({ y: anchor.getFullYear(), m: anchor.getMonth() });
|
|
147
|
+
|
|
148
|
+
/* Follow the selection when it changes from outside — a preset button or a
|
|
149
|
+
* form reset writes a new value while the popover is closed, and reopening
|
|
150
|
+
* on the month captured at first render hides the day that was just picked.
|
|
151
|
+
* Only the anchor's month is a dependency, so paging stays the user's. */
|
|
152
|
+
const anchorMonth = `${anchor.getFullYear()}-${anchor.getMonth()}`;
|
|
153
|
+
React.useEffect(() => {
|
|
154
|
+
const [y, m] = anchorMonth.split('-').map(Number);
|
|
155
|
+
setView({ y, m });
|
|
156
|
+
}, [anchorMonth]);
|
|
157
|
+
|
|
158
|
+
const label = React.useMemo(() => {
|
|
159
|
+
const show = (d: Date) =>
|
|
160
|
+
format ? format(d) : `${d.getDate()} ${MONTHS[d.getMonth()].slice(0, 3)} ${d.getFullYear()}`;
|
|
161
|
+
if (range?.from && range?.to) return `${show(range.from)} – ${show(range.to)}`;
|
|
162
|
+
if (value) return show(value);
|
|
163
|
+
return null;
|
|
164
|
+
}, [value, range?.from, range?.to, format]);
|
|
165
|
+
|
|
166
|
+
const days = buildDays(view.y, view.m, value, range);
|
|
167
|
+
const step = (delta: number) => {
|
|
168
|
+
const next = new Date(view.y, view.m + delta, 1);
|
|
169
|
+
setView({ y: next.getFullYear(), m: next.getMonth() });
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
174
|
+
<PopoverTrigger asChild>
|
|
175
|
+
<button
|
|
176
|
+
ref={ref}
|
|
177
|
+
type="button"
|
|
178
|
+
data-slot="date-picker-trigger"
|
|
179
|
+
data-placeholder={label ? undefined : ''}
|
|
180
|
+
className={cn(
|
|
181
|
+
'flex h-control w-full items-center gap-1.5 rounded-lg border border-input bg-card px-2.5 text-ui whitespace-nowrap transition-[border-color,box-shadow] duration-100 outline-none',
|
|
182
|
+
'focus-visible:border-brand focus-visible:ring-3 focus-visible:ring-brand/12',
|
|
183
|
+
'disabled:cursor-not-allowed disabled:bg-muted disabled:text-subtle',
|
|
184
|
+
'aria-invalid:border-error aria-invalid:focus-visible:border-error aria-invalid:focus-visible:ring-error/18',
|
|
185
|
+
'[&>svg]:pointer-events-none [&>svg]:size-3.5 [&>svg]:shrink-0 [&>svg]:text-subtle',
|
|
186
|
+
className,
|
|
187
|
+
)}
|
|
188
|
+
{...props}
|
|
189
|
+
>
|
|
190
|
+
<CalendarIcon />
|
|
191
|
+
{/* muted-foreground, the same placeholder tone as Select and Combobox */}
|
|
192
|
+
<span className={cn('min-w-0 flex-1 text-left', !label && 'text-muted-foreground')}>
|
|
193
|
+
{label ?? placeholder}
|
|
194
|
+
</span>
|
|
195
|
+
<ChevronDownIcon className="text-muted-foreground!" />
|
|
196
|
+
</button>
|
|
197
|
+
</PopoverTrigger>
|
|
198
|
+
{/* Popover's own padding and width would fight the calendar, which brings
|
|
199
|
+
its own 14px padding and fixed 294px width. */}
|
|
200
|
+
<PopoverContent align="start" className="w-auto gap-0 p-0 shadow-none">
|
|
201
|
+
<Calendar
|
|
202
|
+
title={`${MONTHS[view.m]} ${view.y}`}
|
|
203
|
+
days={days}
|
|
204
|
+
onPrev={() => step(-1)}
|
|
205
|
+
onNext={() => step(1)}
|
|
206
|
+
onSelectDay={(_day, index) => {
|
|
207
|
+
const picked = days[index];
|
|
208
|
+
onValueChange?.(new Date(picked.y, picked.m, picked.d));
|
|
209
|
+
setOpen(false);
|
|
210
|
+
}}
|
|
211
|
+
footer={footer}
|
|
212
|
+
/>
|
|
213
|
+
</PopoverContent>
|
|
214
|
+
</Popover>
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
export { DatePicker };
|
|
@@ -72,7 +72,7 @@ const DialogContent = React.forwardRef<
|
|
|
72
72
|
ref={ref}
|
|
73
73
|
data-slot="dialog-content"
|
|
74
74
|
className={cn(
|
|
75
|
-
'fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-
|
|
75
|
+
'fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-(var(--spacing)*8))] -translate-x-1/2 -translate-y-1/2 gap-6 rounded-2xl bg-popover p-6 text-ui text-popover-foreground shadow-pop duration-100 outline-none sm:max-w-md data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95',
|
|
76
76
|
className,
|
|
77
77
|
)}
|
|
78
78
|
{...props}
|
|
@@ -150,7 +150,7 @@ const DialogDescription = React.forwardRef<
|
|
|
150
150
|
ref={ref}
|
|
151
151
|
data-slot="dialog-description"
|
|
152
152
|
className={cn(
|
|
153
|
-
'text-
|
|
153
|
+
'text-ui text-muted-foreground *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground',
|
|
154
154
|
className,
|
|
155
155
|
)}
|
|
156
156
|
{...props}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Direction as DirectionPrimitive } from 'radix-ui';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Reading direction for every Radix primitive beneath it. Not a visual component
|
|
6
|
+
* — it renders no DOM node at all — but it is the switch that makes the whole kit
|
|
7
|
+
* behave in RTL: menus align from the other edge, sliders fill the other way,
|
|
8
|
+
* and arrow keys reverse.
|
|
9
|
+
*
|
|
10
|
+
* WHY IT HAS TO BE EXPLICIT. Radix reads direction from this provider, not from
|
|
11
|
+
* the `dir` attribute on `<html>`, so setting `dir="rtl"` on the document flips
|
|
12
|
+
* CSS but leaves every popper anchored and every arrow key pointing the wrong
|
|
13
|
+
* way. The host owns the document attribute; this owns the primitives. Set both.
|
|
14
|
+
*
|
|
15
|
+
* Mount it once, above the app — a federated app inherits it from the host along
|
|
16
|
+
* with everything else, so an app should not normally render its own.
|
|
17
|
+
*/
|
|
18
|
+
function DirectionProvider({ dir, children }: { dir: 'ltr' | 'rtl'; children?: React.ReactNode }) {
|
|
19
|
+
return <DirectionPrimitive.Provider dir={dir}>{children}</DirectionPrimitive.Provider>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { DirectionProvider };
|
|
@@ -33,7 +33,7 @@ function DropdownMenuContent({
|
|
|
33
33
|
sideOffset={sideOffset}
|
|
34
34
|
align={align}
|
|
35
35
|
className={cn(
|
|
36
|
-
'z-50 max-h-(--radix-dropdown-menu-content-available-height) w-(--radix-dropdown-menu-trigger-width) min-w-48 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-xl bg-popover p-1.5 text-popover-foreground shadow-pop duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:overflow-hidden
|
|
36
|
+
'z-50 max-h-(--radix-dropdown-menu-content-available-height) w-(--radix-dropdown-menu-trigger-width) min-w-48 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-xl bg-popover p-1.5 text-popover-foreground shadow-pop duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:overflow-hidden data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95',
|
|
37
37
|
className,
|
|
38
38
|
)}
|
|
39
39
|
{...props}
|
|
@@ -218,7 +218,7 @@ function DropdownMenuSubContent({
|
|
|
218
218
|
<DropdownMenuPrimitive.SubContent
|
|
219
219
|
data-slot="dropdown-menu-sub-content"
|
|
220
220
|
className={cn(
|
|
221
|
-
'z-50 min-w-36 origin-(--radix-dropdown-menu-content-transform-origin) overflow-hidden rounded-xl bg-popover p-1.5 text-popover-foreground shadow-pop duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2
|
|
221
|
+
'z-50 min-w-36 origin-(--radix-dropdown-menu-content-transform-origin) overflow-hidden rounded-xl bg-popover p-1.5 text-popover-foreground shadow-pop duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95',
|
|
222
222
|
className,
|
|
223
223
|
)}
|
|
224
224
|
{...props}
|