Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FluentDesignTheme] Add OnLoaded event #1156

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,11 @@
Callback raised when the Dark/Light luminance changes.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDesignTheme.OnLoaded">
<summary>
Callback raised when the component is rendered for the first time.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDesignTheme.ChildContent">
<summary>
Gets or sets the content of the component.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<FluentDesignTheme @bind-Mode="@Mode"
OfficeColor="@OfficeColor"
OfficeColorChanged="@(e => { OfficeColor = e ?? OfficeColor.Default; StateHasChanged(); })"
OnLoaded="@OnLoaded"
OnLuminanceChanged="@OnLuminanceChanged"
StorageName="theme" />

Expand Down Expand Up @@ -43,8 +44,13 @@

public OfficeColor OfficeColor { get; set; }

void OnLoaded(LoadedEventArgs e)
{
DemoLogger.WriteLine($"Loaded: {(e.Mode == DesignThemeModes.System ? "System" : "")} {(e.IsDark ? "Dark" : "Light")}");
}

void OnLuminanceChanged(LuminanceChangedEventArgs e)
{
DemoLogger.WriteLine($"{(e.Mode == DesignThemeModes.System ? "System" : "")} {(e.IsDark ? "Dark" : "Light")}");
DemoLogger.WriteLine($"Changed: {(e.Mode == DesignThemeModes.System ? "System" : "")} {(e.IsDark ? "Dark" : "Light")}");
}
}
18 changes: 17 additions & 1 deletion src/Core.Assets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ interface Blazor {
registerCustomEventType: (
name: string,
options: CustomeventTypeOptions) => void;

theme: {
isSystemDark(): boolean,
isDarkMode(): boolean
}
}

interface CustomeventTypeOptions {
Expand Down Expand Up @@ -103,7 +108,7 @@ export function afterStarted(blazor: Blazor) {
createEventArgs: event => {

// Hacking of a fake update
if (event.target!.isUpdating) {
if (event.target!.isUpdating) {
return {
checked: null,
indeterminate: null
Expand Down Expand Up @@ -259,6 +264,17 @@ export function afterStarted(blazor: Blazor) {
}
});

blazor.theme = {
isSystemDark: () => {
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
},

isDarkMode: () => {
const luminance: string = getComputedStyle(document.documentElement).getPropertyValue('--base-layer-luminance');
return parseFloat(luminance) < 0.5;
}
}

afterStartedCalled = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public string? Direction
[Parameter]
public EventCallback<LuminanceChangedEventArgs> OnLuminanceChanged { get; set; }

/// <summary>
/// Callback raised when the component is rendered for the first time.
/// </summary>
[Parameter]
public EventCallback<LoadedEventArgs> OnLoaded { get; set; }

/// <summary>
/// Gets or sets the content of the component.
/// </summary>
Expand Down Expand Up @@ -162,6 +168,13 @@ protected async override Task OnAfterRenderAsync(bool firstRender)
var theme = themeJSON == null ? null : JsonSerializer.Deserialize<DataLocalStorage>(themeJSON, JSON_OPTIONS);

await ApplyLocalStorageValues(theme);

if (OnLoaded.HasDelegate)
{
var realLuminance = await Module.InvokeAsync<string>("GetGlobalLuminance") ?? "1.0";
var isDark = Convert.ToDouble(realLuminance) < 0.5;
await OnLoaded.InvokeAsync(new LoadedEventArgs(Mode, isDark, CustomColor, OfficeColor, StorageName, Direction));
}
}
}

Expand Down Expand Up @@ -234,4 +247,6 @@ private class DataLocalStorage
}
}

public record LuminanceChangedEventArgs(DesignThemeModes Mode, bool IsDark);
public record LuminanceChangedEventArgs(DesignThemeModes Mode, bool IsDark);

public record LoadedEventArgs(DesignThemeModes Mode, bool IsDark, string? CustomColor, OfficeColor? OfficeColor, string? StorageName, string? Direction);
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ export function UpdateDirection(value) {

export function GetDirection() {
return document.body.dir;
}

export function GetGlobalLuminance() {
return getComputedStyle(document.documentElement).getPropertyValue('--base-layer-luminance');
}