1# ContextMenu
2
3The menu bound to a component through [bindContextMenu](./ts-universal-attributes-menu.md#bindcontextmenu12) on a page can be closed as needed.
4
5>  **NOTE**
6>
7> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8>
9> Since API version 12, you can use the [getContextMenuController](../js-apis-arkui-UIContext.md#contextmenucontroller12) API in [UIContext](../js-apis-arkui-UIContext.md#uicontext) to obtain the UI context.
10
11## ContextMenu.close
12
13static close()
14
15Closes the menu bound to this component through [bindContextMenu](./ts-universal-attributes-menu.md#bindcontextmenu12) on a page.
16
17**Atomic service API**: This API can be used in atomic services since API version 11.
18
19**System capability**: SystemCapability.ArkUI.ArkUI.Full
20
21
22## Example
23
24>  **NOTE**
25>
26> For clarity in UI execution context, you are advised to use the [getContextMenuController](../js-apis-arkui-UIContext.md#contextmenucontroller12) API in [UIContext](../js-apis-arkui-UIContext.md#uicontext).
27
28```ts
29// xxx.ets
30@Entry
31@Component
32struct Index {
33  @Builder MenuBuilder() {
34    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
35      Button('Test ContextMenu1')
36      Divider().strokeWidth(2).margin(5).color(Color.Black)
37      Button('Test ContextMenu2')
38      Divider().strokeWidth(2).margin(5).color(Color.Black)
39      Button('Test ContextMenu3')
40    }
41    .width(200)
42    .height(160)
43  }
44
45  build() {
46    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
47      Column() {
48        Text("Test ContextMenu")
49          .fontSize(20)
50          .width('100%')
51          .height(500)
52          .backgroundColor(0xAFEEEE)
53          .textAlign(TextAlign.Center)
54      }
55      .bindContextMenu(this.MenuBuilder, ResponseType.LongPress)
56      .onDragStart(()=>{
57        // Close the menu when the component is dragged.
58        ContextMenu.close() // You are advised to use this.getUIContext().getContextMenuController().close().
59      })
60    }
61    .width('100%')
62    .height('100%')
63  }
64}
65```
66
67![contextmenu_close.gif](figures/contextmenu_close.gif)
68