Skip to content

Commit

Permalink
Better context menu UI
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed Sep 6, 2023
1 parent 9e4b1bb commit d8c5e56
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/css/components/ContextMenu.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.ContextMenu {
@apply absolute hidden;
@apply absolute hidden flex-col;

padding: 5px;
border-radius: 5px;
Expand All @@ -17,6 +17,7 @@
border-radius: 5px;

&:hover {
user-select: none;
background-color: #0c0c0c;
}
}
4 changes: 4 additions & 0 deletions src/ui/components/common/ContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from "react";

import { openContext } from "@utils/context";

import "@css/components/ContextMenu.scss";

export type ContextOption = {
Expand Down Expand Up @@ -34,6 +36,8 @@ export function showMenu(id: string, x: number, y: number): void {
menu.style.display = "flex";
menu.style.left = x + "px";
menu.style.top = y + "px";

openContext(menu);
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/utils/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let currentContext: HTMLElement | null = null;
window.addEventListener("click", closeContext);

/**
* Handles clicking around the page.
*
* @param e The event.
*/
export function closeContext(e: MouseEvent): void {
// Check if the click was within the confines of the context menu.
if (currentContext && !currentContext.contains(e.target as Node)) {
currentContext.style.display = "none";
currentContext = null;
}
}

/**
* Sets the current context menu.
*
* @param element The context menu to set.
*/
export function openContext(element: HTMLElement): void {
// Close any open context menus.
if (currentContext) {
currentContext.style.display = "none";
}

// Set the current context menu.
currentContext = element;
}

0 comments on commit d8c5e56

Please sign in to comment.