Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hx-Reselect #41

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/Htmx/HtmxResponseHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,32 @@ public class HtmxResponseHeaders

public static class Keys
{
public const string PushUrl = "HX-Push-Url";
// Sorted by https://htmx.org/reference/#response_headers to make it easier to update
public const string Location = "HX-Location";
public const string PushUrl = "HX-Push-Url";
public const string Redirect = "HX-Redirect";
public const string Refresh = "HX-Refresh";
public const string ReplaceUrl = "HX-Replace-Url";
public const string Reswap = "HX-Reswap";
public const string Retarget = "HX-Retarget";
public const string Reselect = "HX-Reselect";
public const string Trigger = "HX-Trigger";
public const string TriggerAfterSettle = "HX-Trigger-After-Settle";
public const string TriggerAfterSwap = "HX-Trigger-After-Swap";
public const string Reswap = "HX-Reswap";
public const string Retarget = "HX-Retarget";
public const string ReplaceUrl = "HX-Replace-Url";

public static string[] All { get; } = new[]
{
PushUrl, Location, Redirect, Refresh, Trigger, TriggerAfterSettle, TriggerAfterSwap, Reswap, Retarget, ReplaceUrl
Location,
PushUrl,
Redirect,
Refresh,
ReplaceUrl,
Reswap,
Retarget,
Reselect,
Trigger,
TriggerAfterSettle,
TriggerAfterSwap,
};
}

Expand Down Expand Up @@ -88,6 +100,19 @@ public HtmxResponseHeaders Reswap(string value)
_headers[Keys.Reswap] = value;
return this;
}

/// <summary>
/// a CSS selector that allows you to choose which part of the response is used to be swapped in.
/// Overrides an existing hx-select on the triggering element
/// See https://htmx.org/attributes/hx-select/ for hx-select behavior.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public HtmxResponseHeaders Reselect(string value)
{
_headers[Keys.Reselect] = value;
return this;
}

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