Skip to content

Commit

Permalink
expose an hx for simple strings
Browse files Browse the repository at this point in the history
  • Loading branch information
will-wow committed Apr 25, 2024
1 parent 9e7430a commit 55ffd95
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 71 deletions.
2 changes: 1 addition & 1 deletion assets/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion htmx/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ExampleHX_Config() {
TriggerSpecsCache("cacheObject"), // Default is nil, this is an example
)

fmt.Println(config.String())
fmt.Println(config)

// output: content='{"addedClass":"htmx-added","allowEval":true,"allowScriptTags":true,"attributesToSettle":["foo"],"defaultFocusScroll":false,"defaultSettleDelay":20,"defaultSwapDelay":0,"defaultSwapStyle":"innerHTML","disableSelector":"[hx-disable], [data-hx-disable]","getCacheBusterParam":false,"globalViewTransitions":false,"historyCacheSize":10,"historyEnabled":true,"ignoreTitle":false,"includeIndicatorStyles":true,"indicatorClass":"htmx-indicator","inlineScriptNonce":"nonce","methodsThatUseUrlParams":["get"],"refreshOnHistoryMiss":false,"requestClass":"htmx-request","scrollBehavior":"smooth","scrollIntoViewOnBoost":true,"selfRequestsOnly":false,"settlingClass":"htmx-settling","swappingClass":"htmx-swapping","timeout":1000,"triggerSpecsCache":"cacheObject","useTemplateFragments":false,"withCredentials":false,"wsBinaryType":"blob","wsReconnectDelay":"full-jitter"}'
}
25 changes: 1 addition & 24 deletions htmx/htmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,7 @@ import (
"github.com/will-wow/typed-htmx-go/htmx/trigger"
)

type Attrs map[string]any

func (a Attrs) String() string {
for k, v := range a {
switch v := v.(type) {
// For strings, print the key='value' pair.
case string:
return fmt.Sprintf(`%s='%v'`, k, v)
// For booleans, print just the key if true.
case bool:
if v {
return k
}
}
}

return ""
}

var hx = htmx.NewHX(func(key htmx.Attribute, value any) Attrs {
return Attrs{string(key): value}
})

type HX = htmx.HX[Attrs]
var hx = htmx.NewStringAttrs()

