htmx

package
v0.175.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attributes

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

Attributes represents htmx hx-* attributes applied to an element.

func NewAttributes

func NewAttributes(options ...AttributesOption) *Attributes

NewAttributes creates a Attributes object for an element with the given options.

func (*Attributes) GetAttributes

func (p *Attributes) GetAttributes() templ.Attributes

GetAttributes returns the hx-* attributes as a templ.Attributes.

func (*Attributes) HasAttribute

func (p *Attributes) HasAttribute(key string) bool

HasAttribute returns a boolean indicating whether there is an attribute with the given key.

func (*Attributes) SetAttribute

func (p *Attributes) SetAttribute(key string, value any)

SetAttribute sets an attribute with the given key to the given value. Any existing value is overridden.

type AttributesOption

type AttributesOption func(*Attributes)

AttributesOption is a functional option to set Properties.

func WithHXInclude

func WithHXInclude(value string) AttributesOption

WithHXInclude sets the hx-include attribute.

func WithHXMethod

func WithHXMethod(method, value string) AttributesOption

WithHXMethod sets the method attribute (i.e., hx-get, hx-post, etc.) to the given value.

func WithHXPushURL

func WithHXPushURL() AttributesOption

WithHXPushURL sets hx-push-url to true.

func WithHXReplaceURL

func WithHXReplaceURL() AttributesOption

WithHXReplaceURL sets hx-replace-url to true.

func WithHXSwap

func WithHXSwap(value string) AttributesOption

WithHXSwap sets the hx-swap attribute.

func WithHXTarget

func WithHXTarget(target string) AttributesOption

WithHXTarget sets the hx-target attribute.

func WithHXTrigger

func WithHXTrigger(value string) AttributesOption

WithHXTrigger sets the hx-trigger attribute.

func WithHXVals

func WithHXVals(vals any) AttributesOption

WithHXVals sets the hx-vals attribute. It can handle a string value directly or a map of values which will get marshaled into a JSON string representation.

type Config

type Config struct {
	// AllowNestedOOBSwaps configures whether to process OOB swaps on elements that are nested within the main response
	// element.
	AllowNestedOOBSwaps bool `json:"allowNestedOobSwaps"`
	// InlineStyleNonce configures a none to be added to inline styles created by htmx.
	InlineStyleNonce string `json:"inlineStyleNonce,omitempty"`
	// InlineStyleNonce configures a none to be added to inline scripts created by htmx.
	InlineScriptNonce string `json:"inlineScriptNonce,omitempty"`
	// IncludeIndicatorStyles configures whether htmx will dynamically add indicator styles inline for requests.
	IncludeIndicatorStyles bool `json:"includeIndicatorStyles"`
	// HistoryRestoreAsHxRequest configures whether to treat history cache miss full page reload requests as a
	// “HX-Request” by returning this response header. This should always be disabled when using HX-Request header to
	// optionally return partial responses
	HistoryRestoreAsHxRequest bool `json:"historyRestoreAsHxRequest"`
	// GlobalViewTransitions configures whether htmx will use the View Transition API when swapping in new content.
	GlobalViewTransitions bool `json:"globalViewTransitions"`
	// ResponseHandling configures how to handle various HTTP response codes.
	ResponseHandling []*ResponseHandling `json:"responseHandling,omitzero"`
	// Number of entries to cache in history. Defaults to 10.
	HistoryCacheSize int `json:"historyCacheSize"`
	// Whether to issue a full page refresh on history misses rather than use an AJAX request. Defaults to false.
	RefreshOnHistoryMiss bool `json:"refreshOnHistoryMiss"`
}

Config defines the htmx config options.

https://htmx.org/docs/#config

type HXLocationRequest

type HXLocationRequest struct {
	// The URL path.
	Path string `json:"path"`
	//  The source element of the request.
	Source string `json:"source,omitzero"`
	// An event that “triggered” the request.
	Event string `json:"event,omitzero"`
	// A JS callback that will handle the response HTML.
	Handler string `json:"handler,omitzero"`
	// The target to swap the response into.
	Target string `json:"target,omitzero"`
	// How the response will be swapped in relative to the target.
	Swap string `json:"swap,omitzero"`
	// Values to submit with the request.
	Values map[string]any `json:"values,omitzero"`
	// Headers to submit with the request.
	Headers map[string]string `json:"headers,omitzero"`
	// Allows you to select the content you want swapped from a response.
	Select string `json:"select,omitzero"`
	// Set to 'false' or a path string to prevent or override the URL pushed to browser location history
	Push string `json:"push,omitzero"`
	// A path string to replace the URL in the browser location history
	Replace string `json:"replace,omitzero"`
}

HXLocationRequest defines the value of the HX-Location header.

https://htmx.org/headers/hx-location/

type ResponseHandling

type ResponseHandling struct {
	// Code is a String representing a regular expression that will be tested against response codes.
	Code string `json:"code"`
	// Swap is true if the response should be swapped into the DOM, false otherwise.
	Swap bool `json:"swap"`
	// Error is true if htmx should treat this response as an error.
	Error bool `json:"error,omitempty,omitzero"`
	// IgnoreTitle is true if htmx should ignore title tags in the response.
	IgnoreTitle bool `json:"ignoreTitle,omitempty,omitzero"`
	// Select is a CSS selector to use to select content from the response.
	Select string `json:"select,omitempty,omitzero"`
	// Target is a CSS selector specifying an alternative target for the response.
	Target string `json:"target,omitempty,omitzero"`
	// SwapOverride is an alternative swap mechanism for the response.
	SwapOverride string `json:"swapOverride,omitempty,omitzero"`
}

ResponseHandling configures how htmx handles different HTTP response codes. When htmx receives a response it will iterate in order over the htmx.config.responseHandling array and test if the code property of a given object, when treated as a Regular Expression, matches the current response. If an entry does match the current response code, it will be used to determine if and how the response will be processed.

https://htmx.org/docs/#response-handling

Jump to

Keyboard shortcuts

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