htmx

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: MIT Imports: 9 Imported by: 24

README

GoDoc GitHub Workflow Status (with event) License Stars Discord

htmx-go is a type-safe library for working with HTMX in Go.

Less time fiddling with HTTP headers, more time developing awesome Hypermedia-driven applications.

Check if requests are from HTMX, and use a type-safe, declarative syntax for HTMX response headers to control HTMX behavior from the server.

Write triggers for client-side events effectively without dealing with JSON serialization. With this approach, event-driven applications are easier to develop.

Use Swap Strategy methods to fine-tune hx-swap behavior.

Uses standard net/http types. Has basic integration with templ components.

import (
	"net/http"

	"github.com/angelofallars/htmx-go"
)

func handler(w http.ResponseWriter, r *http.Request) {
	if htmx.IsHTMX(r) {
		htmx.NewResponse().
			Reswap(htmx.SwapBeforeEnd).
			Retarget("#contacts").
			AddTrigger(htmx.Trigger("enable-submit")).
			AddTrigger(htmx.TriggerDetail("display-message", "Hello world!")).
			Write(w)
	}
}

Think this project is awesome? Consider sponsoring me 💙

Installation

Use go get.

go get github.com/angelofallars/htmx-go

Then import htmx-go:

import "github.com/angelofallars/htmx-go"

HTMX Requests

Check request origin

You can determine if a request is from HTMX. With this, you can add custom handling for non-HTMX requests.

You can also use this for checking if this is a GET request for the initial page load on your website, as initial page load requests don't use HTMX.

func handler(w http.ResponseWriter, r *http.Request) {
	if htmx.IsHTMX(r) {
		// logic for handling HTMX requests
	} else {
		// logic for handling non-HTMX requests (e.g. render a full page for first-time visitors)
	}
}
Check if request is Boosted (hx-boost)
func handler(w http.ResponseWriter, r *http.Request) {
	if htmx.IsBoosted(r) {
		// logic for handling boosted requests
	} else {
		// logic for handling non-boosted requests
	}
}

HTMX responses

htmx-go takes inspiration from Lip Gloss for a declarative way of specifying HTMX response headers.

Basic usage

Make a response writer with htmx.NewResponse(), and add a header to it to make the page refresh:

func handler(w http.ResponseWriter, r *http.Request) {
	writer := htmx.NewResponse().Refresh(true)
	writer.Write(w)
}
Retarget response to a different element
func handler(w http.ResponseWriter, r *http.Request) {
	htmx.NewResponse().
		// Override 'hx-target' to specify which target to load into
		Retarget("#errors").
		// Also override the 'hx-swap' value of the request
		Reswap(htmx.SwapBeforeEnd).
		Write(w)
}
Triggers

HTMX Reference: hx-trigger

You can add triggers to trigger client-side events. htmx-go takes care of formatting and JSON serialization of the header values.

Define event triggers:

  • htmx.Trigger(eventName string) - A trigger with no details.
  • htmx.TriggerDetail(eventName string, detailValue string) - A trigger with one detail value.
  • htmx.TriggerObject(eventName string, detailObject any) - A trigger with a JSON-serializable detail object. Recommended to pass in either map[string]string or structs with JSON field tags.

Set trigger headers using the preceding triggers:

  • Response.AddTrigger(trigger ...EventTrigger) - appends to the HX-Trigger header
  • Response.AddTriggerAfterSettle(trigger ...EventTrigger) - appends to the HX-Trigger-After-Settle header
  • Response.AddTriggerAfterSwap(trigger ...EventTrigger) - appends to the HX-Trigger-After-Swap header
htmx.NewResponse().
	AddTrigger(htmx.Trigger("myEvent"))
// HX-Trigger: myEvent

htmx.NewResponse().
	AddTrigger(htmx.TriggerDetail("showMessage", "Here Is A Message"))
// HX-Trigger: {"showMessage":"Here Is A Message"}

htmx.NewResponse().
	AddTrigger(
		htmx.TriggerDetail("hello", "world"),
		htmx.TriggerObject("myEvent", map[string]string{
			"level":   "info",
			"message": "Here Is A Message",
		}),
	)
