utils

package
v0.0.0-...-44207f1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IsMobileKey is the context key for mobile detection
	IsMobileKey contextKey = "isMobile"
)

Variables

This section is empty.

Functions

func BuildConditional

func BuildConditional(condition, trueExpr, falseExpr string) string

BuildConditional creates a conditional expression

func GetAnchorPosition

func GetAnchorPosition(side AnchorSide, align AnchorAlign, sideOffset int) string

GetAnchorPosition generates CSS anchor positioning based on side and align This function contains the proven working positioning logic from popover

func GetMobile

func GetMobile(ctx context.Context) bool

GetMobile retrieves the mobile detection result from the context

func IsMobile

func IsMobile(c echo.Context) bool

IsMobile detects if the request is from a mobile device based on User-Agent

func ParseConnectViolations

func ParseConnectViolations(err error) map[string]string

ParseConnectViolations extracts field-level validation errors from a Connect error

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"

func WithMobile

func WithMobile(ctx context.Context, isMobile bool) context.Context

WithMobile adds the mobile detection result to the context

Types

type AnchorAlign

type AnchorAlign string

AnchorAlign defines the alignment for anchor-positioned elements

const (
	AnchorAlignStart  AnchorAlign = "start"
	AnchorAlignCenter AnchorAlign = "center"
	AnchorAlignEnd    AnchorAlign = "end"
)

type AnchorSide

type AnchorSide string

AnchorSide defines the positioning side for anchor-positioned elements

const (
	AnchorSideTop    AnchorSide = "top"
	AnchorSideBottom AnchorSide = "bottom"
	AnchorSideLeft   AnchorSide = "left"
	AnchorSideRight  AnchorSide = "right"
)

type DataClass

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

DataClass helps build Datastar data-class attribute expressions It generates object syntax for conditional CSS classes

func NewDataClass

func NewDataClass() *DataClass

NewDataClass creates a new DataClass builder

func (*DataClass) Add

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

Add adds a conditional class

func (*DataClass) Build

func (d *DataClass) Build() string

Build creates the data-class object expression

type DatastarExpression

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

DatastarExpression represents a structured Datastar expression builder

func NewExpression

func NewExpression() *DatastarExpression

NewExpression creates a new Datastar expression builder with semicolon separator

func (*DatastarExpression) Build

func (e *DatastarExpression) Build() string

Build returns the final expression string

func (*DatastarExpression) Conditional

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

Conditional adds a conditional statement

func (*DatastarExpression) SetSignal

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

SetSignal adds a signal assignment statement

func (*DatastarExpression) Statement

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

Statement adds a statement to the expression

type DeviceType

type DeviceType string

DeviceType represents the type of device making the request

const (
	DeviceDesktop DeviceType = "desktop"
	DeviceMobile  DeviceType = "mobile"
	DeviceTablet  DeviceType = "tablet"
)

func GetDeviceType

func GetDeviceType(c echo.Context) DeviceType

GetDeviceType returns a more granular device type

type FocusCapture

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

FocusCapture creates capture handlers

func NewFocusCapture

func NewFocusCapture() *FocusCapture

NewFocusCapture creates a focus capture handler

func (*FocusCapture) Build

func (f *FocusCapture) Build() string

Build creates the focus capture handler

func (*FocusCapture) OnSelector

func (f *FocusCapture) OnSelector(selector string) *FocusCapture

OnSelector restricts to specific selector

func (*FocusCapture) OnlyInputs

func (f *FocusCapture) OnlyInputs() *FocusCapture

OnlyInputs restricts to input elements only

func (*FocusCapture) SetSignal

func (f *FocusCapture) SetSignal(signals *SignalManager, signal, value string) *FocusCapture

SetSignal sets a signal when focus condition is met

type SignalManager

type SignalManager struct {
	ID          string `json:"id"`
	Signals     any    `json:"signals"`
	DataSignals string `json:"dataSignals"`
}

SignalManager provides a structured way to manage Datastar signals It namespaces signals by ID and provides serialization capabilities

func Signals

func Signals(id string, signalsStruct any) *SignalManager

