Repository navigation
Expand file tree
/
Copy pathresponse_headers.go
More file actions
1149 lines (1034 loc) · 35 KB
/
Copy pathresponse_headers.go
File metadata and controls
1149 lines (1034 loc) · 35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package hx
import (
"bytes"
"encoding/json/v2"
"fmt"
"strings"
"time"
)
// Headers Headers
const (
// HxTrigger triggers client-side events after the swap has completed.
//
// See https://four.htmx.org/reference/headers/HX-Trigger for more details.
//
// Use the Trigger(...TriggerEvent) option to set this header in the response.
HxTrigger = "Hx-Trigger"
// HxLocation redirects without reloading the page.
//
// See https://four.htmx.org/reference/headers/HX-Location for more details.
//
// Use the Location(string) option to set this header in the response.
HxLocation = "Hx-Location"
// HxRedirect redirects to a new URL with a full page reload.
//
// Use it for non-htmx endpoints or pages that require a full browser load.
//
// For AJAX navigation inside an htmx application, send HxLocation with Location(path).
//
// See https://four.htmx.org/reference/headers/HX-Redirect for more details.
//
// Use the Redirect(string) option to set this header in the response.
HxRedirect = "Hx-Redirect"
// HxRefresh when set to true, reloads the entire page.
//
// See https://four.htmx.org/reference/headers/HX-Refresh for more details.
//
// Use the Refresh() option to set this header in the response.
HxRefresh = "Hx-Refresh"
// HxRetarget overrides the element that'll receive the swapped content, replacing whatever hx-target was set on the triggering element.
//
// See https://four.htmx.org/reference/headers/HX-Retarget for more details.
//
// Use the Retarget(string) option to set this header in the response.
HxRetarget = "Hx-Retarget"
// HxReswap overrides the swap style used when inserting the response, replacing whatever hx-swap was set on the triggering element.
//
// See https://four.htmx.org/reference/headers/HX-Reswap for more details.
//
// Use either:
// - SwapInnerHtml : Replaces content inside element.
// - SwapOuterHtml : Replaces entire element.
// - SwapTextContent : Replaces the text content of the element, without parsing the response as HTML.
// - SwapBefore : Inserts content before element.
// - SwapPrepend : Inserts content as first child.
// - SwapAppend : Inserts content as last child.
// - SwapAfter : Inserts content after element.
// - SwapInnerMorph : Morphs content inside element, preserving state and focus.
// - SwapOuterMorph : Morphs entire element, preserving state and focus.
// - SwapOuterSync : Morphs the target’s attributes, then replaces its children.
// - SwapDelete : Removes element (ignores response content).
// - SwapNone : Doesn’t insert content.
// - SwapUpsert : Updates existing elements by ID and inserts new ones.
// - Reswap(string) : Set a custom swap style.
HxReswap = "Hx-Reswap"
// HxReselect allows selection of a specific part of the response to be swapped in, using a CSS selector. It overrides any existing hx-select on the triggering element.
//
// See https://four.htmx.org/reference/headers/HX-Reselect for more details.
//
// Use the Reselect(string) option to set this header.
HxReselect = "Hx-Reselect"
// HxReplaceUrl replaces the current browser history URL.
//
// See https://four.htmx.org/reference/headers/HX-Replace-Url for more details.
//
// Use the ReplaceUrl(string) option to set this header in the response.
HxReplaceUrl = "Hx-Replace-Url"
// HxPushUrl pushes a new URL into the history stack.
//
// See https://htmx.org/reference/#response_headers for more details.
//
// Use the PushUrl(string) option to set this header in the response.
HxPushUrl = "Hx-Push-Url"
)
// ----- HxTrigger
type TriggerEvent func() ([]byte, error)
// Event creates an event to pass into the Trigger() function.
//
// Simple named event:
//
// hx.Event("myEvent")
// // Returns {"myEvent":null}
//
// Named event with string data:
//
// hx.Event("myEvent", "myData")
// // Returns {"myEvent":"myData"}
//
// Named event with multiple data items (treated like an array):
//
// hx.Event("myEvent", "myData1", "myData2")
// // Returns {"myEvent":["myData1","myData2"]}
//
// Named event with a struct or map (treated like an object):
//
// hx.Event("myEvent", map[string]string{"myKey": "myValue"})
// // Returns {"myEvent":{"myKey":"myValue"}}
func Event(name string, data ...any) TriggerEvent {
return func() ([]byte, error) {
switch len(data) {
case 0:
return []byte(name), nil
case 1:
return json.Marshal(map[string]any{name: data[0]})
default:
return json.Marshal(map[string]any{name: data})
}
}
}
// Trigger allows you to trigger events on the client
//
// More details: https://four.htmx.org/reference/headers/HX-Trigger
//
// Use Event to create events to pass to this option.
//
// Send simple events:
//
// hx.Headers(w, hx.Trigger("myEvent"))
// // Sets HX-Trigger header to "myEvent"
//
// Send multiple simple events:
//
// hx.Headers(w, hx.Trigger("myEvent", "myOtherEvent"))
// // Sets HX-Trigger header to `myEvent, myOtherEvent`
//
// Send complex events with data:
//
// hx.Headers(w, hx.Trigger(hx.Event("myEvent", "myData")))
// // Sets HX-Trigger header to `{"myEvent":"myData"}`
//
// Send multiple complex events with data:
//
// hx.Headers(w, hx.Trigger(
// hx.Event("myEvent", "myData"),
// hx.Event("myOtherEvent", map[string]any{"fieldA": "keyA"}),
// ))
// // Sets HX-Trigger header to `{"myEvent":"myData","myOtherEvent":{"fieldA":"keyA"}}`
//
// Send simple and complex events together
//
// hx.Headers(w, hx.Trigger(
// "simpleOne", "simpleTwo",
// hx.Event("myEvent", "myData"),
// hx.Event("myOtherEvent", map[string]any{"fieldA": "keyA"}),
// ))
// // Sets HX-Trigger header to `simpleOne, simpleTwo, {"myEvent":"myData","myOtherEvent":{"fieldA":"keyA"}}`
func Trigger(events ...any) HeaderOptionFunc {
return func(o *HxHeaders) {
var (
simple []string
complex [][]byte
)
for _, v := range events {
switch event := v.(type) {
case string:
simple = append(simple, event)
case TriggerEvent:
b, err := event()
if err != nil {
panic(fmt.Errorf("trigger event failed: %w", err))
}
b = bytes.TrimSpace(b)
if !bytes.HasPrefix(b, []byte("{")) {
simple = append(simple, string(b))
continue
}
b = bytes.TrimSuffix(bytes.TrimPrefix(b, []byte("{")), []byte("}"))
if len(b) > 0 {
complex = append(complex, b)
}
default:
panic(fmt.Errorf("invalid event type: %T, use string or hx.Event only", event))
}
}
var buf bytes.Buffer
buf.WriteString(strings.Join(simple, ", "))
if len(complex) > 0 {
if buf.Len() > 0 {
buf.WriteString(", ")
}
buf.WriteByte('{')
buf.Write(bytes.Join(complex, []byte(",")))
buf.WriteByte('}')
}
o.headers[HxTrigger] = buf.String()
}
}
// ----- HxLocation
type (
locationProperties struct {
Path string `json:"path"`
Source string `json:"source,omitempty"` // Element or selector used for attributes, values, and lifecycle events
Event string `json:"event,omitempty"` // Event that triggered the request
Target string `json:"target,omitempty"` // Element or selector that receives the response
Swap string `json:"swap,omitempty"` // Serialized HxSwap value
Select string `json:"select,omitempty"` // Content selected from the response
SelectOOB string `json:"selectOOB,omitempty"` // Out-of-band content selected from the response
Transition string `json:"transition,omitempty"` // Whether to use a view transition
Values any `json:"values,omitempty"` // Values added to the request
Headers map[string]string `json:"headers,omitempty"` // Request headers
}
LocationProperties struct {
properties locationProperties
hasOptions bool
}
)
var _ HeaderOption = (*LocationProperties)(nil)
func (l *LocationProperties) apply(o *HxHeaders) {
if !l.hasOptions {
o.headers[HxLocation] = l.properties.Path
return
}
value, err := json.Marshal(l.properties)
if err != nil {
panic(fmt.Errorf("unable to marshal HX-Location header: %w", err))
}
o.headers[HxLocation] = string(value)
}
// Location sets the HxLocation header on a response
//
// This header is used for client-side redirection without a full page reload.
// Additional properties like LocationTarget can be specified for more complex behaviors.
//
// More details, see: https://four.htmx.org/reference/headers/HX-Location
//
// Simple redirection example:
//
// hx.Headers(w, hx.Location("/test"))
// // Sets HX-Location header to "/test"
//
// Redirection with additional target example:
//
// hx.Headers(w, hx.Location("/test").Target("#testdiv"))
// // Sets HX-Location header to a JSON object: {"path":"/test","target":"#testdiv"}
func Location(path string) *LocationProperties {
return &LocationProperties{
properties: locationProperties{
Path: path,
},
hasOptions: false,
}
}
// Source sets the 'source' property of the HxLocation response header.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Source(src string) *LocationProperties {
l.properties.Source = src
l.hasOptions = true
return l
}
// Event sets the 'event' property of the HxLocation response header.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Event(evt string) *LocationProperties {
l.properties.Event = evt
l.hasOptions = true
return l
}
// Target sets the 'target' property of the HxLocation response header.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Target(tgt string) *LocationProperties {
l.properties.Target = tgt
l.hasOptions = true
return l
}
// Swap sets the 'swap' property of the HxLocation response header.
//
// Either a string or a Reswap constant can be used.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Swap[T string | Reswap | *ReswapModifiers](swap T) *LocationProperties {
switch v := any(swap).(type) {
case string:
l.properties.Swap = v
case *ReswapModifiers:
l.properties.Swap = v.String()
case Reswap:
l.properties.Swap = string(v)
}
l.hasOptions = true
return l
}
// Transition sets the 'transition' property of the HxLocation response header.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Transition() *LocationProperties {
l.properties.Transition = "true"
l.hasOptions = true
return l
}
// Values sets the 'values' property of the HxLocation response header.
//
// Accepts any type, but a map[string]any or a struct with JSON tags is recommended.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Values(values any) *LocationProperties {
l.properties.Values = values
l.hasOptions = true
return l
}
// Headers sets the 'headers' property of the HxLocation response header.
//
// Accepts a map[string]string.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Headers(headers map[string]string) *LocationProperties {
l.properties.Headers = headers
l.hasOptions = true
return l
}
// Select sets the 'select' property of the HxLocation response header.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) Select(sel string) *LocationProperties {
l.properties.Select = sel
l.hasOptions = true
return l
}
// SelectOOB sets the 'selectOOB' property of the HxLocation response header.
//
// More details: https://four.htmx.org/reference/headers/HX-Location
func (l *LocationProperties) SelectOOB(sel string) *LocationProperties {
l.properties.SelectOOB = sel
l.hasOptions = true
return l
}
// ----- HxRedirect
// Redirect sets the HxRedirect header.
//
// More details: https://four.htmx.org/reference/headers/HX-Redirect
//
// It is used for client-side redirects that require a full page reload.
//
// Use it for non-htmx endpoints or pages that require a full browser load.
//
// For AJAX navigation inside an htmx application, send HxLocation with Location(path).
//
// Example usage:
//
// hx.Headers(w, hx.Redirect("/new-url-location"))
// // Sets the HX-Redirect header to "/new-url-location".
type Redirect string
func (r Redirect) apply(o *HxHeaders) { o.headers[HxRedirect] = string(r) }
// ----- HxRefresh
// Refresh sets the HxRefresh header.
//
// More details: https://four.htmx.org/reference/headers/HX-Refresh
//
// When set, it triggers a full refresh of the client-side page.
//
// Example usage:
//
// hx.Headers(w, hx.Refresh())
// // Sets the HX-Refresh header to "true".
func Refresh() HeaderOptionFunc {
return func(o *HxHeaders) {
o.headers[HxRefresh] = "true"
}
}
// ----- HxRetarget
// Retarget sets the HxRetarget header.
//
// More details: https://four.htmx.org/reference/headers/HX-Retarget
//
// This option specifies a new CSS selector to redirect the content update to a different element on the page.
//
// Example usage:
//
// hx.Headers(w, hx.Retarget("#new-target"))
// // Sets the HX-Retarget header to "#new-target".
type Retarget string
func (t Retarget) apply(o *HxHeaders) { o.headers[HxRetarget] = string(t) }
// ----- HxReswap
type (
// Reswap overrides the swap style used when inserting the response, replacing whatever hx-swap was set on the triggering element.
//
// The constants correspond to the different swap styles:
// - SwapInnerHtml : Replaces content inside element.
// - SwapOuterHtml : Replaces entire element.
// - SwapTextContent : Replaces the text content of the element, without parsing the response as HTML.
// - SwapBefore : Inserts content before element.
// - SwapPrepend : Inserts content as first child.
// - SwapAppend : Inserts content as last child.
// - SwapAfter : Inserts content after element.
// - SwapInnerMorph : Morphs content inside element, preserving state and focus.
// - SwapOuterMorph : Morphs entire element, preserving state and focus.
// - SwapOuterSync : Morphs the target’s attributes, then replaces its children.
// - SwapDelete : Removes element (ignores response content).
// - SwapNone : Doesn’t insert content.
// - SwapUpsert : Updates existing elements by ID and inserts new ones.
//
// For more details, see: https://four.htmx.org/reference/attributes/hx-swap
//
// HxReswap also takes additional modifiers, see: https://four.htmx.org/reference/attributes/hx-swap#modifiers
//
// Simple usage:
//
// hx.Headers(w, hx.Reswap("innerHTML"))
// // Sets HX-Reswap header to `innerHTML`
//
// Constant usage example (same as above):
//
// hx.Headers(w, hx.SwapInnerHtml)
// // Also sets HX-Reswap header to `innerHTML`
//
// Constant usage example (with modifiers):
//
// hx.Headers(w, hx.SwapInnerHtml.Swap(1*time.Second).Settle(2*time.Second))
// // Sets HX-Reswap header to `innerHTML swap:1s settle:2s`
Reswap string
// ReswapModifiers overrides the swap style used when inserting the response, replacing whatever hx-swap was set on the triggering element.
//
// For more details, see: https://four.htmx.org/reference/attributes/hx-swap
//
// HxReswap also takes additional modifiers, see: https://four.htmx.org/reference/attributes/hx-swap#modifiers
//
// Simple usage:
// hx.Headers(w, hx.Reswap("innerHTML"))
// // Sets HX-Reswap header to `innerHTML`
//
// Constant usage example (same as above):
// hx.Headers(w, hx.SwapInnerHtml)
// // Also sets HX-Reswap header to `innerHTML`
//
// Constant usage example (with modifiers):
// hx.Headers(w, hx.SwapInnerHtml.Swap(1*time.Second).Settle(2*time.Second))
// // Sets HX-Reswap header to `innerHTML swap:1s settle:2s`
ReswapModifiers struct {
reswap Reswap
modifiers map[string]string
}
)
// Reswap constants
const (
SwapInnerHtml Reswap = "innerHTML" // Replaces content inside element.
SwapOuterHtml Reswap = "outerHTML" // Replaces entire element.
SwapTextContent Reswap = "textContent" // Replaces the text content of the element, without parsing the response as HTML.
SwapBefore Reswap = "before" // Inserts content before element.
SwapPrepend Reswap = "prepend" // Inserts content as first child.
SwapAppend Reswap = "append" // Inserts content as last child.
SwapAfter Reswap = "after" // Inserts content after element.
SwapInnerMorph Reswap = "innerMorph" // Morphs content inside element, preserving state and focus.
SwapOuterMorph Reswap = "outerMorph" // Morphs entire element, preserving state and focus.
SwapOuterSync Reswap = "outerSync" // Morphs the target’s attributes, then replaces its children.
SwapDelete Reswap = "delete" // Removes element (ignores response content).
SwapNone Reswap = "none" // Doesn’t insert content.
SwapUpsert Reswap = "upsert" // Updates existing elements by ID and inserts new ones.
)
func (s Reswap) apply(o *HxHeaders) { o.headers[HxReswap] = string(s) }
func (r *ReswapModifiers) apply(o *HxHeaders) {
o.headers[HxReswap] = r.String()
}
func (r *ReswapModifiers) String() string {
builder := strings.Builder{}
builder.WriteString(string(r.reswap))
for k, v := range r.modifiers {
builder.WriteString(fmt.Sprintf(" %s:%s", k, v))
}
return builder.String()
}
// Transition (HxReswap header modifier) enables View Transitions API for smooth page transitions.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#transition
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition())
// // Sets HX-Reswap header to `innerHTML transition:true`
func (s Reswap) Transition() *ReswapModifiers {
return &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"transition": "true",
},
}
}
// Transition (HxReswap header modifier) enables View Transitions API for smooth page transitions.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#transition
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Swap(1*time.Second).Transition())
// // Sets HX-Reswap header to `innerHTML swap:1s transition:true`
func (r *ReswapModifiers) Transition() *ReswapModifiers {
r.modifiers["transition"] = "true"
return r
}
// Swap (HxReswap header modifier) adds delay before swap.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#swap
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Swap(1*time.Second))
// // Sets HX-Reswap header to `innerHTML swap:1s`
// hx.Headers(w, hx.SwapInnerHtml.Swap("1s"))
// // Sets HX-Reswap header to `innerHTML swap:1s`
func (s Reswap) Swap(dur any) *ReswapModifiers {
var value string
switch v := dur.(type) {
case string:
value = v
case time.Duration:
value = v.String()
}
return &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"swap": value,
},
}
}
// Swap (HxReswap header modifier) adds delay before swap.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#swap
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().Swap(1*time.Second))
// // Sets HX-Reswap header to `innerHTML transition:true swap:1s`
// hx.Headers(w, hx.SwapInnerHtml.Transition().Swap("1s))
// // Sets HX-Reswap header to `innerHTML transition:true swap:1s`
func (r *ReswapModifiers) Swap(dur any) *ReswapModifiers {
var value string
switch v := dur.(type) {
case string:
value = v
case time.Duration:
value = v.String()
}
r.modifiers["swap"] = value
return r
}
// Settle (HxReswap header modifier) adds delay between the swap and the settle phase.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#settle
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Settle(1*time.Second))
// // Sets HX-Reswap header to `innerHTML settle:1s`
func (s Reswap) Settle(dur any) *ReswapModifiers {
var value string
switch v := dur.(type) {
case string:
value = v
case time.Duration:
value = v.String()
}
return &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"settle": value,
},
}
}
// Settle (HxReswap header modifier) adds delay between the swap and the settle phase.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#settle
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().Settle(1*time.Second))
// // Sets HX-Reswap header to `innerHTML transition:true settle:1s`
func (r *ReswapModifiers) Settle(dur any) *ReswapModifiers {
var value string
switch v := dur.(type) {
case string:
value = v
case time.Duration:
value = v.String()
}
r.modifiers["settle"] = value
return r
}
// IgnoreTitle (HxReswap header modifier) prevents updating the page <title>.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#ignoretitle
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.IgnoreTitle())
// // Sets HX-Reswap header to `innerHTML ignoreTitle:true`
func (s Reswap) IgnoreTitle() *ReswapModifiers {
return &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"ignoreTitle": "true",
},
}
}
// IgnoreTitle (HxReswap header modifier) prevents updating the page <title>.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#ignoretitle
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().IgnoreTitle())
// // Sets HX-Reswap header to `innerHTML transition:true ignoreTitle:true`
func (r *ReswapModifiers) IgnoreTitle() *ReswapModifiers {
r.modifiers["ignoreTitle"] = "true"
return r
}
// ScrollTop (HxReswap header modifier) auto-scroll to the top of the swapped content, the window, or target.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#scroll
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.ScrollTop()
// // Sets HX-Reswap header to `innerHTML scroll:top`
// hx.Headers(w, hx.SwapInnerHtml.ScrollTop(true)
// // Sets HX-Reswap header to `innerHTML scroll:window:top`
// hx.Headers(w, hx.SwapInnerHtml.ScrollTop("#another-div"))
// // Sets HX-Reswap header to `innerHTML scroll:top scrollTarget:"#another-div"`
func (s Reswap) ScrollTop(targets ...any) *ReswapModifiers {
mod := &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"scroll": "top",
},
}
if len(targets) == 0 {
return mod
}
target := targets[0]
switch v := target.(type) {
case bool:
if v {
mod.modifiers["scroll"] = "window:top"
delete(mod.modifiers, "scrollTarget") // ensure we overwrite
}
case string:
mod.modifiers["scroll"] = "top" // ensure we overwrite
mod.modifiers["scrollTarget"] = `"` + v + `"`
}
return mod
}
// ScrollTop (HxReswap header modifier) auto-scroll to the top of the swapped content, the window, or target.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#scroll
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().ScrollTop()
// // Sets HX-Reswap header to `innerHTML transition:true scroll:top`
// hx.Headers(w, hx.SwapInnerHtml.Transition().ScrollTop(true)
// // Sets HX-Reswap header to `innerHTML transition:true scroll:window:top`
// hx.Headers(w, hx.SwapInnerHtml.Transition().ScrollTop("#another-div"))
// // Sets HX-Reswap header to `innerHTML transition:true scroll:top scrollTarget:"#another-div"`
func (r *ReswapModifiers) ScrollTop(targets ...any) *ReswapModifiers {
if len(targets) == 0 {
r.modifiers["scroll"] = "top"
delete(r.modifiers, "scrollTarget")
return r
}
target := targets[0]
switch v := target.(type) {
case bool:
if v {
r.modifiers["scroll"] = "window:top"
delete(r.modifiers, "scrollTarget") // ensure we overwrite
}
case string:
r.modifiers["scroll"] = "top" // ensure we overwrite
r.modifiers["scrollTarget"] = `"` + v + `"`
}
return r
}
// ScrollBottom (HxReswap header modifier) auto-scroll to the bottom of the swapped content, the window, or target.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#scroll
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.ScrollBottom()
// // Sets HX-Reswap header to `innerHTML scroll:bottom`
// hx.Headers(w, hx.SwapInnerHtml.ScrollBottom(true)
// // Sets HX-Reswap header to `innerHTML scroll:window:bottom`
// hx.Headers(w, hx.SwapInnerHtml.ScrollBottom("#another-div"))
// // Sets HX-Reswap header to `innerHTML scroll:bottom scrollTarget:"#another-div"`
func (s Reswap) ScrollBottom(targets ...any) *ReswapModifiers {
mod := &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"scroll": "bottom",
},
}
if len(targets) == 0 {
return mod
}
target := targets[0]
switch v := target.(type) {
case bool:
if v {
mod.modifiers["scroll"] = "window:bottom"
delete(mod.modifiers, "scrollTarget") // ensure we overwrite
}
case string:
mod.modifiers["scroll"] = "bottom" // ensure we overwrite
mod.modifiers["scrollTarget"] = `"` + v + `"`
}
return mod
}
// ScrollBottom (HxReswap header modifier) auto-scroll to the bottom of the swapped content, the window, or target.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#scroll
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().ScrollBottom()
// // Sets HX-Reswap header to `innerHTML transition:true scroll:bottom`
// hx.Headers(w, hx.SwapInnerHtml.Transition().ScrollBottom(true)
// // Sets HX-Reswap header to `innerHTML transition:true scroll:window:bottom`
// hx.Headers(w, hx.SwapInnerHtml.Transition().ScrollBottom("#another-div"))
// // Sets HX-Reswap header to `innerHTML transition:true scroll:bottom scrollTarget:"#another-div"`
func (r *ReswapModifiers) ScrollBottom(targets ...any) *ReswapModifiers {
if len(targets) == 0 {
r.modifiers["scroll"] = "bottom"
delete(r.modifiers, "scrollTarget")
return r
}
target := targets[0]
switch v := target.(type) {
case bool:
if v {
r.modifiers["scroll"] = "window:bottom"
delete(r.modifiers, "scrollTarget") // ensure we overwrite
}
case string:
r.modifiers["scroll"] = "bottom" // ensure we overwrite
r.modifiers["scrollTarget"] = `"` + v + `"`
}
return r
}
// ShowTop (HxReswap header modifier) scrolls to show the top of the target or different element in viewport.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#show
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.ShowTop())
// // Sets HX-Reswap header to `innerHTML show:top`
// hx.Headers(w, hx.SwapInnerHtml.ShowTop("#another-div"))
// // Sets HX-Reswap header to `innerHTML show:top showTarget:"#another-div"`
func (s Reswap) ShowTop(other ...string) *ReswapModifiers {
mod := &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"show": "top",
},
}
if len(other) > 0 {
mod.modifiers["showTarget"] = `"` + other[0] + `"`
}
return mod
}
// ShowTop (HxReswap header modifier) scrolls to show the top of the target or different element in viewport.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#show
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().ShowTop())
// // Sets HX-Reswap header to `innerHTML transition:true show:top`
// hx.Headers(w, hx.SwapInnerHtml.Transition().ShowTop("#another-div"))
// // Sets HX-Reswap header to `innerHTML transition:true show:top showTarget:"#another-div"`
func (r *ReswapModifiers) ShowTop(other ...string) *ReswapModifiers {
if len(other) == 0 {
r.modifiers["show"] = `top`
delete(r.modifiers, "showTarget")
return r
}
r.modifiers["show"] = "top"
r.modifiers["showTarget"] = `"` + other[0] + `"`
return r
}
// ShowBottom (HxReswap header modifier) scrolls to show the bottom of the target or different element in viewport.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#show
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.ShowBottom())
// // Sets HX-Reswap header to `innerHTML show:bottom`
// hx.Headers(w, hx.SwapInnerHtml.ShowBottom("#another-div"))
// // Sets HX-Reswap header to `innerHTML show:bottom showTarget:"#another-div"`
func (s Reswap) ShowBottom(other ...string) *ReswapModifiers {
mod := &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"show": "bottom",
},
}
if len(other) > 0 {
mod.modifiers["showTarget"] = `"` + other[0] + `"`
}
return mod
}
// ShowBottom (HxReswap header modifier) scrolls to show the bottom of the target or different element in viewport.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#show
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().ShowBottom())
// // Sets HX-Reswap header to `innerHTML transition:true show:bottom`
// hx.Headers(w, hx.SwapInnerHtml.Transition().ShowBottom("#another-div"))
// // Sets HX-Reswap header to `innerHTML transition:true show:bottom showTarget:"#another-div"`
func (r *ReswapModifiers) ShowBottom(other ...string) *ReswapModifiers {
if len(other) == 0 {
r.modifiers["show"] = `bottom`
delete(r.modifiers, "showTarget")
return r
}
r.modifiers["show"] = "bottom"
r.modifiers["showTarget"] = `"` + other[0] + `"`
return r
}
// ShowNone (HxReswap header modifier) disables scrolling the viewport.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#show
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.ShowNone())
// // Sets HX-Reswap header to `innerHTML show:none`
func (s Reswap) ShowNone() *ReswapModifiers {
return &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"show": "none",
},
}
}
// ShowNone (HxReswap header modifier) disables scrolling the viewport.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#show
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().ShowNone())
// // Sets HX-Reswap header to `innerHTML transition:true show:none`
func (r *ReswapModifiers) ShowNone() *ReswapModifiers {
r.modifiers["show"] = `none`
delete(r.modifiers, "showTarget")
return r
}
// FocusScroll (HxReswap header modifier) controls whether restoring focus after a swap scrolls the focused element into view.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#focusscroll
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.FocusScroll(true))
// // Sets HX-Reswap header to `innerHTML focusScroll:true
func (s Reswap) FocusScroll(focus bool) *ReswapModifiers {
return &ReswapModifiers{
reswap: s,
modifiers: map[string]string{
"focusScroll": fmt.Sprintf("%t", focus),
},
}
}
// FocusScroll (HxReswap header modifier) controls whether restoring focus after a swap scrolls the focused element into view.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#focusscroll
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Transition().FocusScroll(true))
// // Sets HX-Reswap header to `innerHTML transition:true focusScroll:true`
func (r *ReswapModifiers) FocusScroll(focus bool) *ReswapModifiers {
r.modifiers["focusScroll"] = fmt.Sprintf("%t", focus)
return r
}
// Target (HxReswap header modifier) sets the swap target.
//
// More details: https://four.htmx.org/reference/attributes/hx-swap#target
//
// Example usage:
//
// hx.Headers(w, hx.SwapInnerHtml.Target("#some-target")
// // Sets HX-Reswap header to `innerHTML target:"#some-target"`
func (s Reswap) Target(target string) *ReswapModifiers {
return &ReswapModifiers{
reswap: s,
modifiers: map[string]string{