Skip to content

Commit a969e3c

Browse files
authored
Merge pull request #142 from microsoft/design-tokens
(feature) Design tokens
2 parents 512a74b + a290630 commit a969e3c

File tree

113 files changed

+1186
-590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1186
-590
lines changed

README.md

Lines changed: 116 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ To add the script from CDN use the following markup:
3838
The markup above always references the latest release of the components. When deploying to production, you will want to ship with a specific version. Here's an example of the markup for that:
3939

4040
```html
41-
4241
<script type="module" src="https://cdn.jsdelivr.net/npm/@fluentui/web-components@2.0.2/dist/web-components.min.js"></script>
4342
```
4443

@@ -60,7 +59,7 @@ Copy this to your `wwwroot/script` folder and reference it with a script tag as
6059

6160
> :notebook: **Note**
6261
>
63-
> If you are setting up Fluent UI Web Components on a Blazor Server project, you will need to escape the `@` character by repeating it in the source link. For more information check out the [Razor Pages syntax documentation](/aspnet/core/mvc/views/razor).
62+
> If you are setting up Fluent UI Web Components on a Blazor Server project, you will need to escape the `@` character by repeating it in the source link. For more information check out the [Razor Pages syntax documentation](https://docs.microsoft.com/aspnet/core/mvc/views/razor).
6463
6564
In your Program.cs file you need to add the following:
6665
```csharp
@@ -93,33 +92,133 @@ Here's a small example of a `FluentCard` with a `FluentButton` that uses the Flu
9392
```
9493
> :bulb: **Tip**
9594
>
96-
> You can add `@using Microsoft.Fast.Components.FluentUI` to namespace collection in `_Imports.razor`, so that you can avoid repeating it in every single razor page.
95+
> You can add `@using Microsoft.Fast.Components.FluentUI` to the namespace collection in `_Imports.razor`, so that you can avoid repeating it in every single razor page.
9796
9897

9998
### Configuring the Design System
10099

101-
The Fluent UI Web Components are built on FAST's Adaptive UI technology, which enables design customization and personalization, while automatically maintaining accessibility. This is accomplished through setting various "design tokens". The easiest way to accomplish this in Blazor is to wrap the entire UI in a `FluentDesignSystemProvider`. This special element has a number of properties you can set to configure the tokens to your desired settings. Here's an example of changing the "accent base color" and switching the system into dark mode (in the file `app.razor`):
100+
The Fluent UI Web Components are built on FAST's Adaptive UI technology, which enables design customization and personalization, while automatically maintaining accessibility. This is accomplished through setting various "Design Tokens". As of version 1.4 you can use all of the (160) individual Design Tokens, both from code as in a declarative way in your `.razor` pages. See https://docs.microsoft.com/en-us/fluent-ui/web-components/design-system/design-tokens for more information on how Design Tokens work
101+
102+
#### Option 1: Using Design Tokens from C# code
103+
104+
Given the following `.razor` page fragment:
105+
```html
106+
<FluentButton @ref="ref1" Appearance="Appearance.Filled">A button</FluentButton>
107+
<FluentButton @ref="ref2" Appearance="Appearance.Filled">Another button</FluentButton>
108+
<FluentButton @ref="ref3" Appearance="Appearance.Filled">And one more</FluentButton>
109+
<FluentButton @ref="ref4" Appearance="Appearance.Filled" @onclick=OnClick>Last button</FluentButton>
110+
111+
```
112+
You can use Design Tokens to manipulate the styles from C# code as follows:
113+
114+
```csharp
115+
[Inject]
116+
private BaseLayerLuminance BaseLayerLuminance { get; set; } = default!;
117+
118+
[Inject]
119+
private AccentBaseColor AccentBaseColor { get; set; } = default!;
120+
121+
[Inject]
122+
private BodyFont BodyFont { get; set; } = default!;
123+
124+
[Inject]
125+
private StrokeWidth StrokeWidth { get; set; } = default!;
126+
127+
[Inject]
128+
private ControlCornerRadius ControlCornerRadius { get; set; } = default!;
129+
130+
private FluentAnchor? ref1;
131+
private FluentButton? ref2;
132+
private FluentAnchor? ref3;
133+
private FluentButton? ref4;
134+
135+
protected override async Task OnAfterRenderAsync(bool firstRender)
136+
{
137+
if (firstRender)
138+
{
139+
//Set to dark mode
140+
await BaseLayerLuminance.SetValueFor(ref1!.Element, (float)0.15);
141+
142+
//Set to Excel color
143+
await AccentBaseColor.SetValueFor(ref2!.Element, "#185ABD".ToColor());
144+
145+
//Set the font
146+
await BodyFont.SetValueFor(ref3!.Element, "Comic Sans MS");
147+
148+
//Set 'border' width for ref4
149+
await StrokeWidth.SetValueFor(ref4!.Element, 7);
150+
//And change conrner radius as well
151+
await ControlCornerRadius.SetValueFor(ref4!.Element, 15);
152+
153+
StateHasChanged();
154+
}
155+
156+
}
157+
158+
public async Task OnClick()
159+
{
160+
//Remove the wide border
161+
await StrokeWidth.DeleteValueFor(ref4!.Element);
162+
}
163+
```
164+
As can be seen in the code above (with the `ref4.Element`), it is posible to apply multiple tokens to the same component.
165+
166+
For Design Tokens that work with a color value, it is needed to add the `ToColor()` extension method on the string value. This converts the string into a RGB value that the Design Token can operate with. Internally we are using the `System.Drawing.Color` struct for this and this means you can use all the available methods, operators, etc from that namespace in your code too.
167+
168+
> :notebook: **Note**
169+
>
170+
> The Design Tokens are manipulated through JavaScript interop working with an `ElementReference`. There is no JavaScript element until after the component is rendered. This means you can only work with the Design Tokens from code after the component has been rendered in `OnAfterRenderAsync` and not in any earlier lifecycle methods.
171+
172+
#### Option 2: Using Design Tokens as components
173+
The Design Tokens can also be used as components in a `.razor` page directely. It looks like this:
174+
175+
```html
176+
<BaseLayerLuminance Value="(float?)0.15">
177+
<FluentCard BackReference="@context">
178+
<div class="contents">
179+
Dark
180+
<FluentButton Appearance="Appearance.Accent">Accent</FluentButton>
181+
<FluentButton Appearance="Appearance.Stealth">Stealth</FluentButton>
182+
<FluentButton Appearance="Appearance.Outline">Outline</FluentButton>
183+
<FluentButton Appearance="Appearance.Lightweight">Lightweight</FluentButton>
184+
</div>
185+
</FluentCard>
186+
</BaseLayerLuminance>
187+
```
188+
189+
To make this work, a link needs to be created between the Design Token component and its child components. This is done with the `BackReference="@context"` construct.
190+
191+
> :notebook: **Note**
192+
>
193+
> Only one Design Token component at a time can be used this way. If you need to set more tokens, use the code approach as described in Option 1 above.
194+
195+
196+
#### Option 3: Using the `<FluentDesignSystemProvider>`
197+
The third way to customize the design in Blazor is to wrap the entire block you want to manipulate in a `<FluentDesignSystemProvider>`. This special element has a number of properties you can set to configure a subset of the tokens. **Not all tokens are available/supported** and we recommend this to only be used as a fall-back mechanism. The preferred mehod of working with the desgn tokens is to manipulate them from code as described in option 1.
198+
199+
Here's an example of changing the "accent base color" and switching the system into dark mode (in the file `app.razor`):
102200

103201
```html
104202
<FluentDesignSystemProvider AccentBaseColor="#464EB8" BaseLayerLuminance="0">
105-
<Router AppAssembly="@typeof(App).Assembly">
106-
<Found Context="routeData">
107-
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
108-
</Found>
109-
<NotFound>
110-
<PageTitle>Not found</PageTitle>
111-
<LayoutView Layout="@typeof(MainLayout)">
112-
<p role="alert">Sorry, there's nothing at this address.</p>
113-
</LayoutView>
114-
</NotFound>
115-
</Router>
203+
<Router AppAssembly="@typeof(App).Assembly">
204+
<Found Context="routeData">
205+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
206+
</Found>
207+
<NotFound>
208+
<PageTitle>Not found</PageTitle>
209+
<LayoutView Layout="@typeof(MainLayout)">
210+
<p role="alert">Sorry, there's nothing at this address.</p>
211+
</LayoutView>
212+
</NotFound>
213+
</Router>
116214
</FluentDesignSystemProvider>
117215
```
118216

119217
> :notebook: **Note**
120218
>
121-
> Provider token attributes can be changed on-th-fly like any other Blazor component attribute.
219+
> FluentDesignSystemProvider token attributes can be changed on-the-fly like any other Blazor component attribute.
122220
221+
#### Colors for integration with specific Microsoft products
123222
If you are attempting to configure the components for integration into a specific Microsoft product, the following table provides `AccentBaseColor` values you can use:
124223

125224
Product | AccentBaseColor
@@ -135,6 +234,7 @@ Product | AccentBaseColor
135234

136235
For a list of all available token attributes, [see here](https://github.com/microsoft/fast-blazor/blob/main/src/Microsoft.Fast.Components.FluentUI/Components/FluentDesignSystemProvider.razor#L69). More examples for other components can be found in the `examples` folder [of this repository](https://github.com/microsoft/fast-blazor).
137236

237+
138238
## Web components / Blazor components mapping, implementation status and remarks
139239
Web component | Blazor component | Status | Remarks
140240
----------------- | -------------- | ------ | -------

examples/FluentUI.Demo.Client/FluentUI.Demo.Client.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.3" />
11-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.3" PrivateAssets="all" />
10+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.4" />
11+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.4" PrivateAssets="all" />
1212
</ItemGroup>
1313

1414
<ItemGroup>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"_FluentWebComponentsScriptSource": "https://cdn.jsdelivr.net/npm/@fluentui/web-components/dist/web-components.js"
3+
}

examples/FluentUI.Demo.Client/wwwroot/css/site.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
background-color: var(--fill-color);
7878
color: var(--neutral-foreground-rest);
7979
padding-inline-start: calc(var(--design-unit) * 6px);
80+
padding-inline-end: calc(var(--design-unit) * 6px);
8081
}
8182

8283

@@ -90,6 +91,7 @@
9091
position: fixed;
9192
width: 100%;
9293
z-index: 1000;
94+
margin: 20px 0;
9395
}
9496

9597
#blazor-error-ui .dismiss {

examples/FluentUI.Demo.Server/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8-
"AllowedHosts": "*"
8+
"AllowedHosts": "*",
9+
"FluentWebComponentsScriptSource": "https://cdn.jsdelivr.net/npm/@fluentui/web-components/dist/web-components.min.js"
910
}

examples/FluentUI.Demo.Server/wwwroot/css/site.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
background-color: var(--fill-color);
7777
color: var(--neutral-foreground-rest);
7878
padding-inline-start: calc(var(--design-unit) * 6px);
79+
padding-inline-end: calc(var(--design-unit) * 6px);
7980
}
8081

8182

@@ -89,6 +90,7 @@
8990
position: fixed;
9091
width: 100%;
9192
z-index: 1000;
93+
margin: 20px 0;
9294
}
9395

9496
#blazor-error-ui .dismiss {

examples/FluentUI.Demo.Shared/FluentUI.Demo.Shared.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.3" />
15+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.4" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

0 commit comments

Comments
 (0)