Documentation
¶
Overview ¶
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Package hypermedia provides HTML-over-the-wire page, fragment, Datastar, and SSE helpers.
Index ¶
- Constants
- func DataAction(method, route string, options ...DataActionOption) string
- func DispatchCustomEvent(c *echo.Context, eventName string, detail any, ...) error
- func ExecuteScript(c *echo.Context, scriptContents string, opts ...ExecuteScriptOption) error
- func MergeSignals(c *echo.Context, signals map[string]any) error
- func PatchComponent(etx *echo.Context, component templ.Component, opts ...PatchElementOption) error
- func PatchFragment(etx *echo.Context, component templ.Component, key any, ...) error
- func PatchFragments(etx *echo.Context, component templ.Component, keys []any, ...) error
- func PatchHTML(etx *echo.Context, html string, opts ...PatchElementOption) error
- func PatchSignal(c *echo.Context, key string, value any, opts ...PatchSignalsOption) error
- func PatchSignals(c *echo.Context, signals map[string]any, opts ...PatchSignalsOption) error
- func Prefetch(c *echo.Context, urls ...string) error
- func ReadSignals(r *http.Request, signals any) error
- func Redirect(c *echo.Context, url string, opts ...ExecuteScriptOption) error
- func Redirectf(c *echo.Context, format string, args ...any) error
- func RemoveElement(c *echo.Context, selector string, opts ...PatchElementOption) error
- func RemoveElementByID(c *echo.Context, id string, opts ...PatchElementOption) error
- func RemoveElementf(c *echo.Context, selectorFormat string, args ...any) error
- func RenderComponent(etx *echo.Context, component templ.Component, opts ...PageOption) error
- func RenderFragment(ctx context.Context, component templ.Component, key any) (string, error)
- func RenderFragments(ctx context.Context, component templ.Component, keys ...any) (string, error)
- func RenderHTML(ctx context.Context, component templ.Component) (string, error)
- func RenderPage(etx *echo.Context, component templ.Component, opts ...PageOption) error
- func ReplaceURL(c *echo.Context, u url.URL, opts ...ExecuteScriptOption) error
- func ReplaceURLQuery(c *echo.Context, r *http.Request, values url.Values, ...) error
- func Send(c *echo.Context, eventType EventType, dataLines []string, ...) error
- type Broadcaster
- func (sse *Broadcaster) ConsoleError(err error, opts ...ExecuteScriptOption) error
- func (sse *Broadcaster) ConsoleLog(msg string, opts ...ExecuteScriptOption) error
- func (sse *Broadcaster) ConsoleLogf(format string, args ...any) error
- func (sse *Broadcaster) DispatchCustomEvent(eventName string, detail any, opts ...DispatchCustomEventOption) error
- func (sse *Broadcaster) ExecuteScript(scriptContents string, opts ...ExecuteScriptOption) error
- func (sse *Broadcaster) IsClosed() bool
- func (sse *Broadcaster) MergeSignals(signals map[string]any) error
- func (sse *Broadcaster) PatchComponent(comp templ.Component, opts ...PatchElementOption) error
- func (sse *Broadcaster) PatchHTML(html string, opts ...PatchElementOption) error
- func (sse *Broadcaster) PatchSignal(key string, value any, opts ...PatchSignalsOption) error
- func (sse *Broadcaster) PatchSignals(signals map[string]any, opts ...PatchSignalsOption) error
- func (sse *Broadcaster) Prefetch(urls ...string) error
- func (sse *Broadcaster) Redirect(url string, opts ...ExecuteScriptOption) error
- func (sse *Broadcaster) Redirectf(format string, args ...any) error
- func (sse *Broadcaster) RemoveElement(selector string, opts ...PatchElementOption) error
- func (sse *Broadcaster) RemoveElementByID(id string, opts ...PatchElementOption) error
- func (sse *Broadcaster) RemoveElementf(selectorFormat string, args ...any) error
- func (sse *Broadcaster) ReplaceURL(u url.URL, opts ...ExecuteScriptOption) error
- func (sse *Broadcaster) ReplaceURLQuery(r *http.Request, values url.Values, opts ...ExecuteScriptOption) error
- func (sse *Broadcaster) Send(eventType EventType, dataLines []string, opts ...SSEEventOption) error
- type DataActionOption
- type DispatchCustomEventOption
- func WithDispatchCustomEventBubbles(bubbles bool) DispatchCustomEventOption
- func WithDispatchCustomEventCancelable(cancelable bool) DispatchCustomEventOption
- func WithDispatchCustomEventComposed(composed bool) DispatchCustomEventOption
- func WithDispatchCustomEventEventID(id string) DispatchCustomEventOption
- func WithDispatchCustomEventRetryDuration(retryDuration time.Duration) DispatchCustomEventOption
- func WithDispatchCustomEventSelector(selector string) DispatchCustomEventOption
- type ElementPatchMode
- type EventType
- type ExecuteScriptOption
- func WithExecuteScriptAttributeKVs(kvs ...string) ExecuteScriptOption
- func WithExecuteScriptAttributes(attributes ...string) ExecuteScriptOption
- func WithExecuteScriptAutoRemove(autoremove bool) ExecuteScriptOption
- func WithExecuteScriptEventID(id string) ExecuteScriptOption
- func WithExecuteScriptRetryDuration(retryDuration time.Duration) ExecuteScriptOption
- type PageOption
- type PatchElementOption
- func WithMode(merge ElementPatchMode) PatchElementOption
- func WithModeAfter() PatchElementOption
- func WithModeAppend() PatchElementOption
- func WithModeBefore() PatchElementOption
- func WithModeInner() PatchElementOption
- func WithModeOuter() PatchElementOption
- func WithModePrepend() PatchElementOption
- func WithModeRemove() PatchElementOption
- func WithModeReplace() PatchElementOption
- func WithPatchElementsEventID(id string) PatchElementOption
- func WithRetryDuration(retryDuration time.Duration) PatchElementOption
- func WithSelector(selector string) PatchElementOption
- func WithSelectorID(id string) PatchElementOption
- func WithSelectorf(selectorFormat string, args ...any) PatchElementOption
- func WithUseViewTransitions(useViewTransition bool) PatchElementOption
- func WithViewTransitions() PatchElementOption
- func WithoutViewTransitions() PatchElementOption
- type PatchSignalsOption
- type SSEEventOption
Constants ¶
const ( DatastarKey = "datastar" // The default duration for retrying SSE on connection reset. This is part of the underlying retry mechanism of SSE. DefaultSseRetryDuration = 1000 * time.Millisecond DefaultElementsUseViewTransitions = false DefaultPatchSignalsOnlyIfMissing = false SelectorDatalineLiteral = "selector " ModeDatalineLiteral = "mode " ElementsDatalineLiteral = "elements " UseViewTransitionDatalineLiteral = "useViewTransition " SignalsDatalineLiteral = "signals " OnlyIfMissingDatalineLiteral = "onlyIfMissing " )
const ( NewLine = "\n" DoubleNewLine = "\n\n" )
Variables ¶
This section is empty.
Functions ¶
func DataAction ¶
func DataAction(method, route string, options ...DataActionOption) string
DataAction builds a Datastar action expression for an HTTP method and route.
func DispatchCustomEvent ¶
func DispatchCustomEvent( c *echo.Context, eventName string, detail any, opts ...DispatchCustomEventOption, ) error
DispatchCustomEvent dispatches a browser CustomEvent through a Datastar/SSE script patch.
func ExecuteScript ¶
func ExecuteScript(c *echo.Context, scriptContents string, opts ...ExecuteScriptOption) error
ExecuteScript sends JavaScript to the browser as a Datastar/SSE script patch.
func MergeSignals ¶
MergeSignals sends signal values as a Datastar/SSE merge-signals event.
func PatchComponent ¶
PatchComponent renders a templ component and sends it as a Datastar/SSE element patch.
func PatchFragment ¶
func PatchFragment(etx *echo.Context, component templ.Component, key any, opts ...PatchElementOption) error
PatchFragment renders one named templ fragment and sends it as a Datastar/SSE element patch.
func PatchFragments ¶
func PatchFragments(etx *echo.Context, component templ.Component, keys []any, opts ...PatchElementOption) error
PatchFragments renders named templ fragments and sends them as a Datastar/SSE element patch.
func PatchHTML ¶
func PatchHTML(etx *echo.Context, html string, opts ...PatchElementOption) error
PatchHTML sends an already-rendered HTML string as a Datastar/SSE element patch.
func PatchSignal ¶
PatchSignal sends one signal value as a Datastar/SSE signal patch.
func PatchSignals ¶
PatchSignals sends signal values as a Datastar/SSE signal patch.
func ReadSignals ¶
ReadSignals reads Datastar signals from a request into signals.
func Redirect ¶
func Redirect(c *echo.Context, url string, opts ...ExecuteScriptOption) error
Redirect sends a browser redirect script as a Datastar/SSE response.
func Redirectf ¶
Redirectf formats a URL and sends a browser redirect script as a Datastar/SSE response.
func RemoveElement ¶
func RemoveElement(c *echo.Context, selector string, opts ...PatchElementOption) error
RemoveElement sends a Datastar/SSE patch that removes elements matching selector.
func RemoveElementByID ¶
func RemoveElementByID(c *echo.Context, id string, opts ...PatchElementOption) error
RemoveElementByID sends a Datastar/SSE remove patch for the element with id.
func RemoveElementf ¶
RemoveElementf formats a selector and sends a Datastar/SSE remove patch.
func RenderComponent ¶
RenderComponent is identical to RenderPage; use it when naming the templ component improves readability at the call site.
func RenderFragment ¶
RenderFragment renders one named templ fragment from a component to a string.
func RenderFragments ¶
RenderFragments renders one or more named templ fragments from a component to a string.
func RenderHTML ¶
RenderHTML renders a templ component to a string without writing an HTTP response.
func RenderPage ¶
RenderPage renders a templ component as a full HTML page response.
func ReplaceURL ¶
ReplaceURL replaces the current browser URL without reloading the page.
func ReplaceURLQuery ¶
func ReplaceURLQuery( c *echo.Context, r *http.Request, values url.Values, opts ...ExecuteScriptOption, ) error
ReplaceURLQuery replaces the current browser query string without reloading the page.
Types ¶
type Broadcaster ¶
type Broadcaster struct {
// contains filtered or unexported fields
}
func NewBroadcaster ¶
func NewBroadcaster(c *echo.Context) (*Broadcaster, error)
NewBroadcaster opens an SSE response and returns a reusable event broadcaster.
func (*Broadcaster) ConsoleError ¶
func (sse *Broadcaster) ConsoleError(err error, opts ...ExecuteScriptOption) error
ConsoleError sends a browser console.error call as a Datastar/SSE script patch.
func (*Broadcaster) ConsoleLog ¶
func (sse *Broadcaster) ConsoleLog(msg string, opts ...ExecuteScriptOption) error
ConsoleLog sends a browser console.log call as a Datastar/SSE script patch.
func (*Broadcaster) ConsoleLogf ¶
func (sse *Broadcaster) ConsoleLogf(format string, args ...any) error
ConsoleLogf formats a message and sends a browser console.log call.
func (*Broadcaster) DispatchCustomEvent ¶
func (sse *Broadcaster) DispatchCustomEvent( eventName string, detail any, opts ...DispatchCustomEventOption, ) error
DispatchCustomEvent dispatches a browser CustomEvent through a Datastar/SSE script patch.
func (*Broadcaster) ExecuteScript ¶
func (sse *Broadcaster) ExecuteScript(scriptContents string, opts ...ExecuteScriptOption) error
ExecuteScript sends JavaScript to the browser as a Datastar/SSE script patch.
func (*Broadcaster) IsClosed ¶
func (sse *Broadcaster) IsClosed() bool
IsClosed reports whether the request context backing the broadcaster is done.
func (*Broadcaster) MergeSignals ¶
func (sse *Broadcaster) MergeSignals(signals map[string]any) error
MergeSignals sends signal values as a Datastar/SSE merge-signals event.
func (*Broadcaster) PatchComponent ¶
func (sse *Broadcaster) PatchComponent(comp templ.Component, opts ...PatchElementOption) error
PatchComponent renders a templ component and sends it as a Datastar/SSE element patch.
func (*Broadcaster) PatchHTML ¶
func (sse *Broadcaster) PatchHTML(html string, opts ...PatchElementOption) error
PatchHTML sends an already-rendered HTML string as a Datastar/SSE element patch.
func (*Broadcaster) PatchSignal ¶
func (sse *Broadcaster) PatchSignal(key string, value any, opts ...PatchSignalsOption) error
PatchSignal sends one signal value as a Datastar/SSE signal patch.
func (*Broadcaster) PatchSignals ¶
func (sse *Broadcaster) PatchSignals(signals map[string]any, opts ...PatchSignalsOption) error
PatchSignals sends signal values as a Datastar/SSE signal patch.
func (*Broadcaster) Prefetch ¶
func (sse *Broadcaster) Prefetch(urls ...string) error
Prefetch sends a browser speculation-rules prefetch script for the given URLs.
func (*Broadcaster) Redirect ¶
func (sse *Broadcaster) Redirect(url string, opts ...ExecuteScriptOption) error
Redirect sends a browser redirect script as a Datastar/SSE response.
func (*Broadcaster) Redirectf ¶
func (sse *Broadcaster) Redirectf(format string, args ...any) error
Redirectf formats a URL and sends a browser redirect script.
func (*Broadcaster) RemoveElement ¶
func (sse *Broadcaster) RemoveElement(selector string, opts ...PatchElementOption) error
RemoveElement sends a Datastar/SSE patch that removes elements matching selector.
func (*Broadcaster) RemoveElementByID ¶
func (sse *Broadcaster) RemoveElementByID(id string, opts ...PatchElementOption) error
RemoveElementByID sends a Datastar/SSE remove patch for the element with id.
func (*Broadcaster) RemoveElementf ¶
func (sse *Broadcaster) RemoveElementf(selectorFormat string, args ...any) error
RemoveElementf formats a selector and sends a Datastar/SSE remove patch.
func (*Broadcaster) ReplaceURL ¶
func (sse *Broadcaster) ReplaceURL(u url.URL, opts ...ExecuteScriptOption) error
ReplaceURL replaces the current browser URL without reloading the page.
func (*Broadcaster) ReplaceURLQuery ¶
func (sse *Broadcaster) ReplaceURLQuery( r *http.Request, values url.Values, opts ...ExecuteScriptOption, ) error
ReplaceURLQuery replaces the current browser query string without reloading the page.
func (*Broadcaster) Send ¶
func (sse *Broadcaster) Send( eventType EventType, dataLines []string, opts ...SSEEventOption, ) error
Send emits a server-sent event to the client. Method is safe for concurrent use.
type DataActionOption ¶
DataActionOption configures the Datastar action expression built by DataAction.
var ActionTypeForm DataActionOption = func() (string, string) {
return "append", "form"
}
ActionTypeForm configures a Datastar action to submit form data.
func ActionHeaders ¶
func ActionHeaders(headers map[string]string) DataActionOption
ActionHeaders adds request headers to a Datastar action.
func ActionSignalsFilter ¶
func ActionSignalsFilter(signals map[string]string) DataActionOption
ActionSignalsFilter adds a signal filter to a Datastar action.
func KeepConnOpen ¶
func KeepConnOpen() DataActionOption
KeepConnOpen configures a Datastar action to keep the connection open when hidden.
type DispatchCustomEventOption ¶
type DispatchCustomEventOption func(*dispatchCustomEventOptions)
func WithDispatchCustomEventBubbles ¶
func WithDispatchCustomEventBubbles(bubbles bool) DispatchCustomEventOption
WithDispatchCustomEventBubbles controls the CustomEvent bubbles flag.
func WithDispatchCustomEventCancelable ¶
func WithDispatchCustomEventCancelable(cancelable bool) DispatchCustomEventOption
WithDispatchCustomEventCancelable controls the CustomEvent cancelable flag.
func WithDispatchCustomEventComposed ¶
func WithDispatchCustomEventComposed(composed bool) DispatchCustomEventOption
WithDispatchCustomEventComposed controls the CustomEvent composed flag.
func WithDispatchCustomEventEventID ¶
func WithDispatchCustomEventEventID(id string) DispatchCustomEventOption
WithDispatchCustomEventEventID sets the SSE event ID for a custom-event dispatch.
func WithDispatchCustomEventRetryDuration ¶
func WithDispatchCustomEventRetryDuration(retryDuration time.Duration) DispatchCustomEventOption
WithDispatchCustomEventRetryDuration sets the SSE retry duration for a custom-event dispatch.
func WithDispatchCustomEventSelector ¶
func WithDispatchCustomEventSelector(selector string) DispatchCustomEventOption
WithDispatchCustomEventSelector sets the CSS selector whose elements receive the custom event.
type ElementPatchMode ¶
type ElementPatchMode string
const ( // Default value for ElementPatchMode // Morphs the element into the existing element. DefaultElementPatchMode = ElementPatchModeOuter // Morphs the element into the existing element. ElementPatchModeOuter ElementPatchMode = "outer" // Replaces the inner HTML of the existing element. ElementPatchModeInner ElementPatchMode = "inner" // Removes the existing element. ElementPatchModeRemove ElementPatchMode = "remove" // Replaces the existing element with the new element. ElementPatchModeReplace ElementPatchMode = "replace" // Prepends the element inside to the existing element. ElementPatchModePrepend ElementPatchMode = "prepend" // Appends the element inside the existing element. ElementPatchModeAppend ElementPatchMode = "append" // Inserts the element before the existing element. ElementPatchModeBefore ElementPatchMode = "before" // Inserts the element after the existing element. ElementPatchModeAfter ElementPatchMode = "after" )
type EventType ¶
type EventType string
const ( // An event for patching HTML elements into the DOM. EventTypePatchElements EventType = "datastar-patch-elements" // An event for patching signals. EventTypePatchSignals EventType = "datastar-patch-signals" // An event for executing scripts. EventTypeExecuteScript EventType = "datastar-execute-script" // An event for merging signals. EventTypeMergeSignals EventType = "datastar-merge-signals" )
type ExecuteScriptOption ¶
type ExecuteScriptOption func(*executeScriptOptions)
func WithExecuteScriptAttributeKVs ¶
func WithExecuteScriptAttributeKVs(kvs ...string) ExecuteScriptOption
WithExecuteScriptAttributeKVs adds key/value attributes to the injected script tag.
func WithExecuteScriptAttributes ¶
func WithExecuteScriptAttributes(attributes ...string) ExecuteScriptOption
WithExecuteScriptAttributes adds raw attributes to the injected script tag.
func WithExecuteScriptAutoRemove ¶
func WithExecuteScriptAutoRemove(autoremove bool) ExecuteScriptOption
WithExecuteScriptAutoRemove controls whether the injected script removes itself after execution.
func WithExecuteScriptEventID ¶
func WithExecuteScriptEventID(id string) ExecuteScriptOption
WithExecuteScriptEventID sets the SSE event ID for an execute-script response.
func WithExecuteScriptRetryDuration ¶
func WithExecuteScriptRetryDuration(retryDuration time.Duration) ExecuteScriptOption
WithExecuteScriptRetryDuration sets the SSE retry duration for an execute-script response.
type PageOption ¶
type PageOption func(*pageOptions)
PageOption configures a full-page HTML response rendered by RenderPage or RenderComponent.
func WithStatus ¶
func WithStatus(status int) PageOption
WithStatus sets the HTTP status code used by RenderPage or RenderComponent.
type PatchElementOption ¶
type PatchElementOption func(*patchElementOptions)
func WithMode ¶
func WithMode(merge ElementPatchMode) PatchElementOption
WithMode sets how an element patch is applied to the target.
func WithModeAfter ¶
func WithModeAfter() PatchElementOption
WithModeAfter configures an element patch to insert after the target element.
func WithModeAppend ¶
func WithModeAppend() PatchElementOption
WithModeAppend configures an element patch to append inside the target element.
func WithModeBefore ¶
func WithModeBefore() PatchElementOption
WithModeBefore configures an element patch to insert before the target element.
func WithModeInner ¶
func WithModeInner() PatchElementOption
WithModeInner configures an element patch to replace the target element's inner HTML.
func WithModeOuter ¶
func WithModeOuter() PatchElementOption
WithModeOuter configures an element patch to morph the target element.
func WithModePrepend ¶
func WithModePrepend() PatchElementOption
WithModePrepend configures an element patch to prepend inside the target element.
func WithModeRemove ¶
func WithModeRemove() PatchElementOption
WithModeRemove configures an element patch to remove the target element.
func WithModeReplace ¶
func WithModeReplace() PatchElementOption
WithModeReplace configures an element patch to replace the target element.
func WithPatchElementsEventID ¶
func WithPatchElementsEventID(id string) PatchElementOption
WithPatchElementsEventID sets the SSE event ID for an element patch response.
func WithRetryDuration ¶
func WithRetryDuration(retryDuration time.Duration) PatchElementOption
WithRetryDuration sets the SSE retry duration for an element patch response.
func WithSelector ¶
func WithSelector(selector string) PatchElementOption
WithSelector sets the CSS selector targeted by an element patch.
func WithSelectorID ¶
func WithSelectorID(id string) PatchElementOption
WithSelectorID targets an element patch at the element with the given ID.
func WithSelectorf ¶
func WithSelectorf(selectorFormat string, args ...any) PatchElementOption
WithSelectorf formats and sets the CSS selector targeted by an element patch.
func WithUseViewTransitions ¶
func WithUseViewTransitions(useViewTransition bool) PatchElementOption
WithUseViewTransitions controls browser view transitions for an element patch.
func WithViewTransitions ¶
func WithViewTransitions() PatchElementOption
WithViewTransitions enables browser view transitions for an element patch.
func WithoutViewTransitions ¶
func WithoutViewTransitions() PatchElementOption
WithoutViewTransitions disables browser view transitions for an element patch.
type PatchSignalsOption ¶
type PatchSignalsOption func(*patchSignalsOptions)
func WithOnlyIfMissing ¶
func WithOnlyIfMissing(onlyIfMissing bool) PatchSignalsOption
WithOnlyIfMissing controls whether signal patches only set missing client signals.
func WithPatchSignalsEventID ¶
func WithPatchSignalsEventID(id string) PatchSignalsOption
WithPatchSignalsEventID sets the SSE event ID for a signal patch response.
func WithPatchSignalsRetryDuration ¶
func WithPatchSignalsRetryDuration(retryDuration time.Duration) PatchSignalsOption
WithPatchSignalsRetryDuration sets the SSE retry duration for a signal patch response.
type SSEEventOption ¶
type SSEEventOption func(*serverSentEventData)
SSEEventOption modifies one server-sent event.
func WithSSEEventID ¶
func WithSSEEventID(id string) SSEEventOption
WithSSEEventID configures an optional event ID for one server-sent event. The client message field lastEventId will be set to this value. If the next event does not have an event ID, the last used event ID will remain.
func WithSSERetryDuration ¶
func WithSSERetryDuration(retryDuration time.Duration) SSEEventOption
WithSSERetryDuration overrides the DefaultSseRetryDuration for one server-sent event.