@caqophony/ui 0.8.0 → 0.9.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/dist/index.d.mts CHANGED
@@ -1135,6 +1135,23 @@ declare const RadioGroup: React.ForwardRefExoticComponent<RadioGroupProps & Reac
1135
1135
  * reset. `defaultLabel` names a start value that is not among `options` (a
1136
1136
  * record past a list's first page, named from its own detail read).
1137
1137
  *
1138
+ * Keyboard (#6699, kit 0.9.0), the WAI-ARIA APG select-only combobox
1139
+ * (https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/):
1140
+ * the trigger is a `<button role="combobox">` that takes `id`, so a
1141
+ * `<label htmlFor>` names and focuses it. Enter, Space and ArrowDown open it;
1142
+ * the rows are `role="option"` in a `role="listbox"`, walked by
1143
+ * `aria-activedescendant` (ArrowUp/ArrowDown, Home/End on the trigger); Enter
1144
+ * picks; Escape closes. After a pick or Escape focus is back on the trigger.
1145
+ * With a search box, focus moves into it on open and it takes the same keys;
1146
+ * Enter and Space open with no row active, so Enter in the search box picks
1147
+ * nothing until a row is arrowed to, and never submits the form. `size`,
1148
+ * `error` and `errorMessageId` draw the field as `Select` draws them.
1149
+ *
1150
+ * Load more (#6691): with `hasMore`, the list ends with a "Load more" option
1151
+ * (a button, reached by the arrows as a row) that calls `onLoadMore`; the
1152
+ * caller appends the next page to `options`, and the list stays open on the
1153
+ * first new row.
1154
+ *
1138
1155
  * Issue #2633 — B-7: Advanced Form Family
1139
1156
  * Authored by Caqophony
1140
1157
  */
@@ -1173,6 +1190,20 @@ interface ComboboxProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onCh
1173
1190
  onSearchChange?: (text: string) => void;
1174
1191
  /** How long typing pauses before `onSearchChange` is called (ms). */
1175
1192
  searchDelayMs?: number;
1193
+ /** The id of the trigger (the focusable `role="combobox"`), for `<label htmlFor>`. */
1194
+ id?: string;
1195
+ /** The field's size, as `Select`'s. */
1196
+ size?: FieldSize;
1197
+ error?: boolean;
1198
+ /** ID of the element describing the error, for aria-describedby */
1199
+ errorMessageId?: string;
1200
+ /**
1201
+ * More rows exist past `options`: the list ends with a "Load more" option
1202
+ * that calls `onLoadMore`, and the caller appends the next page to `options`.
1203
+ */
1204
+ hasMore?: boolean;
1205
+ /** Called by the "Load more" option (with `hasMore`). */
1206
+ onLoadMore?: () => void;
1176
1207
  }
1177
1208
  declare const Combobox: React.ForwardRefExoticComponent<ComboboxProps & React.RefAttributes<HTMLDivElement>>;
1178
1209
 
package/dist/index.js CHANGED
@@ -60,6 +60,14 @@ var FIELD_HEIGHT = {
60
60
  md: "h-11",
61
61
  lg: "h-12"
62
62
  };
63
+ var PICKER_CHEVRON = {
64
+ sm: { endPadding: "pr-8", inset: "right-2.5" },
65
+ md: { endPadding: "pr-9", inset: "right-3" },
66
+ lg: { endPadding: "pr-10", inset: "right-4" }
67
+ };
68
+ function pickerChevron(size) {
69
+ return PICKER_CHEVRON[size];
70
+ }
63
71
  function fieldAria(error, errorMessageId) {
64
72
  return {
65
73
  "aria-invalid": error || void 0,
@@ -3331,6 +3339,12 @@ RadioGroup.displayName = "RadioGroup";
3331
3339
  import * as React23 from "react";
3332
3340
  import { Fragment as Fragment7, jsx as jsx34, jsxs as jsxs27 } from "react/jsx-runtime";
3333
3341
  var SEARCH_DELAY_MS = 250;
3342
+ var MULTI_MIN_HEIGHT = {
3343
+ sm: "min-h-9",
3344
+ md: "min-h-11",
3345
+ lg: "min-h-12"
3346
+ };
3347
+ var NONE = -1;
3334
3348
  var Combobox = React23.forwardRef(
3335
3349
  ({
3336
3350
  className,
@@ -3345,12 +3359,25 @@ var Combobox = React23.forwardRef(
3345
3359
  filterable = true,
3346
3360
  onSearchChange,
3347
3361
  searchDelayMs = SEARCH_DELAY_MS,
3362
+ id,
3363
+ size = "md",
3364
+ error,
3365
+ errorMessageId,
3366
+ hasMore = false,
3367
+ onLoadMore,
3348
3368
  ...props
3349
3369
  }, ref) => {
3350
3370
  const [open, setOpen] = React23.useState(false);
3351
3371
  const [search, setSearch] = React23.useState("");
3352
3372
  const searchesOnServer = onSearchChange !== void 0;
3353
3373
  const showsSearch = searchesOnServer || filterable;
3374
+ const autoId = React23.useId();
3375
+ const triggerId = id ?? `${autoId}-trigger`;
3376
+ const listboxId = `${triggerId}-listbox`;
3377
+ const optionId = (index) => `${listboxId}-option-${index}`;
3378
+ const loadMoreId = `${listboxId}-load-more`;
3379
+ const triggerRef = React23.useRef(null);
3380
+ const pressed = React23.useRef(false);
3354
3381
  const onSearchRef = React23.useRef(onSearchChange);
3355
3382
  onSearchRef.current = onSearchChange;
3356
3383
  const sentRef = React23.useRef("");
@@ -3403,6 +3430,31 @@ var Combobox = React23.forwardRef(
3403
3430
  const filtered = filterable && !searchesOnServer ? options.filter(
3404
3431
  (o) => o.label.toLowerCase().includes(search.toLowerCase())
3405
3432
  ) : options;
3433
+ const rowCount = filtered.length + (hasMore ? 1 : 0);
3434
+ const [activeIndex, setActiveIndex] = React23.useState(NONE);
3435
+ const active = activeIndex < rowCount ? activeIndex : NONE;
3436
+ const activeId = !open || active === NONE ? void 0 : active === filtered.length ? loadMoreId : optionId(active);
3437
+ React23.useEffect(() => {
3438
+ if (activeId === void 0) return;
3439
+ document.getElementById(activeId)?.scrollIntoView({ block: "nearest" });
3440
+ }, [activeId]);
3441
+ const close = () => {
3442
+ setOpen(false);
3443
+ setActiveIndex(NONE);
3444
+ triggerRef.current?.focus();
3445
+ };
3446
+ const openList = (activeRow) => {
3447
+ setActiveIndex(activeRow);
3448
+ setOpen(true);
3449
+ };
3450
+ const pickedRow = () => {
3451
+ const at = filtered.findIndex((o) => o.value === pick);
3452
+ return at === NONE ? 0 : at;
3453
+ };
3454
+ const loadMore = () => {
3455
+ setActiveIndex(filtered.length);
3456
+ onLoadMore?.();
3457
+ };
3406
3458
  const handleSelect = (optValue) => {
3407
3459
  if (!interactive) return;
3408
3460
  const picked = options.find((o) => o.value === optValue);
@@ -3414,8 +3466,71 @@ var Combobox = React23.forwardRef(
3414
3466
  );
3415
3467
  } else {
3416
3468
  commit(optValue);
3417
- setOpen(false);
3418
3469
  setSearch("");
3470
+ close();
3471
+ }
3472
+ };
3473
+ const chooseActive = () => {
3474
+ if (active === NONE) return;
3475
+ if (active === filtered.length) loadMore();
3476
+ else handleSelect(filtered[active].value);
3477
+ };
3478
+ const onListKey = (e, inSearch) => {
3479
+ const last = rowCount - 1;
3480
+ switch (e.key) {
3481
+ case "ArrowDown":
3482
+ e.preventDefault();
3483
+ setActiveIndex(active === NONE ? 0 : Math.min(active + 1, last));
3484
+ return;
3485
+ case "ArrowUp":
3486
+ e.preventDefault();
3487
+ setActiveIndex(active === NONE ? last : Math.max(active - 1, 0));
3488
+ return;
3489
+ case "Home":
3490
+ case "End":
3491
+ if (inSearch) return;
3492
+ e.preventDefault();
3493
+ setActiveIndex(e.key === "Home" ? 0 : last);
3494
+ return;
3495
+ case "Enter":
3496
+ e.preventDefault();
3497
+ chooseActive();
3498
+ return;
3499
+ case " ":
3500
+ if (inSearch) return;
3501
+ e.preventDefault();
3502
+ chooseActive();
3503
+ return;
3504
+ case "Escape":
3505
+ e.preventDefault();
3506
+ e.stopPropagation();
3507
+ close();
3508
+ return;
3509
+ default:
3510
+ return;
3511
+ }
3512
+ };
3513
+ const onTriggerKeyDown = (e) => {
3514
+ if (open) {
3515
+ onListKey(e, false);
3516
+ return;
3517
+ }
3518
+ switch (e.key) {
3519
+ case "Enter":
3520
+ case " ":
3521
+ e.preventDefault();
3522
+ openList(NONE);
3523
+ return;
3524
+ case "ArrowDown":
3525
+ e.preventDefault();
3526
+ openList(pickedRow());
3527
+ return;
3528
+ case "ArrowUp":
3529
+ e.preventDefault();
3530
+ openList(pick === "" ? Math.max(rowCount - 1, 0) : pickedRow());
3531
+ return;
3532
+ default:
3533
+ return;
3419
3534
  }
3420
3535
  };
3421
3536
  const displayValue = multi || typeof selected !== "string" ? null : labelOf(selected);
@@ -3423,10 +3538,27 @@ var Combobox = React23.forwardRef(
3423
3538
  /* @__PURE__ */ jsxs27(
3424
3539
  "div",
3425
3540
  {
3426
- className: "flex min-h-9 cursor-pointer items-center gap-1 rounded-md border border-input bg-background px-3 py-1.5",
3427
- onClick: () => setOpen(!open),
3541
+ className: cn(
3542
+ "relative flex cursor-pointer items-center gap-1",
3543
+ multi ? MULTI_MIN_HEIGHT[size] : FIELD_HEIGHT[size],
3544
+ fieldVariants({ size, error: Boolean(error) }),
3545
+ pickerChevron(size).endPadding,
3546
+ "has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-offset-2",
3547
+ error ? "has-[:focus-visible]:ring-intent-error" : "has-[:focus-visible]:ring-ring"
3548
+ ),
3549
+ onPointerDown: () => {
3550
+ pressed.current = true;
3551
+ },
3552
+ onClick: () => {
3553
+ const byPointer = pressed.current;
3554
+ pressed.current = false;
3555
+ triggerRef.current?.focus();
3556
+ if (!byPointer) return;
3557
+ if (open) setActiveIndex(NONE);
3558
+ setOpen(!open);
3559
+ },
3428
3560
  children: [
3429
- multi && Array.isArray(selected) && selected.length > 0 ? /* @__PURE__ */ jsx34("div", { className: "flex flex-wrap gap-1", children: selected.map((v) => /* @__PURE__ */ jsxs27(
3561
+ multi && picks.length > 0 && /* @__PURE__ */ jsx34("div", { className: "flex flex-wrap gap-1", children: picks.map((v) => /* @__PURE__ */ jsxs27(
3430
3562
  "span",
3431
3563
  {
3432
3564
  className: "text-primary-text inline-flex items-center gap-1 rounded bg-primary/10 px-2 py-0.5 text-xs font-medium",
@@ -3439,6 +3571,7 @@ var Combobox = React23.forwardRef(
3439
3571
  "aria-label": `Remove ${labelOf(v) ?? v}`,
3440
3572
  onClick: (e) => {
3441
3573
  e.stopPropagation();
3574
+ pressed.current = false;
3442
3575
  handleSelect(v);
3443
3576
  },
3444
3577
  className: "text-primary-text hover:opacity-80",
@@ -3448,14 +3581,30 @@ var Combobox = React23.forwardRef(
3448
3581
  ]
3449
3582
  },
3450
3583
  v
3451
- )) }) : /* @__PURE__ */ jsx34(
3452
- "span",
3584
+ )) }),
3585
+ /* @__PURE__ */ jsx34(
3586
+ "button",
3453
3587
  {
3454
- className: cn(
3455
- "text-sm",
3456
- displayValue ? "text-foreground" : "text-muted-foreground"
3457
- ),
3458
- children: displayValue || placeholder
3588
+ ref: triggerRef,
3589
+ id: triggerId,
3590
+ type: "button",
3591
+ role: "combobox",
3592
+ "aria-expanded": open,
3593
+ "aria-controls": listboxId,
3594
+ "aria-haspopup": "listbox",
3595
+ "aria-activedescendant": activeId,
3596
+ ...fieldAria(error, errorMessageId),
3597
+ className: "min-w-0 flex-1 cursor-pointer truncate text-left focus-visible:outline-none",
3598
+ onKeyDown: onTriggerKeyDown,
3599
+ children: multi ? picks.length > 0 ? /* @__PURE__ */ jsx34("span", { className: "sr-only", children: picks.map((v) => labelOf(v) ?? v).join(", ") }) : /* @__PURE__ */ jsx34("span", { className: "text-muted-foreground", children: placeholder }) : /* @__PURE__ */ jsx34(
3600
+ "span",
3601
+ {
3602
+ className: cn(
3603
+ displayValue ? "text-foreground" : "text-muted-foreground"
3604
+ ),
3605
+ children: displayValue || placeholder
3606
+ }
3607
+ )
3459
3608
  }
3460
3609
  ),
3461
3610
  !multi && interactive && selected !== "" && /* @__PURE__ */ jsx34(
@@ -3463,22 +3612,24 @@ var Combobox = React23.forwardRef(
3463
3612
  {
3464
3613
  type: "button",
3465
3614
  "aria-label": "Clear",
3466
- className: "ml-auto text-xs text-muted-foreground hover:text-foreground",
3615
+ className: "text-xs text-muted-foreground hover:text-foreground",
3467
3616
  onClick: (e) => {
3468
3617
  e.stopPropagation();
3618
+ pressed.current = false;
3469
3619
  commit("");
3470
3620
  },
3471
3621
  children: "\u2715"
3472
3622
  }
3473
3623
  ),
3474
3624
  /* @__PURE__ */ jsx34(
3475
- "span",
3625
+ Icon,
3476
3626
  {
3627
+ name: "chevron-down",
3628
+ size: "sm",
3477
3629
  className: cn(
3478
- "text-muted-foreground",
3479
- (multi || !interactive || selected === "") && "ml-auto"
3480
- ),
3481
- children: "\u25BE"
3630
+ "pointer-events-none absolute top-1/2 -translate-y-1/2 text-muted-foreground",
3631
+ pickerChevron(size).inset
3632
+ )
3482
3633
  }
3483
3634
  )
3484
3635
  ]
@@ -3508,34 +3659,82 @@ var Combobox = React23.forwardRef(
3508
3659
  type: "text",
3509
3660
  className: "w-full rounded border-0 bg-transparent px-2 py-1 text-sm text-foreground outline-none placeholder:text-muted-foreground",
3510
3661
  placeholder: "Search...",
3662
+ "aria-label": "Search",
3663
+ "aria-controls": listboxId,
3664
+ "aria-autocomplete": "list",
3665
+ "aria-activedescendant": activeId,
3511
3666
  value: search,
3512
- onChange: (e) => setSearch(e.target.value),
3513
- onKeyDown: (e) => {
3514
- if (e.key === "Enter") e.preventDefault();
3667
+ onChange: (e) => {
3668
+ setSearch(e.target.value);
3669
+ setActiveIndex(NONE);
3515
3670
  },
3671
+ onKeyDown: (e) => onListKey(e, true),
3516
3672
  autoFocus: true
3517
3673
  }
3518
3674
  ) }),
3519
- /* @__PURE__ */ jsx34("div", { className: "max-h-60 overflow-y-auto p-1", children: filtered.length === 0 ? /* @__PURE__ */ jsx34("div", { className: "px-3 py-2 text-sm text-muted-foreground", children: "No results" }) : filtered.map((option) => {
3520
- const isSelected = multi ? Array.isArray(selected) && selected.includes(option.value) : selected === option.value;
3521
- return /* @__PURE__ */ jsxs27(
3522
- "button",
3523
- {
3524
- type: "button",
3525
- className: cn(
3526
- "flex w-full items-center rounded-sm px-3 py-2 text-left text-sm transition-colors",
3527
- isSelected ? "text-primary-text bg-primary/10 font-medium" : "text-foreground hover:bg-muted"
3675
+ /* @__PURE__ */ jsxs27(
3676
+ "div",
3677
+ {
3678
+ id: listboxId,
3679
+ role: "listbox",
3680
+ "aria-multiselectable": multi || void 0,
3681
+ "aria-labelledby": triggerId,
3682
+ className: "max-h-60 overflow-y-auto p-1",
3683
+ children: [
3684
+ filtered.length === 0 && /* @__PURE__ */ jsx34(
3685
+ "div",
3686
+ {
3687
+ role: "presentation",
3688
+ className: "px-3 py-2 text-sm text-muted-foreground",
3689
+ children: "No results"
3690
+ }
3528
3691
  ),
3529
- onClick: () => handleSelect(option.value),
3530
- children: [
3531
- multi && /* @__PURE__ */ jsx34("span", { className: "mr-2", children: isSelected ? "\u2611" : "\u2610" }),
3532
- option.label,
3533
- !multi && isSelected && /* @__PURE__ */ jsx34("span", { className: "text-primary-text ml-auto", children: "\u2713" })
3534
- ]
3535
- },
3536
- option.value
3537
- );
3538
- }) })
3692
+ filtered.map((option, index) => {
3693
+ const isSelected = multi ? picks.includes(option.value) : pick === option.value;
3694
+ return /* @__PURE__ */ jsxs27(
3695
+ "button",
3696
+ {
3697
+ id: optionId(index),
3698
+ type: "button",
3699
+ role: "option",
3700
+ "aria-selected": isSelected,
3701
+ tabIndex: -1,
3702
+ className: cn(
3703
+ "flex w-full items-center rounded-sm px-3 py-2 text-left text-sm transition-colors",
3704
+ isSelected ? "text-primary-text bg-primary/10 font-medium" : "text-foreground hover:bg-muted",
3705
+ index === active && "bg-muted"
3706
+ ),
3707
+ onMouseDown: (e) => e.preventDefault(),
3708
+ onClick: () => handleSelect(option.value),
3709
+ children: [
3710
+ multi && /* @__PURE__ */ jsx34("span", { className: "mr-2", children: isSelected ? "\u2611" : "\u2610" }),
3711
+ option.label,
3712
+ !multi && isSelected && /* @__PURE__ */ jsx34("span", { className: "text-primary-text ml-auto", children: "\u2713" })
3713
+ ]
3714
+ },
3715
+ option.value
3716
+ );
3717
+ }),
3718
+ hasMore && /* @__PURE__ */ jsx34(
3719
+ "button",
3720
+ {
3721
+ id: loadMoreId,
3722
+ type: "button",
3723
+ role: "option",
3724
+ "aria-selected": false,
3725
+ tabIndex: -1,
3726
+ className: cn(
3727
+ "flex w-full items-center justify-center rounded-sm px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",
3728
+ active === filtered.length && "bg-muted text-foreground"
3729
+ ),
3730
+ onMouseDown: (e) => e.preventDefault(),
3731
+ onClick: loadMore,
3732
+ children: "Load more"
3733
+ }
3734
+ )
3735
+ ]
3736
+ }
3737
+ )
3539
3738
  ] })
3540
3739
  ] });
3541
3740
  }
@@ -5023,16 +5222,6 @@ Textarea.displayName = "Textarea";
5023
5222
  // src/select.tsx
5024
5223
  import * as React43 from "react";
5025
5224
  import { jsx as jsx57, jsxs as jsxs46 } from "react/jsx-runtime";
5026
- var CHEVRON_END = {
5027
- sm: "right-2.5",
5028
- md: "right-3",
5029
- lg: "right-4"
5030
- };
5031
- var END_PADDING = {
5032
- sm: "pr-8",
5033
- md: "pr-9",
5034
- lg: "pr-10"
5035
- };
5036
5225
  var Select = React43.forwardRef(
5037
5226
  ({
5038
5227
  className,
@@ -5063,7 +5252,7 @@ var Select = React43.forwardRef(
5063
5252
  "scheme-light dark:scheme-dark block cursor-pointer appearance-none truncate",
5064
5253
  FIELD_HEIGHT[size],
5065
5254
  fieldVariants({ size, error: Boolean(error) }),
5066
- END_PADDING[size],
5255
+ pickerChevron(size).endPadding,
5067
5256
  'has-[option[value=""]:checked]:text-muted-foreground [&>option]:text-foreground',
5068
5257
  className
5069
5258
  ),
@@ -5082,7 +5271,7 @@ var Select = React43.forwardRef(
5082
5271
  size: "sm",
5083
5272
  className: cn(
5084
5273
  "pointer-events-none absolute top-1/2 -translate-y-1/2 text-muted-foreground",
5085
- CHEVRON_END[size]
5274
+ pickerChevron(size).inset
5086
5275
  )
5087
5276
  }
5088
5277
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caqophony/ui",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Generic, optional React UI reuse library (Gossamer tokens). Centrally maintained by Caqophony.",
5
5
  "license": "MIT",
6
6
  "author": "Caqophony",