Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 | 5x 5x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 371x 371x 371x 124x 124x 124x 620x 452x 169x 124x 267x 266x 241x 241x 262x 262x 262x 124x 95x 38x 124x 169x 1x 169x 124x 35x 35x 12x 23x 23x 124x 49x 49x 1x 48x 48x 1x 1x 47x 47x 47x 1x 1x 46x 46x 124x 33x 33x 33x 33x 33x 33x 33x 33x 32x 33x 33x 1x 1x 33x 32x 32x 33x 11x 11x 21x 6x 6x 15x 14x 14x 124x 54x 54x 54x 2x 2x 2x 2x 52x 52x 52x 52x 13x 13x 39x 39x 38x 39x 39x 39x 39x 28x 11x 2x 2x 9x 124x 6x 6x 6x 5x 5x 5x 5x 3x 124x 88x 88x 15x 15x 15x 72x 72x 67x 67x 5x 5x 3x 3x 1x 5x 5x 88x 124x 190x 190x 190x 190x 190x 190x 65x 65x 65x 65x 1x 1x 1x 124x 75x 75x 71x 75x 71x 71x 57x 57x 57x 56x 57x 55x 55x 55x 2x 53x 57x 57x 6x 6x 6x 50x 50x 14x 14x 12x 10x 10x 1x 9x 9x 9x 9x 3x 12x 12x 5x 5x 7x 124x 5x 124x 126x 124x 124x 124x 124x 124x 124x 124x 124x 25x 25x 25x 25x 22x 22x 25x 22x 25x 16x 25x 2x 25x 2x 25x 124x 25x 25x 16x 25x 124x 115x 83x 83x 8x 164x 162x 162x 162x 164x 164x 164x 181x 181x 181x 19x 15x 4x 162x 16x 164x 162x 161x 161x 161x 161x 117x 161x 161x 164x 164x 164x 164x 5x 5x 5x 5x 157x 162x 25x 21x 21x 1x 1x 20x 20x 20x 20x 15x 15x 20x 15x 20x 20x 20x 25x 25x 25x 2x 20x 57x 56x 56x 55x 55x 55x 55x 1x 54x 57x 54x 54x 54x 54x 54x 54x 54x 54x 2x 1x 2x 52x 52x 52x 52x 37x 37x 37x 37x 37x 37x 3x 2x 2x 52x 36x 36x 36x 52x 115x 115x 115x 36x 36x 36x 36x 2x 2x 2x 2x 1x 52x 52x 52x 52x 12x 12x 6x 6x 6x 1x 5x 54x 54x 18x 16x 16x 5x 5x 11x 62x 51x 48x 46x 43x 40x 4x 4x 36x 4x 4x 4x 1x 32x 18x 18x 6x 14x 5x 5x 5x 9x 2x 2x 7x 5x 5x 5x 5x 2x 41x 124x 124x 124x 124x 124x 30x 30x 30x 30x 30x 124x 124x 124x 7x 7x 7x 4x 3x 4x 7x 4x 4x 4x 7x 9x 8x 8x 7x 7x 9x 9x 9x 5x 9x 9x 7x 7x 6x 1x 1x 8x 7x 7x 7x 7x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 1x 2x 2x 2x 4x 3x 1x 2x 2x 2x 5x 4x 4x 4x | import {
createCollection,
findTypeaheadMatch,
type CollectionItem,
} from '../collections/collection';
import { createControllerHost } from '../core/host';
import { createTimeoutManager } from '../core/timers';
import type {
ChangeDetails,
ControllableValueOptions,
RuntimeController,
RuntimeEventSource,
} from '../core/types';
import { isHTMLElement, isKeyboardLikeEvent } from '../dom/dom';
import { focusById } from '../focus/focus';
import type {
FloatingPositionOptions,
PositionResult,
VirtualAnchor,
} from '../positioning/positioning';
import {
createOpenController,
type OpenChangeEvent,
type OpenChangeReason,
type OpenLifecycleEvents,
type OverlayElements,
} from './openable';
/** Menu item kind used to separate actionable items from visual separators. @public */
export type MenuItemKind = 'item' | 'separator';
/** Item metadata registered with the shared menu engine. @public */
export interface MenuItem extends CollectionItem {
/** Actionable item or non-focusable separator. */
readonly kind?: MenuItemKind;
/** Consumer value emitted on activation. */
readonly value?: string;
/** Optional submenu identifier opened with ArrowRight. */
readonly submenuId?: string;
}
/** Menu selection causes. @public */
export type MenuSelectReason = 'pointer' | 'keyboard' | 'programmatic';
/** Menu selection lifecycle payload. @public */
export interface MenuSelectEvent {
/** Activated item. */
readonly item: Readonly<MenuItem>;
/** Typed activation cause and optional native event. */
readonly details: ChangeDetails<MenuSelectReason>;
}
/** Menu lifecycle events, including cancellable item activation. @public */
export interface MenuEvents extends Omit<OpenLifecycleEvents, 'stateChange'> {
/** Cancellable event emitted before an item activates. */
readonly beforeSelect: MenuSelectEvent;
/** Event emitted after an item activates. */
readonly select: MenuSelectEvent;
/** Event emitted for accepted open-state or selection changes. */
readonly stateChange: OpenChangeEvent | MenuSelectEvent;
}
/** Immutable menu state and WAI-ARIA metadata. @public */
export interface MenuSnapshot {
/** Stable content ID used by trigger `aria-controls` relationships. */
readonly contentId: string;
/** Current open state. */
readonly open: boolean;
/** Whether open state is consumer-owned. */
readonly controlled: boolean;
/** ID of the keyboard-active item. */
readonly activeId: string | null;
/** ID of the last activated item. */
readonly selectedId: string | null;
/** ID of the currently requested submenu. */
readonly openSubmenuId: string | null;
/** Ordered registered items. */
readonly items: readonly Readonly<MenuItem>[];
/** Menu role for consumer markup. */
readonly role: 'menu';
/** Latest collision-aware position. */
readonly position: PositionResult | null;
}
/** Menu engine configuration. @public */
export interface MenuOptions extends Partial<ControllableValueOptions<boolean, OpenChangeReason>> {
/** Deterministic menu content ID. */
readonly id?: string;
/** Whether ArrowUp/ArrowDown wrap at collection edges. @defaultValue `true` */
readonly loop?: boolean;
/** Whether item activation closes the menu. @defaultValue `true` */
readonly closeOnSelect?: boolean;
/** Shared anchored positioning configuration. */
readonly positioning?: FloatingPositionOptions;
}
/** Shared Menu controller reused by dropdown and context-menu factories. @public */
export interface MenuController
extends RuntimeController<MenuSnapshot>, RuntimeEventSource<MenuEvents> {
/** Opens and focuses the first enabled item by default. */
open(details?: ChangeDetails<OpenChangeReason>): void;
/** Closes and restores focus when configured by the binding. */
close(details?: ChangeDetails<OpenChangeReason>): void;
/** Requests the opposite open state. */
toggle(details?: ChangeDetails<OpenChangeReason>): void;
/** Registers dynamic item metadata and optional focusable DOM. */
registerItem(item: MenuItem, element?: HTMLElement): () => void;
/** Connects a registered submenu trigger to a child Menu controller. */
registerSubmenu(itemId: string, submenu: MenuController): () => void;
/** Binds trigger/content/anchor DOM for focus, outside interaction, and positioning. */
bind(elements: OverlayElements): () => void;
/** Handles Arrow keys, Home/End, typeahead, Enter/Space, Escape, and submenu keys. */
handleKeyDown(event: KeyboardEvent): void;
/** Activates an enabled item with a cancellable lifecycle event. */
select(id: string, details?: ChangeDetails<MenuSelectReason>): void;
/** Sets the active item for pointer integrations without selecting it. */
setActive(id: string | null): void;
}
interface ParentMenuContext {
readonly owner: MenuController;
readonly token: symbol;
closeAndRestore(event: KeyboardEvent): void;
}
const parentMenuContexts = new WeakMap<MenuController, ParentMenuContext>();
interface MenuCloseOutcome {
readonly details: ChangeDetails<OpenChangeReason>;
readonly result: 'allowed' | 'pending' | 'blocked';
}
const menuCloseOutcomes = new WeakMap<MenuController, MenuCloseOutcome>();
interface SubmenuRegistration {
readonly submenu: MenuController;
readonly submenuId: string;
readonly itemId: string;
readonly token: symbol;
readonly contextToken: symbol;
readonly intentToken: symbol;
readonly releaseOpen: () => void;
readonly releaseClose: () => void;
readonly releaseSnapshot: () => void;
}
interface PendingSubmenuClose {
readonly submenuId: string;
readonly registrationToken: symbol;
readonly details: ChangeDetails<OpenChangeReason>;
readonly onClosed?: () => void;
}
interface PendingSubmenuOpen {
readonly submenuId: string;
readonly registrationToken: symbol;
readonly itemId: string;
readonly itemToken: symbol;
readonly details: ChangeDetails<OpenChangeReason>;
readonly deferredUntilClose?: true;
}
/** Creates the shared headless Menu engine. @public */
export function createMenu(options: MenuOptions = {}): MenuController {
const collection = createCollection<MenuItem>();
const elements = new Map<string, HTMLElement>();
const itemRegistrations = new Map<string, { readonly token: symbol }>();
const itemRegistrationIntents = new Map<string, symbol>();
const submenuRegistrations = new Map<string, SubmenuRegistration>();
const submenuRegistrationIntents = new Map<string, symbol>();
let pendingSubmenuClose: PendingSubmenuClose | undefined;
let pendingSubmenuOpen: PendingSubmenuOpen | undefined;
const rejectedSubmenuOpens = new Set<symbol>();
const overlay = createOpenController({
role: 'menu',
closeOnFocusOutside: true,
...(options.id ? { id: options.id } : {}),
...(options.defaultValue !== undefined ? { defaultValue: options.defaultValue } : {}),
...(options.getValue ? { getValue: options.getValue } : {}),
...(options.onValueChange ? { onValueChange: options.onValueChange } : {}),
...(options.subscribeValue ? { subscribeValue: options.subscribeValue } : {}),
...(options.positioning ? { positioning: options.positioning } : {}),
});
const initialOverlay = overlay.getSnapshot();
const host = createControllerHost<MenuSnapshot, MenuEvents>({
contentId: initialOverlay.contentId,
open: initialOverlay.open,
controlled: initialOverlay.controlled,
activeId: null,
selectedId: null,
openSubmenuId: null,
items: [],
role: 'menu',
position: initialOverlay.position,
});
let typeahead = '';
const typeaheadTimer = createTimeoutManager();
const sync = (): void => {
const overlaySnapshot = overlay.getSnapshot();
const current = host.getSnapshot();
host.update({
...current,
contentId: overlaySnapshot.contentId,
open: overlaySnapshot.open,
controlled: overlaySnapshot.controlled,
items: collection.items(),
position: overlaySnapshot.position,
});
};
let previousOverlayOpen = initialOverlay.open;
const openEventNames = ['beforeOpen', 'open', 'afterOpen', 'close', 'afterClose'] as const;
for (const name of openEventNames) {
host.resources.add(
overlay.on(name, (event) => {
if (!host.emit(name, event.detail)) event.preventDefault();
}),
);
}
host.resources.add(overlay.on('stateChange', (event) => host.emit('stateChange', event.detail)));
const setActive = (id: string | null, focus = true): boolean => {
if (!host.alive()) return false;
if (id) {
const item = collection.get(id);
if (!item || item.disabled || item.kind === 'separator') return false;
}
if (host.getSnapshot().activeId !== id) host.update({ ...host.getSnapshot(), activeId: id });
if (focus && id) focusById(elements, id);
return true;
};
const clearOpenSubmenu = (submenuId: string): void => {
if (host.getSnapshot().openSubmenuId === submenuId)
host.update({ ...host.getSnapshot(), openSubmenuId: null });
};
const cancelPendingSubmenuOpen = (): void => {
if (pendingSubmenuOpen && !pendingSubmenuOpen.deferredUntilClose)
rejectedSubmenuOpens.add(pendingSubmenuOpen.registrationToken);
pendingSubmenuOpen = undefined;
};
const completePendingSubmenuClose = (
registration: SubmenuRegistration,
changeDetails: ChangeDetails<OpenChangeReason>,
): void => {
const pending = pendingSubmenuClose;
if (
!pending ||
pending.submenuId !== registration.submenuId ||
pending.registrationToken !== registration.token
)
return;
pendingSubmenuClose = undefined;
if (pending.details === changeDetails) pending.onClosed?.();
};
const completePendingSubmenuOpen = (
registration: SubmenuRegistration,
changeDetails: ChangeDetails<OpenChangeReason>,
): void => {
const pending = pendingSubmenuOpen;
if (
!pending ||
pending.submenuId !== registration.submenuId ||
pending.registrationToken !== registration.token
)
return;
pendingSubmenuOpen = undefined;
if (pending.details !== changeDetails) {
rejectedSubmenuOpens.add(registration.token);
return;
}
rejectedSubmenuOpens.delete(registration.token);
const item = collection.get(pending.itemId);
if (
itemRegistrations.get(pending.itemId)?.token !== pending.itemToken ||
!item ||
item.disabled ||
item.kind === 'separator' ||
item.submenuId !== pending.submenuId
) {
registration.submenu.close({ reason: 'programmatic' });
return;
}
setActive(pending.itemId, false);
host.update({ ...host.getSnapshot(), openSubmenuId: pending.submenuId });
};
const requestSubmenuClose = (
submenuId: string,
closeDetails: ChangeDetails<OpenChangeReason>,
onClosed: () => void,
): 'completed' | 'pending' | 'blocked' => {
cancelPendingSubmenuOpen();
pendingSubmenuClose = undefined;
const registration = submenuRegistrations.get(submenuId);
Iif (!registration || !registration.submenu.getSnapshot().open) {
clearOpenSubmenu(submenuId);
onClosed();
return 'completed';
}
const pending: PendingSubmenuClose = {
submenuId,
registrationToken: registration.token,
details: closeDetails,
onClosed,
};
pendingSubmenuClose = pending;
const cancellation = { prevented: false };
const observeCancellation = registration.submenu.on('beforeClose', (event) => {
cancellation.prevented = event.defaultPrevented;
});
try {
registration.submenu.close(closeDetails);
} catch (error) {
Eif (pendingSubmenuClose === pending) pendingSubmenuClose = undefined;
throw error;
} finally {
observeCancellation();
}
const nestedOutcome = menuCloseOutcomes.get(registration.submenu);
const nestedResult = nestedOutcome?.details === closeDetails ? nestedOutcome.result : undefined;
if (!registration.submenu.getSnapshot().open) {
Iif (pendingSubmenuClose === pending) {
pendingSubmenuClose = undefined;
clearOpenSubmenu(submenuId);
onClosed();
}
return 'completed';
}
if (cancellation.prevented || nestedResult === 'blocked') {
Eif (pendingSubmenuClose === pending) pendingSubmenuClose = undefined;
return 'blocked';
}
if (nestedResult === 'pending') return 'pending';
Iif (!registration.submenu.getSnapshot().controlled) {
if (pendingSubmenuClose === pending) pendingSubmenuClose = undefined;
return 'blocked';
}
return 'pending';
};
const requestSubmenuOpen = (
registration: SubmenuRegistration,
itemToken: symbol,
openDetails: ChangeDetails<OpenChangeReason>,
): 'completed' | 'pending' | 'blocked' => {
const closing = pendingSubmenuClose;
pendingSubmenuOpen = undefined;
if (
closing?.submenuId === registration.submenuId &&
closing.registrationToken === registration.token &&
registration.submenu.getSnapshot().open
) {
const deferred: PendingSubmenuOpen = {
submenuId: registration.submenuId,
registrationToken: registration.token,
itemId: registration.itemId,
itemToken,
details: openDetails,
deferredUntilClose: true,
};
pendingSubmenuOpen = deferred;
pendingSubmenuClose = {
submenuId: closing.submenuId,
registrationToken: closing.registrationToken,
details: closing.details,
};
return 'pending';
}
pendingSubmenuClose = undefined;
const pending: PendingSubmenuOpen = {
submenuId: registration.submenuId,
registrationToken: registration.token,
itemId: registration.itemId,
itemToken,
details: openDetails,
};
pendingSubmenuOpen = pending;
if (registration.submenu.getSnapshot().open) {
completePendingSubmenuOpen(registration, openDetails);
return 'completed';
}
const cancellation = { prevented: false };
const observeCancellation = registration.submenu.on('beforeOpen', (event) => {
cancellation.prevented = event.defaultPrevented;
});
try {
registration.submenu.open(openDetails);
} catch (error) {
if (pendingSubmenuOpen === pending) pendingSubmenuOpen = undefined;
throw error;
} finally {
observeCancellation();
}
if (registration.submenu.getSnapshot().open) {
return 'completed';
}
if (cancellation.prevented || !registration.submenu.getSnapshot().controlled) {
if (pendingSubmenuOpen === pending) pendingSubmenuOpen = undefined;
return 'blocked';
}
return 'pending';
};
const closeSubmenu = (event: KeyboardEvent): boolean => {
const snapshot = host.getSnapshot();
const submenuId = snapshot.openSubmenuId;
if (!submenuId) return true;
const registration = submenuRegistrations.get(submenuId);
Iif (!registration) return true;
const closeDetails: ChangeDetails<OpenChangeReason> = { reason: 'keyboard', event };
return (
requestSubmenuClose(submenuId, closeDetails, () => setActive(registration.itemId)) ===
'completed'
);
};
host.resources.add(
overlay.on('beforeClose', (event) => {
const closeDetails = event.detail.details;
if (!host.emit('beforeClose', event.detail)) {
menuCloseOutcomes.set(controller, { details: closeDetails, result: 'blocked' });
event.preventDefault();
return;
}
const submenuId = host.getSnapshot().openSubmenuId;
if (!submenuId) {
menuCloseOutcomes.set(controller, { details: closeDetails, result: 'allowed' });
return;
}
let retryAfterChildClose = false;
const result = requestSubmenuClose(submenuId, closeDetails, () => {
queueMicrotask(() => {
if (retryAfterChildClose && host.alive() && overlay.getSnapshot().open)
overlay.close(closeDetails);
});
});
retryAfterChildClose = result === 'pending';
menuCloseOutcomes.set(controller, {
details: closeDetails,
result: result === 'completed' ? 'allowed' : result,
});
if (result !== 'completed') event.preventDefault();
}),
);
host.resources.add(
overlay.subscribe((snapshot) => {
const opened = snapshot.open && !previousOverlayOpen;
const closed = !snapshot.open && previousOverlayOpen;
previousOverlayOpen = snapshot.open;
sync();
if (opened) setActive(collection.edge('first') ?? null);
if (closed) {
cancelPendingSubmenuOpen();
pendingSubmenuClose = undefined;
const submenuId = host.getSnapshot().openSubmenuId;
if (submenuId) {
try {
submenuRegistrations.get(submenuId)?.submenu.close({ reason: 'programmatic' });
} finally {
clearOpenSubmenu(submenuId);
}
}
}
}),
);
const select = (
id: string,
selectDetails: ChangeDetails<MenuSelectReason> = { reason: 'programmatic' },
): void => {
const item = collection.get(id);
if (!item || item.disabled || item.kind === 'separator') return;
const itemToken = itemRegistrations.get(id)?.token;
Iif (!itemToken) return;
cancelPendingSubmenuOpen();
if (item.submenuId) {
const submenuId = item.submenuId;
const registration = submenuRegistrations.get(submenuId);
if (!registration || registration.itemId !== id) return;
const openDetails: ChangeDetails<OpenChangeReason> = {
reason: selectDetails.reason === 'keyboard' ? 'keyboard' : 'trigger',
...(selectDetails.event ? { event: selectDetails.event } : {}),
};
const openTarget = (): void => {
const currentItem = collection.get(id);
const currentRegistration = submenuRegistrations.get(submenuId);
if (
itemRegistrations.get(id)?.token !== itemToken ||
currentRegistration?.token !== registration.token ||
!currentItem ||
currentItem.disabled ||
currentItem.kind === 'separator' ||
currentItem.submenuId !== submenuId
)
return;
requestSubmenuOpen(currentRegistration, itemToken, openDetails);
};
const current = host.getSnapshot().openSubmenuId;
if (current && current !== submenuId) {
const closeDetails: ChangeDetails<OpenChangeReason> = {
reason: 'selection',
...(selectDetails.event ? { event: selectDetails.event } : {}),
};
requestSubmenuClose(current, closeDetails, openTarget);
return;
}
openTarget();
return;
}
const payload = { item, details: selectDetails };
if (!host.emit('beforeSelect', payload)) return;
const finishSelection = (): void => {
const current = collection.get(id);
if (
itemRegistrations.get(id)?.token !== itemToken ||
!current ||
current.disabled ||
current.kind === 'separator'
)
return;
host.update({
...host.getSnapshot(),
activeId: id,
selectedId: id,
openSubmenuId: null,
});
host.emit('select', payload);
host.emit('stateChange', payload);
if (options.closeOnSelect ?? true)
overlay.close({
reason: 'selection',
...(selectDetails.event ? { event: selectDetails.event } : {}),
});
};
const openSubmenuId = host.getSnapshot().openSubmenuId;
if (openSubmenuId) {
requestSubmenuClose(
openSubmenuId,
{
reason: 'selection',
...(selectDetails.event ? { event: selectDetails.event } : {}),
},
finishSelection,
);
return;
}
finishSelection();
};
const move = (delta: 1 | -1): void => {
setActive(
collection.move(host.getSnapshot().activeId ?? undefined, delta, options.loop ?? true) ??
null,
);
};
const resetTypeahead = (): void => {
typeahead = '';
};
host.resources.add(resetTypeahead);
host.resources.add(typeaheadTimer.clear);
host.resources.add(() => overlay.destroy());
host.resources.add(() => collection.clear());
host.resources.add(() => elements.clear());
host.resources.add(() => itemRegistrations.clear());
host.resources.add(() => itemRegistrationIntents.clear());
const releaseSubmenuRegistration = (registration: SubmenuRegistration): void => {
registration.releaseOpen();
registration.releaseClose();
registration.releaseSnapshot();
if (submenuRegistrations.get(registration.submenuId)?.token === registration.token) {
submenuRegistrations.delete(registration.submenuId);
clearOpenSubmenu(registration.submenuId);
}
if (parentMenuContexts.get(registration.submenu)?.token === registration.contextToken)
parentMenuContexts.delete(registration.submenu);
if (submenuRegistrationIntents.get(registration.submenuId) === registration.intentToken)
submenuRegistrationIntents.delete(registration.submenuId);
if (pendingSubmenuClose?.registrationToken === registration.token)
pendingSubmenuClose = undefined;
if (pendingSubmenuOpen?.registrationToken === registration.token)
pendingSubmenuOpen = undefined;
rejectedSubmenuOpens.delete(registration.token);
};
const detachSubmenuRegistration = (registration: SubmenuRegistration): void => {
try {
if (registration.submenu.getSnapshot().open)
registration.submenu.close({ reason: 'programmatic' });
} catch {
// Structural ownership must still detach when consumer-controlled close logic throws.
} finally {
releaseSubmenuRegistration(registration);
}
};
const controller: MenuController = {
getSnapshot: host.getSnapshot,
subscribe: host.subscribe,
on: host.on,
off: host.off,
once: host.once,
open(changeDetails = { reason: 'programmatic' }) {
overlay.open(changeDetails);
},
close(changeDetails = { reason: 'programmatic' }) {
menuCloseOutcomes.delete(controller);
overlay.close(changeDetails);
},
toggle(changeDetails = { reason: 'programmatic' }) {
overlay.toggle(changeDetails);
},
registerItem(item, element) {
if (!host.alive()) return () => undefined;
const intentToken = Symbol(`item-intent-${item.id}`);
itemRegistrationIntents.set(item.id, intentToken);
const kind = item.kind ?? 'item';
const normalized: MenuItem = {
...item,
kind,
disabled: item.disabled ?? kind === 'separator',
};
let active = true;
let applied: { readonly token: symbol; readonly unregister: () => void } | undefined;
const reconcileItem = (): void => {
sync();
const current = collection.get(item.id);
if (host.getSnapshot().activeId === item.id) {
if (!current || current.disabled || current.kind === 'separator')
setActive(collection.edge('first') ?? null);
else setActive(item.id);
} else if (
overlay.getSnapshot().open &&
host.getSnapshot().activeId === null &&
current &&
!current.disabled &&
current.kind !== 'separator'
)
setActive(item.id);
};
const apply = (): void => {
if (!active || applied || itemRegistrationIntents.get(item.id) !== intentToken) return;
const token = Symbol(item.id);
itemRegistrations.set(item.id, { token });
const unregister = collection.register(normalized);
if (element) elements.set(item.id, element);
else elements.delete(item.id);
applied = { token, unregister };
reconcileItem();
};
const currentItem = collection.get(item.id);
const openSubmenuId = host.getSnapshot().openSubmenuId;
const invalidatesOpenSubmenu =
openSubmenuId !== null &&
currentItem?.submenuId === openSubmenuId &&
(normalized.disabled ||
normalized.kind === 'separator' ||
normalized.submenuId !== openSubmenuId);
if (invalidatesOpenSubmenu) {
const registration = submenuRegistrations.get(openSubmenuId);
try {
if (registration) detachSubmenuRegistration(registration);
else EclearOpenSubmenu(openSubmenuId);
} finally {
apply();
}
} else apply();
return () => {
if (!active) return;
active = false;
if (!applied) {
Iif (itemRegistrationIntents.get(item.id) === intentToken)
itemRegistrationIntents.delete(item.id);
return;
}
const owned = applied;
const release = (): void => {
owned.unregister();
if (itemRegistrations.get(item.id)?.token === owned.token) {
itemRegistrations.delete(item.id);
elements.delete(item.id);
}
if (itemRegistrationIntents.get(item.id) === intentToken)
itemRegistrationIntents.delete(item.id);
reconcileItem();
};
const registeredItem = collection.get(item.id);
const ownsItem = itemRegistrations.get(item.id)?.token === owned.token;
const submenuRegistration = registeredItem?.submenuId
? submenuRegistrations.get(registeredItem.submenuId)
: undefined;
try {
if (ownsItem && submenuRegistration?.itemId === item.id)
detachSubmenuRegistration(submenuRegistration);
} finally {
release();
}
};
},
registerSubmenu(itemId, submenu) {
if (!host.alive()) return () => undefined;
const item = collection.get(itemId);
if (!item?.submenuId || item.disabled || item.kind === 'separator') return () => undefined;
const submenuId = item.submenuId;
const previous = submenuRegistrations.get(submenuId);
const existingParent = parentMenuContexts.get(submenu);
if (
existingParent &&
(existingParent.owner !== controller || existingParent.token !== previous?.contextToken)
)
return () => undefined;
const itemToken = itemRegistrations.get(itemId)?.token;
Iif (!itemToken) return () => undefined;
const intentToken = Symbol(`submenu-intent-${submenuId}`);
submenuRegistrationIntents.set(submenuId, intentToken);
let active = true;
let installed: SubmenuRegistration | undefined;
const install = (): void => {
Iif (submenuRegistrationIntents.get(submenuId) !== intentToken) return;
if (previous) detachSubmenuRegistration(previous);
const validatedItem = collection.get(itemId);
if (
submenuRegistrationIntents.get(submenuId) !== intentToken ||
submenuRegistrations.has(submenuId) ||
parentMenuContexts.has(submenu) ||
itemRegistrations.get(itemId)?.token !== itemToken ||
!validatedItem ||
validatedItem.disabled ||
validatedItem.kind === 'separator' ||
validatedItem.submenuId !== submenuId
) {
if (submenuRegistrationIntents.get(submenuId) === intentToken)
submenuRegistrationIntents.delete(submenuId);
return;
}
const token = Symbol(submenuId);
const contextToken = Symbol(`parent-${submenuId}`);
let previousOpen = submenu.getSnapshot().open;
const releaseOpen = submenu.on('open', (event) => {
const pending = pendingSubmenuOpen;
const matchesPending =
pending?.registrationToken === token && pending.details === event.detail.details;
const current = submenuRegistrations.get(submenuId);
if (current?.token === token) completePendingSubmenuOpen(current, event.detail.details);
const rejected = rejectedSubmenuOpens.has(token);
if (matchesPending) rejectedSubmenuOpens.delete(token);
else if (rejected && host.getSnapshot().openSubmenuId !== submenuId) {
rejectedSubmenuOpens.delete(token);
submenu.close({ reason: 'programmatic' });
}
});
const releaseClose = submenu.on('close', (event) => {
clearOpenSubmenu(submenuId);
const current = submenuRegistrations.get(submenuId);
if (current?.token === token) completePendingSubmenuClose(current, event.detail.details);
});
const releaseSnapshot = submenu.subscribe((snapshot) => {
const closed = !snapshot.open && previousOpen;
previousOpen = snapshot.open;
if (closed) {
clearOpenSubmenu(submenuId);
const deferred =
pendingSubmenuOpen?.registrationToken === token &&
pendingSubmenuOpen.deferredUntilClose
? pendingSubmenuOpen
: undefined;
queueMicrotask(() => {
if (deferred && pendingSubmenuOpen === deferred && !submenu.getSnapshot().open) {
pendingSubmenuOpen = undefined;
const current = submenuRegistrations.get(submenuId);
const currentItem = collection.get(deferred.itemId);
if (
current?.token === token &&
itemRegistrations.get(deferred.itemId)?.token === deferred.itemToken &&
currentItem &&
!currentItem.disabled &&
currentItem.kind !== 'separator' &&
currentItem.submenuId === submenuId
)
requestSubmenuOpen(current, deferred.itemToken, deferred.details);
}
});
}
});
const registration: SubmenuRegistration = {
submenu,
submenuId,
itemId,
token,
contextToken,
intentToken,
releaseOpen,
releaseClose,
releaseSnapshot,
};
installed = registration;
submenuRegistrations.set(submenuId, registration);
parentMenuContexts.set(submenu, {
owner: controller,
token: contextToken,
closeAndRestore(event) {
const closeDetails: ChangeDetails<OpenChangeReason> = { reason: 'keyboard', event };
requestSubmenuClose(submenuId, closeDetails, () => {
const current = submenuRegistrations.get(submenuId);
const currentItem = collection.get(itemId);
if (
!overlay.getSnapshot().open ||
current?.token !== token ||
!currentItem ||
currentItem.disabled ||
currentItem.kind === 'separator' ||
currentItem.submenuId !== submenuId
)
return;
setActive(itemId);
});
},
});
};
install();
return () => {
if (!active) return;
active = false;
if (!installed || submenuRegistrations.get(submenuId)?.token !== installed.token) {
Iif (submenuRegistrationIntents.get(submenuId) === intentToken)
submenuRegistrationIntents.delete(submenuId);
return;
}
detachSubmenuRegistration(installed);
};
},
bind: overlay.bind,
handleKeyDown(event) {
if (event.defaultPrevented || !host.alive() || !overlay.getSnapshot().open) return;
if (event.key === 'ArrowDown') move(1);
else if (event.key === 'ArrowUp') move(-1);
else if (event.key === 'Home') setActive(collection.edge('first') ?? null);
else if (event.key === 'End') setActive(collection.edge('last') ?? null);
else if (event.key === 'Enter' || event.key === ' ') {
const activeId = host.getSnapshot().activeId;
if (activeId) select(activeId, { reason: 'keyboard', event });
} else if (event.key === 'ArrowRight') {
const active = host.getSnapshot().activeId;
const submenuId = active ? collection.get(active)?.submenuId : undefined;
if (active && submenuId && submenuRegistrations.has(submenuId))
select(active, { reason: 'keyboard', event });
} else if (event.key === 'ArrowLeft') {
const parentContext = parentMenuContexts.get(controller);
if (parentContext) parentContext.closeAndRestore(event);
else closeSubmenu(event);
} else if (event.key === 'Escape') {
overlay.close({ reason: 'escape-key', event });
event.preventDefault();
return;
} else if (event.key === 'Tab') {
overlay.close({ reason: 'focus-out', event });
return;
} else if (event.key.length === 1 && !event.altKey && !event.ctrlKey && !event.metaKey) {
typeahead += event.key;
const match = findTypeaheadMatch(
collection.items(),
typeahead,
host.getSnapshot().activeId ?? undefined,
);
if (match) setActive(match);
typeaheadTimer.schedule(resetTypeahead, 500);
} else return;
event.preventDefault();
},
select,
setActive,
destroy: host.destroy,
};
host.resources.add(() => {
pendingSubmenuClose = undefined;
pendingSubmenuOpen = undefined;
rejectedSubmenuOpens.clear();
for (const registration of submenuRegistrations.values()) {
registration.releaseOpen();
registration.releaseClose();
registration.releaseSnapshot();
Eif (parentMenuContexts.get(registration.submenu)?.token === registration.contextToken)
parentMenuContexts.delete(registration.submenu);
}
submenuRegistrations.clear();
submenuRegistrationIntents.clear();
});
return controller;
}
/** Dropdown Menu controller with trigger-specific activation. @public */
export interface DropdownMenuController extends MenuController {
/** Opens on pointer click or ArrowDown/Enter/Space keyboard activation. */
handleTrigger(event: MouseEvent | KeyboardEvent): void;
}
/** Creates a Dropdown Menu backed by the shared Menu engine and positioning layer. @public */
export function createDropdownMenu(options: MenuOptions = {}): DropdownMenuController {
const menu = createMenu(options);
let destroyed = false;
let pendingLastOpen: ChangeDetails<OpenChangeReason> | undefined;
const focusLast = (): void => {
const last = [...menu.getSnapshot().items]
.reverse()
.find((item) => !item.disabled && item.kind !== 'separator');
menu.setActive(last?.id ?? null);
};
const releaseOpen = menu.on('open', (event) => {
const pending = pendingLastOpen;
pendingLastOpen = undefined;
if (pending && pending === event.detail.details) focusLast();
});
return {
...menu,
handleTrigger(event) {
if (destroyed) return;
const keyboard = isKeyboardLikeEvent(event);
if (keyboard && !['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(event.key)) return;
event.preventDefault();
const openDetails: ChangeDetails<OpenChangeReason> = {
reason: keyboard ? 'keyboard' : 'trigger',
event,
};
pendingLastOpen = keyboard && event.key === 'ArrowUp' ? openDetails : undefined;
const cancellation = { prevented: false };
const observeCancellation = menu.on('beforeOpen', (openEvent) => {
cancellation.prevented = openEvent.defaultPrevented;
});
try {
menu.open(openDetails);
} finally {
observeCancellation();
}
if (cancellation.prevented) pendingLastOpen = undefined;
else if (pendingLastOpen === openDetails && menu.getSnapshot().open) {
pendingLastOpen = undefined;
focusLast();
}
},
destroy() {
if (destroyed) return;
destroyed = true;
pendingLastOpen = undefined;
releaseOpen();
menu.destroy();
},
};
}
/** Context Menu controller supporting pointer and keyboard virtual anchors. @public */
export interface ContextMenuController extends MenuController {
/** Opens at pointer coordinates and prevents the native menu only while active. */
handleContextMenu(event: MouseEvent, content: HTMLElement): () => void;
/** Opens from Shift+F10 or ContextMenu keyboard activation. */
handleKeyboardOpen(event: KeyboardEvent, trigger: HTMLElement, content: HTMLElement): () => void;
}
/** Creates a Context Menu backed by the same item, submenu, and typeahead engine. @public */
export function createContextMenu(options: MenuOptions = {}): ContextMenuController {
const menu = createMenu(options);
let unbind: () => void = () => undefined;
let destroyed = false;
const bindAt = (
anchor: Element | VirtualAnchor,
trigger: HTMLElement,
content: HTMLElement,
openDetails: ChangeDetails<OpenChangeReason>,
): (() => void) => {
unbind();
unbind = menu.bind({ anchor, trigger, content });
menu.open(openDetails);
return unbind;
};
return {
...menu,
handleContextMenu(event, content) {
if (destroyed || !isHTMLElement(event.currentTarget)) return () => undefined;
const anchor: VirtualAnchor = {
contextElement: event.currentTarget,
getBoundingClientRect: () => ({ x: event.clientX, y: event.clientY, width: 0, height: 0 }),
};
const release = bindAt(anchor, event.currentTarget, content, {
reason: 'context-menu',
event,
});
if (menu.getSnapshot().open) event.preventDefault();
return release;
},
handleKeyboardOpen(event, trigger, content) {
if (destroyed) return () => undefined;
if (!(event.key === 'ContextMenu' || (event.shiftKey && event.key === 'F10')))
return () => undefined;
const release = bindAt(trigger, trigger, content, { reason: 'keyboard', event });
if (menu.getSnapshot().open) event.preventDefault();
return release;
},
destroy() {
if (destroyed) return;
destroyed = true;
unbind();
menu.destroy();
},
};
}
|