connector

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package connector tells go-partial how a client library makes its requests and reads its responses: htmx 2 (NewHTMX), htmx 4 (NewHTMX4), Unpoly (NewUnpoly), Turbo (NewTurbo), or plain fetch and tests (NewPartial). A connector reads the target, selection and action a request asks for, and writes response instructions, interaction and link attributes, out-of-band markers and redirects in the library's terms.

Index

Constants

View Source
const (
	InteractionAsync    InteractionKind = "async"
	InteractionReveal   InteractionKind = "reveal"
	InteractionPoll     InteractionKind = "poll"
	InteractionStream   InteractionKind = "stream"
	InteractionPrefetch InteractionKind = "prefetch"
	InteractionRefresh  InteractionKind = "refresh"
	InteractionOn       InteractionKind = "on"
	// InteractionSubmit submits a form and swaps the response into the
	// target, such as htmx's hx-post. Options["method"] picks another
	// method, such as "put" or "delete", where the connector can.
	InteractionSubmit InteractionKind = "submit"
	// InteractionLive requests the URL as the user types, such as a live
	// search: Options["delay"] waits for a pause in typing, 300ms by
	// default.
	InteractionLive InteractionKind = "live"

	OptionInclude    = "include"
	OptionPushURL    = "pushURL"
	OptionReplaceURL = "replaceURL"
	OptionMethod     = "method"
	OptionDelay      = "delay"

	HeaderTarget HeaderKey = "X-Target"
	HeaderSelect HeaderKey = "X-Select"
	HeaderAction HeaderKey = "X-Action"

	HeaderLocation           HeaderKey = "X-Location"
	HeaderPushURL            HeaderKey = "X-Push-Url"
	HeaderRedirect           HeaderKey = "X-Redirect"
	HeaderRefresh            HeaderKey = "X-Refresh"
	HeaderReplaceURL         HeaderKey = "X-Replace-Url"
	HeaderReswap             HeaderKey = "X-Reswap"
	HeaderRetarget           HeaderKey = "X-Retarget"
	HeaderReselect           HeaderKey = "X-Reselect"
	HeaderTrigger            HeaderKey = "X-Trigger"
	HeaderTriggerAfterSettle HeaderKey = "X-Trigger-After-Settle"
	HeaderTriggerAfterSwap   HeaderKey = "X-Trigger-After-Swap"
)
View Source
const (
	HTMXHeaderBoosted               HeaderKey = "HX-Boosted"
	HTMXHeaderCurrentURL            HeaderKey = "HX-Current-URL"
	HTMXHeaderHistoryRestoreRequest HeaderKey = "HX-History-Restore-Request"
	// HTMXHeaderPrompt is sent by HTMX 2 core; HTMX 4 requires hx-prompt.
	HTMXHeaderPrompt  HeaderKey = "HX-Prompt"
	HTMXHeaderRequest HeaderKey = "HX-Request"
	HTMXHeaderTarget  HeaderKey = "HX-Target"
	// HTMX 2 request keys. HTMX 4 replaces both with HTMXHeaderSource.
	HTMXHeaderTriggerName    HeaderKey = "HX-Trigger-Name"
	HTMXHeaderTriggerRequest HeaderKey = "HX-Trigger"

	HTMXHeaderLocation   HeaderKey = "HX-Location"
	HTMXHeaderPushURL    HeaderKey = "HX-Push-Url"
	HTMXHeaderRedirect   HeaderKey = "HX-Redirect"
	HTMXHeaderRefresh    HeaderKey = "HX-Refresh"
	HTMXHeaderReplaceURL HeaderKey = "HX-Replace-Url"
	HTMXHeaderReswap     HeaderKey = "HX-Reswap"
	HTMXHeaderRetarget   HeaderKey = "HX-Retarget"
	HTMXHeaderReselect   HeaderKey = "HX-Reselect"
	HTMXHeaderTrigger    HeaderKey = "HX-Trigger"
	// Timing response headers are HTMX 2-only; NewHTMX4 omits them.
	HTMXHeaderTriggerAfterSettle HeaderKey = "HX-Trigger-After-Settle"
	HTMXHeaderTriggerAfterSwap   HeaderKey = "HX-Trigger-After-Swap"

	HTMXAttrGet = "hx-get"
	// HTMXAttrExt is HTMX 2-only. HTMX 4 loads extension scripts directly.
	HTMXAttrExt     = "hx-ext"
	HTMXAttrTrigger = "hx-trigger"
	HTMXAttrTarget  = "hx-target"
	HTMXAttrSwap    = "hx-swap"
	HTMXAttrSelect  = "hx-select"
	// HTMX 2 SSE extension attributes. HTMX 4 uses HTMX4AttrSSEConnect.
	HTMXAttrSSEConnect = "sse-connect"
	HTMXAttrSSESwap    = "sse-swap"
)
View Source
const (
	SwapModifierSwap        = "swap"
	SwapModifierSettle      = "settle"
	SwapModifierTransition  = "transition"
	SwapModifierIgnoreTitle = "ignoreTitle"
	SwapModifierFocusScroll = "focus-scroll"
	SwapModifierScroll      = "scroll"
	SwapModifierShow        = "show"
)
View Source
const (
	TurboHeaderTarget HeaderKey = "Turbo-Frame"
	TurboHeaderSelect HeaderKey = "Turbo-Select"
	TurboHeaderAction HeaderKey = "Turbo-Action"

	TurboAttrFrameID = "id"
	TurboAttrSource  = "src"
	TurboAttrLoading = "loading"
)
View Source
const (
	UnpolyHeaderTarget HeaderKey = "X-Up-Target"
	UnpolyHeaderSelect HeaderKey = "X-Up-Select"
	UnpolyHeaderAction HeaderKey = "X-Up-Action"

	UnpolyAttrGet     = "up-get"
	UnpolyAttrTrigger = "up-trigger"
	UnpolyAttrTarget  = "up-target"
)
View Source
const ReloadOnErrorAttr = "data-partial-reload-on-error"

