utils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: MIT Imports: 10 Imported by: 8

Documentation

Overview

Package utils provides shared types, Tailwind class merging, and generic helpers used across all templ-components packages.

BaseProps is embedded by every component props struct for consistent ID, Class, Attrs, AriaLabel, and Nonce propagation. Class() merges Tailwind classes with conflict resolution via tailwind-merge-go.

Package utils provides shared utilities for templ components including Tailwind class merging, attribute helpers, and test rendering utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertContains

func AssertContains(t *testing.T, output, want string)

AssertContains checks that the rendered output contains a substring

func AssertContainsAll added in v0.3.0

func AssertContainsAll(t *testing.T, output string, wants ...string)

AssertContainsAll checks that the rendered output contains every substring in wants. Reports a single test error per missing substring.

func AssertEqual

func AssertEqual[T comparable](t *testing.T, context string, got, want T)

AssertEqual checks that got equals want, reporting a test error with context if not

func AssertNotContains

func AssertNotContains(t *testing.T, output, notWant string)

AssertNotContains checks that the rendered output does not contain a substring

func Class

func Class(classes ...string) string

Class merges Tailwind classes intelligently using tailwind-merge-go. Conflicting classes are resolved with later arguments overriding earlier ones. Thread-safe via sync.Mutex to protect the shared LRU cache.

func CurrentYear

func CurrentYear() string

CurrentYear returns the current year as a string

func DismissScript

func DismissScript() string

DismissScript returns shared JavaScript for dismissing elements via [data-dismiss] click delegation. Used by Alert, Toast, and ErrorAlert components.

func EnsureID added in v0.3.0

func EnsureID(prefix, id string) string

EnsureID returns id if non-empty, otherwise generates a unique ID with the given prefix using crypto/rand for collision safety across HTMX page loads. Format: tc-<prefix>-<16 hex chars> (e.g. tc-modal-a1b2c3d4e5f6a7b8).

func Lookup added in v0.3.0

func Lookup[K comparable, V any](m map[K]V, key K, fallback V) V

Lookup returns the map value for key, or fallback if not found. Replaces the repetitive if-ok-return pattern used across all enum lookups.

func Render

func Render(t *testing.T, c templ.Component) string

Render renders a templ component to a string for testing

func RenderAll added in v0.3.0

func RenderAll(t *testing.T, components ...templ.Component) string

RenderAll renders multiple templ components into a single concatenated string. Useful for integration tests that verify component composition.

func Ternary

func Ternary[T any](condition bool, a, b T) T

Ternary returns a if condition is true, otherwise b

func ValidateID added in v0.3.0

func ValidateID(componentName, id string) error

ValidateID returns an error if id is empty, using componentName in the message.

Types

type BaseProps

type BaseProps struct {
	ID        string
	Class     string
	Attrs     templ.Attributes
	AriaLabel string
	Nonce     string
}

BaseProps provides common configurable attributes for all components

func (*BaseProps) GetBaseProps

func (p *BaseProps) GetBaseProps() BaseProps

GetBaseProps returns the embedded BaseProps. This method is promoted to all structs that embed BaseProps, satisfying the ComponentProps interface.

func (*BaseProps) SetBaseProps

func (p *BaseProps) SetBaseProps(bp BaseProps)

SetBaseProps updates the embedded BaseProps. This method is promoted to all structs that embed BaseProps, satisfying the ComponentProps interface.

type ComponentProps

type ComponentProps interface {
	GetBaseProps() BaseProps
	SetBaseProps(BaseProps)
}

ComponentProps is the interface implemented by all component props structs that embed BaseProps. It enables generic component composition, validation, and wrapper functions across the entire component library.

Jump to

Keyboard shortcuts

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