accordion

package
v0.1.2 Latest Latest
Warning

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

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

Documentation

Overview

Package accordion provides collapsible content sections for organizing information.

Available variants:

  • New() creates a basic accordion (template: "lvt:accordion:default:v1")
  • NewSingle() creates single-open accordion (template: "lvt:accordion:single:v1")

Required lvt-* attributes: name

Example usage:

// In your controller/state
FAQ: accordion.New("faq", []accordion.Item{
    {ID: "q1", Title: "What is LiveTemplate?", Content: "..."},
    {ID: "q2", Title: "How do I get started?", Content: "..."},
})

// In your template
{{template "lvt:accordion:default:v1" .FAQ}}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Templates

func Templates() *base.TemplateSet

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

Example usage in main.go:

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

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

Available templates:

  • "lvt:accordion:default:v1" - Multi-open accordion
  • "lvt:accordion:single:v1" - Single-open accordion

Types

type Accordion

type Accordion struct {
	base.Base

	// Items is the list of accordion sections
	Items []Item

	// OpenIDs contains the IDs of currently open items
	OpenIDs map[string]bool

	// AllowMultiple allows multiple items to be open at once
	// When false, opening one item closes others (single-open mode)
	AllowMultiple bool
}

Accordion is the base component for collapsible sections. Use template "lvt:accordion:default:v1" to render.

func New

func New(id string, items []Item, opts ...Option) *Accordion

New creates an accordion that allows multiple items open.

Example:

faq := accordion.New("faq", []accordion.Item{
    {ID: "q1", Title: "Question 1", Content: "Answer 1"},
    {ID: "q2", Title: "Question 2", Content: "Answer 2"},
},
    accordion.WithOpen("q1"),
)

func NewSingle

func NewSingle(id string, items []Item, opts ...Option) *Accordion

NewSingle creates an accordion where only one item can be open at a time.

Example:

nav := accordion.NewSingle("nav", sections,
    accordion.WithOpen("section1"),
)

func (*Accordion) AddItem

func (a *Accordion) AddItem(item Item)

AddItem adds a new item to the accordion.

func (*Accordion) Close

func (a *Accordion) Close(itemID string)

Close closes an item.

func (*Accordion) CloseAll

func (a *Accordion) CloseAll()

CloseAll closes all items.

func (*Accordion) GetItem

func (a *Accordion) GetItem(itemID string) *Item

GetItem returns an item by ID, or nil if not found.

func (*Accordion) IsOpen

func (a *Accordion) IsOpen(itemID string) bool

IsOpen checks if an item is currently open.

func (*Accordion) ItemCount

func (a *Accordion) ItemCount() int

ItemCount returns the total number of items.

func (*Accordion) Open

func (a *Accordion) Open(itemID string)

Open opens an item.

func (*Accordion) OpenAll

func (a *Accordion) OpenAll()

OpenAll opens all non-disabled items.

func (*Accordion) OpenCount

func (a *Accordion) OpenCount() int

OpenCount returns the number of open items.

func (*Accordion) RemoveItem

func (a *Accordion) RemoveItem(itemID string)

RemoveItem removes an item by ID.

func (*Accordion) Styles

func (a *Accordion) Styles() styles.AccordionStyles

Styles returns the resolved CSS class set for this component. It lazily resolves from the registered style adapter and caches the result.

func (*Accordion) Toggle

func (a *Accordion) Toggle(itemID string)

Toggle toggles the open state of an item.

type Item

type Item struct {
	ID       string // Unique identifier for this item
	Title    string // Header text shown (always visible)
	Content  string // Body content (shown when expanded)
	Disabled bool   // Whether this item can be toggled
	Icon     string // Optional icon (HTML or class name)
}

Item represents a single accordion section.

type Option

type Option func(*Accordion)

Option is a functional option for configuring accordions.

func WithAllOpen

func WithAllOpen() Option

WithAllOpen opens all items initially.

func WithOpen

func WithOpen(itemIDs ...string) Option

WithOpen sets initially open item(s) by ID.

func WithStyled

func WithStyled(styled bool) Option

WithStyled enables Tailwind CSS styling for the component. When false, renders semantic HTML without styling classes.

Jump to

Keyboard shortcuts

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