Documentation
¶
Overview ¶
Package toggle provides toggle/switch components.
Available variants:
- New() creates a toggle switch (template: "lvt:toggle:default:v1")
- NewCheckbox() creates a styled checkbox (template: "lvt:toggle:checkbox:v1")
Required lvt-* attributes: lvt-on:click, lvt-on:change
Example usage:
// In your controller/state
DarkMode: toggle.New("dark-mode",
toggle.WithLabel("Dark Mode"),
toggle.WithChecked(true),
)
// In your template
{{template "lvt:toggle:default:v1" .DarkMode}}
Index ¶
- func Templates() *base.TemplateSet
- type Checkbox
- func (c *Checkbox) Check()
- func (c *Checkbox) CheckboxStateClass() string
- func (c *Checkbox) HasDescription() bool
- func (c *Checkbox) HasLabel() bool
- func (c *Checkbox) SetIndeterminate(indeterminate bool)
- func (c *Checkbox) Styles() styles.CheckboxStyles
- func (c *Checkbox) Toggle()
- func (c *Checkbox) Uncheck()
- type CheckboxOption
- func WithCheckboxChecked(checked bool) CheckboxOption
- func WithCheckboxDescription(description string) CheckboxOption
- func WithCheckboxDisabled(disabled bool) CheckboxOption
- func WithCheckboxIndeterminate(indeterminate bool) CheckboxOption
- func WithCheckboxLabel(label string) CheckboxOption
- func WithCheckboxName(name string) CheckboxOption
- func WithCheckboxRequired(required bool) CheckboxOption
- func WithCheckboxStyled(styled bool) CheckboxOption
- func WithCheckboxValue(value string) CheckboxOption
- type Option
- func WithChecked(checked bool) Option
- func WithDescription(description string) Option
- func WithDisabled(disabled bool) Option
- func WithLabel(label string) Option
- func WithLabelPosition(position string) Option
- func WithName(name string) Option
- func WithRequired(required bool) Option
- func WithSize(size Size) Option
- func WithStyled(styled bool) Option
- func WithValue(value string) Option
- type Size
- type Toggle
- func (t *Toggle) Check()
- func (t *Toggle) HasDescription() bool
- func (t *Toggle) HasLabel() bool
- func (t *Toggle) IsLabelLeft() bool
- func (t *Toggle) IsLabelRight() bool
- func (t *Toggle) IsOff() bool
- func (t *Toggle) IsOn() bool
- func (t *Toggle) KnobSizeClasses() string
- func (t *Toggle) KnobTranslateClass() string
- func (t *Toggle) SetChecked(checked bool)
- func (t *Toggle) SizeClasses() string
- func (t *Toggle) Styles() styles.ToggleStyles
- func (t *Toggle) Toggle()
- func (t *Toggle) TrackColorClass() string
- func (t *Toggle) Uncheck()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Templates ¶
func Templates() *base.TemplateSet
Templates returns the toggle component's template set for registration with the LiveTemplate framework.
Example usage in main.go:
import "github.com/livetemplate/lvt/components/toggle"
tmpl, err := livetemplate.New("app",
livetemplate.WithComponentTemplates(toggle.Templates()),
)
Available templates:
- "lvt:toggle:default:v1" - Switch toggle
- "lvt:toggle:checkbox:v1" - Styled checkbox
Types ¶
type Checkbox ¶
type Checkbox struct {
base.Base
// Checked indicates whether the checkbox is checked
Checked bool
// Indeterminate shows the indeterminate state
Indeterminate bool
// Disabled prevents interaction
Disabled bool
// Label is the checkbox label text
Label string
// Name for form submission
Name string
// Value for form submission when checked
Value string
// Required for form validation
Required bool
// Description is helper text
Description string
}
Checkbox is a styled checkbox component.
func NewCheckbox ¶
func NewCheckbox(id string, opts ...CheckboxOption) *Checkbox
NewCheckbox creates a styled checkbox.
Example:
c := toggle.NewCheckbox("terms",
toggle.WithCheckboxLabel("I agree to the terms"),
toggle.WithCheckboxRequired(true),
)
func (*Checkbox) CheckboxStateClass ¶
CheckboxStateClass returns CSS class for checkbox state.
func (*Checkbox) HasDescription ¶
HasDescription returns true if checkbox has a description.
func (*Checkbox) SetIndeterminate ¶
SetIndeterminate sets the indeterminate state.
func (*Checkbox) Styles ¶
func (c *Checkbox) Styles() styles.CheckboxStyles
Styles returns the resolved CheckboxStyles for this component.
type CheckboxOption ¶
type CheckboxOption func(*Checkbox)
CheckboxOption is a functional option for checkboxes.
func WithCheckboxChecked ¶
func WithCheckboxChecked(checked bool) CheckboxOption
WithCheckboxChecked sets the initial checked state.
func WithCheckboxDescription ¶
func WithCheckboxDescription(description string) CheckboxOption
WithCheckboxDescription sets the helper text.
func WithCheckboxDisabled ¶
func WithCheckboxDisabled(disabled bool) CheckboxOption
WithCheckboxDisabled sets the disabled state.
func WithCheckboxIndeterminate ¶
func WithCheckboxIndeterminate(indeterminate bool) CheckboxOption
WithCheckboxIndeterminate sets the indeterminate state.
func WithCheckboxLabel ¶
func WithCheckboxLabel(label string) CheckboxOption
WithCheckboxLabel sets the checkbox label.
func WithCheckboxName ¶
func WithCheckboxName(name string) CheckboxOption
WithCheckboxName sets the form field name.
func WithCheckboxRequired ¶
func WithCheckboxRequired(required bool) CheckboxOption
WithCheckboxRequired sets the required state.
func WithCheckboxStyled ¶
func WithCheckboxStyled(styled bool) CheckboxOption
WithCheckboxStyled enables Tailwind CSS styling.
func WithCheckboxValue ¶
func WithCheckboxValue(value string) CheckboxOption
WithCheckboxValue sets the value when checked.
type Option ¶
type Option func(*Toggle)
Option is a functional option for configuring toggles.
func WithChecked ¶
WithChecked sets the initial checked state.
func WithDescription ¶
WithDescription sets the helper text.
func WithLabelPosition ¶
WithLabelPosition sets where the label appears.
type Toggle ¶
type Toggle struct {
base.Base
// Checked indicates whether the toggle is on
Checked bool
// Disabled prevents interaction
Disabled bool
// Label is the toggle label text
Label string
// LabelPosition is where the label appears (left, right)
LabelPosition string
// Size of the toggle
Size Size
// Name for form submission
Name string
// Value for form submission when checked
Value string
// Required for form validation
Required bool
// Description is helper text below the toggle
Description string
}
Toggle is a switch/toggle component. Use template "lvt:toggle:default:v1" to render.
func New ¶
New creates a toggle switch.
Example:
t := toggle.New("notifications",
toggle.WithLabel("Email Notifications"),
toggle.WithChecked(user.EmailNotifications),
)
func (*Toggle) HasDescription ¶
HasDescription returns true if toggle has a description.
func (*Toggle) IsLabelLeft ¶
IsLabelLeft returns true if label is on left.
func (*Toggle) IsLabelRight ¶
IsLabelRight returns true if label is on right.
func (*Toggle) KnobSizeClasses ¶
KnobSizeClasses returns CSS classes for the toggle knob.
func (*Toggle) KnobTranslateClass ¶
KnobTranslateClass returns CSS class for knob position.
func (*Toggle) SetChecked ¶
SetChecked sets the checked state.
func (*Toggle) SizeClasses ¶
SizeClasses returns CSS classes for the toggle track.
func (*Toggle) Styles ¶
func (t *Toggle) Styles() styles.ToggleStyles
Styles returns the resolved ToggleStyles for this component.
func (*Toggle) TrackColorClass ¶
TrackColorClass returns CSS class for track color.