utils

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 12 Imported by: 8

Documentation

Overview

templ: version: v0.3.1020

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.

Test rendering utilities: Render, RenderAll, Assert* helpers for component testing.

Index

Constants

View Source
const (
	TransitionFast      = "transition-all duration-150 ease-out motion-reduce:transition-none motion-reduce:duration-0"
	TransitionNormal    = "transition-all duration-200 ease-out motion-reduce:transition-none motion-reduce:duration-0"
	TransitionColors    = "transition-colors motion-reduce:transition-none motion-reduce:duration-0"
	TransitionTransform = "transition-transform motion-reduce:transition-none"
)

Motion class constants for consistent animation timing across all packages. Durations follow motion design research guidelines:

  • 150ms: micro-interactions (hover, toggle, tooltip)
  • 200ms: overlays (modal, drawer, dropdown)
  • 300ms: panel transitions, large element movement

Every transition includes motion-reduce fallbacks for vestibular safety. Use these instead of inline strings so timing is consistent and auditable.

View Source
const Version = "1.3.0"

Version is the semantic version of the templ-components library. It is the single source of truth for the current release and should be bumped in lockstep with the CHANGELOG and Git tag.

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 DismissButton added in v0.10.0

func DismissButton(bgClass string, textClass string) templ.Component

DismissButton renders the shared alert/notification dismiss control used by feedback.Alert and errorpage.ErrorAlert. The bg/text classes must be passed in because the caller owns the visual style set (feedback.FeedbackType vs errorpage.Family).

The X icon path (M6 18L18 6M6 6l12 12) is inlined rather than going through github.com/larsartmann/templ-components/icons to keep utils a leaf package that does not depend on icons. The SVG matches the icons package's stroke template (class, fill, viewBox, stroke-width, stroke, aria-hidden).

func DismissScript

func DismissScript() string

DismissScript returns shared JavaScript for dismissing elements via [data-dismiss] click delegation. Used by Alert, Toast, and ErrorAlert components. Handles both role="alert" (Alert) and role="status" (Toast) containers.

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). If crypto/rand fails (extremely unlikely), falls back to an atomic counter + nanosecond timestamp — never raw nanoseconds (predictable under concurrency).

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. Note: both a and b are eagerly evaluated (this is a function, not a macro), so callers passing function calls or side-effecting expressions should use inline if/else in templ instead: { if cond }...{ else }...{ end }.

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(props 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