Signals creates a new SignalManager instance with the given ID and signals struct The signalsStruct should be any struct that has json tags for each property The ID will be sanitized to replace hyphens with underscores for JavaScript compatibility Example:

type MySignals struct {
    Open     bool   `json:"open"`
    Value    string `json:"value"`
    Count    int    `json:"count"`
}
signals := Signals("myComponent", MySignals{Open: false, Value: "", Count: 0})
// Use in templ: data-signals={ signals.DataSignals }

func (*SignalManager) Conditional

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

Conditional returns a conditional expression for a signal property Example: signals.Conditional("loading", "Saving...", "Save") returns "$myComponent.loading ? 'Saving...' : 'Save'"

func (*SignalManager) ConditionalAction

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

ConditionalAction creates a safe conditional action expression using ternary operator Example: signals.ConditionalAction("evt.target === evt.currentTarget", "open", "false") Returns: "evt.target === evt.currentTarget ? ($component.open = false) : void 0"

func (*SignalManager) ConditionalMultiAction

func (sm *SignalManager) ConditionalMultiAction(condition string, actions ...string) string

ConditionalMultiAction creates a safe conditional expression with multiple actions using ternary operator Example: signals.ConditionalMultiAction("condition", []string{"action1", "action2"}) Returns: "condition ? (action1, action2) : void 0"

func (*SignalManager) DataClass

func (sm *SignalManager) DataClass(classConditions map[string]string) string

DataClass creates a clean JSON object for data-class attributes from a map of class names to conditions This allows for more maintainable conditional class logic Example:

classes := signals.DataClass(map[string]string{
  "bg-primary text-white": "$component.active",
  "opacity-50": "!$component.enabled",
  "hidden": "$component.mode === 'hidden'",
})
// Use in templ: data-class={ classes }

func (*SignalManager) Equals

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

Equals creates a comparison expression between a signal and a value Example: signals.Equals("value", "option1") returns "$component.value === 'option1'"

func (*SignalManager) MultiStateConditional

func (sm *SignalManager) MultiStateConditional(states []StateAction) string

func (*SignalManager) NotEquals

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

NotEquals creates a not-equals comparison expression Example: signals.NotEquals("value", "option1") returns "$component.value !== 'option1'"

func (*SignalManager) Set

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

Set returns a set expression for a signal property Example: signals.Set("value", "'hello'") returns "$myComponent.value = 'hello'"

func (*SignalManager) SetString

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

SetString returns a set expression for a string signal property with proper quoting Example: signals.SetString("value", "hello") returns "$myComponent.value = 'hello'"

func (*SignalManager) Signal

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

Signal returns a reference to a specific signal property Example: signals.Signal("open") returns "$myComponent.open"

func (*SignalManager) TernaryClass

func (sm *SignalManager) TernaryClass(property, trueClass, falseClass string) string

TernaryClass creates a ternary expression for conditional CSS classes Example: signals.TernaryClass("checked", "bg-primary", "bg-secondary") Returns: "$component.checked ? 'bg-primary' : 'bg-secondary'"

func (*SignalManager) TernaryStyle

func (sm *SignalManager) TernaryStyle(property, trueStyle, falseStyle string) string

TernaryStyle creates a ternary expression for conditional inline styles Example: signals.TernaryStyle("visible", "opacity: 1", "opacity: 0") Returns: "$component.visible ? 'opacity: 1' : 'opacity: 0'"

func (*SignalManager) Toggle

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

Toggle returns a toggle expression for a boolean signal property Example: signals.Toggle("open") returns "$myComponent.open = !$myComponent.open"

type StateAction

type StateAction struct {
	Condition string
	Actions   []string
}

MultiStateConditional creates a chain of conditional expressions for handling multiple states

Example: signals.MultiStateConditional([]StateAction{
  {Condition: "!$comp.start", Actions: []string{"setStart", "clearEnd"}},
  {Condition: "!$comp.end", Actions: []string{"setEnd", "dispatchEvent"}},
  {Condition: "true", Actions: []string{"resetRange"}},
})

Jump to

Keyboard shortcuts

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