Skip to content

Commit d0e3e73

Browse files
authoredMay 1, 2024··
feat: adds missing reswap modifier and adds reswap builder along with StopPolling (#23)
1 parent 9daf7ae commit d0e3e73

File tree

5 files changed

+900
-2
lines changed

5 files changed

+900
-2
lines changed
 

‎src/Htmxor/Http/HtmxResponse.cs

+41-2
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,25 @@ public HtmxResponse ReplaceUrl(string url)
137137
return this;
138138
}
139139

140+
/// <summary>
141+
/// Allows you to specify how the response will be swapped.
142+
/// </summary>
143+
/// <param name="modifier">The hx-swap attributes supports modifiers for changing the behavior of the swap.</param>
144+
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
145+
public HtmxResponse Reswap(string modifier)
146+
{
147+
headers[HtmxResponseHeaderNames.Reswap] = modifier;
148+
149+
return this;
150+
}
151+
140152
/// <summary>
141153
/// Allows you to specify how the response will be swapped.
142154
/// </summary>
143155
/// <param name="swapStyle"></param>
156+
/// <param name="modifier">The hx-swap attributes supports modifiers for changing the behavior of the swap.</param>
144157
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
145-
public HtmxResponse Reswap(SwapStyle swapStyle)
158+
public HtmxResponse Reswap(SwapStyle swapStyle, string? modifier = null)
146159
{
147160
AssertIsHtmxRequest();
148161

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

162-
headers[HtmxResponseHeaderNames.Reswap] = style;
175+
var value = modifier != null ? $"{style} {modifier}" : style;
176+
177+
headers[HtmxResponseHeaderNames.Reswap] = value;
163178

164179
return this;
165180
}
166181

182+
/// <summary>
183+
/// Allows you to specify how the response will be swapped.
184+
/// </summary>
185+
/// <param></param>
186+
/// <param name="swapStyle"></param>
187+
/// <returns></returns>
188+
public HtmxResponse Reswap(SwapStyleBuilder swapStyle)
189+
{
190+
var (style, modifier) = swapStyle.Build();
191+
192+
return style is null ? Reswap(modifier) : Reswap((SwapStyle)style, modifier);
193+
}
194+
167195
/// <summary>
168196
/// A CSS selector that updates the target of the content update to a different element on the page.
169197
/// </summary>
@@ -192,6 +220,17 @@ public HtmxResponse Reselect(string selector)
192220
return this;
193221
}
194222

223+
/// <summary>
224+
/// Sets response code to stop polling
225+
/// </summary>
226+
/// <returns></returns>
227+
public HtmxResponse StopPolling()
228+
{
229+
context.Response.StatusCode = HtmxStatusCodes.StopPolling;
230+
231+
return this;
232+
}
233+
195234
/// <summary>
196235
/// Allows you to trigger client-side events.
197236
/// </summary>

‎src/Htmxor/Http/HtmxStatusCodes.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Htmxor.Http;
2+
3+
public static class HtmxStatusCodes
4+
{
5+
public static readonly int StopPolling = 286;
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.