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

feat: adds missing reswap modifier and adds reswap builder along with StopPolling #23

Merged
merged 8 commits into from
May 1, 2024
43 changes: 41 additions & 2 deletions src/Htmxor/Http/HtmxResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,25 @@ public HtmxResponse ReplaceUrl(string url)
return this;
}

/// <summary>
/// Allows you to specify how the response will be swapped.
/// </summary>
/// <param name="modifier">The hx-swap attributes supports modifiers for changing the behavior of the swap.</param>
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
public HtmxResponse Reswap(string modifier)
{
headers[HtmxResponseHeaderNames.Reswap] = modifier;

return this;
}

/// <summary>
/// Allows you to specify how the response will be swapped.
/// </summary>
/// <param name="swapStyle"></param>
/// <param name="modifier">The hx-swap attributes supports modifiers for changing the behavior of the swap.</param>
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
public HtmxResponse Reswap(SwapStyle swapStyle)
public HtmxResponse Reswap(SwapStyle swapStyle, string? modifier = null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be compatible with hx-swap="show:none"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Took a dive into the htmx source. It seems like modifiers can be used without necessarily specifying a swap style. The swap specification is loaded over top of the defaults. It defaults swapStyle to innerHTML if boosted, otherwise whatever the configured defaultSwapStyle is. Then reswap can come in and change the swapstyle and/or add modifiers. Modifiers can be used independently of Swapstyle.

I'm going to overload Reswap with just a string parameter to allow modifiers only.

{
AssertIsHtmxRequest();

Expand All @@ -159,11 +172,26 @@ public HtmxResponse Reswap(SwapStyle swapStyle)
_ => throw new SwitchExpressionException(swapStyle),
};

headers[HtmxResponseHeaderNames.Reswap] = style;
var value = modifier != null ? $"{style} {modifier}" : style;

headers[HtmxResponseHeaderNames.Reswap] = value;

return this;
}

/// <summary>
/// Allows you to specify how the response will be swapped.
/// </summary>
/// <param></param>
/// <param name="swapStyle"></param>
/// <returns></returns>
public HtmxResponse Reswap(SwapStyleBuilder swapStyle)
{
var (style, modifier) = swapStyle.Build();

return style is null ? Reswap(modifier) : Reswap((SwapStyle)style, modifier);
}

/// <summary>
/// A CSS selector that updates the target of the content update to a different element on the page.
/// </summary>
Expand Down Expand Up @@ -192,6 +220,17 @@ public HtmxResponse Reselect(string selector)
return this;
}

/// <summary>
/// Sets response code to stop polling
/// </summary>
/// <returns></returns>
public HtmxResponse StopPolling()
{
context.Response.StatusCode = HtmxStatusCodes.StopPolling;

return this;
}
egil marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Allows you to trigger client-side events.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Htmxor/Http/HtmxStatusCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Htmxor.Http;

public static class HtmxStatusCodes
{
public static readonly int StopPolling = 286;
}
Loading