menu

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package menu provides navigation and context menu components.

Available variants:

  • New() creates a dropdown menu (template: "lvt:menu:default:v1")
  • NewContext() creates a context/right-click menu (template: "lvt:menu:context:v1")
  • NewNav() creates a navigation menu (template: "lvt:menu:nav:v1")

Open/close is handled client-side via CSS classes and onclick handlers. Server actions handle data operations only (Select, Highlight).

Example usage:

// In your controller/state
UserMenu: menu.New("user-menu",
    menu.WithItems([]menu.Item{
        {ID: "profile", Label: "Profile", Icon: "user"},
        {ID: "settings", Label: "Settings", Icon: "cog"},
        {Type: menu.ItemTypeDivider},
        {ID: "logout", Label: "Logout", Icon: "logout"},
    }),
)

// In your template
{{template "lvt:menu:default:v1" .UserMenu}}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Templates

func Templates() *base.TemplateSet

Templates returns the menu component's template set for registration with the LiveTemplate framework.

Example usage in main.go:

import "github.com/livetemplate/lvt/components/menu"

tmpl, err := livetemplate.New("app",
    livetemplate.WithComponentTemplates(menu.Templates()),
)

Available templates:

  • "lvt:menu:default:v1" - Dropdown action menu
  • "lvt:menu:context:v1" - Context/right-click menu
  • "lvt:menu:nav:v1" - Navigation menu

Types

type ContextMenu

type ContextMenu struct {
	Menu

	// X is the horizontal position
	X int
	// Y is the vertical position
	Y int
}

ContextMenu is a right-click context menu. Use template "lvt:menu:context:v1" to render.

func NewContext

func NewContext(id string, opts ...Option) *ContextMenu

NewContext creates a context menu.

func (*ContextMenu) ShowAt

func (cm *ContextMenu) ShowAt(x, y int)

ShowAt sets the context menu position. The consuming page is responsible for adding the "open" CSS class to show it.

type Item

type Item struct {
	// ID is the item identifier
	ID string
	// Type is the item type (default, divider, header, submenu)
	Type ItemType
	// Label is the display text
	Label string
	// Icon is an optional icon class/name
	Icon string
	// Shortcut is an optional keyboard shortcut text
	Shortcut string
	// Disabled prevents interaction
	Disabled bool
	// Href makes this a link
	Href string
	// Target for link (e.g., "_blank")
	Target string
	// Items holds submenu items (when Type is ItemTypeSubmenu)
	Items []Item
	// Data holds arbitrary custom data
	Data map[string]any
	// Badge is optional badge text
	Badge string
	// BadgeColor is the badge color (e.g., "red", "blue")
	BadgeColor string
	// Active highlights the item
	Active bool
}

Item represents a menu item.

func (Item) HasBadge

func (i Item) HasBadge() bool

HasBadge checks if item has a badge.

func (Item) HasIcon

func (i Item) HasIcon() bool

HasIcon checks if item has an icon.

func (Item) HasShortcut

func (i Item) HasShortcut() bool

HasShortcut checks if item has a keyboard shortcut.

func (Item) IsDivider

func (i Item) IsDivider() bool

IsDivider checks if item is a divider.

func (Item) IsHeader

func (i Item) IsHeader() bool

IsHeader checks if item is a header.

func (i Item) IsLink() bool

IsLink checks if item is a link.

func (Item) IsSubmenu

func (i Item) IsSubmenu() bool

IsSubmenu checks if item has submenu items.

type ItemType

type ItemType int

ItemType defines the type of menu item.

const (
	// ItemTypeDefault is a normal clickable item
	ItemTypeDefault ItemType = iota
	// ItemTypeDivider is a separator line
	ItemTypeDivider
	// ItemTypeHeader is a non-clickable section header
	ItemTypeHeader
	// ItemTypeSubmenu is an item with nested items
	ItemTypeSubmenu
)
type Menu struct {
	base.Base

	// Items is the list of menu items
	Items []Item

	// Trigger is the button/element that opens the menu
	Trigger string

	// TriggerIcon is an optional icon for the trigger
	TriggerIcon string

	// Position is the menu position relative to trigger ("bottom-left", "bottom-right", etc.)
	Position string

	// HighlightedIndex is the currently highlighted item (-1 for none)
	HighlightedIndex int
}

