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
- func AssertContains(t *testing.T, output, want string)
- func AssertContainsAll(t *testing.T, output string, wants ...string)
- func AssertEqual[T comparable](t *testing.T, context string, got, want T)
- func AssertNotContains(t *testing.T, output, notWant string)
- func Class(classes ...string) string
- func CurrentYear() string
- func DismissButton(bgClass string, textClass string) templ.Component
- func DismissScript() string
- func EnsureID(prefix, id string) string
- func Lookup[K comparable, V any](m map[K]V, key K, fallback V) V
- func Render(t *testing.T, c templ.Component) string
- func RenderAll(t *testing.T, components ...templ.Component) string
- func Ternary[T any](condition bool, a, b T) T
- func ValidateID(componentName, id string) error
- type BaseProps
- type ComponentProps
Constants ¶
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.
const Version = "1.1.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 ¶
AssertContains checks that the rendered output contains a substring.
func AssertContainsAll ¶ added in v0.3.0
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 ¶
AssertNotContains checks that the rendered output does not contain a substring.
func Class ¶
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 DismissButton ¶ added in v0.10.0
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
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 RenderAll ¶ added in v0.3.0
RenderAll renders multiple templ components into a single concatenated string. Useful for integration tests that verify component composition.
func Ternary ¶
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
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 ¶
GetBaseProps returns the embedded BaseProps. This method is promoted to all structs that embed BaseProps, satisfying the ComponentProps interface.
func (*BaseProps) SetBaseProps ¶
SetBaseProps updates the embedded BaseProps. This method is promoted to all structs that embed BaseProps, satisfying the ComponentProps interface.
type ComponentProps ¶
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.