ReloadOnErrorAttr is the attribute of a link or form, or of an element around them, that makes exp/interactions' browser client load it as a whole page when its request fails; see Attr.ReloadOnError.

View Source
const SubmitConfirmAttr = "data-partial-confirm"

SubmitConfirmAttr is the attribute of a submit button whose question exp/interactions' browser client asks before the button sends its form; see Attr.SubmitConfirm.

Variables

This section is empty.

Functions

func Apply added in v0.15.0

func Apply(c Connector, headers http.Header, response Response)

Apply sets the response headers of response in the connector's syntax on headers, such as HX-Push-Url: false for htmx, in one call:

connector.Apply(conn, w.Header(), connector.Response{PushURL: "false"})

A ResponseBuilder gives its Response with Value. A nil connector sets nothing.

func ApplyHeaders added in v0.9.0

func ApplyHeaders(headers http.Header, values map[string]string)

func Attrs added in v0.15.0

func Attrs(c Connector, attr Attr) map[string]string

Attrs returns the attributes of attr in the connector's client library, such as hx-confirm and hx-boost for htmx, up-confirm for Unpoly and data-turbo-confirm for Turbo, for a link or form of the template's own. Connectors that do not implement AttrWriter get data-confirm, data-boost, data-target and data-swap.

func CurrentURL added in v0.15.0

func CurrentURL(c Connector, r *http.Request) *url.URL

CurrentURL returns the URL of the page the client shows when it made the request, such as htmx's HX-Current-URL, or nil when the request does not tell. For a form posted with htmx, it is the page with the form.

func CurrentURLHeader added in v0.15.0

func CurrentURLHeader(c Connector) string

CurrentURLHeader names the request header in which the connector's client library sends the URL of the page it shows (see CurrentURL), such as htmx's HX-Current-URL, for the Vary header of a response that depends on it; "" when the library sends none, or for a nil connector.

partial.OOBElements{IDs: ids, Vary: []string{connector.CurrentURLHeader(conn)}}

func FormatDuration added in v0.16.0

func FormatDuration(duration time.Duration) string

FormatDuration writes duration as htmx parses it, such as for a swap delay or a polling interval: in seconds from one second up, in milliseconds below, with decimals only where needed. Go's own format cannot be used, as htmx does not read "1m0s" or "500µs".

func IsBoosted added in v0.15.0

func IsBoosted(c Connector, r *http.Request) bool

IsBoosted reports whether the request follows a boosted link or form; false for connectors without boosting.

func IsRequest added in v0.15.0

func IsRequest(c Connector, r *http.Request) bool

IsRequest reports whether the connector's client library made the request. Connectors that do not implement RequestInspector answer whether the request asks for a partial.

func MergeAttrs added in v0.15.0

func MergeAttrs(c Connector, existing map[string]string, attr Attr, remove ...AttrKind) map[string]string

MergeAttrs returns a copy of existing, such as the attributes of a form a library built, with the attributes of attr in the connector's syntax: for every setting attr has, and every kind in remove, the attributes the connector writes for that setting are taken out first, so attr's replace them. existing is not changed.

// Ask another question, or none when question is empty.
attrs = connector.MergeAttrs(c, attrs, connector.Attr{Confirm: question}, connector.AttrConfirm)

func OOBAttrs added in v0.15.0

func OOBAttrs(c Connector, swap string) map[string]string

