Skip to content
Open
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
20 changes: 18 additions & 2 deletions src/React.AspNet/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -193,13 +194,28 @@ public static IHtmlString ReactGetScriptPaths(this IHtmlHelper htmlHelper, IUrlH
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="urlHelper">Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file.</param>
/// <param name="lazy">Specifies if lazy style load technique should be used</param>
/// <returns></returns>
public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null)
public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null, bool lazy = false)
{
return new HtmlString(string.Join("", Environment.GetStylePaths()
return lazy
? GetStylePathsLazy(urlHelper)
: GetStylePaths(urlHelper);
}

private static IHtmlString GetStylePaths(IUrlHelper urlHelper = null)
{
return new HtmlString(string.Concat(Environment.GetStylePaths()
.Select(stylePath => $"<link rel=\"stylesheet\" href=\"{(urlHelper == null ? stylePath : urlHelper.Content(stylePath))}\" />")));
}

private static IHtmlString GetStylePathsLazy(IUrlHelper urlHelper = null)
{
var elements = Environment.GetStylePaths()
.Select(stylePath => $"createStyleElem('{(urlHelper == null ? stylePath : urlHelper.Content(stylePath))}');");
return new HtmlString($"<script>(function(){{function createStyleElem(link){{var e=document.createElement('link');e.href=link;e.rel='stylesheet';e.type='text/css';document.getElementsByTagName('head')[0].appendChild(e);}}{ string.Concat(elements) }}})();</script>");
}

private static IHtmlString RenderToString(Action<StringWriter> withWriter)
{
var stringWriter = _sharedStringWriter;
Expand Down
12 changes: 12 additions & 0 deletions tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,5 +315,17 @@ public void ReactGetStylePaths()

Assert.Equal("<link rel=\"stylesheet\" href=\"/dist/vendor.css\" /><link rel=\"stylesheet\" href=\"/dist/app.css\" />", result.ToHtmlString());
}

[Fact]
public void ReactGetStylePathsLazy()
{
var environment = ConfigureMockEnvironment();

environment.Setup(x => x.GetStylePaths()).Returns(new[] { "/dist/vendor.css", "/dist/app.css" });

var result = HtmlHelperExtensions.ReactGetStylePaths(null, lazy: true);

Assert.Equal("<script>(function(){function createStyleElem(link){var e=document.createElement('link');e.href=link;e.rel='stylesheet';e.type='text/css';document.getElementsByTagName('head')[0].appendChild(e);}createStyleElem('/dist/vendor.css');createStyleElem('/dist/app.css');})();</script>", result.ToHtmlString());
}
}
}