Skip to content

Added support for Alerts Headers #44 #47

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

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,29 @@
</code>
</td>
</tr>
<tr>
<th>Warning Dismissible</th>
<td>
<alert alert-color="Warning" dismissible="true">
This is a dismissible warning alert!
</alert>
</td>
<td>
<code>
&lt;alert alert-color=&quot;Warning&quot; dismissible=&quot;false&quot;&gt;
This is a dismissible warning alert!
&lt;/alert&gt;
</code>
</td>
</tr>
<tr>
<th>Warning Dismissible</th>
<td>
<alert alert-color="Warning" dismissible="true">
This is a dismissible warning alert!
</alert>
</td>
<td>
<code>
&lt;alert alert-color=&quot;Warning&quot; dismissible=&quot;false&quot;&gt;
This is a dismissible warning alert!
&lt;/alert&gt;
</code>
</td>
</tr>
<tr>
<th>Alert With Header</th>
<td>
<alert alert-color="Info" heading-text="This is a Header">
Look at me! I have a header!
</alert>
</td>
</tr>
</tbody>
</table>

Expand Down
18 changes: 17 additions & 1 deletion src/AspNetCore.Utilities.Bootstrap5TagHelpers/AlertTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class AlertTagHelper : TagHelper
/// </summary>
public bool Dismissible { get; set; } = false;

/// <summary>
/// If supplied this will render as a heading inside the alert.
/// </summary>
public string? HeadingText { get; set; }

/// <summary>
/// Processes the tag helper
/// </summary>
Expand All @@ -50,10 +55,21 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.AddClass("fade", HtmlEncoder.Default);
output.AddClass("show", HtmlEncoder.Default);
}

output.Attributes.Add("role", "alert");

if (!string.IsNullOrEmpty(HeadingText))
{
var headingBuilder = new TagBuilder("h4");
headingBuilder.AddCssClass("alert-heading");
headingBuilder.InnerHtml.Append(HeadingText);
output.PreContent.AppendHtml(headingBuilder);
}

if (!Dismissible)
{
return;
}

var buttonBuilder = new TagBuilder("button");
buttonBuilder.Attributes.Add("type", "button");
Expand All @@ -62,7 +78,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
buttonBuilder.Attributes.Add("aria-label", "Close");

//Get existing content
var existing = await output.GetChildContentAsync();
TagHelperContent existing = await output.GetChildContentAsync();
output.Content.AppendHtml(existing.GetContent());
output.Content.AppendHtml(buttonBuilder);
}
Expand Down