// HX-Trigger: {"hello":"world","myEvent":{"level":"info","message":"Here is a Message"}}

[!TIP] Alpine.js and Hyperscript can listen to and receive details from events triggered by htmx-go. This makes triggers initiated by the server very handy for event-driven applications!

For Alpine.js, you can register an x-on:<EventName>.window listener. The .window modifier is important because HTMX dispatches events from the root window object. To receive values sent by htmx.TriggerDetail and htmx.TriggerObject, you can use $event.detail.value.

Swap strategy

HTMX Reference: hx-swap

Response.Reswap() takes in SwapStrategy values from this library.

htmx.NewResponse().
	Reswap(htmx.SwapInnerHTML)
// HX-Reswap: innerHTML

htmx.NewResponse().
	Reswap(htmx.SwapAfterEnd.Transition(true))
// HX-Reswap: innerHTML transition:true

Exported SwapStrategy constant values can be appended with modifiers through their methods. If successive methods write to the same modifier, the modifier is always replaced with the latest one.

import "time"

htmx.SwapInnerHTMl.After(time.Second * 1)
// HX-Reswap: innerHTML swap:1s

htmx.SwapBeforeEnd.Scroll(htmx.Bottom)
// HX-Reswap: beforeend scroll:bottom

htmx.SwapAfterEnd.IgnoreTitle(true)
// HX-Reswap: afterend ignoreTitle:true

htmx.SwapAfterEnd.FocusScroll(true)
// HX-Reswap: afterend ignoreTitle:true

htmx.SwapInnerHTML.ShowOn("#another-div", htmx.Top)
// HX-Reswap: innerHTML show:#another-div:top

// Modifier chaining
htmx.SwapInnerHTML.ShowOn("#another-div", htmx.Top).After(time.Millisecond * 500)
// HX-Reswap: innerHTML show:#another-div:top swap:500ms

htmx.SwapBeforeBegin.ShowWindow(htmx.Top)
// HX-Reswap: beforebegin show:window:top

htmx.SwapDefault.ShowNone()
// HX-Reswap: show:none
Code organization

HTMX response writers can be declared outside of functions with var so you can reuse them in several places.

[!CAUTION] If you're adding additional headers to a global response writer, always use the .Clone() method to avoid accidentally modifying the global response writer.

var deleter = htmx.NewResponse().
    Reswap(htmx.SwapDelete)

func(w http.ResponseWriter, r *http.Request) {
	deleter.Clone().
		Reselect("#messages").
		Write(w)
}
Templ integration

HTMX pairs well with Templ, and this library is no exception. You can render both the necessary HTMX response headers and Templ components in one step with the .RenderTempl() method.

// hello.templ
templ Hello() {
    <div>Hello { name }!</div>
}

// main.go
func(w http.ResponseWriter, r *http.Request) {
	htmx.NewResponse().
		Retarget("#hello").
		RenderTempl(r.Context(), w, Hello())
}

[!NOTE] To avoid issues with custom HTTP status code headers with this approach, it's recommended to use Response().StatusCode() so the status code header is always set after the HTMX headers.

Stop polling

If you have an element that is polling a URL and you want it to stop, use the htmx.StatusStopPolling 286 status code in a response to cancel the polling. HTMX documentation reference

w.WriteHeader(htmx.StatusStopPolling)

Header names

If you need to work with HTMX headers directly, htmx-go provides constant values for all HTTP header field names of HTMX so you don't have to write them yourself. This mitigates the risk of writing header names with typos.

// Request headers
const (
	HeaderBoosted               = "HX-Boosted"
	HeaderCurrentURL            = "HX-Current-URL"
	HeaderHistoryRestoreRequest = "HX-History-Restore-Request"
	HeaderPrompt                = "HX-Prompt"
	HeaderRequest               = "Hx-Request"
	HeaderTarget                = "HX-Target"
	HeaderTriggerName           = "Hx-Trigger-Name"
)

// Common headers
const (
	HeaderTrigger = "HX-Trigger"
)

