golazy.dev golazy.dev / lazyturbo Index | Files

package lazyturbo

import "golazy.dev/lazyturbo"

Package lazyturbo provides Hotwire Turbo helpers for controllers and views.

Turbo is the Hotwire browser protocol for replacing parts of a page without writing custom JavaScript for each interaction. A Turbo Frame is a named region of HTML, rendered as a <turbo-frame id="..."> element, that the browser can request and replace independently. A Turbo Stream response is an HTML response with media type text/vnd.turbo-stream.html that asks the browser to append, replace, remove, or otherwise mutate page elements.

The package has two jobs. First, it exposes request helpers such as FrameID, IsFrameRequest, IsPrefetch, and AcceptsStream so higher-level packages can recognize Turbo requests. lazycontroller uses those helpers during format negotiation: a request with a Turbo-Frame header becomes the lazycontroller.TurboFrame format, while an Accept header that includes StreamMIME becomes lazycontroller.TurboStream.

Second, it exposes lazyview helpers through Helpers. lazyapp installs those helpers automatically on its renderer, after route, asset, form, and SEO helpers and before application Config.Helpers. In a normal GoLazy app, views can call turbo_frame directly without importing this package. Direct use is for standalone lazyview renderers or lower-level apps that want the same helpers without constructing a lazyapp.App.

The turbo_frame helper renders a partial named "_<id>_frame.html.tpl" and wraps the rendered body in a <turbo-frame>. Frame ids are intentionally restricted by ValidateFrameID because the same value becomes both a DOM id and a partial name. FrameTag is the lower-level wrapper when the body was already rendered by another package.

lazycontroller connects this package to controller rendering. Base.Render sees Turbo-Frame requests and renders the matching "_<id>_frame" partial without the layout. Base.RenderTurboFrame renders a specific frame even when the current request was not negotiated from Turbo headers, and Base.SetTurboFrameOptions passes lazyturbo.FrameOption values such as Src, Loading, Action, and RefreshMorph into the generated frame tag.

Constants

Functions

func AcceptsStream

AcceptsStream reports whether r advertises Turbo Stream response support.

func AcceptsStream(r *http.Request) bool

func FrameID

FrameID returns the Turbo frame id requested by r.

func FrameID(r *http.Request) string

func Helpers

Helpers returns the template helpers provided by lazyturbo.

func Helpers() map[string]any

func IsFrameRequest

IsFrameRequest reports whether r was issued for a Turbo Frame.

func IsFrameRequest(r *http.Request) bool

func IsPrefetch

IsPrefetch reports whether r is a Turbo link prefetch request.

func IsPrefetch(r *http.Request) bool

func ValidateFrameID

ValidateFrameID checks whether id is safe to use as both a DOM id and a frame partial name.

func ValidateFrameID(id string) error

Types

type FrameOption

FrameOption configures a rendered <turbo-frame> element.

type FrameOption struct {
	// contains filtered or unexported fields
}
func Action

Action sets data-turbo-action. Valid values are advance and replace.

func Action(action string) FrameOption
func Autoscroll

Autoscroll sets the frame autoscroll boolean attribute.

func Autoscroll() FrameOption
func AutoscrollBehavior

AutoscrollBehavior sets data-autoscroll-behavior. Valid values are auto and smooth.

func AutoscrollBehavior(behavior string) FrameOption
func AutoscrollBlock

AutoscrollBlock sets data-autoscroll-block. Valid values are end, start, center, and nearest.

func AutoscrollBlock(block string) FrameOption
func Busy

Busy sets the frame busy boolean attribute.

func Busy() FrameOption
func Complete

Complete sets the frame complete boolean attribute.

func Complete() FrameOption
func Disabled

Disabled sets the frame disabled boolean attribute.

func Disabled() FrameOption
func Loading

Loading sets the frame loading attribute. Valid values are eager and lazy.

func Loading(loading string) FrameOption
func Recurse

Recurse sets the frame recurse attribute.

func Recurse(frameID string) FrameOption
func Refresh

Refresh sets the frame refresh attribute. Turbo currently defines morph.

func Refresh(refresh string) FrameOption
func RefreshMorph

RefreshMorph sets refresh="morph".

func RefreshMorph() FrameOption
func Src

Src sets the frame src attribute.

func Src(src string) FrameOption
func Target

Target sets the frame target attribute.

func Target(target string) FrameOption