Documentation
¶
Overview ¶
Package base provides common types and utilities for LiveTemplate components.
All components embed the Base struct which provides ID handling and common functionality. Components use functional options for configuration and implement action handlers that are automatically registered with the LiveTemplate framework.
Index ¶
- type ActionContext
- func (ctx *ActionContext) AllData() map[string]string
- func (ctx *ActionContext) Data(key string) string
- func (ctx *ActionContext) DataBool(key string) bool
- func (ctx *ActionContext) DataFloat(key string) float64
- func (ctx *ActionContext) DataInt(key string) int
- func (ctx *ActionContext) HasData(key string) bool
- type ActionHandler
- type ActionProvider
- type Base
- type TemplateProvider
- type TemplateSet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActionContext ¶
type ActionContext struct {
// Action is the action name that was triggered (e.g., "select", "toggle").
Action string
// ComponentID is the ID of the component that triggered the action.
ComponentID string
// contains filtered or unexported fields
}
ActionContext provides context for component action handlers. It wraps the data passed from lvt-data-* attributes and provides convenient accessor methods.
Components receive ActionContext in their action methods:
func (d *Dropdown) Select(ctx *ActionContext) error {
value := ctx.Data("value")
d.Selected = d.findOption(value)
return nil
}
func NewActionContext ¶
func NewActionContext(action, componentID string, data map[string]string) *ActionContext
NewActionContext creates a new ActionContext with the given action name, component ID, and data map.
func (*ActionContext) AllData ¶
func (ctx *ActionContext) AllData() map[string]string
AllData returns a copy of all data key-value pairs.
func (*ActionContext) Data ¶
func (ctx *ActionContext) Data(key string) string
Data returns the value for the given key, or empty string if not present. Keys correspond to lvt-data-* attributes without the "lvt-data-" prefix.
Example:
<button lvt-click="select_myid" lvt-data-value="option1">Click</button>
// In action handler:
value := ctx.Data("value") // returns "option1"
func (*ActionContext) DataBool ¶
func (ctx *ActionContext) DataBool(key string) bool
DataBool returns the value for the given key as a boolean. Returns false if the key is not present. Values "true", "1", "yes", "on" are considered true (case-insensitive).
func (*ActionContext) DataFloat ¶
func (ctx *ActionContext) DataFloat(key string) float64
DataFloat returns the value for the given key as a float64. Returns 0.0 if the key is not present or cannot be parsed as float.
func (*ActionContext) DataInt ¶
func (ctx *ActionContext) DataInt(key string) int
DataInt returns the value for the given key as an integer. Returns 0 if the key is not present or cannot be parsed as int.
func (*ActionContext) HasData ¶
func (ctx *ActionContext) HasData(key string) bool
HasData returns true if the given key exists in the data map.
type ActionHandler ¶
type ActionHandler func(ctx *ActionContext) error
ActionHandler is the function signature for component action handlers. Components implement methods matching this signature to handle user interactions.
Example:
func (d *Dropdown) Toggle(ctx *ActionContext) error {
d.Open = !d.Open
return nil
}
type ActionProvider ¶
type ActionProvider interface {
// Actions returns a map of action names to handlers.
// Action names should be without the component ID suffix.
// The framework will automatically match "toggle_myid" to the "toggle" handler.
Actions() map[string]ActionHandler
}
ActionProvider is an interface for components that provide action handlers. Components implement this to expose their handlers to the LiveTemplate framework.
type Base ¶
type Base struct {
// ComponentID is the unique identifier for this component instance.
// Used in action names like "toggle_{ID}", "select_{ID}".
// Exported for JSON serialization. Use ID() method to access.
ComponentID string `json:"id"`
// ComponentNamespace is the component type, e.g., "dropdown", "tabs".
// Used for action naming and template resolution.
// Exported for JSON serialization. Use Namespace() method to access.
ComponentNamespace string `json:"namespace"`
// Styled indicates whether to use Tailwind CSS classes (true) or semantic HTML only (false).
Styled bool `json:"styled"`
// contains filtered or unexported fields
}
Base provides common functionality for all components. Components should embed this struct to gain ID handling and common utilities.
IMPORTANT: Base uses exported fields with JSON tags for proper serialization. Do NOT add custom MarshalJSON/UnmarshalJSON to Base - it will break parent struct serialization because Go promotes embedded methods to the parent, making the parent's json.Marshal only serialize Base fields.
Example:
type Dropdown struct {
base.Base
Options []Option
Selected *Option
}
func NewBase ¶
NewBase creates a new Base with the given ID and namespace.
Example:
func New(id string, opts ...Option) *Dropdown {
d := &Dropdown{
Base: base.NewBase(id, "dropdown"),
}
for _, opt := range opts {
opt(d)
}
return d
}
func (*Base) ActionName ¶
ActionName generates a namespaced action name for this component. For example: ActionName("toggle") returns "toggle_myid" if ID is "myid".
func (*Base) SetStyleData ¶
SetStyleData caches a resolved style struct on this component.
type TemplateProvider ¶
type TemplateProvider interface {
// Templates returns the component's template set for registration.
Templates() *TemplateSet
}
TemplateProvider is implemented by components that provide templates. The LiveTemplate framework uses this interface to collect all component templates.
type TemplateSet ¶
type TemplateSet = livetemplate.TemplateSet
TemplateSet is a type alias for livetemplate.TemplateSet, allowing component Templates() functions to return values directly compatible with livetemplate.WithComponentTemplates() without conversion.
func NewTemplateSet ¶
func NewTemplateSet(fs embed.FS, pattern, namespace string) *TemplateSet
NewTemplateSet creates a new TemplateSet with the given filesystem and pattern.
Example:
//go:embed templates/*.tmpl
var templateFS embed.FS
func Templates() *TemplateSet {
return NewTemplateSet(templateFS, "templates/*.tmpl", "dropdown")
}
func WithFuncs ¶
func WithFuncs(ts *TemplateSet, funcs template.FuncMap) *TemplateSet
WithFuncs returns a copy of the TemplateSet with additional template functions.
Example:
func Templates() *TemplateSet {
return WithFuncs(NewTemplateSet(templateFS, "templates/*.tmpl", "dropdown"),
template.FuncMap{
"dropdownClass": func() string { return "dropdown" },
})
}