// Response headers
const (
	HeaderLocation           = "HX-Location"
	HeaderPushURL            = "HX-Push-Url"
	HeaderRedirect           = "HX-Redirect"
	HeaderRefresh            = "HX-Refresh"
	HeaderReplaceUrl         = "HX-Replace-Url"
	HeaderReswap             = "HX-Reswap"
	HeaderRetarget           = "HX-Retarget"
	HeaderReselect           = "HX-Reselect"
	HeaderTriggerAfterSettle = "HX-Trigger-After-Settle"
	HeaderTriggerAfterSwap   = "HX-Trigger-After-Swap"
)

Compatibility

This library is compatible with the standard net/http library, as well as other routers like Chi and Gorilla Mux that use the standard http.HandlerFunc handler type.

With the Echo web framework, try passing in context.Request() and context.Response().Writer for requests and responses, respectively.

With the Gin web framework on the other hand, try using context.Request and context.Writer.

If you use Fiber, it is recommended to use htmx-fiber instead, which is a fork of htmx-go.

Additional resources

Contributing

Pull requests are welcome!

License

MIT

Documentation

Overview

Package htmx provides utilities to build HTMX-powered web applications.

Index

Constants

View Source
const (
	// Request header that is "true" if the request was made from an element using 'hx-boost'.
	HeaderBoosted = "HX-Boosted"
	// Request header for the current URL of the browser.
	HeaderCurrentURL = "HX-Current-URL"
	// Request header that is “true” if the request is for history restoration after a miss in the local history cache.
	HeaderHistoryRestoreRequest = "HX-History-Restore-Request"
	// Request header for the user response to an hx-prompt.
	HeaderPrompt = "HX-Prompt"
	// Request header that is always “true” for HTMX requests.
	HeaderRequest = "Hx-Request"
	// Request header of the id of the target element if it exists.
	HeaderTarget = "HX-Target"
	// Request header of the name of the triggered element if it exists.
	HeaderTriggerName = "Hx-Trigger-Name"
)

HTTP request headers

View Source
const (
	// Response header that allows you to do a client-side redirect that does not do a full page reload.
	HeaderLocation = "HX-Location"
	// Response header that pushes a new url into the history stack.
	HeaderPushURL = "HX-Push-Url"
	// Response header that can be used to do a client-side redirect to a new location.
	HeaderRedirect = "HX-Redirect"
	// Response header that if set to “true” the client-side will do a full refresh of the page.
	HeaderRefresh = "HX-Refresh"
	// Response header that replaces the current URL in the location bar.
	HeaderReplaceUrl = "HX-Replace-Url"
	// Response header that allows you to specify how the response will be swapped.
	HeaderReswap = "HX-Reswap"
	// Response header that uses a CSS selector that updates the target of the content update to a
	// different element on the page.
	HeaderRetarget = "HX-Retarget"
	// Response header that uses a CSS selector that allows you to choose which
	// part of the response is used to be swapped in. Overrides an existing hx-select
	// on the triggering element.
	HeaderReselect = "HX-Reselect"
	// Response header that allows you to trigger client-side events after the settle step.
	HeaderTriggerAfterSettle = "HX-Trigger-After-Settle"
	// Response header that allows you to trigger client-side events after the swap step.
	HeaderTriggerAfterSwap = "HX-Trigger-After-Swap"
)

HTTP response headers

View Source
const (
	// Direction value for the 'scroll' and 'show' swap modifier methods.
	Top direction = "top"
	// Direction value for the 'scroll' and 'show' swap modifier methods.
	Bottom direction = "bottom"
)

Direction values for the htmx.SwapStrategy 'scroll' and 'show' modifier methods.

View Source
const (
	// As a request header: The ID of the triggered element if it exists.
	//
	// As a response header: Allows you to trigger client-side events.
	HeaderTrigger = "HX-Trigger"
)

Common HTTP headers

View Source
const StatusStopPolling int = 286

286 Stop Polling

HTTP status code that tells HTMX to stop polling from a server response.

For more info, see https://htmx.org/docs/#load_polling

Variables

This section is empty.

Functions

func GetCurrentURL

func GetCurrentURL(r *http.Request) (string, bool)

