Skip to content

Commit

Permalink
Merge pull request #8 from xt0rted/options
Browse files Browse the repository at this point in the history
Add a setting to include an html comment with icon info
  • Loading branch information
xt0rted authored Oct 28, 2020
2 parents 092cc4a + e781a41 commit 48f6ae3
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 21 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ In your `_ViewImports.cshtml` add:
@addTagHelper *, HeroiconsTagHelper
```

In your `Startup.cs` add:

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddHeroicons(Configuration);
}
```

In your `appsettings.json` add:

```json
{
"Heroicons": {
"IncludeComments": true
}
}

```

## Settings

### IncludeComments

Setting this to `true` will add an html comment before the svg tag with the style and name of the icon to help make development/debugging easier.

## Usage

There are two versions of the tag helper which are used to pick between the `outline` and `solid` icon styles.
Expand Down
2 changes: 1 addition & 1 deletion generator/IconExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace IconSourceGenerator
namespace IconSourceGenerator
{
using System;
using System.Linq;
Expand Down
4 changes: 3 additions & 1 deletion generator/IconSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace IconSourceGenerator
namespace IconSourceGenerator
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -117,6 +117,7 @@ public static class IconList
source.AppendLine(" case IconSymbol." + icon.ClassName + ":");
source.AppendLine(" return new Icon");
source.AppendLine(" {");
source.Append(" Name = \"").Append(icon.Name).AppendLine("\",");
source.Append(" Path = \"").Append(path.Replace("\"", "\\\"")).AppendLine("\",");
source.Append(" ViewBox = \"").Append(viewBox).AppendLine("\",");
source.AppendLine(" };");
Expand All @@ -135,6 +136,7 @@ public static class IconList
public class Icon
{
public string Name { get; set; }
public string Path { get; set; }
public string ViewBox { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion generator/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace IconSourceGenerator
namespace IconSourceGenerator
{
internal static class StringExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion sample/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Sample.Controllers
namespace Sample.Controllers
{
using Microsoft.AspNetCore.Mvc;

Expand Down
2 changes: 1 addition & 1 deletion sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Sample
namespace Sample
{
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
Expand Down
2 changes: 1 addition & 1 deletion sample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
Expand Down
4 changes: 3 additions & 1 deletion sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Sample
namespace Sample
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -19,6 +19,8 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();

services.AddHeroicons(Configuration);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion sample/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<div>
<div class="p-4">
<div class="flex justify-center items-end">
<heroicon-outline icon="Sun" class="w-6 text-red-500" />
Expand Down
2 changes: 1 addition & 1 deletion sample/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down
2 changes: 1 addition & 1 deletion sample/Views/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@addTagHelper *, HeroiconsTagHelper
@addTagHelper *, HeroiconsTagHelper
3 changes: 3 additions & 0 deletions sample/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"Heroicons": {
"IncludeComments": true
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
4 changes: 2 additions & 2 deletions sample/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"AllowedHosts": "*",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
}
11 changes: 11 additions & 0 deletions src/HeroiconOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Tailwind.Heroicons
{
public class HeroiconOptions
{
/// <summary>
/// Add an html comment before the svg tag with the style and name of the icon.
/// </summary>
/// <remarks>This is off by default.</remarks>
public bool IncludeComments { get; set; }
}
}
31 changes: 31 additions & 0 deletions src/HeroiconsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Microsoft.Extensions.DependencyInjection
{
using System;

using Microsoft.Extensions.Configuration;

using Tailwind.Heroicons;

public static class HeroiconsExtensions
{
public static IServiceCollection AddHeroicons(this IServiceCollection services, IConfiguration configuration)
{
if (services is null) throw new ArgumentNullException(nameof(services));
if (configuration is null) throw new ArgumentNullException(nameof(configuration));

services.Configure<HeroiconOptions>(configuration.GetSection("Heroicons"));

return services;
}

public static IServiceCollection AddHeroicons(this IServiceCollection services, Action<HeroiconOptions> configureOptions)
{
if (services is null) throw new ArgumentNullException(nameof(services));
if (configureOptions is null) throw new ArgumentNullException(nameof(configureOptions));

services.Configure(configureOptions);

return services;
}
}
}
23 changes: 20 additions & 3 deletions src/IconTagHelper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
namespace Tailwind.Heroicons
namespace Tailwind.Heroicons
{
using System;

using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Options;

[HtmlTargetElement("heroicon-outline", TagStructure = TagStructure.WithoutEndTag)]
[HtmlTargetElement("heroicon-solid", TagStructure = TagStructure.WithoutEndTag)]
public class IconTagHelper : TagHelper
{
private readonly HeroiconOptions _settings;

public IconTagHelper(IOptions<HeroiconOptions> settings)
{
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
}

[HtmlAttributeName("icon")]
public IconSymbol Icon { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (output == null) throw new ArgumentNullException(nameof(output));
if (context is null) throw new ArgumentNullException(nameof(context));
if (output is null) throw new ArgumentNullException(nameof(output));

var isSolid = context.TagName.Equals("heroicon-solid", StringComparison.OrdinalIgnoreCase);

Expand All @@ -40,6 +48,15 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
output.Attributes.Add("viewbox", icon.ViewBox);

output.Content.AppendHtml(icon.Path);

if (_settings.IncludeComments)
{
output.PreElement.AppendHtml("<!-- Heroicon name: ");
output.PreElement.AppendHtml(isSolid ? "solid" : "outline");
output.PreElement.AppendHtml(" ");
output.PreElement.Append(icon.Name);
output.PreElement.AppendHtml(" -->");
}
}
}
}
2 changes: 1 addition & 1 deletion test/IconListTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Tailwind.Heroicons
namespace Tailwind.Heroicons
{
using Shouldly;

Expand Down
61 changes: 56 additions & 5 deletions test/IconTagHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
namespace Tailwind.Heroicons
namespace Tailwind.Heroicons
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Options;

using Shouldly;

Expand All @@ -19,7 +20,8 @@ public void Should_set_svg_attributes()
var context = MakeTagHelperContext(tagName: "heroicon-outline");
var output = MakeTagHelperOutput(tagName: "heroicon-outline");

var helper = new IconTagHelper();
var options = Options.Create(new HeroiconOptions());
var helper = new IconTagHelper(options);

// When
helper.Process(context, output);
Expand All @@ -41,7 +43,8 @@ public void Should_output_outline_bell_icon(string tagName)
var context = MakeTagHelperContext(tagName);
var output = MakeTagHelperOutput(tagName);

var helper = new IconTagHelper
var options = Options.Create(new HeroiconOptions());
var helper = new IconTagHelper(options)
{
Icon = IconSymbol.Bell
};
Expand All @@ -66,7 +69,8 @@ public void Should_output_solid_bell_icon(string tagName)
var context = MakeTagHelperContext(tagName);
var output = MakeTagHelperOutput(tagName);

var helper = new IconTagHelper
var options = Options.Create(new HeroiconOptions());
var helper = new IconTagHelper(options)
{
Icon = IconSymbol.Bell
};
Expand Down Expand Up @@ -101,7 +105,8 @@ public void Should_apply_custom_attributes(string attributeName, string attribut
{ attributeName, attributeValue },
});

var helper = new IconTagHelper
var options = Options.Create(new HeroiconOptions());
var helper = new IconTagHelper(options)
{
Icon = IconSymbol.Bell
};
Expand All @@ -113,6 +118,52 @@ public void Should_apply_custom_attributes(string attributeName, string attribut
output.Attributes.ShouldContain(new TagHelperAttribute(attributeName, attributeValue));
}

[Fact]
public void Should_not_include_html_comment_when_IncludeComments_is_false()
{
// Given
var context = MakeTagHelperContext(tagName: "heroicon-outline");
var output = MakeTagHelperOutput(tagName: "heroicon-outline");

var options = Options.Create(new HeroiconOptions
{
IncludeComments = false,
});
var helper = new IconTagHelper(options)
{
Icon = IconSymbol.Bell,
};

// When
helper.Process(context, output);

// Then
output.PreElement.GetContent().ShouldBeEmpty();
}

[Fact]
public void Should_include_html_comment_when_IncludeComments_is_true()
{
// Given
var context = MakeTagHelperContext(tagName: "heroicon-outline");
var output = MakeTagHelperOutput(tagName: "heroicon-outline");

var options = Options.Create(new HeroiconOptions
{
IncludeComments = true,
});
var helper = new IconTagHelper(options)
{
Icon = IconSymbol.Bell,
};

// When
helper.Process(context, output);

// Then
output.PreElement.GetContent().ShouldBe("<!-- Heroicon name: outline bell -->");
}

private static TagHelperContext MakeTagHelperContext(string tagName, TagHelperAttributeList attributes = null)
{
attributes ??= new TagHelperAttributeList();
Expand Down

0 comments on commit 48f6ae3

Please sign in to comment.