diff --git a/Heron.MudCalendar.UnitTests.Viewer/Shared/MainLayout.razor b/Heron.MudCalendar.UnitTests.Viewer/Shared/MainLayout.razor index cbb9613..1f291ab 100644 --- a/Heron.MudCalendar.UnitTests.Viewer/Shared/MainLayout.razor +++ b/Heron.MudCalendar.UnitTests.Viewer/Shared/MainLayout.razor @@ -1,7 +1,17 @@ @inherits LayoutComponentBase + + + + @Body + +@code { + + private string _description = "Description set by code"; + +} \ No newline at end of file diff --git a/Heron.MudCalendar/Components/MudCalendar.razor b/Heron.MudCalendar/Components/MudCalendar.razor index 72245ee..f7e908b 100644 --- a/Heron.MudCalendar/Components/MudCalendar.razor +++ b/Heron.MudCalendar/Components/MudCalendar.razor @@ -1,32 +1,18 @@ @namespace Heron.MudCalendar @inherits MudComponentBase @using CategoryTypes = Heron.MudCalendar.Attributes.CategoryTypes +@using Microsoft.JSInterop +@inject IJSRuntime JsRuntime @Render @code { - /// - /// Renders the html page head element. - /// - protected virtual RenderFragment RenderHead => - @ - @RenderHeaderContent - ; - - /// - /// Renders the contents of the head element. - /// - protected virtual RenderFragment RenderHeaderContent => - @; - /// /// Renders the component. /// protected virtual RenderFragment Render => __builder => { - RenderHead(__builder); -
diff --git a/Heron.MudCalendar/Components/MudCalendar.razor.cs b/Heron.MudCalendar/Components/MudCalendar.razor.cs index 74160d1..85db28c 100644 --- a/Heron.MudCalendar/Components/MudCalendar.razor.cs +++ b/Heron.MudCalendar/Components/MudCalendar.razor.cs @@ -1,3 +1,4 @@ +using Heron.MudCalendar.Services; using Microsoft.AspNetCore.Components; using MudBlazor.Utilities; using MudBlazor; @@ -8,6 +9,8 @@ namespace Heron.MudCalendar; public partial class MudCalendar : MudComponentBase { + private JsService? _jsService; + /// /// The higher the number, the heavier the drop-shadow. 0 for no shadow. /// @@ -255,6 +258,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender) { //await DateRangeChanged.InvokeAsync(new CalendarDateRange(CurrentDay, View)); await ChangeDateRange(); + + await SetLinks(); } } @@ -305,6 +310,17 @@ protected virtual Task OnPreviousClicked() return ChangeDateRange(); } + private async Task SetLinks() + { + // Check if link is already set + _jsService ??= new JsService(JsRuntime); + var head = await _jsService.GetHeadContent(); + if (!string.IsNullOrEmpty(head) && head.Contains("Heron.MudCalendar.min.css")) return; + + // Add link + await _jsService.AddLink("_content/Heron.MudCalendar/Heron.MudCalendar.min.css", "stylesheet"); + } + private Task DatePickerDateChanged(DateTime? dateTime) { PickerDate = dateTime; diff --git a/Heron.MudCalendar/Services/JsService.cs b/Heron.MudCalendar/Services/JsService.cs index 1046efa..c7620eb 100644 --- a/Heron.MudCalendar/Services/JsService.cs +++ b/Heron.MudCalendar/Services/JsService.cs @@ -1,3 +1,4 @@ +using System.Runtime.InteropServices; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; @@ -18,6 +19,18 @@ public async Task Scroll(ElementReference element, int top) var module = await _moduleTask.Value; await module.InvokeVoidAsync("scroll", element, top); } + + public async Task GetHeadContent() + { + var module = await _moduleTask.Value; + return await module.InvokeAsync("getHeadContent"); + } + + public async Task AddLink(string href, string rel) + { + var module = await _moduleTask.Value; + await module.InvokeVoidAsync("addLink", href, rel); + } public async ValueTask DisposeAsync() { diff --git a/Heron.MudCalendar/wwwroot/Heron.MudCalendar.js b/Heron.MudCalendar/wwwroot/Heron.MudCalendar.js index 6e31c39..206b96e 100644 --- a/Heron.MudCalendar/wwwroot/Heron.MudCalendar.js +++ b/Heron.MudCalendar/wwwroot/Heron.MudCalendar.js @@ -1,3 +1,14 @@ export function scroll(element, top) { element.scrollTo(0, top); +} + +export function getHeadContent() { + return document.head.innerHTML; +} + +export function addLink(href, rel) { + const link = document.createElement("link"); + link.href = href; + link.rel = rel; + document.head.appendChild(link); } \ No newline at end of file