trigger

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: 0BSD Imports: 6 Imported by: 0

Documentation

Overview

package trigger provides builders for hx-trigger attribute values.

Index

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 NewEvent

func NewEvent(event string) *Event

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

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.

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.

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).

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.

func (*Event) Filter

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

Filter 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-trigger="click[ctrlKey]">Control Click Me</div>

Conditions can also refer to global functions or state

<div hx-get="/clicked" hx-trigger="click[checkGlobalState()]">Control Click Me</div>

And can also be combined using the standard javascript syntax

<div hx-get="/clicked" hx-trigger="click[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

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.

func (*Event) Once

func (e *Event) Once() *Event

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

func (*Event) OverrideEvent

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

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

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.

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.

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.

type FromSelector added in v0.0.4

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 added in v0.0.4

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.

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 NewIntersectEvent

func NewIntersectEvent() *IntersectEvent

NewIntersectEvent 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.

func (*IntersectEvent) Root

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

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

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

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 NewFilteredPoll

func NewFilteredPoll(timing time.Duration, filter string) *Poll

NewPoll creates a new polling trigger with a javascript expression as a filter. When the timer goes off, the trigger will only occur if the expression evaluates to true.

func NewPoll

func NewPoll(timing time.Duration) *Poll

NewPoll creates a new polling trigger.

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 added in v0.0.4

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.

Jump to

Keyboard shortcuts

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