Add ordered menu example to docs

This commit is contained in:
Luke committed 2022-05-12 23:55:45 +02:00
1 parent ed40d0d67a
commit d7cf46fa2a
1 file changed
+73 -15
+73 -15
View File
@@ -2,6 +2,9 @@
title: Context Menu
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
### lib.registerContext
Used for registering a context menu.
@@ -10,11 +13,12 @@ Used for registering a context menu.
-- title: string
-- menu: string (optional)
-- options: table
-- item: key as string
-- item: key as string or table
-- title: string (optional - if item isn't key)
-- menu: string (optional)
-- arrow: boolean (optional)
-- description: string (optional)
-- metadata: string table or key, value table (optional)
-- metadata: string table or key, value table or table (optional)
-- event: string (optional)
-- serverEvent: string (optional)
-- args: any (optional)
@@ -30,6 +34,7 @@ as you'd like.
that will take you to the menu you defined.
`options` - Contains all the clickable menu items.
`item` - Defined as a key, can be empty if you don't want it to do anything.
`item/title` - If not using keys then sets the title for the button.
`item/menu` - Menu identifier that the button will take you to, when defined an arrow
pointing to the right to indicate a menu will be shown.
`item/arrow` - Shows an arrow on the right side like `menu` does, useful when you are
@@ -41,6 +46,11 @@ as a key.
`item/serverEvent` - Server event that the button is going to trigger.
`args` - Arguments that will be sent to the events.
The menu can be either in the order you write it in, or sorted alphabetically.
To sort the menu alphabetically the buttons (and/or metadata) need to be defined as keys,
otherwise not using keys and instead using tables will make the menu be in the order you
define it as.
### lib.showContext
Opens a registered context menu by it's id.
@@ -50,10 +60,67 @@ Opens a registered context menu by it's id.
lib.showContext(id)
```
### lib.getOpenContextMenu
Returns the id of the currently open context menu.
If not context menu is open returns `nil`.
```lua
lib.getOpenContextMenu()
```
### Usage Example
This is a simple command that will register and open a
context menu.
<Tabs>
<TabItem value='custom' label='Custom order'>
```lua
RegisterCommand('testcontext', function()
lib.registerContext({
id = 'example_menu',
title = 'Example Context',
options = {
{title = 'Empty button'},
{
title = 'Example button',
description = 'Example button description',
metadata = {
{label = 'Value 1', value = 'Some value'},
{label = 'Value 2', value = 300},
}
},
{
title = 'Menu button',
menu = 'other_example_menu',
description = 'Takes you to another menu!',
metadata = {'It also has metadata support'}
},
{
title = 'Event button',
description = 'Open a menu and send event data',
arrow = true,
event = 'some_event',
args = {value1 = 300, value2 = 'Other value'}
}
},
{
id = 'other_example_menu',
title = 'Other Context Menu',
menu = 'example_menu',
options = {
['Nothing here'] = {}
}
}
})
lib.showContext('example_menu')
end)
```
</TabItem>
<TabItem value='ordered' label='Alphabetically ordered'>
```lua
RegisterCommand('testcontext', function()
lib.registerContext({
@@ -92,6 +159,9 @@ RegisterCommand('testcontext', function()
lib.showContext('example_menu')
end)
```
</TabItem>
</Tabs>
To trigger the event from the `Event button` and get it's data we first
need to register the event properly:
@@ -130,18 +200,6 @@ the button will do nothing upon being clicked.
Of course just registering the event won't do much so we need to display it
with the `lib.showContext` function, passing in the menu's id.
As of the time writing this the buttons are sorted **alphabetically**.
![menu_example](https://i.imgur.com/aJu92dv.png)
![metadata](https://i.imgur.com/kFGSlBF.png)
![event_menu](https://i.imgur.com/r0Ln4VP.png)
### lib.getOpenContextMenu
Returns the id of the currently open context menu.
If not context menu is open returns `nil`.
```lua
lib.getOpenContextMenu()
```
![event_menu](https://i.imgur.com/r0Ln4VP.png)