Menu is a dropdown/action menu component. Use template "lvt:menu:default:v1" to render.

func New

func New(id string, opts ...Option) *Menu

New creates a dropdown menu.

Example:

m := menu.New("actions",
    menu.WithTrigger("Actions"),
    menu.WithItems(items),
)
func (m *Menu) BadgeClass(color string) string

BadgeClass returns the CSS class for a badge color.

func (m *Menu) ClickableItems() []Item

ClickableItems returns items that can be clicked (excludes dividers, headers, disabled).

func (m *Menu) GetItem(id string) *Item

GetItem returns an item by ID.

func (m *Menu) HighlightNext()

HighlightNext moves highlight to the next clickable item.

func (m *Menu) HighlightPrevious()

HighlightPrevious moves highlight to the previous clickable item.

func (m *Menu) IsHighlighted(id string) bool

IsHighlighted checks if an item ID is highlighted.

func (m *Menu) ItemClass(item Item) string

ItemClass returns the CSS class for an item based on its state.

func (m *Menu) PositionClass() string

PositionClass returns the CSS classes for the menu position.

func (m *Menu) SelectIndex(index int) string

SelectIndex selects the item at the given index and returns its ID.

func (m *Menu) SetItemDisabled(id string, disabled bool)

SetItemDisabled enables or disables an item.

func (m *Menu) Styles() styles.MenuStyles

Styles returns the resolved MenuStyles for this component.

type NavMenu struct {
	base.Base

	// Items is the list of menu items
	Items []Item

	// Orientation is "horizontal" or "vertical"
	Orientation string

	// ActiveID is the ID of the active/current item
	ActiveID string
}

NavMenu is a navigation menu with support for nested items. Use template "lvt:menu:nav:v1" to render.

func NewNav

func NewNav(id string, opts ...NavOption) *NavMenu

NewNav creates a navigation menu.

func (nm *NavMenu) GetItem(id string) *Item

GetItem returns an item by ID (searches nested items too).

func (nm *NavMenu) IsActive(id string) bool

IsActive checks if an item is active.

func (nm *NavMenu) SetActive(id string)

SetActive sets the active item.

func (nm *NavMenu) Styles() styles.MenuStyles

Styles returns the resolved MenuStyles for this component.

type NavOption func(*NavMenu)

NavOption is a functional option for configuring navigation menus.

func WithActiveID

func WithActiveID(id string) NavOption

WithActiveID sets the active item ID.

func WithNavItems

func WithNavItems(items []Item) NavOption

WithNavItems sets the navigation items.

func WithNavStyled

func WithNavStyled(styled bool) NavOption

WithNavStyled enables Tailwind CSS styling for navigation menu.

func WithOrientation

func WithOrientation(orientation string) NavOption

WithOrientation sets the menu orientation. Options: "horizontal", "vertical"

type Option

type Option func(*Menu)

Option is a functional option for configuring menu components.

func WithItems

func WithItems(items []Item) Option

WithItems sets the menu items.

func WithOpen

func WithOpen(_ bool) Option

WithOpen is a no-op. Open/close is now handled client-side via CSS classes. Deprecated: This option has no effect.

func WithPosition

func WithPosition(position string) Option

WithPosition sets the menu position relative to trigger. Options: "bottom-left", "bottom-right", "top-left", "top-right"

func WithStyled

func WithStyled(styled bool) Option

WithStyled enables Tailwind CSS styling for the component.

func WithTrigger

func WithTrigger(trigger string) Option

WithTrigger sets the trigger button text.

func WithTriggerIcon

func WithTriggerIcon(icon string) Option

WithTriggerIcon sets the trigger button icon.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL