trigger

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: 0BSD Imports: 6 Imported by: 6

Documentation

Overview

package trigger provides builders for hx-trigger attribute values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

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

An Event is a builder to create a new user Event hx-trigger.

func On added in v0.0.7

func On(eventName TriggerEvent) *Event

On starts a builder chain for creating a new hx-trigger for user events.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click")
	fmt.Println(trig.String())
}
Output:

click
Example (Ordering_multiple)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").When("isActive").Queue(trigger.First).Consume().Target("#element").From("#parent > #child")
	fmt.Println(trig.String())
}
Output:

click[isActive] consume from:(#parent > #child) queue:first target:#element

func (*Event) Changed

func (e *Event) Changed() *Event

Changed makes the event only if the value of the element has changed. Please pay attention change is the name of the event and changed is the name of the modifier.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Changed()
	fmt.Println(trig.String())
}
Output:

click changed

func (*Event) Clear

func (s *Event) Clear(modifier Modifier) *Event

Clear removes a modifier entirely from the builder. Used to undo an previously set modifier.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Consume().Clear(trigger.Consume)
	fmt.Println(trig.String())
}
Output:

click

func (*Event) Consume

func (e *Event) Consume() *Event

Consume causes the event not to trigger any other htmx requests on parents (or on elements listening on parents).

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Consume()
	fmt.Println(trig.String())
}
Output:

click consume

func (*Event) Delay

func (e *Event) Delay(timing time.Duration) *Event

Delay will cause a delay before an event triggers a request. If the event is seen again it will reset the delay.

Example
package main

import (
	"fmt"
	"time"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Delay(time.Second)
	fmt.Println(trig.String())
}
Output:

click delay:1s

func (*Event) From

func (e *Event) From(extendedSelector FromSelector) *Event

From allows the event that triggers a request to come from another element in the document (e.g. listening to a key event on the body, to support hot keys) A standard CSS selector resolves to all elements matching that selector. Thus, from:input would listen on every input on the page. If the selector contains whitespace, it will be wrapped in () to disambiguate it from other modifiers.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").From("#element")
	fmt.Println(trig.String())
}
Output:

click from:(#element)
Example (NonStandard)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").From(trigger.FromDocument)
	fmt.Println(trig.String())
}
Output:

click from:(document)
Example (WithSpaces)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").From("parent > child")
	fmt.Println(trig.String())
}
Output:

click from:(parent > child)

func (*Event) Once

func (e *Event) Once() *Event

Once makes the event will only trigger once (e.g. the first click)

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Once()
	fmt.Println(trig.String())
}
Output:

click once

func (*Event) OverrideEvent

func (e *Event) OverrideEvent(event TriggerEvent) *Event

OverrideEvent overrides the initial event name. This is useful if you are forking a default trigger setup.

Example
package main

import (
	"fmt"
	"time"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("keyup").Delay(time.Second).OverrideEvent("input")
	fmt.Println(trig.String())
}
Output:

input delay:1s

func (*Event) Queue

func (e *Event) Queue(option QueueOption) *Event

Queue determines how events are queued if an event occurs while a request for another event is in flight.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Queue(trigger.First)
	fmt.Println(trig.String())
}
Output:

click queue:first

func (*Event) String

func (e *Event) String() string

String returns the final hx-swap string.

func (*Event) Target

func (e *Event) Target(selector string) *Event

Target allows you to filter via a CSS selector on the target of the event. This can be useful when you want to listen for triggers from elements that might not be in the DOM at the point of initialization, by, for example, listening on the body, but with a target filter for a child element. If the selector contains whitespace, it will be wrapped in () to disambiguate it from other modifiers.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Target("#element")
	fmt.Println(trig.String())
}
Output:

click target:#element
Example (WithSpaces)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Target("parent > child")
	fmt.Println(trig.String())
}
Output:

click target:(parent > child)

func (*Event) Throttle

func (e *Event) Throttle(timing time.Duration) *Event

Throttle will cause a throttle to occur after an event triggers a request. If the event is seen again before the delay completes, it is ignored, the element will trigger at the end of the delay.

Example
package main

import (
	"fmt"
	"time"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").Throttle(500 * time.Millisecond)
	fmt.Println(trig.String())
}
Output:

click throttle:500ms

func (*Event) When added in v0.2.1

func (e *Event) When(filter string) *Event

When specifies a boolean javascript expression as an event filter.

If this expression evaluates to true the event will be triggered, otherwise it will be ignored.

<div
	{ hx.Get("/clicked")...}
	{ hx.TriggerExtended(trigger.On("click").When("ctrlKey"))...}
>
	Control Click Me
</div>

Conditions can also refer to global functions or state

<div
	{ hx.Get("/clicked")... }
	{ hx.TriggerExtended(trigger.On("click").When("checkGlobalState()"))...}
>
	Control Click Me
</div>

And can also be combined using the standard javascript syntax

<div
	{ hx.Get("/clicked")...}
	{ hx.TriggerExtended(trigger.On("click).When("ctrlKey&&shiftKey"))... }
>
	Control-Shift Click Me
</div>

Note that all symbols used in the expression will be resolved first against the triggering event, and then next against the global namespace, so myEvent[foo] will first look for a property named foo on the event, then look for a global symbol with the name foo

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").When("checkGlobalState()")
	fmt.Println(trig.String())
}
Output:

click[checkGlobalState()]

type FromSelector

type FromSelector string

A FromSelector is a non-standard selector for the From modifier.

const (
	FromDocument FromSelector = "document" // listen for events on the document
	FromWindow   FromSelector = "window"   // listen for events on the window
	FromNext     FromSelector = "next"     // resolves to element.nextElementSibling
	FromPrevious FromSelector = "previous" // resolves to element.previousElementSibling
)

func FromRelative

func FromRelative(modifier SelectorModifier, selector string) FromSelector

FromRelative creates a relative selector for an Event.From modifier. It always wraps the selector in (), in case it contains a space.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").From(trigger.FromRelative(trigger.Next, "#alert"))
	fmt.Println(trig.String())
}
Output:

click from:next (#alert)
Example (WithWhitespace)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.On("click").From(trigger.FromRelative(trigger.Next, "#alert > button"))
	fmt.Println(trig.String())
}
Output:

click from:next (#alert > button)

type IntersectEvent

type IntersectEvent struct {
	Event
}

An IntersectEvent fires once when an element first intersects the viewport. This supports additional options to a normal trigger, IntersectEvent.Root and IntersectEvent.Threshold.

func Intersect added in v0.0.7

func Intersect() *IntersectEvent

Intersect configures a trigger that fires once when an element first intersects the viewport. This supports additional options to a normal trigger, IntersectEvent.Root and IntersectEvent.Threshold.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.Intersect()
	fmt.Println(trig.String())
}
Output:

intersect
Example (SupportsOtherOptions)
package main

import (
	"fmt"
	"time"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.Intersect().Root("#element").Delay(time.Second)
	fmt.Println(trig.String())
}
Output:

intersect delay:1s root:#element

func (*IntersectEvent) Root

func (e *IntersectEvent) Root(selector string) *IntersectEvent

Root configures a CSS selector of the root element for intersection.

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.Intersect().Root("#element")
	fmt.Println(trig.String())
}
Output:

intersect root:#element
Example (WithSpaces)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.Intersect().Root("#parent > #child")
	fmt.Println(trig.String())
}
Output:

intersect root:(#parent > #child)

func (*IntersectEvent) Threshold

func (e *IntersectEvent) Threshold(threshold float64) *IntersectEvent

Threshold takes a floating point number between 0.0 and 1.0, indicating what amount of intersection to fire the event on

Example
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.Intersect().Threshold(0.2)
	fmt.Println(trig.String())
}
Output:

intersect threshold:0.2

type Modifier

type Modifier string

Modifier is an enum of the possible hx-trigger modifiers.

const (
	Once      Modifier = "once"      // the event will only trigger once (e.g. the first click)
	Changed   Modifier = "changed"   // the event will only change if the value of the element has changed. Please pay attention change is the name of the event and changed is the name of the modifier.
	Delay     Modifier = "delay"     // a delay will occur before an event triggers a request. If the event is seen again it will reset the delay.
	Throttle  Modifier = "throttle"  // a throttle will occur after an event triggers a request. If the event is seen again before the delay completes, it is ignored, the element will trigger at the end of the delay.
	From      Modifier = "from"      // allows the event that triggers a request to come from another element in the document (e.g. listening to a key event on the body, to support hot keys)
	Target    Modifier = "target"    // allows you to filter via a CSS selector on the target of the event. This can be useful when you want to listen for triggers from elements that might not be in the DOM at the point of initialization, by, for example, listening on the body, but with a target filter for a child element
	Consume   Modifier = "consume"   // if this option is included the event will not trigger any other htmx requests on parents (or on elements listening on parents)
	Queue     Modifier = "queue"     // determines how events are queued if an event occurs while a request for another event is in flight. Options are:
	Root      Modifier = "root"      // a CSS selector of the root element for intersection. Only used by the intersect event.
	Threshold Modifier = "threshold" // a floating point number between 0.0 and 1.0, indicating what amount of intersection to fire the event on. Only used by the intersect event.
)

type Poll

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

func Every added in v0.0.7

func Every(timing time.Duration) *Poll

Every creates a new polling trigger for [htmx.HX.TriggerExtended].

Example
package main

import (
	"fmt"
	"time"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.Every(5 * time.Second)
	fmt.Println(trig.String())
}
Output:

every 5s

func (*Poll) Filter added in v0.0.7

func (p *Poll) Filter(filter string) *Poll

Filter adds a filter to the polling trigger, so that when the timer goes off, the trigger will only occur if the expression evaluates to true.

Example
package main

import (
	"fmt"
	"time"

	"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

func main() {
	trig := trigger.Every(5 * time.Second).Filter("isActive")
	fmt.Println(trig.String())
}
Output:

every 5s [isActive]

func (*Poll) String

func (p *Poll) String() string

String returns the final hx-trigger string.

type QueueOption

type QueueOption string

A QueueOption determines how events are queued if an event occurs while a request for another event is in flight.

const (
	First QueueOption = "first" // queue the first event
	Last  QueueOption = "last"  // queue the last event (default)
	All   QueueOption = "all"   // queue all events (issue a request for each event)
	None  QueueOption = "none"  // do not queue new events
)

type SelectorModifier

type SelectorModifier string

A SelectorModifier is a relative modifier to a CSS selector. This is used for "extended selectors". Some attributes only support a subset of these, but any Relative function that takes this type supports the full set..

const (
	Closest  SelectorModifier = "closest"  // find the closest ancestor element or itself, that matches the given CSS selector
	Find     SelectorModifier = "find"     // find the first child descendant element that matches the given CSS selector
	Next     SelectorModifier = "next"     // scan the DOM forward for the first element that matches the given CSS selector. (e.g. next .error will target the closest following sibling element with error class)
	Previous SelectorModifier = "previous" // scan the DOM backwards fo
)

type Trigger

type Trigger interface {
	String() string
	// contains filtered or unexported methods
}

A Trigger is a builder for hx-trigger attribute values.

type TriggerEvent added in v0.0.6

type TriggerEvent string

A TriggerEvent is any standard DOM event like `click` or `input`, a custom event name, plus two non-standard ones (`load` and `revealed`.).

const (
	Load     TriggerEvent = "load"     // triggered on load (useful for lazy-loading something)
	Revealed TriggerEvent = "revealed" // triggered when an element is scrolled into the viewport (also useful for lazy-loading). If you are using overflow in css like overflow-y: scroll you should use intersect once instead of revealed.
)

Jump to

Keyboard shortcuts

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