Skip to main content

DropDownMenu

The DropDown Menu widget provides a ready-to-use wrapper for adding a menu to button-like elements. This type of menu expands when the content inside of its container is clicked. The DropDown Menu is useful for displaying additional options or actions related to the element.

DropDown Menu

Initialization

To create a DropDownMenu on a page, complete the following steps:

  • import the source file of the DropDownMenu widget on a page:
App.svelte
<script>
import { DropDownMenu } from "wx-svelte-menu";
</script>
  • apply the <DropDownMenu> tag to initialize a drop-down menu and wrap a button-like element in it. In case of a button, mind including it into the <script> part of your app as well:
App.svelte
<script>
import { DropDownMenu } from "wx-svelte-menu";
import { Button } from "@xbs/svelte-wx";
</script>

<DropDownMenu>
<Button type="primary">Click me</Button>
</DropDownMenu>
App.svelte
<DropDownMenu {options}>
<Button type="primary">Click me</Button>
</DropDownMenu>

Loading options

Options can be loaded into DropDownMenu on its initialization or after it. To begin with, you need to define a data set in the JSON format.

Specifying a data set

A data set will contain options of the DropDown Menu. You need to specify them as objects with attributes presented as key:value pairs. A data set for DropDownMenu can have both a plain structure and a more complex one - tree-like structure.

  • a plain data set structure looks like this:
var options = [
{ id: "add-task:child", text: "Child task" },
{ id: "add-task:above", text: "Task above" },
{ id: "add-task:below", text: "Task below" },
];
  • a tree-like data set structure is the following:
var options = [
{ id: "add-task", text: "Add", icon: "wxi wxi-plus", data:[
{ id: "child", text: "Child task" },
{ id: "above", text: "Task above" },
{ id: "below", text: "Task below" }
]}
];

