# ui/uicontextmenu (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uicontextmenu/

Context menu. (right-click / long-press menu)

## UIContextMenu extends WorldNode

Context menu. (right-click / long-press menu)
- show(position, itemList) pops up the item list at that position.
  Pressing outside closes it, and pressing an item calls its handler and closes it.
- addChild it at the top (last child) of the scene root before use.
- Usage:
    const contextMenu = new UIContextMenu();
    scene.getRoot().addChild(contextMenu);
    contextMenu.show(viewPosition, [
        { text: "Copy", handler: () => {} },
        { text: "Delete", handler: () => {} },
    ]);

Example: Showing a menu attached on top of the root

```js
import { Engine, EngineConfiguration, UIScene, UIContextMenu, Color, Vector2 } from "./build/vanilla.js";

class ContextMenuScene extends UIScene {
	create() {
		super.create();
		const contextMenu = new UIContextMenu({ menuWidth: 180, fontSize: 13, panelColor: new Color(0.1, 0.12, 0.18, 1) });
		const root = this.getRoot();
		root.addChild(contextMenu);
		contextMenu.show(Vector2.create(200, 120), [
			{ text: "Copy", handler: () => {
				console.log("copy");
			} },
			{ text: "Paste", isDisabled: true },
			{ text: "Delete", handler: () => {
				console.log("delete");
			} },
		]);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new ContextMenuScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor(options = {})` (constructor, since 0.3.0): Constructor.
  - `options` object: { menuWidth = 168, itemHeight = 28, fontSize = 12, panelColor?, textColor? }
- `show(position, itemList)` (method, since 0.3.0): Opens the menu.
  - itemList: [{ text, handler?, isDisabled? }]
  - `position` Vector2: View coordinates. (top-left of the menu)
  - `itemList` object[]: Item list. ({ text, handler?, isDisabled? }, disabled items show dim text and cannot be pressed)
- `close()` (method, since 0.3.0): Close.
- `isShowing()` (method, since 0.3.0): Whether it is showing.
  - returns boolean: True if shown.