GetCurrentURL returns the current URL that HTMX made this request from.

Returns false if header 'HX-Current-URL' does not exist.

func GetPrompt

func GetPrompt(r *http.Request) (string, bool)

GetPrompt returns the user response to an hx-prompt from a given request.

Returns false if header 'HX-Prompt' does not exist.

For more info, see https://htmx.org/attributes/hx-prompt/

func GetTarget

func GetTarget(r *http.Request) (string, bool)

GetTarget returns the ID of the target element if it exists from a given request.

Returns false if header 'HX-Target' does not exist.

For more info, see https://htmx.org/attributes/hx-target/

func GetTrigger

func GetTrigger(r *http.Request) (string, bool)

GetTrigger returns the ID of the triggered element if it exists from a given request.

Returns false if header 'HX-Trigger' does not exist.

For more info, see https://htmx.org/attributes/hx-trigger/

func GetTriggerName

func GetTriggerName(r *http.Request) (string, bool)

GetTriggerName returns the 'name' of the triggered element if it exists from a given request.

Returns false if header 'HX-Trigger-Name' does not exist.

For more info, see https://htmx.org/attributes/hx-trigger/

func IsBoosted

func IsBoosted(r *http.Request) bool

IsBoosted returns true if the given request was made via an element using 'hx-boost'.

This can be used to add special logic for boosted requests.

Checks if header 'HX-Boosted' is 'true'.

For more info, see https://htmx.org/attributes/hx-boost/

func IsHTMX

func IsHTMX(r *http.Request) bool

IsHTMX returns true if the given request was made by HTMX.

This can be used to add special logic for HTMX requests.

Checks if header 'HX-Request' is 'true'.

func IsHistoryRestoreRequest

func IsHistoryRestoreRequest(r *http.Request) bool

IsHistoryRestoreRequest returns true if the given request is for history restoration after a miss in the local history cache.

Checks if header 'HX-History-Restore-Request' is 'true'.

func Trigger

func Trigger(eventName string) triggerPlain

Trigger returns an event trigger with no additional details.

Example:

htmx.Trigger("myEvent")

Output header:

HX-Trigger: myEvent

For more info, see https://htmx.org/headers/hx-trigger/

func TriggerDetail added in v0.2.0

func TriggerDetail(eventName string, detailValue string) triggerDetail

TriggerDetail returns an event trigger with one detail string. Will be encoded as JSON.

Example:

htmx.TriggerDetail("showMessage", "Here Is A Message")

Output header:

HX-Trigger: {"showMessage":"Here Is A Message"}

For more info, see https://htmx.org/headers/hx-trigger/

func TriggerObject added in v0.2.0

func TriggerObject(eventName string, detailObject any) triggerObject

TriggerObject returns an event trigger with a given detail object that **must** be serializable to JSON.

Structs with JSON tags can work, and so does `map[string]string` values which are safe to serialize.

Example:

htmx.TriggerObject("showMessage", map[string]string{
  "level": "info",
  "message": "Here Is A Message",
})

Output header:

HX-Trigger: {"showMessage":{"level" : "info", "message" : "Here Is A Message"}}

For more info, see https://htmx.org/headers/hx-trigger/

Types

type Direction added in v0.4.0

type Direction interface {
	// contains filtered or unexported methods
}

Direction is a value for the htmx.SwapStrategy 'scroll' and 'show' modifier methods.

Possible values are htmx.Top and htmx.Bottom.

type EventTrigger

type EventTrigger interface {
	// contains filtered or unexported methods
}

EventTrigger gives an HTMX response directives to triggers events on the client side.

type LocationContext

type LocationContext struct {
	// The source element of the request.
	Source string
	// An event that “triggered” the request.
	Event string
	// A JavaScript callback that will handle the response HTML.
	Handler string
	// The target to swap the response into.
	Target string
	// How the response will be swapped in relative to the target.
	Swap SwapStrategy
	// Values to submit with the request.
	Values map[string]string
	// Headers to submit with the request
	Headers map[string]string
	// Allows you to select the content you want swapped from a response.
	Select string
}