A menu item object may contain a set of properties, they are listed below:

  • id - the id of a menu item
  • text - the text of a menu item
  • subtext - a dimmed text displayed to the right of the main item's text, such as a hot key or other additional info
  • icon - the name of an icon displayed before the text. It converts to adding <i class={item.icon}>, so any icons with the wxi- prefix can be used here, e.g ".wxi-plus" or any custom icons defined on the page. Read more about the usage of icons
  • css - the name of a CSS class that will be applied to an item to change its style
  • type - the type of an item. It can be a "separator" or a custom type registered as a menu item (e.g. type:"button)
  • data - an array of sub-items for a menu item. Used to create a tree-like menu structure
  • handler - a click handler function, takes an item object as a parameter

Loading options on initialization

To load a prepared data set on initialization of the DropDown Menu, you should use the options property:

<script>
const options = [
{ id: "add-task:child", text: "Child task" },
{ id: "add-task:above", text: "Task above" },
{ id: "add-task:below", text: "Task below" },
];
</script>

<DropDownMenu {options}>
<Button type="primary">Click me</Button>
</DropDownMenu>

Loading options after initialization

You can load options into DropDownMenu after its initialization from a separate JS file. These are the steps to follow:

  • prepare a file with data ("data.js") in the same directory where your application is placed
  • specify a function that will return an array of menu options in the data file and export this function to make it available from outside:
data.js
export function getProjects(){
return [
{ id:"a", text:"Project A" },
{ id:"b", text:"Project B" },
{ id:"c", text:"Project C" },
{ id:"d", text:"Project D" }
]
}
  • import the function into your application and assign it to the variable specified for options
  • use the options configuration property in the <DropDownMenu> tag. Set the name of the variable as its value or use the shorthand, if the name of the variable coincides with the name of the property:
App.svelte
<script>
import { DropDownMenu } from "wx-svelte-menu";

import { getProjects } from "./data";

const options = getProjects();
</script>

<DropDownMenu {options}>
<Button type="primary">Click me</Button>
</DropDownMenu>

Related sample: Drop-down menu

Adding custom options

Creating a menu option

You can use a custom component inside of a DropDownMenu item. To add a custom menu item, you need to create a file that will contain the code of your item, for example:

ButtonMenuItem.svelte
<script>
import { Button, Text } from "wx-svelte-wx";
export let item;
</script>

<div>
<div style="width: 120px" on:click={ev => ev.preventDefault()}>
<Text placeholder={item.name} />
</div>
<Button icon="wxi-plus" type="primary" />
</div>

By default, clicking anywhere inside of a custom item component will activate menu closing and the click handler triggering. You can prevent it by intercepting the click event and using the preventDefault() method as in the above example.

Registering the item

When a component is ready, you should register it like this:

App.svelte
<script>
import { Button} from "wx-svelte-wx";
import { DropDownMenu } from "wx-svelte-menu";

import ButtonMenuItem from "./your_items/ButtonMenuItem.svelte";
import { registerMenuItem } from "../your_sources/helpers";
registerMenuItem("button", ButtonMenuItem);
</script>

The above code has added the type:button component.

Using the item as a DropDownMenu option

Now you can use the newly created item in the options configuration. A custom "Add New" button is added into the menu below:

App.svelte
<script>
// applying a newly registered menu item type for an option
const options = [
{ id:1, text:"Add User", subtext:"Ctrl+A" },
{ id:2, text:"Refresh", subtext:"Ctrl+R" },
{ id:3, text:"Delete User", css:"danger" },
{ id:"btn", type:"button", name:"Add New" }
];
</script>

<DropDownMenu {options} on:click={clicked}>
<Button type="primary">Click me</Button>
</DropDownMenu>

The result of adding a custom option into the menu is given below:

Custom option

Related sample: Custom options

Setting the position of DropDownMenu

You can control the position of DropDownMenu relative to the target via the at property. In the example below DropDownMenu appears to the right of the target.

<DropDownMenu {options} at="bottom-fit" on:click={clicked}>
<Button type="primary">Click me (fit )</Button>
</DropDownMenu>

Related sample: Drop-down menu

The at property can have one of the values listed below:

  • "bottom" - (default) a menu appears below the target node, left borders aligned (the menu goes to the right of the action area)

DropDownMenu bottom position

  • "right" - a menu appears right to the target node

DropDownMenu right position

  • "left" - a menu appears left to the target node

DropDownMenu left position

  • "bottom-left" - a menu appears below the target node, right borders aligned (the menu goes to the left of the action area)

DropDownMenu bottom left position

  • "bottom-fit" - a menu appears below the target node, the width of the menu is equal to the width of the target node

DropDownMenu bottom fit position

  • "point" - a menu appears at the specified left/top position and ignores the provided node

Related sample: Drop-down menu

Custom triggering of DropDownMenu

You can use DropDownMenu with a custom trigger. In this case, instead of wrapping the content in the <DropDownMenu> tag, you can initialize DropDownMenu anywhere and link its handler to an HTML element in the following way:

<script>
var activate = null;
</script>

<DropDownMenu {options} bind:handler={activate} />

<div data-context-id={item.id} on:click={activate}></div>

Here we've received the handler function from the menu and linked it to the markup.

The usage of the handler is not limited to the click event, as in the above example. You can use on:contextmenu or any other DOM event as well. It is also possible to show DropDownMenu from a custom code. It is important that the first argument will be an object that contains a target or the clientX/clientY coordinates. For example:

// show a menu manually with a DOM event 
activate(ev);

// or with a "fake" event related to the node
activate({ target: DOMTarget });
// at a specific position
activate({ clientX: 100, clientY: 100 });

// if the necessary handler can receive the related item as well
// this value will be returned as the item's parameter in the click event
activate(ev, item);

Catching the change of a clicked option

You can catch the change of a clicked option by handling the click event. Inside the event you can get an object of the clicked option as in:

<script>
function clicked(ev){
const action = ev.detail.action;
message = action ? `clicked on ${action.id}` : "closed";
}
</script>

<DropDownMenu {options} on:click={clicked}>
<Button type="primary">Click me</Button>
</DropDownMenu>

The detail property of CustomEvent (ev.detail) will contain an object related to the clicked menu item.

Related sample: Drop-down menu

Styling an item of DropDownMenu

You can apply a particular style to a menu item via the css attribute of an item object. Use the global keyword while specifying a style to reach an isolated menu item. In the example below, the text of the third menu option turns red:

<script>
const options = [
{ id:1, text:"Add User", subtext:"Ctrl+A" },
{ id:2, text:"Refresh", subtext:"Ctrl+R" },
{ id:3, text:"Delete User", css:"my-color" }
];
</script>

<DropDownMenu {options}>
<Button type="primary">Click me</Button>
</DropDownMenu>

<style>
:global(.item.my-color span){
color: red;
}
</style>

As a result, the menu will look like this:

Menu item styling

Related sample: Item styling