Last active
October 7, 2022 06:43
-
-
Save hypeartist/ad710ee8efe309a22d1ac4a2af999be6 to your computer and use it in GitHub Desktop.
Small IJSRuntime extension method to load CSS file at runtime
This file contains 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
public static class Utils | |
{ | |
public static async ValueTask LoadCss(this IJSRuntime jsRuntime, string cssUrl) | |
{ | |
await jsRuntime.InvokeVoidAsync("eval",/*lang=javascript*/$$""" | |
import(URL.createObjectURL(new File([`import sheet from '{{cssUrl}}' assert { type: 'css' }; document.adoptedStyleSheets = [sheet];`], `./{{Guid.NewGuid()}}.js`, { type: "text/javascript" }))).then(_=>_); | |
"""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment