toggle

package
v0.1.1 Latest Latest
Warning

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

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

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-click, lvt-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

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) Check

func (c *Checkbox) Check()

Check sets checked to true.

func (*Checkbox) CheckboxStateClass

func (c *Checkbox) CheckboxStateClass() string

CheckboxStateClass returns CSS class for checkbox state.

func (*Checkbox) HasDescription

func (c *Checkbox) HasDescription() bool

HasDescription returns true if checkbox has a description.

func (*Checkbox) HasLabel

func (c *Checkbox) HasLabel() bool

HasLabel returns true if checkbox has a label.

func (*Checkbox) SetIndeterminate

func (c *Checkbox) SetIndeterminate(indeterminate bool)

SetIndeterminate sets the indeterminate state.

func (*Checkbox) Styles

func (c *Checkbox) Styles() styles.CheckboxStyles

Styles returns the resolved CheckboxStyles for this component.

func (*Checkbox) Toggle

func (c *Checkbox) Toggle()

Toggle toggles the checked state.

func (*Checkbox) Uncheck

func (c *Checkbox) Uncheck()

Uncheck sets checked to false.

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

func WithChecked(checked bool) Option

WithChecked sets the initial checked state.

func WithDescription

func WithDescription(description string) Option

WithDescription sets the helper text.

func WithDisabled

func WithDisabled(disabled bool) Option

WithDisabled sets the disabled state.

func WithLabel

func WithLabel(label string) Option

WithLabel sets the toggle label.

func WithLabelPosition

func WithLabelPosition(position string) Option

WithLabelPosition sets where the label appears.

func WithName

func WithName(name string) Option

WithName sets the form field name.

func WithRequired

func WithRequired(required bool) Option

WithRequired sets the required state.

func WithSize

func WithSize(size Size) Option

WithSize sets the toggle size.

func WithStyled

func WithStyled(styled bool) Option

WithStyled enables Tailwind CSS styling.

func WithValue

func WithValue(value string) Option

WithValue sets the value when checked.

type Size

type Size string

Size defines the toggle size.

const (
	SizeSm Size = "sm"
	SizeMd Size = "md"
	SizeLg Size = "lg"
)

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

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

New creates a toggle switch.

Example:

t := toggle.New("notifications",
    toggle.WithLabel("Email Notifications"),
    toggle.WithChecked(user.EmailNotifications),
)

func (*Toggle) Check

func (t *Toggle) Check()

Check sets checked to true.

func (*Toggle) HasDescription

func (t *Toggle) HasDescription() bool

HasDescription returns true if toggle has a description.

func (*Toggle) HasLabel

func (t *Toggle) HasLabel() bool

HasLabel returns true if toggle has a label.

func (*Toggle) IsLabelLeft

func (t *Toggle) IsLabelLeft() bool

IsLabelLeft returns true if label is on left.

func (*Toggle) IsLabelRight

func (t *Toggle) IsLabelRight() bool

IsLabelRight returns true if label is on right.

func (*Toggle) IsOff

func (t *Toggle) IsOff() bool

IsOff returns true if toggle is not checked.

func (*Toggle) IsOn

func (t *Toggle) IsOn() bool

IsOn returns true if toggle is checked.

func (*Toggle) KnobSizeClasses

func (t *Toggle) KnobSizeClasses() string

KnobSizeClasses returns CSS classes for the toggle knob.

func (*Toggle) KnobTranslateClass

func (t *Toggle) KnobTranslateClass() string

KnobTranslateClass returns CSS class for knob position.

func (*Toggle) SetChecked

func (t *Toggle) SetChecked(checked bool)

SetChecked sets the checked state.

func (*Toggle) SizeClasses

func (t *Toggle) SizeClasses() string

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) Toggle

func (t *Toggle) Toggle()

Toggle toggles the checked state.

func (*Toggle) TrackColorClass

func (t *Toggle) TrackColorClass() string

TrackColorClass returns CSS class for track color.

func (*Toggle) Uncheck

func (t *Toggle) Uncheck()

Uncheck sets checked to false.

Jump to

Keyboard shortcuts

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