js-components-basic-menu.md 5.4 KB
Newer Older
G
ge-yafang 已提交
1
# menu
Z
zengyawen 已提交
2

G
ge-yafang 已提交
3
The **\<menu>** component provides menus as temporary pop-up windows to display operations that can be performed by users.
Z
zengyawen 已提交
4

G
ge-yafang 已提交
5
## Required Permissions
Z
zengyawen 已提交
6 7 8

None

G
ge-yafang 已提交
9
## Child Components
Z
zengyawen 已提交
10

G
ge-yafang 已提交
11
**[option](js-components-basic-option.md)**
Z
zengyawen 已提交
12

G
ge-yafang 已提交
13
## Attributes
Z
zengyawen 已提交
14

G
ge-yafang 已提交
15
In addition to the attributes in [Universal Attributes](js-components-common-attributes.md), the following attributes are supported.
Z
zengyawen 已提交
16 17 18



G
ge-yafang 已提交
19 20 21 22 23 24 25 26 27
| Name   | Type   | Default Value | Mandatory | Description                                                  |
| ------ | ------ | ------------- | --------- | ------------------------------------------------------------ |
| target | string | -             | No        | Target element selector. After the target element selector is used, a menu is automatically displayed when you click the target element. The pop-up menu is preferentially displayed in the lower right corner of the target element. When the visible space on the right is insufficient, the menu is moved leftward. When the visible space in the lower part is insufficient, the menu is moved upward. |
| type   | string | click         | No        | Mode in which the target element triggers the pop-up window. Available values are as follows:<br/>-**click**: Click the element to trigger the pop-up window.<br/>-**longpress**: Press and hold the element to trigger the pop-up window. |
| title  | string | -             | No        | Title of the pop-up window.                                  |

> ![img](https://gitee.com/openharmony/docs/raw/OpenHarmony-3.1-Release/en/application-dev/public_sys-resources/icon-note.gif) **NOTE:** The **focusable** and **disabled** attributes are not supported.

## Styles
Z
zengyawen 已提交
28 29 30 31

Only the following style attributes are supported.


G
ge-yafang 已提交
32 33 34 35 36 37 38 39 40 41 42 43

| Name           | Type             | Default Value | Mandatory | Description                                                  |
| -------------- | ---------------- | ------------- | --------- | ------------------------------------------------------------ |
| text-color     | \<color>          | -             | No        | Font color of the menu.                                      |
| font-size      | \<length>         | 30px          | No        | Font size of the menu.                                       |
| allow-scale    | boolean          | true          | No        | Whether the font size changes with the system's font size settings.NOTE:If the **config-changes** tag of **fontSize** is configured for abilities in the **config.json** file, the setting takes effect without application restart. |
| letter-spacing | \<length>         | 0             | No        | Character spacing of the menu.                               |
| font-style     | string           | normal        | No        | Font style of the menu. For details, see [font-style](js-components-basic-text.md#section5775351116) of the **text** component. |
| font-weight    | number \| string | normal        | No        | Font weight of the menu. For details, see [font-weight](js-components-basic-text.md#section5775351116) of the **text** component. |
| font-family    | string           | sans-serif    | No        | Font family, in which fonts are separated by commas (,). Each font is set using a font name or font family name. The first font that exists in the system or the font specified by [Custom Font Styles](js-components-common-customizing-font.md) in the family is selected as the font for the text. |

## Events
Z
zengyawen 已提交
44 45 46 47

The following events are supported.


G
ge-yafang 已提交
48 49 50 51 52 53 54

| Name     | Parameter       | Description                                                  |
| -------- | --------------- | ------------------------------------------------------------ |
| selected | { value:value } | Triggered when a value in the menu is selected. The returned value is the **value** attribute of the **\<option>** component. |
| cancel   | -               | Triggered when an operation is canceled by the user          |

## Methods
Z
zengyawen 已提交
55 56 57 58

The following methods are supported.


G
ge-yafang 已提交
59 60 61 62 63 64

| Name | Parameter    | Description                                                  |
| ---- | ------------ | ------------------------------------------------------------ |
| show | { x:x, y:y } | Displays the menu. *x* and *y* specify the position of the displayed menu. *x* indicates the X-axis coordinate from the left edge of the visible area, and does not include any scrolling offset. *y* indicates the Y-axis coordinate from the upper edge of the visible area, and does not include any scrolling offset or a status bar. The menu is preferentially displayed in the lower right corner. When the visible space on the right is insufficient, the menu is moved leftward. When the visible space in the lower part is insufficient, the menu is moved upward. |

## Example
Z
zengyawen 已提交
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

```
<!-- xxx.hml -->
<div class="container">
  <text onclick="onTextClick" class="title-text">Show popup menu.</text>
  <menu id="apiMenu" onselected="onMenuSelected">
    <option value="Item 1">Item 1</option>
    <option value="Item 2">Item 2</option>
    <option value="Item 3">Item 3</option>
  </menu>
</div>
/* xxx.css */
.container {
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.title-text {
  margin: 20px;
}
// xxx.js
import prompt from '@system.prompt';
export default {
  onMenuSelected(e) {
    prompt.showToast({
      message: e.value
    })
  },
  onTextClick() {
    this.$element("apiMenu").show({x:280,y:120});
  }
}
```

G
ge-yafang 已提交
99
![img](https://gitee.com/openharmony/docs/raw/OpenHarmony-3.1-Release/en/application-dev/reference/arkui-js/figures/menu13.gif)