Skip to content

Commit 0fc9372

Browse files
Update HtmxResponseHeaders with new constants and methods
The HtmxResponseHeaders class has been updated to include new constants and a new method 'Reselect'. The constants have been rearranged for better reference based on the htmx documentation. The 'Reselect' method allows to choose specific parts of the response for swapping in, overriding an existing 'hx-select' on the triggering element. #40
1 parent bbed67b commit 0fc9372

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/Htmx/HtmxResponseHeaders.cs

+31-6
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,32 @@ public class HtmxResponseHeaders
2222

2323
public static class Keys
2424
{
25-
public const string PushUrl = "HX-Push-Url";
25+
// Sorted by https://htmx.org/reference/#response_headers to make it easier to update
2626
public const string Location = "HX-Location";
27+
public const string PushUrl = "HX-Push-Url";
2728
public const string Redirect = "HX-Redirect";
2829
public const string Refresh = "HX-Refresh";
30+
public const string ReplaceUrl = "HX-Replace-Url";
31+
public const string Reswap = "HX-Reswap";
32+
public const string Retarget = "HX-Retarget";
33+
public const string Reselect = "HX-Reselect";
2934
public const string Trigger = "HX-Trigger";
3035
public const string TriggerAfterSettle = "HX-Trigger-After-Settle";
3136
public const string TriggerAfterSwap = "HX-Trigger-After-Swap";
32-
public const string Reswap = "HX-Reswap";
33-
public const string Retarget = "HX-Retarget";
34-
public const string ReplaceUrl = "HX-Replace-Url";
3537

3638
public static string[] All { get; } = new[]
3739
{
38-
PushUrl, Location, Redirect, Refresh, Trigger, TriggerAfterSettle, TriggerAfterSwap, Reswap, Retarget, ReplaceUrl
40+
Location,
41+
PushUrl,
42+
Redirect,
43+
Refresh,
44+
ReplaceUrl,
45+
Reswap,
46+
Retarget,
47+
Reselect,
48+
Trigger,
49+
TriggerAfterSettle,
50+
TriggerAfterSwap,
3951
};
4052
}
4153

@@ -88,6 +100,19 @@ public HtmxResponseHeaders Reswap(string value)
88100
_headers[Keys.Reswap] = value;
89101
return this;
90102
}
103+
104+
/// <summary>
105+
/// a CSS selector that allows you to choose which part of the response is used to be swapped in.
106+
/// Overrides an existing hx-select on the triggering element
107+
/// See https://htmx.org/attributes/hx-select/ for hx-select behavior.
108+
/// </summary>
109+
/// <param name="value"></param>
110+
/// <returns></returns>
111+
public HtmxResponseHeaders Reselect(string value)
112+
{
113+
_headers[Keys.Reselect] = value;
114+
return this;
115+
}
91116

92117
/// <summary>
93118
/// Allows you to do a client-side redirect that does not do a full page reload
@@ -246,7 +271,7 @@ private void ParsePossibleExistingTriggers(string headerKey, HtmxTriggerTiming t
246271
// this might still throw :(
247272
var jsonObject = JsonNode.Parse(ref reader)?.AsObject();
248273
// Load any existing triggers
249-
foreach (var (key, value) in jsonObject!)
274+
foreach (var (key, value) in jsonObject!)
250275
WithTrigger(key, value, timing);
251276
}
252277
else

0 commit comments

Comments
 (0)