-
Notifications
You must be signed in to change notification settings - Fork 4
Upgrade HeroIcons library for .NET 9 auto render support #4
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
Open
TaylorWatson
wants to merge
1
commit into
main
Choose a base branch
from
codex/upgrade-to-.net-9-and-enable-auto-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 19 additions & 22 deletions
41
src/Samples/WASM-PWA-Sample/WASM-PWA-Sample/WASM-PWA-Sample.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| <RestoreSources>$(RestoreSources);/Users/{REPLACEWITHUSER}/Developer/TailBlazor/HeroIcons/src/TailBlazor.HeroIcons/bin/release;https://api.nuget.org/v3/index.json</RestoreSources> | ||
|
|
||
| <ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest> | ||
|
|
||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.3" /> | ||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.3" PrivateAssets="all" /> | ||
| <PackageReference Include="System.Net.Http.Json" Version="5.0.0" /> | ||
| <PackageReference Include="TailBlazor.HeroIcons" Version="1.1.6" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" /> | ||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.0" PrivateAssets="all" /> | ||
| <PackageReference Include="System.Net.Http.Json" Version="9.0.0" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="../../../TailBlazor.HeroIcons/TailBlazor.HeroIcons.csproj" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" /> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,74 +1,75 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| using System.Xml.Linq; | ||
| using Microsoft.AspNetCore.Components; | ||
| using System.Threading.Tasks; | ||
| using System.Threading; | ||
| using System.IO; | ||
|
|
||
| namespace TailBlazor.HeroIcons | ||
| { | ||
| public class TailBlazorHeroIconBase : ComponentBase | ||
| { | ||
|
|
||
| [Inject] protected NavigationManager NavigationManager { get; set; } | ||
| private const string ResourcePrefix = "TailBlazor.HeroIcons.icons."; | ||
| private static readonly Assembly IconAssembly = typeof(TailBlazorHeroIconBase).Assembly; | ||
|
|
||
| [Parameter] public HeroIcon Icon { get; set; } | ||
| [Parameter] public int Width { get; set; } = 24; | ||
| [Parameter] public int Height { get; set; } = 24; | ||
| [Parameter] public int StrokeWidth { get; set; } = 2; | ||
| [Parameter] public string Class { get; set; } | ||
| [Parameter] public string? Class { get; set; } | ||
| [Parameter] public IconStyle IconStyle { get; set; } = IconStyle.Outline; | ||
| [Parameter] public bool EnableComments { get; set; } = false; | ||
| [Parameter] public bool EnableComments { get; set; } | ||
|
|
||
| protected string _svgIcon = string.Empty; | ||
| protected string _svgIconComment = string.Empty; | ||
|
|
||
| protected string _classStroke = "stroke-current "; | ||
| protected string _svgIcon = ""; | ||
| protected string _svgIconComment = ""; | ||
| private string _classStroke = string.Empty; | ||
|
|
||
| protected override async Task OnInitializedAsync() | ||
| protected override async Task OnParametersSetAsync() | ||
| { | ||
| _classStroke += Class; | ||
| _classStroke = "stroke-current"; | ||
|
|
||
| if (EnableComments) | ||
| if (!string.IsNullOrWhiteSpace(Class)) | ||
| { | ||
| // HeroIcon: annotation (style: outlined, size: 64x64, stroke (colour): text-grey-500, stroke-width: 2) | ||
| _svgIconComment = $"<!-- TailBlazor.HeroIcon: {EnumExtension.GetEnumDescription(Icon)} (style: {IconStyle.Outline.ToString()}, size: {Height}x{Width}, stroke (colour): {Class}, stroke-width: {StrokeWidth}) -->"; | ||
| _classStroke += $" {Class}"; | ||
| } | ||
|
|
||
| var baseUri = NavigationManager.BaseUri; | ||
| var token = new CancellationToken(); | ||
|
|
||
| var document = await Task.Run(() => { | ||
| return XDocument.Load($"{baseUri}_content/TailBlazor.HeroIcons/icons/{EnumExtension.GetEnumDescription(IconStyle)}/{EnumExtension.GetEnumDescription(Icon)}.svg"); | ||
| }); | ||
| if (EnableComments) | ||
| { | ||
| _svgIconComment = $"<!-- TailBlazor.HeroIcon: {EnumExtension.GetEnumDescription(Icon)} (style: {EnumExtension.GetEnumDescription(IconStyle)}, size: {Height}x{Width}, stroke (colour): {Class}, stroke-width: {StrokeWidth}) -->"; | ||
| } | ||
| else | ||
| { | ||
| _svgIconComment = string.Empty; | ||
| } | ||
|
|
||
| var iconStyleName = EnumExtension.GetEnumDescription(IconStyle); | ||
| var iconName = EnumExtension.GetEnumDescription(Icon); | ||
| var resourceName = $"{ResourcePrefix}{iconStyleName}.{iconName}.svg"; | ||
|
|
||
| //XDocument document = | ||
| // await XDocument.LoadAsync($"{baseUri}_content/TailBlazor.HeroIcons/icons/{EnumExtension.GetEnumDescription(IconStyle)}/{EnumExtension.GetEnumDescription(Icon)}.svg", LoadOptions.None, token); | ||
| await using var resourceStream = IconAssembly.GetManifestResourceStream(resourceName) | ||
| ?? throw new InvalidOperationException($"Could not find embedded icon resource '{resourceName}'."); | ||
|
|
||
| XElement svg_Element = document.Root; | ||
| var document = await XDocument.LoadAsync(resourceStream, LoadOptions.PreserveWhitespace, CancellationToken.None); | ||
|
|
||
| svg_Element.SetAttributeValue("width", Width); | ||
| svg_Element.SetAttributeValue("height", Height); | ||
| svg_Element.SetAttributeValue("stroke", ""); | ||
| svg_Element.SetAttributeValue("class", _classStroke); | ||
| if (document.Root is null) | ||
| { | ||
| throw new InvalidOperationException($"The SVG document for icon '{iconName}' is empty."); | ||
| } | ||
|
|
||
| IEnumerable<XElement> descendants = from path in svg_Element.Descendants() select path; | ||
| document.Root.SetAttributeValue("width", Width); | ||
| document.Root.SetAttributeValue("height", Height); | ||
| document.Root.SetAttributeValue("stroke", null); | ||
| document.Root.SetAttributeValue("class", _classStroke); | ||
|
|
||
| foreach (XElement path in descendants) | ||
| foreach (var path in document.Root.Descendants()) | ||
| { | ||
| if (path.Attribute("stroke-width") != null) | ||
| { | ||
| path.SetAttributeValue("stroke-width", StrokeWidth); | ||
| } | ||
| if (path.Attribute("fill") != null) | ||
| { | ||
| path.SetAttributeValue("fill", StrokeWidth); | ||
| } | ||
| } | ||
|
|
||
| _svgIcon = svg_Element.ToString(); | ||
| _svgIcon = document.Root.ToString(SaveOptions.DisableFormatting); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The project now embeds the SVGs with
LogicalNameconstructed from%(RecursiveDir), which preserves the directory separator. That yields resource names likeTailBlazor.HeroIcons.icons.outline/academic-cap.svg, but the runtime lookup inTailBlazorHeroIconBaserequestsTailBlazor.HeroIcons.icons.{style}.{icon}.svg(dots instead of/). As a resultGetManifestResourceStreamnever finds any icon and the component throws theInvalidOperationExceptionfor every render. Replace the slash in%(RecursiveDir)with a dot or adjust the lookup string so the resource names match.Useful? React with 👍 / 👎.