A context object that is used by htmx.Response.LocationWithContext to finely determine the behavior of client-side redirection.

In the browser, these are the parameters that will be used by 'htmx.ajax()'.

For more info, see https://htmx.org/headers/hx-location/

type Response

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

Response contains HTMX headers to write to a response.

func NewResponse

func NewResponse() Response

NewResponse returns a new HTMX response header writer.

Any subsequent method calls that write to the same header will overwrite the last set header value.

func (Response) AddTrigger

func (r Response) AddTrigger(trigger ...EventTrigger) Response

AddTrigger adds trigger(s) for events that trigger as soon as the response is received.

This can be called multiple times so you can add as many triggers as you need.

Sets the 'HX-Trigger' header.

For more info, see https://htmx.org/headers/hx-trigger/

func (Response) AddTriggerAfterSettle

func (r Response) AddTriggerAfterSettle(trigger ...EventTrigger) Response

AddTriggerAfterSettle adds trigger(s) for events that trigger after the settling step.

This can be called multiple times so you can add as many triggers as you need.

Sets the 'HX-Trigger-After-Settle' header.

For more info, see https://htmx.org/headers/hx-trigger/

func (Response) AddTriggerAfterSwap

func (r Response) AddTriggerAfterSwap(trigger ...EventTrigger) Response

AddTriggerAfterSwap adds trigger(s) for events that trigger after the swap step.

This can be called multiple times so you can add as many triggers as you need.

Sets the 'HX-Trigger-After-Swap' header.

For more info, see https://htmx.org/headers/hx-trigger/

func (Response) Clone

func (r Response) Clone() Response

Clone returns a clone of this HTMX response writer, preventing any mutation on the original response.

func (Response) Headers

func (r Response) Headers() (map[string]string, error)

Headers returns a copied map of the headers. Any modifications to the returned headers will not affect the headers in this struct.

func (Response) Location

func (r Response) Location(path string) Response

Location allows you to do a client-side redirect that does not do a full page reload.

If you want to redirect to a specific target on the page rather than the default of document.body, you can use htmx.Response.LocationWithContext.

Sets the 'HX-Location' header.

For more info, see https://htmx.org/headers/hx-location/

func (Response) LocationWithContext

func (r Response) LocationWithContext(path string, ctx LocationContext) Response

LocationWithContext allows you to do a client-side redirect that does not do a full page reload, redirecting to a specific target on the page with the given context.

For simple redirects, you can just use htmx.Response.Location.

Sets the 'HX-Location' header.

For more info, see https://htmx.org/headers/hx-location/

func (Response) MustRenderHTML added in v0.5.0

func (r Response) MustRenderHTML(w http.ResponseWriter, html template.HTML)

MustRenderHTML renders an HTML document fragment along with the defined HTMX headers, otherwise it panics.

Under the hood this uses Response.RenderHTML.

func (Response) MustRenderTempl added in v0.5.0

func (r Response) MustRenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent)

MustRenderTempl renders a Templ component along with the defined HTMX headers, otherwise it panics.

Under the hood this uses Response.RenderTempl.

func (Response) MustWrite added in v0.5.0

func (r Response) MustWrite(w http.ResponseWriter)

MustWrite applies the defined HTMX headers to a given response writer, otherwise it panics.

Under the hood this uses Response.Write.

func (Response) PreventPushURL

func (r Response) PreventPushURL() Response

PreventPushURL prevents the browser’s history from being updated.

Sets the same header as htmx.Response.PushURL, overwriting previous set headers.

Sets the 'HX-Push-Url' header.

For more info, see https://htmx.org/headers/hx-push-url/

func (Response) PreventReplaceURL

func (r Response) PreventReplaceURL() Response

PreventReplaceURL prevents the browser’s current URL from being updated.

Sets the same header as htmx.Response.ReplaceURL, overwriting previous set headers.

Sets the 'HX-Replace-Url' header to 'false'.

For more info, see https://htmx.org/headers/hx-replace-url/

func (Response) PushURL

func (r Response) PushURL(url string) Response

PushURL pushes a new URL into the browser location history.

Sets the same header as htmx.Response.PreventPushURL, overwriting previous set headers.

