wip js/ts examples

This commit is contained in:
Luke committed 2022-09-16 17:43:47 +02:00
1 parent 3aee4976c3
commit d44c42104e
7 files changed
+602 -163

No files matched your search

+161 -23
View File
@@ -2,72 +2,169 @@
title: Menu
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Keyboard navigation menu with specific event functions.
### lib.registerMenu
Registers and caches a menu under the specified id.
<Tabs>
<TabItem value='Lua'>
```lua
-- data: table
-- id: string
-- title: string
-- options: table
-- label: string
-- icon: string (optinal)
-- values: table of strings (optional - if provided creates a side scrollable list)
-- description?: string (optional)
-- defaultIndex: number (optional - sets the current index for the list to specified number)
-- args: any (optional)
-- position: 'top-left' or 'top-right' or 'bottom-left' or 'bottom-right' (optional - default is top-left)
-- disableInput: boolean (optional - default false)
-- onClose: function (optional)
-- onSelected: function (optional - triggers every time a new button is selected)
-- onSideScroll: function (optional - triggers every time a side list is scrolled)
-- cb: function
lib.registerMenu(data, cb)
```
</TabItem>
<TabItem value='JS/TS'>
```ts
import lib from '@overextended/ox_lib/client'
lib.registerMenu(data, cb)
```
</TabItem>
</Tabs>
* data: `table` (`object`)
* id: `string`
* title: `string`
* options: `table` (`array`)
* label: `string`
* icon?: `string`
* values?: `table` (`string[]`)
* If provided creates a side scrollabel list.
* description?: `string`
* Displays tooltip below menu on hovered item with provided description.
* defaultIndex?: `number`
* Sets the current index for the list to specified number.
* args?: `any`
* position?: `'top-left'` or `'top-right'` or `'bottom-left'` or `'bottom-right'`
* Default: `'top-left'`
* disableInput?: `boolean`
* Default: `false`
* onClose: `function`
* Function that runs when the menu is exited via ESC/Backspace.
* onSelected: `function`(selected: `number`: scrollIndex: `number`: args: `any`)
* Function being ran when the selected button in the menu changes.
* onSideScroll: `function`(selected: `number`: scrollIndex: `number`: args: `any`)
* Function ran whenever a scroll list item is changed.
* cb: `function`(selected: `number`: scrollIndex: `number`: args: `any`)
* Callback function when the menu item is pressed.
### lib.showMenu
Displays the menu with the provided id.
<Tabs>
<TabItem value='Lua'>
```lua
-- id: string
lib.showMenu(id)
```
</TabItem>
<TabItem value='JS/TS'>
```ts
import lib from '@overextended/ox_lib/client'
lib.showMenu(id)
```
</TabItem>
</Tabs>
* id: `string`
### lib.hideMenu
<Tabs>
<TabItem value='Lua'>
```lua
-- onExit: boolean (optional - if true runs the menu's onClose function)
lib.hideMenu(onExit)
```
</TabItem>
<TabItem value='JS/TS'>
```ts
import lib from '@overextended/ox_lib/client'
lib.hideMenu(onExit)
```
</TabItem>
</Tabs>
* onExit?: `boolean`
* If true runs the menu's `onClose` function.
### lib.getOpenMenu
Returns the id of the currently open menu.
<Tabs>
<TabItem value='Lua'>
```lua
lib.getOpenMenu()
```
</TabItem>
<TabItem value='JS/TS'>
```ts
import lib from '@overextended/ox_lib/client'
lib.getOpenMenu()
```
</TabItem>
</Tabs>
### lib.setMenuOptions
<Tabs>
<TabItem value='Lua'>
```lua
-- id: string
-- options: table
-- index: number (optional - if specified only sets the options table on the specified options index)
lib.setMenuOptions(id, options, index)
```
</TabItem>
<TabItem value='JS/TS'>
```ts
import lib from '@overextended/ox_lib/client'
lib.setMenuOptions(id, options, index)
```
</TabItem>
</Tabs>
* id: `string`
* options: `table` (`object` or `array`)
* index?: `number`
* If specified only sets the options table on the specified options index.
**Example:**
```lua
-- Replaces the option on 3rd index of specified menu
Replaces the 3rd index option of the specified menu
<Tabs>
<TabItem value='Lua'>
```lua
lib.setMenuOptions('some_menu_id', {label = 'New option', icon = 'plus'}, 3)
```
</TabItem>
<TabItem value='JS/TS'>
```ts
import lib from '@overextended/ox_lib/client'
lib.setMenuOptions('some_menu_id', {label: 'New option', icon: 'plus'}, 3)
```
</TabItem>
</Tabs>
### Usage Example
First we register the menu with our specified options then we call the show function in the command.
@@ -77,6 +174,9 @@ Avoid constantly re-registering a menu that does not depend on any outside value
:::
<Tabs>
<TabItem value='Lua'>
```lua
lib.registerMenu({
id = 'some_menu_id',
@@ -107,4 +207,42 @@ RegisterCommand('testmenu', function()
lib.showMenu('some_menu_id')
end)
```
</TabItem>
<TabItem value='JS/TS'>
```ts
import lib from '@overextended/ox_lib/client'
lib.registerMenu({
id: 'some_menu_id',
title: 'Menu title',
position: 'top-right',
onSideScroll: (selected, scrollIndex, args) => {
console.log(selected, scrollIndex, args)
},
onSelected: (selected, scrollIndex, args) => {
console.log(selected, scrollIndex, args)
},
onClose: () => {
console.log('Menu closed')
},
options: [
{label: 'Simple button', description: 'It has a description!'},
{label: 'Simple button with icon', icon: 'arrows-up-down-left-right'},
{label: 'Button with args', args: 'nice_button'},
{label: 'List button', values: ['You', 'can', 'side', 'scroll', 'this'], description: 'It also has a description!'},
{label: 'List button with default index', values: ['You', 'can', 'side', 'scroll', 'this'], defaultIndex: 5},
{label: 'List button with args', values: ['You', 'can', 'side', 'scroll', 'this'], args: {someValue: 3, otherValue: 'value'}},
]
}, (selected, scrollIndex, args) => {
console.log(selected, scrollIndex, args)
})
RegisterCommand('testmenu', () => {
lib.showMenu('some_menu_id')
}, false)
```
</TabItem>
</Tabs>
![Example](https://i.imgur.com/lmimH7e.png)