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 ¶
- func Templates() *base.TemplateSet
- type ContextMenu
- type Item
- type ItemType
- type Menu
- func (m *Menu) BadgeClass(color string) string
- func (m *Menu) ClickableItems() []Item
- func (m *Menu) GetItem(id string) *Item
- func (m *Menu) HighlightNext()
- func (m *Menu) HighlightPrevious()
- func (m *Menu) IsHighlighted(id string) bool
- func (m *Menu) ItemClass(item Item) string
- func (m *Menu) PositionClass() string
- func (m *Menu) SelectIndex(index int) string
- func (m *Menu) SetItemDisabled(id string, disabled bool)
- func (m *Menu) Styles() styles.MenuStyles
- type NavMenu
- type NavOption
- type Option
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) HasShortcut ¶
HasShortcut checks if item has a keyboard shortcut.
type Menu ¶
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 ¶
New creates a dropdown menu.
Example:
m := menu.New("actions",
menu.WithTrigger("Actions"),
menu.WithItems(items),
)
func (*Menu) BadgeClass ¶
BadgeClass returns the CSS class for a badge color.
func (*Menu) ClickableItems ¶
ClickableItems returns items that can be clicked (excludes dividers, headers, disabled).
func (*Menu) HighlightNext ¶
func (m *Menu) HighlightNext()
HighlightNext moves highlight to the next clickable item.
func (*Menu) HighlightPrevious ¶
func (m *Menu) HighlightPrevious()
HighlightPrevious moves highlight to the previous clickable item.
func (*Menu) IsHighlighted ¶
IsHighlighted checks if an item ID is highlighted.
func (*Menu) PositionClass ¶
PositionClass returns the CSS classes for the menu position.
func (*Menu) SelectIndex ¶
SelectIndex selects the item at the given index and returns its ID.
func (*Menu) SetItemDisabled ¶
SetItemDisabled enables or disables an item.
func (*Menu) Styles ¶
func (m *Menu) Styles() styles.MenuStyles
Styles returns the resolved MenuStyles for this component.
type NavMenu ¶
NavMenu is a navigation menu with support for nested items. Use template "lvt:menu:nav:v1" to render.
func (*NavMenu) Styles ¶
func (nm *NavMenu) Styles() styles.MenuStyles
Styles returns the resolved MenuStyles for this component.
type NavOption ¶
type NavOption func(*NavMenu)
NavOption is a functional option for configuring navigation menus.
func WithNavItems ¶
WithNavItems sets the navigation items.
func WithNavStyled ¶
WithNavStyled enables Tailwind CSS styling for navigation menu.
func WithOrientation ¶
WithOrientation sets the menu orientation. Options: "horizontal", "vertical"
type Option ¶
type Option func(*Menu)
Option is a functional option for configuring menu components.
func WithOpen ¶
WithOpen is a no-op. Open/close is now handled client-side via CSS classes. Deprecated: This option has no effect.
func WithPosition ¶
WithPosition sets the menu position relative to trigger. Options: "bottom-left", "bottom-right", "top-left", "top-right"
func WithStyled ¶
WithStyled enables Tailwind CSS styling for the component.
func WithTrigger ¶
WithTrigger sets the trigger button text.
func WithTriggerIcon ¶
WithTriggerIcon sets the trigger button icon.