feat(web): add explicit context-menu layout and focus options #8458
ClosedAdamulek123(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/8458/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -234,9 +283,20 @@283+ options?.restoreFocus !== false &&284+ (options?.restoreFocus !== "on-dismiss" || reason === "interaction") &&285+ shouldRestoreFocus &&286+ previouslyFocusedElement?.isConnected287+ ) {234288 previouslyFocusedElement.focus({ preventScroll: true });235289 }236290 resolve(result);237291 };238292293+ const onAbort = () => cleanup(null, "programmatic");294+239295 const onKeyDown = (event: KeyboardEvent) => {296+ if (event.isComposing) return;240297 if (event.key === "Escape") {241298 event.preventDefault();242- cleanup(null);299+ cleanup(null, "interaction");300+ return;301+ }302+ const action = contextMenuAcceleratorAction(items, event);@@ -234,9 +282,20 @@282+ if (283+ options?.restoreFocus !== false &&284+ (options?.restoreFocus !== "on-dismiss" || reason === "interaction") &&285+ shouldRestoreFocus &&286+ previouslyFocusedElement?.isConnected287+ ) {234288 previouslyFocusedElement.focus({ preventScroll: true });235289 }236290 resolve(result);237291 };238292293+ const onAbort = () => cleanup(null, "programmatic");294+239295 const onKeyDown = (event: KeyboardEvent) => {296+ if (event.isComposing) return;240297 if (event.key === "Escape") {241298 event.preventDefault();242- cleanup(null);299+ cleanup(null, "interaction");300+ return;301+ }@@ -197,3 +197,17 @@197197 activeContextMenuDismiss = null;198198 }199199200+export function contextMenuAcceleratorAction<T extends string>(201+ items: readonly ContextMenuItem<T>[],202+ event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "isComposing" | "key" | "metaKey" | "shiftKey">,203+): T | null {204+ if (event.isComposing) return null;205+206+ for (const item of items) {207+ if (item.disabled) continue;208+ if (item.children) {209+ const childAction = contextMenuAcceleratorAction(item.children, event);210+ if (childAction !== null) return childAction;211+ }212+ if (!item.accelerator) continue;213+@@ -234,9 +283,20 @@283+ options?.restoreFocus !== false &&284+ (options?.restoreFocus !== "on-dismiss" || reason === "interaction") &&285+ shouldRestoreFocus &&286+ previouslyFocusedElement?.isConnected287+ ) {234288 previouslyFocusedElement.focus({ preventScroll: true });235289 }236290 resolve(result);237291 };238292293+ const onAbort = () => cleanup(null, "programmatic");294+239295 const onKeyDown = (event: KeyboardEvent) => {296+ if (event.isComposing) return;240297 if (event.key === "Escape") {241298 event.preventDefault();242- cleanup(null);299+ cleanup(null, "interaction");300+ return;301+ }302+ const action = contextMenuAcceleratorAction(items, event);
- @@ -199,0 +216,21 @@216+ event.key.toLowerCase() === parts.at(-1) &&217+ event.altKey === parts.includes("alt") &&218+ event.ctrlKey === parts.includes("ctrl") &&219+ event.metaKey === (parts.includes("command") || parts.includes("cmd")) &&220+ event.shiftKey === parts.includes("shift")221+ ) {222+ return item.id;223+ }224+ }225+226+ return null;227+}228+229+function formatContextMenuAccelerator(accelerator: string): string {230+ const parts = accelerator.split("+");231+ if (!parts.some((part) => part === "Command" || part === "Cmd")) return accelerator;232+ const key = parts.at(-1) ?? "";233+ return `${parts.includes("Ctrl") ? "⌃" : ""}${parts.includes("Alt") ? "⌥" : ""}${234+ parts.includes("Shift") ? "⇧" : ""235+ }⌘${key}`;236+}@@ -199,0 +201,21 @@201+ items: readonly ContextMenuItem<T>[],202+ event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "isComposing" | "key" | "metaKey" | "shiftKey">,203+): T | null {204+ if (event.isComposing) return null;205+206+ for (const item of items) {207+ if (item.disabled) continue;208+ if (item.children) {209+ const childAction = contextMenuAcceleratorAction(item.children, event);210+ if (childAction !== null) return childAction;211+ }212+ if (!item.accelerator) continue;213+214+ const parts = item.accelerator.toLowerCase().split("+");215+ if (216+ event.key.toLowerCase() === parts.at(-1) &&217+ event.altKey === parts.includes("alt") &&218+ event.ctrlKey === parts.includes("ctrl") &&219+ event.metaKey === (parts.includes("command") || parts.includes("cmd")) &&220+ event.shiftKey === parts.includes("shift")221+ ) {
- @@ -246,13 +310,19 @@246310 const onPointerDown = (event: PointerEvent) => {247311 if (!canDismissFromPointer || isNodeWithinMenuStack(event.target, menuStack)) {248312 return;249313 }250- cleanup(null);314+ cleanup(null, "interaction");251315 };252316253317 const onContextMenu = (event: MouseEvent) => {254318 if (!canDismissFromPointer || isNodeWithinMenuStack(event.target, menuStack)) {255319 return;256320 }257321 event.preventDefault();258- cleanup(null);322+ cleanup(null, "interaction");323+ };324+325+ const onWheel = (event: WheelEvent) => {326+ if (!isNodeWithinMenuStack(event.target, menuStack)) {327+ cleanup(null, "interaction");328+ }
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
988e3cc3-c580-414c-bb9d-c79723bc424dYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Comment `@coderabbitai help` to get the list of available commands.