func ExampleHX_Boost() {
fmt.Println(hx.Boost(true))
Expand Down
21 changes: 21 additions & 0 deletions htmx/string_attrs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package htmx

import "fmt"

// NewStringAttrs returns a HX instance that returns stringified attributes for direct use in HTML.
func NewStringAttrs() HX[string] {
return NewHX(func(k Attribute, v any) string {
switch v := v.(type) {
// For strings, print the key='value' pair.
case string:
return fmt.Sprintf(`%s='%v'`, k, v)
// For booleans, print just the key if true.
case bool:
if v {
return string(k)
}
}

return ""
})
}
18 changes: 18 additions & 0 deletions htmx/string_attrs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package htmx_test

import (
"fmt"

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

func ExampleNewStringAttrs() {
hx := htmx.NewStringAttrs()

fmt.Println(hx.Target(htmx.TargetNext))
fmt.Println(hx.Preserve())

// Output:
// hx-target='next'
// hx-preserve
}
108 changes: 63 additions & 45 deletions htmx/swap/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,113 @@ import (
"fmt"
"time"

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

var hx = htmx.NewStringAttrs()

func ExampleNew_default() {
builder := swap.New()
fmt.Println(builder.String())
// Output: innerHTML
fmt.Println(
hx.SwapExtended(swap.New()),
)
// output: hx-swap='innerHTML'
}

func ExampleBuilder_Strategy() {
builder := swap.New().Strategy(swap.OuterHTML)
fmt.Println(builder.String())
// Output: outerHTML
fmt.Println(
hx.SwapExtended(swap.New().Strategy(swap.OuterHTML)),
)
// output: hx-swap='outerHTML'
}

func ExampleBuilder_Transition() {
builder := swap.New().Transition()
fmt.Println(builder.String())
// Output: innerHTML transition:true
fmt.Println(
hx.SwapExtended(swap.New().Transition()),
)
// output: hx-swap='innerHTML transition:true'
}

func ExampleBuilder_Swap_timing() {
builder := swap.New().Swap(500 * time.Millisecond)
fmt.Println(builder.String())
// Output: innerHTML swap:500ms
fmt.Println(
hx.SwapExtended(swap.New().Swap(500 * time.Millisecond)),
)
// output: hx-swap='innerHTML swap:500ms'
}

func ExampleBuilder_Settle_timing() {
builder := swap.New().Settle(500 * time.Millisecond)
fmt.Println(builder.String())
// Output: innerHTML settle:500ms
fmt.Println(
hx.SwapExtended(swap.New().Settle(500 * time.Millisecond)),
)
// output: hx-swap='innerHTML settle:500ms'
}

func ExampleBuilder_IgnoreTitle() {
builder := swap.New().IgnoreTitle()
fmt.Println(builder.String())
// Output: innerHTML ignoreTitle:true
fmt.Println(
hx.SwapExtended(swap.New().IgnoreTitle()),
)
// output: hx-swap='innerHTML ignoreTitle:true'
}

func ExampleBuilder_Scroll() {
builder := swap.New().Scroll(swap.Top)
fmt.Println(builder.String())
// Output: innerHTML scroll:top
fmt.Println(
hx.SwapExtended(swap.New().Scroll(swap.Top)),
)
// output: hx-swap='innerHTML scroll:top'
}

func ExampleBuilder_ScrollElement() {
builder := swap.New().ScrollElement("#example", swap.Top)
fmt.Println(builder.String())
// Output: innerHTML scroll:#example:top
fmt.Println(
hx.SwapExtended(swap.New().ScrollElement("#example", swap.Top)),
)
// output: hx-swap='innerHTML scroll:#example:top'
}

func ExampleBuilder_Show() {
builder := swap.New().Show(swap.Bottom)
fmt.Println(builder.String())
// Output: innerHTML show:bottom
fmt.Println(
hx.SwapExtended(swap.New().Show(swap.Bottom)),
)
// output: hx-swap='innerHTML show:bottom'
}

func ExampleBuilder_ShowElement() {
builder := swap.New().ShowElement("#example", swap.Bottom)
fmt.Println(builder.String())
// Output: innerHTML show:#example:bottom
fmt.Println(
hx.SwapExtended(swap.New().ShowElement("#example", swap.Bottom)),
)
// output: hx-swap='innerHTML show:#example:bottom'
}

func ExampleBuilder_Show_window() {
builder := swap.New().ShowElement(swap.ShowWindow, swap.Top)
fmt.Println(builder.String())
// Output: innerHTML show:window:top
fmt.Println(
hx.SwapExtended(swap.New().ShowElement(swap.ShowWindow, swap.Top)),
)
// output: hx-swap='innerHTML show:window:top'
}

func ExampleBuilder_ShowNone() {
builder := swap.New().ShowNone()
fmt.Println(builder.String())
// Output: innerHTML show:none
fmt.Println(
hx.SwapExtended(swap.New().ShowNone()),
)
// output: hx-swap='innerHTML show:none'
}

func ExampleBuilder_FocusScroll() {
builder := swap.New().FocusScroll(true)
fmt.Println(builder.String())
// Output: innerHTML focus-scroll:true
fmt.Println(
hx.SwapExtended(swap.New().FocusScroll(true)),
)
// output: hx-swap='innerHTML focus-scroll:true'
}

func ExampleBuilder_FocusScroll_disable() {
builder := swap.New().FocusScroll(false)
fmt.Println(builder.String())
// Output: innerHTML focus-scroll:false
fmt.Println(
hx.SwapExtended(swap.New().FocusScroll(false)),
)
// output: hx-swap='innerHTML focus-scroll:false'
}

func ExampleBuilder_Clear() {
builder := swap.New().Transition().Clear(swap.Transition)
fmt.Println(builder.String())
// Output: innerHTML
fmt.Println(
hx.SwapExtended(swap.New().Transition().Clear(swap.Transition)),
)
// output: hx-swap='innerHTML'
}

0 comments on commit 55ffd95

Please sign in to comment.