docs: missing clear instructions/example on how to set design tokens application wide #2075
-
First of all, thank you for yet another great addition to the Blazor ecosystem. I am looking forward to use Fluent UI Blazor in my projects. I looked at the following sources for clear instructions on how to set design tokens at application level, and also went through design-tokens-related issues in this github repo:
It is still not clear for me. I like the concept of design tokens, which - to my knowledge - are implemented as CSS Variables in the underlying Fluent UI Web Components solution. Being able to set all the design tokens gives us very considerable control of the application's style. My particular question below. Is it viable and recommended to simply:
:root {
--foreground-color: #000000;
--background-color: #FFFFFF;
--accent-fill-rest: #0078D4;
--accent-fill-hover: #106EBE;
--accent-fill-active: #005A9E;
--accent-fill-focus: #005A9E;
--control-corner-radius: 50;
}
html
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Blazor App</title>
<link href="_content/MyApp/css/styles.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
</head>
<body>
<app>Loading...</app>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html> I tried it and in my experiment it worked but required the addition of Is there any thing in how Fluent UI Blazor works that does not advise setting the CSS Variables as above? If the above is not recommended, how exactly and where in a Blazor Wasm app, for example, should we set the values of Design Tokens for site-wide application? All the examples in the docs seem to focus on changes at component level. But how can we set them at application level in a direct straight forward way? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Not all design tokens are 'just' CSS variables. The The first code example at https://www.fluentui-blazor.net/DesignTokens#using-design-tokens-from-code explicitly mentions how you can use a token 'globally':
To answer your questions:
You can look at the code for the demo site on how we use the tokens to set application wide tokens as well. See also #2070 |
Beta Was this translation helpful? Give feedback.
-
For the sake of completion. I am using a Razor Class Library (RCL) to share UI components between platforms (Blazor WebAssembly for Web, Blazor Hybrid for Native). The objective is for this RCL to serve as an internal UI Kit, in a way that abstracts consuming cross-platform apps from knowing other libraries but the RCL. I was unsure of how to set application-wide defaults for the Design Tokens that rule the overall style of the Fluent UI Blazor Components. After multiple surprisingly fast clarifications from @vnbaaij, I did the following. In the Razor Class LibraryStep 1 (specific to my use-case): I created my own component Button.razor, wrapping @namespace MyRcl.Components
<FluentButton Disabled="@Disabled" @attributes="UnmatchedValues">
@ChildContent
</FluentButton>
@code {
[Parameter]
[EditorRequired]
public RenderFragment ChildContent { get; set; }
[Parameter]
public bool Disabled { get; set; }
[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object> UnmatchedValues { get; set; }
// other parameters to be exposed
} Step 2: I created razor component DesignTokens.razor with the following code: @using Microsoft.FluentUI.AspNetCore.Components.DesignTokens
@namespace MyRcl.Components
<!-- Set the default theme using builtin FluentDesignToken machinery -->
<!-- This is appropriate for the color theme, not for general control of tokens -->
<!-- See: https://www.fluentui-blazor.net/DesignTheme -->
<FluentDesignTheme StorageName="theme" />
<script src="_content/Microsoft.FluentUI.AspNetCore.Components/js/loading-theme.js" type="text/javascript"></script>
<loading-theme storage-name="theme"></loading-theme>
@code {
[Inject]
private ControlCornerRadius ControlCornerRadius { get; set; } = default!;
protected override async Task OnAfterRenderAsync(bool firstRender) {
if (firstRender) {
// WithDefault() allows us to control any exposed design token, not just the color theme
await ControlCornerRadius.WithDefault(50); // <-- setting the default design token value here
// other tokens to be tested
}
}
} Step 3 (specific to my use-case): To have the RCL inject Fluent UI Blazor Components in order to abstract the latter from consuming apps, I created a using Microsoft.Extensions.DependencyInjection;
using Microsoft.FluentUI.AspNetCore.Components;
namespace MyRcl.Components;
public static class ServiceCollectionExtensions {
public static IServiceCollection AddMyUIComponents(this IServiceCollection services) {
services.AddFluentUIComponents();
return services;
}
} In the consuming Blazor WebAssembly app, as an example:Step 4: I added the following line in the <link rel="stylesheet" href="_content/Microsoft.FluentUI.AspNetCore.Components/css/reboot.css"/> NOTE: one can abstract this by having the RCL directly supply Step 5 (specific to my use-case): I added the following line in Program.cs to inject the underlying components: builder.Services.AddMyUIComponents();
// In normal circumstances, one will simply add `builder.Services.AddFluentUIComponents();` Step 6: Finally, in App.razor, I added the <DesignTokens /> @* <-- add this *@
<MyApp Title="My UI Kit"
Assemblies="[typeof(App).Assembly]"
DefaultLayout="typeof(DefaultLayout)"
</MyApp> NOTE: this will fail if step 5 is missed. Conclusions
|
Beta Was this translation helpful? Give feedback.
For the sake of completion.
I am using a Razor Class Library (RCL) to share UI components between platforms (Blazor WebAssembly for Web, Blazor Hybrid for Native). The objective is for this RCL to serve as an internal UI Kit, in a way that abstracts consuming cross-platform apps from knowing other libraries but the RCL.
I was unsure of how to set application-wide defaults for the Design Tokens that rule the overall style of the Fluent UI Blazor Components.
After multiple surprisingly fast clarifications from @vnbaaij, I did the following.
In the Razor Class Library
Step 1 (specific to my use-case): I created my own component Button.razor, wrapping
FluentButton
:@namespace MyRcl.Components <