OOBAttrs returns the attributes that make the client library swap an element of a response out of band, into the element of the page with the same ID, with swap, such as "beforeend" to add to it ("true" or "" for the library's default, which replaces it): hx-swap-oob for htmx, up-hungry for Unpoly, whose hungry elements are replaced whatever the swap. Turbo, which has no such attribute, and connectors that do not implement OOBWriter get hx-swap-oob.

func Redirect added in v0.15.0

func Redirect(c Connector, w http.ResponseWriter, r *http.Request, location Location)

Redirect answers r by sending the client to location, such as after a form is posted: for a request the connector's client library made, the way the library follows it, such as htmx's HX-Location with status 200, so the client swaps location.Path into location.Target without loading a whole page; otherwise with a 303 See Other to location.Path, which browsers, Turbo and Unpoly follow with a GET.

connector.Redirect(conn, w, r, connector.Location{Path: "/orders", Target: "#content"})

func SetRequestTarget added in v0.15.0

func SetRequestTarget(c Connector, r *http.Request, target string)

SetRequestTarget makes r ask for the partial target, as the connector's client library would, so GetTargetValue(r) returns target: for tests and for requests a server makes to itself. Connectors that do not implement RequestTargeter get target in their target header.

req := httptest.NewRequest(http.MethodGet, "/", nil)
connector.SetRequestTarget(conn, req, "comments")

func VaryHeaders added in v0.15.0

func VaryHeaders(c Connector) []string

VaryHeaders returns the request headers that change the connector's responses, for the Vary response header, so caches keep a page and a fragment of it apart. partial.Write adds them.

Types

type Attr added in v0.15.0

type Attr struct {
	// Confirm is a question the client asks before it follows the link
	// or submits the form, such as htmx's hx-confirm.
	Confirm string
	// Boost turns the library's handling of the element on or off,
	// such as htmx's hx-boost="false" for a link that must load a
	// whole page.
	Boost Boost
	// Target is the selector of the element the response is swapped
	// into; TargetSelf for the element itself.
	Target string
	// Swap is how the response is swapped in, such as outerHTML, for
	// connectors that can choose it.
	Swap string
	// Inherit gives the settings to the element's descendants, for
	// settings on the body or a container, such as boosting every link
	// into #content: htmx 4 writes its attributes with :inherited,
	// and a boost with a target or swap as hx-boost:inherited="target:#content
	// swap:outerHTML", which applies them to boosted links and forms
	// only; htmx 2 and the other libraries inherit them as they are.
	Inherit bool
	// DisableWhileRequest is the selector of the elements disabled
	// while a request of the element is under way, such as
	// "find button[type='submit']": htmx 4's hx-disable, htmx 2's
	// hx-disabled-elt, Unpoly's up-disable. Turbo disables the
	// submit button of a form itself.
	DisableWhileRequest string
	// KeepOnServerError leaves the page as it is when a response has a
	// 5xx status, such as a proxy's error page, without changing the
	// address: htmx 4's hx-status:5xx="swap:none push:false". Out-of-
	// band parts of the response, such as an error flash message, are
	// still swapped. htmx 2 swaps no error response by default.
	KeepOnServerError bool
	// SubmitConfirm is a question asked before a submit button sends
	// its form, for a form whose buttons do different things, such as
	// "Restore" and "Delete for good": data-turbo-confirm with Turbo,
	// which reads it from the button; data-partial-confirm otherwise,
	// which exp/interactions' browser client asks.
	SubmitConfirm string
	// ReloadOnError makes a link or form whose request gets a 5xx
	// status, or no answer at all, load as a whole page instead, so the
	// visitor sees what went wrong rather than nothing: a link is
	// followed and a GET form submitted again by the browser itself; a
	// POST, PUT, PATCH or DELETE is never sent twice, the server's HTML
	// error page replacing the page instead, if it sent one. On
	// a body or container it applies to the links and forms in it. It
	// is data-partial-reload-on-error with every connector, which
	// exp/interactions' browser client reads.
	ReloadOnError bool
}

Attr describes how the client library treats a link or form, in terms that do not depend on the library; see Attrs.

type AttrKind added in v0.15.0

type AttrKind int

AttrKind names a setting of Attr, for MergeAttrs.

const (
	// AttrConfirm is Attr.Confirm.
	AttrConfirm AttrKind = iota + 1
	// AttrBoost is Attr.Boost.
	AttrBoost
	// AttrTarget is Attr.Target.
	AttrTarget
	// AttrSwap is Attr.Swap.
	AttrSwap
	// AttrDisableWhileRequest is Attr.DisableWhileRequest.
	AttrDisableWhileRequest
	// AttrKeepOnServerError is Attr.KeepOnServerError.
	AttrKeepOnServerError
	// AttrSubmitConfirm is Attr.SubmitConfirm.
	AttrSubmitConfirm
	// AttrReloadOnError is Attr.ReloadOnError.
	AttrReloadOnError
)

type AttrWriter added in v0.15.0

type AttrWriter interface {
	Attrs(attr Attr) map[string]string
}

AttrWriter is implemented by connectors that write Attr as their client library's attributes; see Attrs.

type Boost added in v0.15.0

type Boost int

Boost says whether the client library takes over the links and forms of an element, such as htmx's hx-boost; see Attr.

const (
	// BoostDefault leaves the element as the library treats it.
	BoostDefault Boost = iota
	// BoostOn makes the library take over the element.
	BoostOn
	// BoostOff makes the browser load the link or submit the form itself.
	BoostOff
)

type Config

type Config struct {
	UseURLQuery bool
}

type Connector

type Connector interface {
	RenderPartial(r *http.Request) bool
	GetTargetValue(r *http.Request) string
	GetSelectValue(r *http.Request) string
	GetActionValue(r *http.Request) string

	GetTargetHeader() string
	GetSelectHeader() string
	GetActionHeader() string
	InteractionAttrs(interaction Interaction) map[string]string
	ResponseHeaders(response Response) map[string]string
}

func NewHTMX

func NewHTMX(c *Config) Connector

func NewHTMX4 added in v0.12.0

func NewHTMX4(c *Config) Connector

func NewPartial

func NewPartial(c *Config) Connector

func NewTurbo

func NewTurbo(c *Config) Connector

func NewUnpoly

func NewUnpoly(c *Config) Connector

type CurrentURLHeaderNamer added in v0.15.0

type CurrentURLHeaderNamer interface {
	CurrentURLHeader() string
}

CurrentURLHeaderNamer is implemented by connectors whose client library sends the URL of the page it shows in a request header; see CurrentURLHeader.

type HTMX

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

func (*HTMX) Attrs added in v0.15.0

func (h *HTMX) Attrs(attr Attr) map[string]string

Attrs writes attr as hx-confirm, hx-boost, hx-target, hx-swap and hx-disabled-elt; htmx 2 inherits them as they are and keeps the page on error responses by default.

func (*HTMX) CurrentURL added in v0.15.0

func (h *HTMX) CurrentURL(r *http.Request) *url.URL

CurrentURL returns the URL htmx sends as HX-Current-URL, or nil.

func (*HTMX) CurrentURLHeader added in v0.15.0

func (h *HTMX) CurrentURLHeader() string

CurrentURLHeader names HX-Current-URL, in which htmx sends the URL of the page it shows.

func (*HTMX) GetActionHeader

func (x *HTMX) GetActionHeader() string

func (*HTMX) GetActionValue

func (x *HTMX) GetActionValue(r *http.Request) string

func (*HTMX) GetSelectHeader

func (x *HTMX) GetSelectHeader() string

func (*HTMX) GetSelectValue

func (x *HTMX) GetSelectValue(r *http.Request) string

func (*HTMX) GetTargetHeader

func (x *HTMX) GetTargetHeader() string

func (*HTMX) GetTargetValue

func (x *HTMX) GetTargetValue(r *http.Request) string

func (*HTMX) InteractionAttrs added in v0.9.0

func (h *HTMX) InteractionAttrs(interaction Interaction) map[string]string

func (*HTMX) IsBoosted added in v0.15.0

func (h *HTMX) IsBoosted(r *http.Request) bool

IsBoosted reports whether htmx made the request for a boosted link or form.

func (*HTMX) IsRequest added in v0.15.0

func (h *HTMX) IsRequest(r *http.Request) bool

IsRequest reports whether htmx made the request.

func (*HTMX) OOBAttrs added in v0.15.0

func (h *HTMX) OOBAttrs(swap string) map[string]string

OOBAttrs writes hx-swap-oob; see the package function OOBAttrs.

func (*HTMX) Redirect added in v0.15.0

func (h *HTMX) Redirect(w http.ResponseWriter, r *http.Request, location Location) bool

Redirect sends a request htmx made to location with HX-Location; see the package function Redirect.

func (*HTMX) RenderPartial

func (h *HTMX) RenderPartial(r *http.Request) bool

func (*HTMX) ResponseHeaders added in v0.9.0

func (h *HTMX) ResponseHeaders(response Response) map[string]string

func (*HTMX) SetRequestTarget added in v0.15.0

func (h *HTMX) SetRequestTarget(r *http.Request, target string)

SetRequestTarget marks r as an htmx request for target.

func (*HTMX) VaryHeaders added in v0.15.0

func (h *HTMX) VaryHeaders() []string

VaryHeaders returns the htmx request headers that decide what is rendered, and the select and action headers.

type HTMX4 added in v0.12.0

type HTMX4 struct{ *HTMX }

HTMX4 implements the htmx 4 protocol. NewHTMX remains the htmx 2 connector.

func (*HTMX4) Attrs added in v0.15.0

func (h *HTMX4) Attrs(attr Attr) map[string]string

Attrs writes attr as hx-confirm, hx-boost, hx-target, hx-swap, hx-disable and hx-status:5xx, with :inherited for Attr.Inherit; an inherited boost carries its target and swap, so they apply to boosted links and forms only, quoted when they hold whitespace or a comma, such as hx-boost:inherited="target:#content swap:'outerHTML showTarget:body'".

func (HTMX4) GetActionHeader added in v0.12.0

func (x HTMX4) GetActionHeader() string

func (HTMX4) GetActionValue added in v0.12.0

func (x HTMX4) GetActionValue(r *http.Request) string

func (HTMX4) GetSelectHeader added in v0.12.0

func (x HTMX4) GetSelectHeader() string

func (HTMX4) GetSelectValue added in v0.12.0

func (x HTMX4) GetSelectValue(r *http.Request) string

func (HTMX4) GetTargetHeader added in v0.12.0

func (x HTMX4) GetTargetHeader() string

func (*HTMX4) GetTargetValue added in v0.12.0

func (h *HTMX4) GetTargetValue(r *http.Request) string

func (*HTMX4) InteractionAttrs added in v0.12.0

func (h *HTMX4) InteractionAttrs(interaction Interaction) map[string]string

func (*HTMX4) RenderPartial added in v0.12.0

func (h *HTMX4) RenderPartial(r *http.Request) bool

func (*HTMX4) ResponseHeaders added in v0.12.0

func (h *HTMX4) ResponseHeaders(response Response) map[string]string

ResponseHeaders omits the two timing headers removed by htmx 4. Use Trigger for server events, or browser after:swap / after:settle listeners for timing.

func (*HTMX4) SetRequestTarget added in v0.15.0

func (h *HTMX4) SetRequestTarget(r *http.Request, target string)

SetRequestTarget marks r as an htmx 4 partial request for target, which htmx 4 sends as tag#id.

func (*HTMX4) VaryHeaders added in v0.15.0

func (h *HTMX4) VaryHeaders() []string

VaryHeaders adds HX-Request-Type, with which htmx 4 asks for a whole page, to the headers of htmx 2.

type HeaderKey added in v0.9.0

type HeaderKey string
const (
	HTMXHeaderRequestType HeaderKey = "HX-Request-Type"
	HTMXHeaderSource      HeaderKey = "HX-Source"
	HTMX4AttrSSEConnect             = "hx-sse:connect"
)
const (
	// TargetSelf targets the element an interaction is placed on, for
	// interaction attributes on an existing element. Connectors write it in
	// their own syntax.
	TargetSelf = "this"

	// UnpolyHeaderVersion is sent by Unpoly with every request it makes.
	UnpolyHeaderVersion HeaderKey = "X-Up-Version"
)

func (HeaderKey) String added in v0.9.0

func (h HeaderKey) String() string

type HeaderVarier added in v0.15.0

type HeaderVarier interface {
	VaryHeaders() []string
}

HeaderVarier is implemented by connectors that know which request headers change the response; see VaryHeaders.

type Interaction added in v0.9.0

type Interaction struct {
	Kind        InteractionKind
	ID          string
	URL         string
	Target      string
	Swap        string
	Trigger     string
	Interval    string
	Placeholder string
	Name        string
	Params      map[string]string
	Options     map[string]string
	// Select picks the part of the response to swap, such as htmx's
	// hx-select, for connectors that can.
	Select string
	// Attrs are attributes written as given, such as hx-indicator or
	// hx-push-url, after the connector's own: they replace those of the
	// same name.
	Attrs map[string]string
}

type InteractionKind added in v0.9.0

type InteractionKind string

type Location added in v0.15.0

type Location struct {
	Path   string `json:"path"`
	Target string `json:"target,omitempty"`
	Swap   string `json:"swap,omitempty"`
	Select string `json:"select,omitempty"`
	// Source and Event name the element and event the navigation is
	// treated as coming from.
	Source  string            `json:"source,omitempty"`
	Event   string            `json:"event,omitempty"`
	Values  map[string]any    `json:"values,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
}

Location is a client-side navigation with options, such as htmx's HX-Location object: the client fetches Path and swaps it into Target, without a full page load.

func (Location) String added in v0.15.0

func (l Location) String() string

String returns the Location as its header value: the path alone when that is all it has, otherwise a JSON object.

type OOBWriter added in v0.15.0

type OOBWriter interface {
	OOBAttrs(swap string) map[string]string
}

OOBWriter is implemented by connectors whose client library swaps an element of a response out of band, outside the target, by an attribute of the element; see OOBAttrs.

type Partial

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

func (*Partial) Attrs added in v0.15.0

func (x *Partial) Attrs(attr Attr) map[string]string

Attrs writes attr as data-confirm, data-boost, data-target, data-swap, data-disable, data-keep-on-server-error, data-partial-confirm and data-partial-reload-on-error.

func (*Partial) CurrentURL added in v0.15.0

func (x *Partial) CurrentURL(*http.Request) *url.URL

CurrentURL is nil: the protocol does not send it.

func (*Partial) GetActionHeader

func (x *Partial) GetActionHeader() string

func (*Partial) GetActionValue

func (x *Partial) GetActionValue(r *http.Request) string

func (*Partial) GetSelectHeader

func (x *Partial) GetSelectHeader() string

func (*Partial) GetSelectValue

func (x *Partial) GetSelectValue(r *http.Request) string

func (*Partial) GetTargetHeader

func (x *Partial) GetTargetHeader() string

func (*Partial) GetTargetValue

func (x *Partial) GetTargetValue(r *http.Request) string

func (*Partial) InteractionAttrs added in v0.9.0

func (x *Partial) InteractionAttrs(interaction Interaction) map[string]string

func (*Partial) IsBoosted added in v0.15.0

func (x *Partial) IsBoosted(*http.Request) bool

IsBoosted is false: the connector has no boosting.

func (*Partial) IsRequest added in v0.15.0

func (x *Partial) IsRequest(r *http.Request) bool

IsRequest reports whether the request names a target.

func (*Partial) RenderPartial

func (x *Partial) RenderPartial(r *http.Request) bool

func (*Partial) ResponseHeaders added in v0.9.0

func (x *Partial) ResponseHeaders(response Response) map[string]string

func (*Partial) SetRequestTarget added in v0.15.0

func (x *Partial) SetRequestTarget(r *http.Request, target string)

SetRequestTarget sets the connector's target header to target.

func (*Partial) VaryHeaders added in v0.15.0

func (x *Partial) VaryHeaders() []string

VaryHeaders returns the target, select and action headers.

type Redirector added in v0.15.0

type Redirector interface {
	// Redirect answers r by sending the client to location, and reports
	// whether it did; when it reports false, Redirect answers with a
	// 303 See Other.
	Redirect(w http.ResponseWriter, r *http.Request, location Location) bool
}

Redirector is implemented by connectors whose client library follows a redirect of its own, such as htmx's HX-Location; see Redirect.

type RequestInspector added in v0.15.0

type RequestInspector interface {
	// IsRequest reports whether the client library made the request,
	// also when it asks for a whole page, such as an htmx 4 request for
	// the body.
	IsRequest(r *http.Request) bool
	// IsBoosted reports whether the request follows a boosted link or
	// form.
	IsBoosted(r *http.Request) bool
	// CurrentURL returns the URL of the page the client shows, or nil
	// when the request does not tell.
	CurrentURL(r *http.Request) *url.URL
}

RequestInspector is implemented by connectors that can tell more about a request than which partial it asks for. The connectors of this package implement it; use the package functions IsRequest, IsBoosted and CurrentURL, which also accept connectors that do not.

type RequestTargeter added in v0.15.0

type RequestTargeter interface {
	SetRequestTarget(r *http.Request, target string)
}

RequestTargeter is implemented by connectors that can make a request ask for a target the way their client library does; see SetRequestTarget.

type Response added in v0.9.0

type Response struct {
	Location           string
	PushURL            string
	Redirect           string
	Refresh            *bool
	ReplaceURL         string
	Reswap             string
	Retarget           string
	Reselect           string
	Trigger            string
	TriggerAfterSettle string
	TriggerAfterSwap   string
	// contains filtered or unexported fields
}

type ResponseBuilder added in v0.9.0

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

ResponseBuilder mutates a Response through fluent methods.

It is intended for single-goroutine request/configuration use. Do not share a builder across goroutines without external synchronization.

func NewResponseBuilder added in v0.9.0

func NewResponseBuilder(response *Response) *ResponseBuilder

func (*ResponseBuilder) Location added in v0.9.0

func (b *ResponseBuilder) Location(value string) *ResponseBuilder

func (*ResponseBuilder) PushURL added in v0.9.0

func (b *ResponseBuilder) PushURL(value string) *ResponseBuilder

func (*ResponseBuilder) Redirect added in v0.9.0

func (b *ResponseBuilder) Redirect(value string) *ResponseBuilder

func (*ResponseBuilder) Refresh added in v0.9.0

func (b *ResponseBuilder) Refresh(value bool) *ResponseBuilder

func (*ResponseBuilder) ReplaceURL added in v0.9.0

func (b *ResponseBuilder) ReplaceURL(value string) *ResponseBuilder

func (*ResponseBuilder) Reselect added in v0.9.0

func (b *ResponseBuilder) Reselect(value string) *ResponseBuilder

func (*ResponseBuilder) Reswap added in v0.9.0

func (b *ResponseBuilder) Reswap(value string) *ResponseBuilder

func (*ResponseBuilder) ReswapWith added in v0.9.0

func (b *ResponseBuilder) ReswapWith(swap *Swap) *ResponseBuilder

func (*ResponseBuilder) Retarget added in v0.9.0

func (b *ResponseBuilder) Retarget(value string) *ResponseBuilder

func (*ResponseBuilder) Trigger added in v0.9.0

func (b *ResponseBuilder) Trigger(value string) *ResponseBuilder

func (*ResponseBuilder) TriggerAfterSettle added in v0.9.0

func (b *ResponseBuilder) TriggerAfterSettle(value string) *ResponseBuilder

func (*ResponseBuilder) TriggerAfterSettleWith added in v0.9.0

func (b *ResponseBuilder) TriggerAfterSettleWith(trigger *Trigger) *ResponseBuilder

func (*ResponseBuilder) TriggerAfterSwap added in v0.9.0

func (b *ResponseBuilder) TriggerAfterSwap(value string) *ResponseBuilder

func (*ResponseBuilder) TriggerAfterSwapWith added in v0.9.0

func (b *ResponseBuilder) TriggerAfterSwapWith(trigger *Trigger) *ResponseBuilder

func (*ResponseBuilder) TriggerWith added in v0.9.0

func (b *ResponseBuilder) TriggerWith(trigger *Trigger) *ResponseBuilder

func (*ResponseBuilder) Value added in v0.9.0

func (b *ResponseBuilder) Value() Response

type Swap added in v0.9.0

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

Swap builds an HTMX-compatible swap header value.

Swap is mutable and is not safe for concurrent mutation.

func NewSwap added in v0.9.0

func NewSwap() *Swap

func (*Swap) FocusScroll added in v0.9.0

func (s *Swap) FocusScroll(enabled bool) *Swap

func (*Swap) IgnoreTitle added in v0.9.0

func (s *Swap) IgnoreTitle(enabled bool) *Swap

func (*Swap) Scroll added in v0.9.0

func (s *Swap) Scroll(target, direction string) *Swap

func (*Swap) Settle added in v0.9.0

func (s *Swap) Settle(duration time.Duration) *Swap

func (*Swap) Show added in v0.9.0

func (s *Swap) Show(target, direction string) *Swap

func (*Swap) String added in v0.9.0

func (s *Swap) String() string

func (*Swap) Style added in v0.9.0

func (s *Swap) Style(style SwapStyle) *Swap

func (*Swap) Swap added in v0.9.0

func (s *Swap) Swap(duration time.Duration) *Swap

func (*Swap) Transition added in v0.9.0

func (s *Swap) Transition(enabled bool) *Swap

type SwapStyle added in v0.9.0

type SwapStyle string
const (
	SwapInnerHTML   SwapStyle = "innerHTML"
	SwapOuterHTML   SwapStyle = "outerHTML"
	SwapBeforeBegin SwapStyle = "beforebegin"
	SwapAfterBegin  SwapStyle = "afterbegin"
	SwapBeforeEnd   SwapStyle = "beforeend"
	SwapAfterEnd    SwapStyle = "afterend"
	SwapDelete      SwapStyle = "delete"
	SwapNone        SwapStyle = "none"
	// Additional built-in HTMX 4 styles. Morph/sync require an extension in HTMX 2.
	SwapInnerMorph  SwapStyle = "innerMorph"
	SwapOuterMorph  SwapStyle = "outerMorph"
	SwapOuterSync   SwapStyle = "outerSync"
	SwapTextContent SwapStyle = "textContent"
)

type Trigger added in v0.9.0

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

Trigger builds an HTMX-compatible trigger header value.

Trigger is mutable and is not safe for concurrent mutation.

func NewTrigger added in v0.9.0

func NewTrigger() *Trigger

func (*Trigger) AddEvent added in v0.9.0

func (t *Trigger) AddEvent(event string) *Trigger

func (*Trigger) AddEventDetailed added in v0.9.0

func (t *Trigger) AddEventDetailed(event, message string) *Trigger

func (*Trigger) AddEventObject added in v0.9.0

func (t *Trigger) AddEventObject(event string, details map[string]any) *Trigger

func (*Trigger) String added in v0.9.0

func (t *Trigger) String() string

type Turbo

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

func (*Turbo) Attrs added in v0.15.0

func (t *Turbo) Attrs(attr Attr) map[string]string

Attrs writes attr as data-turbo-confirm, also for a submit button, data-turbo and, for a target, data-turbo-frame with the frame's ID; Turbo has no swap of its own choosing.

func (*Turbo) CurrentURL added in v0.15.0

func (x *Turbo) CurrentURL(*http.Request) *url.URL

CurrentURL is nil: the protocol does not send it.

func (*Turbo) GetActionHeader

func (x *Turbo) GetActionHeader() string

func (*Turbo) GetActionValue

func (x *Turbo) GetActionValue(r *http.Request) string

func (*Turbo) GetSelectHeader

func (x *Turbo) GetSelectHeader() string

func (*Turbo) GetSelectValue

func (x *Turbo) GetSelectValue(r *http.Request) string

func (*Turbo) GetTargetHeader

func (x *Turbo) GetTargetHeader() string

func (*Turbo) GetTargetValue

func (x *Turbo) GetTargetValue(r *http.Request) string

func (*Turbo) InteractionAttrs added in v0.9.0

func (t *Turbo) InteractionAttrs(interaction Interaction) map[string]string

func (*Turbo) IsBoosted added in v0.15.0

func (x *Turbo) IsBoosted(*http.Request) bool

IsBoosted is false: the connector has no boosting.

func (*Turbo) IsRequest added in v0.15.0

func (x *Turbo) IsRequest(r *http.Request) bool

IsRequest reports whether the request names a target.

func (*Turbo) RenderPartial

func (x *Turbo) RenderPartial(r *http.Request) bool

func (*Turbo) ResponseHeaders added in v0.9.0

func (x *Turbo) ResponseHeaders(response Response) map[string]string

func (*Turbo) SetRequestTarget added in v0.15.0

func (x *Turbo) SetRequestTarget(r *http.Request, target string)

SetRequestTarget sets the connector's target header to target.

func (*Turbo) VaryHeaders added in v0.15.0

func (x *Turbo) VaryHeaders() []string

VaryHeaders returns the target, select and action headers.

type Unpoly

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

func (*Unpoly) Attrs added in v0.15.0

func (u *Unpoly) Attrs(attr Attr) map[string]string

Attrs writes attr as up-confirm, up-follow and up-submit, up-target and up-disable; Unpoly has no swap of its own choosing.

func (*Unpoly) CurrentURL added in v0.15.0

func (x *Unpoly) CurrentURL(*http.Request) *url.URL

CurrentURL is nil: the protocol does not send it.

func (*Unpoly) GetActionHeader

func (x *Unpoly) GetActionHeader() string

func (*Unpoly) GetActionValue

func (x *Unpoly) GetActionValue(r *http.Request) string

func (*Unpoly) GetSelectHeader

func (x *Unpoly) GetSelectHeader() string

func (*Unpoly) GetSelectValue

func (x *Unpoly) GetSelectValue(r *http.Request) string

func (*Unpoly) GetTargetHeader

func (x *Unpoly) GetTargetHeader() string

func (*Unpoly) GetTargetValue

func (x *Unpoly) GetTargetValue(r *http.Request) string

func (*Unpoly) InteractionAttrs added in v0.9.0

func (u *Unpoly) InteractionAttrs(interaction Interaction) map[string]string

func (*Unpoly) IsBoosted added in v0.15.0

func (x *Unpoly) IsBoosted(*http.Request) bool

IsBoosted is false: the connector has no boosting.

func (*Unpoly) IsRequest added in v0.15.0

func (u *Unpoly) IsRequest(r *http.Request) bool

IsRequest reports whether Unpoly made the request.

func (*Unpoly) OOBAttrs added in v0.15.0

func (u *Unpoly) OOBAttrs(string) map[string]string

OOBAttrs writes up-hungry; Unpoly replaces hungry elements whatever the swap. See the package function OOBAttrs.

func (*Unpoly) RenderPartial

func (u *Unpoly) RenderPartial(r *http.Request) bool

func (*Unpoly) ResponseHeaders added in v0.9.0

func (x *Unpoly) ResponseHeaders(response Response) map[string]string

func (*Unpoly) SetRequestTarget added in v0.15.0

func (x *Unpoly) SetRequestTarget(r *http.Request, target string)

SetRequestTarget sets the connector's target header to target.

func (*Unpoly) VaryHeaders added in v0.15.0

func (x *Unpoly) VaryHeaders() []string

VaryHeaders returns the target, select and action headers.

Jump to

Keyboard shortcuts

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