utils

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildConditional

func BuildConditional(condition, trueExpr, falseExpr string) string

BuildConditional creates a standalone ternary expression.

func If

func If[T comparable](condition bool, value T) T

If returns value if condition is true, otherwise an empty value of type T. Example: true, "bg-red-500" → "bg-red-500"

func IfElse

func IfElse[T any](condition bool, trueValue T, falseValue T) T

IfElse returns trueValue if condition is true, otherwise falseValue. Example: true, "bg-red-500", "bg-gray-300" → "bg-red-500"

func MergeAttributes

func MergeAttributes(attrs ...templ.Attributes) templ.Attributes

MergeAttributes combines multiple Attributes into one. Example: MergeAttributes(attr1, attr2) → combined attributes

func RandomID

func RandomID() string

RandomID generates a random ID string. Example: RandomID() → "id-1a2b3c"

func TwMerge

func TwMerge(classes ...string) string

TwMerge combines Tailwind classes and resolves conflicts. Example: "bg-red-500 hover:bg-blue-500", "bg-green-500" → "hover:bg-blue-500 bg-green-500"

Types

type DataClass

type DataClass struct {
	// contains filtered or unexported fields
}

DataClass builds data-class attribute values: JS objects mapping CSS class names to signal conditions.

func NewDataClass

func NewDataClass() *DataClass

NewDataClass creates a new DataClass builder.

func (*DataClass) Add

func (d *DataClass) Add(className, condition string) *DataClass

Add maps a CSS class name to a signal condition.

func (*DataClass) Build

func (d *DataClass) Build() string

Build returns the JS object string for the data-class attribute.

type DatastarExpression

type DatastarExpression struct {
	// contains filtered or unexported fields
}

DatastarExpression builds composable multi-statement Datastar expressions.

func NewExpression

func NewExpression() *DatastarExpression

NewExpression creates a new expression builder.

func (*DatastarExpression) Build

func (e *DatastarExpression) Build() string

Build joins all statements with "; " and returns the final expression.

func (*DatastarExpression) Conditional

func (e *DatastarExpression) Conditional(condition, trueExpr, falseExpr string) *DatastarExpression

Conditional adds a ternary expression.

func (*DatastarExpression) SetSignal

func (e *DatastarExpression) SetSignal(signal, value string) *DatastarExpression

SetSignal adds a signal assignment: $signal = value

func (*DatastarExpression) Statement

func (e *DatastarExpression) Statement(stmt string) *DatastarExpression

Statement adds a raw statement to the expression.

type SignalManager

type SignalManager struct {
	ID          string
	Signals     any
	DataSignals string
}

SignalManager provides a structured way to manage Datastar signals. It namespaces signals by ID so multiple instances of the same component on a page don't collide.

func Signals

func Signals(id string, signalsStruct any) *SignalManager

Signals creates a new SignalManager with the given ID and initial state. The ID is sanitized (hyphens → underscores) for JavaScript compatibility. The signalsStruct should have json tags for each property.

func (*SignalManager) Conditional

func (sm *SignalManager) Conditional(property, trueValue, falseValue string) string

Conditional returns a ternary expression based on a signal property.

func (*SignalManager) ConditionalAction

func (sm *SignalManager) ConditionalAction(condition, property, value string) string

ConditionalAction executes an action only when a condition is true.

func (*SignalManager) Equals

func (sm *SignalManager) Equals(property, value string) string

Equals returns a strict equality comparison expression.

func (*SignalManager) NotEquals

func (sm *SignalManager) NotEquals(property, value string) string

NotEquals returns a strict inequality comparison expression.

func (*SignalManager) Set

func (sm *SignalManager) Set(property, value string) string

Set returns an assignment expression for a signal property.

func (*SignalManager) SetString

func (sm *SignalManager) SetString(property, value string) string

SetString returns an assignment expression with proper JS string quoting.

func (*SignalManager) Signal

func (sm *SignalManager) Signal(property string) string

Signal returns a reference to a signal property: "$componentID.property"

func (*SignalManager) Toggle

func (sm *SignalManager) Toggle(property string) string

Toggle returns an expression that flips a boolean signal.

Jump to

Keyboard shortcuts

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