Skip to content

Instantly share code, notes, and snippets.

@hypeartist
Last active October 7, 2022 06:43
Show Gist options
  • Save hypeartist/ad710ee8efe309a22d1ac4a2af999be6 to your computer and use it in GitHub Desktop.
Save hypeartist/ad710ee8efe309a22d1ac4a2af999be6 to your computer and use it in GitHub Desktop.
Small IJSRuntime extension method to load CSS file at runtime
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