utils

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 8 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 AssertContainsClass

func AssertContainsClass(t *testing.T, output, class string)

AssertContainsClass checks that the rendered output contains a CSS class in a class attribute. Handles classes embedded in larger class strings.

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 MapEnum

func MapEnum[T ~string](m map[string]T, fallback T, key string) T

MapEnum looks up a string key in a map and returns the corresponding enum value, or the fallback if the key is not found.

func Render

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

Render renders a templ component to a string for testing

func Ternary

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

Ternary returns a if condition is true, otherwise b

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