Sets the 'HX-Push-Url' header.

For more info, see https://htmx.org/headers/hx-push-url/

func (Response) Redirect

func (r Response) Redirect(path string) Response

Redirect does a client-side redirect to a new location.

Sets the 'HX-Redirect' header.

func (Response) Refresh

func (r Response) Refresh(shouldRefresh bool) Response

If set to true, Refresh makes the client-side do a full refresh of the page.

Sets the 'HX-Refresh' header.

func (Response) RenderHTML added in v0.5.0

func (r Response) RenderHTML(w http.ResponseWriter, html template.HTML) (int, error)

RenderHTML renders an HTML document fragment along with the defined HTMX headers.

func (Response) RenderTempl

func (r Response) RenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent) error

RenderTempl renders a Templ component along with the defined HTMX headers.

func (Response) ReplaceURL

func (r Response) ReplaceURL(url string) Response

ReplaceURL replaces the current URL in the browser location history.

Sets the same header as htmx.Response.PreventReplaceURL, overwriting previous set headers.

Sets the 'HX-Replace-Url' header.

For more info, see https://htmx.org/headers/hx-replace-url/

func (Response) Reselect

func (r Response) Reselect(cssSelector string) Response

Reselect accepts a CSS selector that allows you to choose which part of the response is used to be swapped in. Overrides an existing hx-select on the triggering element.

Sets the 'HX-Reselect' header.

For more info, see https://htmx.org/attributes/hx-select/

func (Response) Reswap

func (r Response) Reswap(s SwapStrategy) Response

Reswap allows you to specify how the response will be swapped.

Sets the 'HX-Reswap' header.

For more info, see https://htmx.org/attributes/hx-swap/

func (Response) Retarget

func (r Response) Retarget(cssSelector string) Response

Retarget accepts a CSS selector that updates the target of the content update to a different element on the page. Overrides an existing 'hx-select' on the triggering element.

Sets the 'HX-Retarget' header.

For more info, see https://htmx.org/attributes/hx-target/

func (Response) StatusCode

func (r Response) StatusCode(statusCode int) Response

StatusCode sets the HTTP response header of this response.

If StatusCode is not called, the default status code will be 200 OK.

func (Response) Write

func (r Response) Write(w http.ResponseWriter) error

Write applies the defined HTMX headers to a given response writer.

type SwapStrategy added in v0.4.0

type SwapStrategy string

SwapStrategy is an 'hx-swap' value that determines the swapping strategy of htmx.Response.Reswap and LocationContext.

SwapStrategy methods add modifiers to change the behavior of the swap.

const (
	// Replace the inner html of the target element.
	//
	// Valid value for [Response.Reswap].
	SwapInnerHTML SwapStrategy = "innerHTML"

	// Replace the entire target element with the response.
	//
	// Valid value for [Response.Reswap].
	SwapOuterHTML SwapStrategy = "outerHTML"

	// Insert the response before the target element.
	//
	// Valid value for [Response.Reswap].
	SwapBeforeBegin SwapStrategy = "beforebegin"

	// Insert the response before the first child of the target element.
	//
	// Valid value for [Response.Reswap].
	SwapAfterBegin SwapStrategy = "afterbegin"

	// Insert the response after the last child of the target element.
	//
	// Valid value for [Response.Reswap].
	SwapBeforeEnd SwapStrategy = "beforeend"

	// Insert the response after the target element.
	//
	// Valid value for [Response.Reswap].
	SwapAfterEnd SwapStrategy = "afterend"

	// Deletes the target element regardless of the response.
	//
	// Valid value for [Response.Reswap].
	SwapDelete SwapStrategy = "delete"

	// Does not append content from response (out of band items will still be processed).
	//
	// Valid value for [Response.Reswap].
	SwapNone SwapStrategy = "none"

	// Uses the default swap style (default in HTMX is [SwapInnerHTML]).
	// This value is useful for adding modifiers to the [SwapStrategy]
	// through methods
	// without changing the default swap style.
	//
	//
	// Valid value for [Response.Reswap].
	SwapDefault SwapStrategy = ""
)

