@yuhuanowo/yunui 0.1.4 → 0.1.6

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.
@@ -2,7 +2,7 @@
2
2
  import { cn } from './chunk-DRZ7UCRU.js';
3
3
  import { useYunUI } from './chunk-XZGNL5A6.js';
4
4
  import * as React from 'react';
5
- import { forwardRef, useState, useEffect, useRef, useCallback } from 'react';
5
+ import { forwardRef, useState, useEffect, useRef, useCallback, useId } from 'react';
6
6
  import * as DialogPrimitive from '@radix-ui/react-dialog';
7
7
  import * as TooltipPrimitive from '@radix-ui/react-tooltip';
8
8
  import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
@@ -1378,8 +1378,18 @@ function CustomSelect({
1378
1378
  const resolvedPlaceholder = placeholder || t("placeholder");
1379
1379
  const [isOpen, setIsOpen] = useState(false);
1380
1380
  const [searchQuery, setSearchQuery] = useState("");
1381
+ const [highlighted, setHighlighted] = useState(-1);
1381
1382
  const containerRef = useRef(null);
1382
1383
  const inputRef = useRef(null);
1384
+ const triggerRef = useRef(null);
1385
+ const listRef = useRef(null);
1386
+ const baseId = useId();
1387
+ const listboxId = `${baseId}-listbox`;
1388
+ const optionId = (i) => `${baseId}-opt-${i}`;
1389
+ const selectedOption = options.find((o) => o.value === value);
1390
+ const filteredOptions = searchQuery ? options.filter(
1391
+ (o) => o.label.toLowerCase().includes(searchQuery.toLowerCase()) || o.value.toLowerCase().includes(searchQuery.toLowerCase())
1392
+ ) : options;
1383
1393
  useEffect(() => {
1384
1394
  const handleClickOutside = (e) => {
1385
1395
  if (containerRef.current && !containerRef.current.contains(e.target)) {
@@ -1391,35 +1401,98 @@ function CustomSelect({
1391
1401
  return () => document.removeEventListener("mousedown", handleClickOutside);
1392
1402
  }, []);
1393
1403
  useEffect(() => {
1394
- if (isOpen && searchable && inputRef.current) {
1395
- inputRef.current.focus();
1396
- }
1404
+ if (isOpen && searchable && inputRef.current) inputRef.current.focus();
1397
1405
  }, [isOpen, searchable]);
1398
- const selectedOption = options.find((o) => o.value === value);
1399
- const filteredOptions = searchQuery ? options.filter(
1400
- (o) => o.label.toLowerCase().includes(searchQuery.toLowerCase()) || o.value.toLowerCase().includes(searchQuery.toLowerCase())
1401
- ) : options;
1406
+ useEffect(() => {
1407
+ if (isOpen) {
1408
+ const sel = filteredOptions.findIndex((o) => o.value === value);
1409
+ setHighlighted(sel >= 0 ? sel : 0);
1410
+ } else {
1411
+ setHighlighted(-1);
1412
+ }
1413
+ }, [isOpen]);
1414
+ useEffect(() => {
1415
+ if (highlighted >= filteredOptions.length) setHighlighted(filteredOptions.length - 1);
1416
+ }, [filteredOptions.length, highlighted]);
1417
+ useEffect(() => {
1418
+ if (!isOpen || highlighted < 0 || !listRef.current) return;
1419
+ listRef.current.querySelector(`#${CSS.escape(optionId(highlighted))}`)?.scrollIntoView({ block: "nearest" });
1420
+ }, [highlighted, isOpen]);
1421
+ const open = () => !disabled && setIsOpen(true);
1422
+ const close = () => {
1423
+ setIsOpen(false);
1424
+ setSearchQuery("");
1425
+ triggerRef.current?.focus();
1426
+ };
1402
1427
  const handleSelect = (optionValue) => {
1403
1428
  onChange(optionValue);
1404
1429
  setIsOpen(false);
1405
1430
  setSearchQuery("");
1431
+ triggerRef.current?.focus();
1406
1432
  };
1407
- const handleWheel = (e) => {
1408
- const target = e.currentTarget;
1409
- const { scrollTop, scrollHeight, clientHeight } = target;
1410
- const isAtTop = scrollTop <= 1;
1411
- const isAtBottom = scrollTop + clientHeight >= scrollHeight - 1;
1412
- if (isAtTop && e.deltaY < 0 || isAtBottom && e.deltaY > 0) {
1413
- e.preventDefault();
1433
+ const handleKeyDown = (e) => {
1434
+ if (disabled) return;
1435
+ switch (e.key) {
1436
+ case "ArrowDown":
1437
+ e.preventDefault();
1438
+ if (!isOpen) return open();
1439
+ setHighlighted((h) => Math.min(h + 1, filteredOptions.length - 1));
1440
+ break;
1441
+ case "ArrowUp":
1442
+ e.preventDefault();
1443
+ if (!isOpen) return open();
1444
+ setHighlighted((h) => Math.max(h - 1, 0));
1445
+ break;
1446
+ case "Home":
1447
+ if (isOpen) {
1448
+ e.preventDefault();
1449
+ setHighlighted(0);
1450
+ }
1451
+ break;
1452
+ case "End":
1453
+ if (isOpen) {
1454
+ e.preventDefault();
1455
+ setHighlighted(filteredOptions.length - 1);
1456
+ }
1457
+ break;
1458
+ case "Enter":
1459
+ if (isOpen && filteredOptions[highlighted]) {
1460
+ e.preventDefault();
1461
+ handleSelect(filteredOptions[highlighted].value);
1462
+ } else if (!isOpen) {
1463
+ e.preventDefault();
1464
+ open();
1465
+ }
1466
+ break;
1467
+ case " ":
1468
+ if (!isOpen && !searchable) {
1469
+ e.preventDefault();
1470
+ open();
1471
+ }
1472
+ break;
1473
+ case "Escape":
1474
+ if (isOpen) {
1475
+ e.preventDefault();
1476
+ close();
1477
+ }
1478
+ break;
1479
+ case "Tab":
1480
+ if (isOpen) setIsOpen(false);
1481
+ break;
1414
1482
  }
1415
1483
  };
1416
- return /* @__PURE__ */ jsxs("div", { ref: containerRef, className: `relative ${className}`, children: [
1484
+ return /* @__PURE__ */ jsxs("div", { ref: containerRef, className: `relative ${className}`, onKeyDown: handleKeyDown, children: [
1417
1485
  /* @__PURE__ */ jsxs(
1418
1486
  "button",
1419
1487
  {
1488
+ ref: triggerRef,
1420
1489
  type: "button",
1421
1490
  onClick: () => !disabled && setIsOpen(!isOpen),
1422
1491
  disabled,
1492
+ "aria-haspopup": "listbox",
1493
+ "aria-expanded": isOpen,
1494
+ "aria-controls": isOpen ? listboxId : void 0,
1495
+ "aria-activedescendant": isOpen && !searchable && highlighted >= 0 ? optionId(highlighted) : void 0,
1423
1496
  className: `
1424
1497
  w-full flex items-center justify-between gap-2 px-3 py-2
1425
1498
  rounded-xl border border-(--border-default) bg-(--bg-elevated)
@@ -1451,6 +1524,11 @@ function CustomSelect({
1451
1524
  {
1452
1525
  ref: inputRef,
1453
1526
  type: "text",
1527
+ role: "combobox",
1528
+ "aria-expanded": isOpen,
1529
+ "aria-controls": listboxId,
1530
+ "aria-activedescendant": highlighted >= 0 ? optionId(highlighted) : void 0,
1531
+ "aria-autocomplete": "list",
1454
1532
  value: searchQuery,
1455
1533
  onChange: (e) => setSearchQuery(e.target.value),
1456
1534
  placeholder: t("search"),
@@ -1460,7 +1538,9 @@ function CustomSelect({
1460
1538
  searchQuery && /* @__PURE__ */ jsx(
1461
1539
  "button",
1462
1540
  {
1541
+ type: "button",
1463
1542
  onClick: () => setSearchQuery(""),
1543
+ "aria-label": t("clearSearch"),
1464
1544
  className: "absolute right-2.5 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors p-0.5 rounded-md hover:bg-(--bg-hover)",
1465
1545
  title: t("clearSearch"),
1466
1546
  children: /* @__PURE__ */ jsx(X, { size: 12 })
@@ -1470,29 +1550,39 @@ function CustomSelect({
1470
1550
  /* @__PURE__ */ jsx(
1471
1551
  "div",
1472
1552
  {
1473
- onWheel: handleWheel,
1553
+ ref: listRef,
1554
+ role: "listbox",
1555
+ id: listboxId,
1474
1556
  className: "max-h-52 overflow-y-auto overscroll-contain",
1475
- children: filteredOptions.length === 0 ? /* @__PURE__ */ jsx("div", { className: "px-3 py-2 text-sm text-muted-foreground text-center whitespace-nowrap", children: t("noOptions") }) : filteredOptions.map((option) => /* @__PURE__ */ jsxs(
1476
- "button",
1477
- {
1478
- type: "button",
1479
- onClick: () => handleSelect(option.value),
1480
- className: `
1481
- w-full flex items-center gap-2 px-3 py-2 text-sm text-left
1482
- hover:bg-(--bg-hover) transition-colors
1483
- ${option.value === value ? "bg-primary/10 text-primary" : ""}
1484
- `,
1485
- children: [
1486
- option.icon && /* @__PURE__ */ jsx("span", { className: "shrink-0 w-5 h-5 flex items-center justify-center", children: option.icon }),
1487
- /* @__PURE__ */ jsxs("div", { className: "flex-1 min-w-0", children: [
1488
- /* @__PURE__ */ jsx("div", { className: "truncate", children: option.label }),
1489
- option.description && /* @__PURE__ */ jsx("div", { className: "text-xs text-muted-foreground truncate", children: option.description })
1490
- ] }),
1491
- option.value === value && /* @__PURE__ */ jsx(Check, { size: 14, className: "shrink-0 text-primary" })
1492
- ]
1493
- },
1494
- option.value
1495
- ))
1557
+ children: filteredOptions.length === 0 ? /* @__PURE__ */ jsx("div", { className: "px-3 py-2 text-sm text-muted-foreground text-center whitespace-nowrap", children: t("noOptions") }) : filteredOptions.map((option, i) => {
1558
+ const isSelected = option.value === value;
1559
+ const isHigh = i === highlighted;
1560
+ return /* @__PURE__ */ jsxs(
1561
+ "button",
1562
+ {
1563
+ id: optionId(i),
1564
+ type: "button",
1565
+ role: "option",
1566
+ "aria-selected": isSelected,
1567
+ onClick: () => handleSelect(option.value),
1568
+ onMouseEnter: () => setHighlighted(i),
1569
+ className: `
1570
+ w-full flex items-center gap-2 px-3 py-2 text-sm text-left transition-colors
1571
+ ${isHigh ? "bg-(--bg-hover)" : ""}
1572
+ ${isSelected ? "bg-primary/10 text-primary" : ""}
1573
+ `,
1574
+ children: [
1575
+ option.icon && /* @__PURE__ */ jsx("span", { className: "shrink-0 w-5 h-5 flex items-center justify-center", children: option.icon }),
1576
+ /* @__PURE__ */ jsxs("div", { className: "flex-1 min-w-0", children: [
1577
+ /* @__PURE__ */ jsx("div", { className: "truncate", children: option.label }),
1578
+ option.description && /* @__PURE__ */ jsx("div", { className: "text-xs text-muted-foreground truncate", children: option.description })
1579
+ ] }),
1580
+ isSelected && /* @__PURE__ */ jsx(Check, { size: 14, className: "shrink-0 text-primary" })
1581
+ ]
1582
+ },
1583
+ option.value
1584
+ );
1585
+ })
1496
1586
  }
1497
1587
  )
1498
1588
  ] })
@@ -1835,5 +1925,5 @@ var toast = {
1835
1925
  };
1836
1926
 
1837
1927
  export { AnimatedNumber, Avatar, AvatarFallback, AvatarImage, Badge, BentoCard, BentoGrid, Button, Card, Checkbox, Collapsible, CollapsibleContent2 as CollapsibleContent, CollapsibleTrigger2 as CollapsibleTrigger, Combobox, ConfirmModal, CustomSelect, DeleteConfirmModal, Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuPortal, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, EmptyState, IconButton, Input, Label2 as Label, Marquee, Modal, MotionDiv, MotionSpan, NavTabs, PageLoader, Popover, PopoverClose2 as PopoverClose, PopoverContent, PopoverTrigger, Progress, RegenerateConfirmModal, SegmentedSelect, Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, Sheet, ShinyButton, Skeleton, Slider, Spinner, Switch, Tabs, TabsContent, TabsList, TabsTrigger, Textarea, Toaster, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, fadeIn, staggerContainer, staggerItem, toast, useBodyScrollLock, useEscapeKey, useModalBehavior };
1838
- //# sourceMappingURL=chunk-OHIFY64L.js.map
1839
- //# sourceMappingURL=chunk-OHIFY64L.js.map
1928
+ //# sourceMappingURL=chunk-FJ454NQS.js.map
1929
+ //# sourceMappingURL=chunk-FJ454NQS.js.map