Menu

The Menu system provides hierarchical navigation menus with support for dynamic content, search functionality, and customizable layouts. Perfect for creating complex user interfaces with multiple levels of navigation.

Dark Menu

registerMenu

Register a menu definition that can be displayed later with customizable structure and behavior.

Parameters:

  • menuId (string): Unique menu identifier for registration and display

  • menuData (table): Menu configuration object

    • title (string, optional): Menu title displayed in header (default: empty string)

    • subtitle (string, optional): Menu subtitle displayed below title (default: empty string)

    • type (string, optional): Menu layout type - 'list', 'grid', 'compact' (default: 'list')

    • position (string, optional): Menu position on screen (default: from config)

    • showSearch (boolean, optional): Enable search functionality (default: false)

    • showHeader (boolean, optional): Show menu header with title/subtitle (default: true)

    • showClose (boolean, optional): Show close button in header (default: true)

    • maxHeight (number, optional): Maximum menu height in pixels (default: 600)

    • width (number, optional): Menu width in pixels (default: from config)

    • items (table, optional): Array of menu items (default: empty array)

    • searchPlaceholder (string, optional): Search input placeholder text

    • enableKeyboard (boolean, optional): Enable keyboard navigation (default: true)

    • closeOnSelect (boolean, optional): Close menu when item selected (default: true)

Returns: boolean - Success status

Example:

showMenu

Display a registered menu with optional display options and context data.

Parameters:

  • playerId (number, server-side only): Player server ID to show menu to

  • menuId (string): Menu ID to display (must be registered first)

  • options (table, optional): Additional display options

    • position (string, optional): Override default menu position

    • contextData (table, optional): Dynamic data to pass to menu items

    • selectedIndex (number, optional): Initially selected item index

    • searchTerm (string, optional): Pre-populate search field

    • isSubMenu (boolean, optional): Internal flag for submenu navigation

Returns: boolean - Success status

Example:

hideMenu

Hide the currently active menu with optional animation and cleanup.

Parameters:

  • playerId (number, server-side only): Player server ID to hide menu for

Returns: boolean - Success status

Example:

Navigate to a registered submenu while maintaining navigation history.

Parameters:

  • playerId (number, server-side only): Player server ID

  • submenuId (string): Submenu ID to navigate to (must be registered)

Returns: boolean - Success status

Example:

updateMenuItems

Update menu items dynamically for an active or registered menu.

Parameters:

  • playerId (number, server-side only): Player server ID

  • menuId (string): Menu ID to update items for

  • items (table): New items array to replace existing items

Returns: boolean - Success status

Example:

getActiveMenu

Get information about the currently active menu.

Returns: table|nil - Active menu data or nil if no menu is active

  • id (string): Menu identifier

  • title (string): Menu title

  • type (string): Menu type

  • position (string): Menu position

  • itemCount (number): Number of items

  • selectedIndex (number): Currently selected item index

  • searchTerm (string): Current search term

Example:

isMenuActive

Check if a specific menu is currently active.

Parameters:

  • menuId (string, optional): Menu ID to check (if nil, checks if any menu is active)

Returns: boolean - True if specified menu (or any menu) is active

Example:

Menu items support various properties for different functionality and appearance:

List Menu

Standard vertical list layout with icons and descriptions.

Grid Menu

Grid layout for visual items like vehicles, weapons, or categories.

Compact Menu

Minimal layout for simple option lists.

Server-Side Usage

Server-side menu functions allow you to manage menus for specific players:

Configuration

Complete menu configuration options in config.lua:

Advanced Examples

Dynamic Player List Menu

Multi-Level Admin Menu

Inventory Menu with Categories

Context-Aware Vehicle Menu

Search-Enabled Command Menu

Events

The Menu system triggers comprehensive events for handling user interactions:

b2lib:menu-item-selected

Triggered when user selects a menu item.

b2lib:menu-closed

Triggered when a menu is closed.

Triggered when user searches in a menu.

b2lib:submenu-navigated

Triggered when navigating to a submenu.

Best Practices

  1. Use Descriptive Menu IDs: Choose clear, unique identifiers that describe the menu's purpose

  2. Implement Search for Large Lists: Enable search functionality for menus with more than 10 items

  3. Provide Clear Item Descriptions: Include helpful descriptions for complex or ambiguous menu items

  4. Use Appropriate Icons: Select relevant Lucide icons that enhance visual understanding

  5. Group Related Items: Use separators and headers to organize menu items logically

  6. Handle Menu Events: Always implement event handlers for menu interactions

  7. Update Dynamic Content: Refresh menu items when underlying data changes

  8. Consider Performance: Avoid creating menus with hundreds of items; use pagination instead

  9. Test Keyboard Navigation: Ensure all menu functions work with keyboard-only input

  10. Provide Context Data: Pass relevant context information to menu items for dynamic behavior

Troubleshooting

Problem: Menu doesn't appear when calling showMenu()

Solutions:

Problem: updateMenuItems() doesn't refresh the display

Solutions:

Problem: Submenu navigation not working properly

Solutions:

Search Functionality Not Working

Problem: Menu search doesn't filter items correctly

Solutions:

Performance Issues with Large Menus

Problem: Menu becomes slow with many items

Solutions:

Problem: Menu appears in wrong position or off-screen

Solutions:

Last updated