func (SwapStrategy) After added in v0.4.0

func (s SwapStrategy) After(duration time.Duration) SwapStrategy

After modifies the amount of time that HTMX will wait after receiving a response to swap the content.

Adds the 'swap:<duration>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) FocusScroll added in v0.4.0

func (s SwapStrategy) FocusScroll(shouldFocus bool) SwapStrategy

FocusScroll enables focus scroll to automatically scroll to the focused element after a request.

Adds the 'focusScroll:<true | false>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) IgnoreTitle added in v0.4.0

func (s SwapStrategy) IgnoreTitle(shouldIgnore bool) SwapStrategy

IgnoreTitle prevents HTMX from updating the title of the page if there is a '<title>' tag in the response content.

By default, HTMX updates the title.

Adds the 'ignoreTitle:<true | false>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) Scroll added in v0.4.0

func (s SwapStrategy) Scroll(direction Direction) SwapStrategy

Scroll changes the scrolling behavior based on the given direction.

Scroll(htmx.Top) scrolls to the top of the swapped-in element.

Scroll(htmx.Bottom) scrolls to the bottom of the swapped-in element.

Adds the 'scroll:<direction ("top" | "bottom")>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) ScrollOn added in v0.4.0

func (s SwapStrategy) ScrollOn(cssSelector string, direction Direction) SwapStrategy

ScrollOn changes the scrolling behavior based on the given direction and CSS selector.

ScrollOn(cssSelector, htmx.Top) scrolls to the top of the element found by the selector.

ScrollOn(cssSelector, htmx.Bottom) scrolls to the bottom of the element found by the selector.

Adds the 'scroll:<cssSelector>:<direction ("top" | "bottom")>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) ScrollWindow added in v0.4.0

func (s SwapStrategy) ScrollWindow(direction Direction) SwapStrategy

ScrollWindow changes the scrolling behavior based on the given direction.

ScrollWindow(htmx.Top) scrolls to the very top of the window.

ScrollWindow(htmx.Bottom) scrolls to the very bottom of the window.

Adds the 'scroll:window:<direction ("top" | "bottom")>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) SettleAfter added in v0.4.0

func (s SwapStrategy) SettleAfter(duration time.Duration) SwapStrategy

SettleAfter modifies the amount of time that HTMX will wait after the swap before executing the settle logic.

Adds the 'settle:<duration>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) Show added in v0.4.0

func (s SwapStrategy) Show(direction Direction) SwapStrategy

Show changes the show behavior based on the given direction.

Show(htmx.Top) shows the top of the swapped-in element.

Show(htmx.Bottom) shows the bottom of the swapped-in element.

Adds the 'show:<direction ("top" | "bottom")>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) ShowNone added in v0.4.0

func (s SwapStrategy) ShowNone() SwapStrategy

ShowNone disables 'show'.

Adds the 'show:none' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) ShowOn added in v0.4.0

func (s SwapStrategy) ShowOn(cssSelector string, direction Direction) SwapStrategy

ShowOn changes the show behavior based on the given direction and CSS selector.

ShowOn(cssSelector, htmx.Top) shows the top of the element found by the selector.

ShowOn(cssSelector, htmx.Bottom) shows the bottom of the element found by the selector.

Adds the 'show:<cssSelector>:<direction ("top" | "bottom")>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) ShowWindow added in v0.4.0

func (s SwapStrategy) ShowWindow(direction Direction) SwapStrategy

ShowWindow changes the show behavior based on the given direction.

ScrollWindow(htmx.Top) shows the very top of the window.

ScrollWindow(htmx.Bottom) shows the very bottom of the window.

Adds the 'show:window:<direction ("top" | "bottom")>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

func (SwapStrategy) Transition added in v0.4.0

func (s SwapStrategy) Transition(shouldTransition bool) SwapStrategy

Transition makes the swap use the new View Transitions API.

Adds the 'transition:<true | false>' modifier.

For more info, see https://htmx.org/attributes/hx-swap/

Directories

Path Synopsis
Just some basic example usage of the library
Just some basic example usage of the library

Jump to

Keyboard shortcuts

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