Skip to content

ThemeProvider: Add bind-CurrentPalette parameter#12149

Merged
danielchalmers merged 7 commits intodevfrom
copilot/add-bind-currentpalette-to-theme-provider
Nov 25, 2025
Merged

ThemeProvider: Add bind-CurrentPalette parameter#12149
danielchalmers merged 7 commits intodevfrom
copilot/add-bind-currentpalette-to-theme-provider

Conversation

Copy link
Contributor

Copilot AI commented Nov 23, 2025

Adds CurrentPalette parameter to MudThemeProvider for direct access to the active palette instance, automatically reflecting dark mode state.

Changes:

  • Added CurrentPalette parameter with CurrentPaletteChanged EventCallback for two-way binding
  • Implemented ParameterState-based management with async change handlers for IsDarkMode and Theme updates
  • Updated OnParametersSet() to OnParametersSetAsync() for proper async/await support
  • Added 9 unit tests covering palette switching, custom themes, and binding behavior

Usage:

<MudThemeProvider @bind-CurrentPalette="@currentPalette" />

@code {
    private Palette? currentPalette;
    
    // Direct access to current colors without checking IsDarkMode
    var primary = currentPalette?.Primary;
}

Eliminates need to manually select between PaletteLight and PaletteDark based on dark mode state.

Checklist:

  • I've read the contribution guidelines
  • My code follows the style of this project
  • I've added or updated relevant unit tests

Fixes #12109

Original prompt

This section details on the original issue you should resolve

<issue_title>Add bind-CurrentPalette to theme provider</issue_title>
<issue_description>### Describe your idea

Add a bind-CurrentPalette property to the theme provider so developers can directly access the palette instance currently being used, which automatically reflects whether dark mode is enabled or not. This binding would allow better real-time customization and inspection of theme colors, making dynamic theming easier and more robust in Blazor apps.

Example code and screenshots

Example usage:

<MudThemeProvider bind-CurrentPalette="@currentPalette">
  <!-- Use currentPalette to inspect or react to theme colors in code -->
</MudThemeProvider>

This would expose the current palette object for the theme (Primary, Secondary, etc.), allowing visibility and direct use depending on dark mode state without extra logic.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

private void UpdateCurrentPalette()
{
var newPalette = GetCurrentPalette();
_ = _currentPaletteState.SetValueAsync(newPalette);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's fire and forget? it should be a task method UpdateCurrentPaletteAsync and the SetValueAsync should be awaited

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Why it's fire and forget? it should be a task method UpdateCurrentPaletteAsync and the SetValueAsync should be awaited

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to async method UpdateCurrentPaletteAsync() and properly awaited. Also updated OnParametersSet() to OnParametersSetAsync() to support async operations. Fixed in commit d69e192.

comp.Instance.GetState(x => x.CurrentPalette).Should().BeOfType<PaletteLight>();

// Act
await comp.InvokeAsync(() => comp.SetParametersAndRender(parameters => parameters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think InvokeAsync is necessary for SetParametersAndRender

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed unnecessary InvokeAsync wrappers around SetParametersAndRender calls in all affected tests. Fixed in commit d69e192.


private async Task OnIsDarkModeChangedAsync(ParameterChangedEventArgs<bool> arg)
{
await _currentPaletteState.SetValueAsync(GetCurrentPalette());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just not retrun _currentPaletteState.SetValueAsync(GetCurrentPalette());?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified to directly return the Task from SetValueAsync. Fixed in commit d69e192.

}

// Update CurrentPalette if theme changed or this is the first time
if (themeChanged || !_currentPaletteInitialized)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain me this part what are you doing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initialization logic ensures CurrentPalette is set on component startup (first OnParametersSet call) and whenever the Theme property changes. The _currentPaletteInitialized flag tracks whether we've set the initial palette value, and we also update it when the theme reference changes. This prevents unnecessary updates on every parameter set while ensuring the palette is always current.

@danielchalmers
Copy link
Member

@copilot respond to comments

Copilot AI changed the title [WIP] Add bind-CurrentPalette to theme provider Add bind-CurrentPalette to MudThemeProvider Nov 23, 2025
Copilot AI requested a review from danielchalmers November 23, 2025 20:22
@mudbot mudbot bot added the needs: changes A maintainer has asked for further modifications to be made to this pull request label Nov 23, 2025
@mudbot
Copy link

mudbot bot commented Nov 23, 2025

I've received the request to respond to the review comments. However, the specific feedback from the review is not accessible to me. Could you please summarize the required changes in a new comment? I'll address them once the details are available.


I'm an AI assistant — If I missed something or made a mistake, please let me know in a reply!

@danielchalmers danielchalmers marked this pull request as ready for review November 23, 2025 20:35
@mudbot mudbot bot changed the title Add bind-CurrentPalette to MudThemeProvider MudThemeProvider: Add bind-CurrentPalette parameter Nov 23, 2025
@mudbot mudbot bot removed the needs: changes A maintainer has asked for further modifications to be made to this pull request label Nov 23, 2025
@mudbot mudbot bot added the enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library label Nov 24, 2025
Copy link
Member

@ScarletKuro ScarletKuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, it's good to go now. Copilot over-complicated it, and some parts didn't make sense to me.
It's easier to do this job in OnParametersSetAsync. Even if unrelated parameters change, the parameter state won't do anything if GetCurrentPalette is the same as _currentPaletteState.Value, so it's actually safe to call it as often as you want.

@danielchalmers danielchalmers merged commit a47f81a into dev Nov 25, 2025
7 of 8 checks passed
@ScarletKuro ScarletKuro deleted the copilot/add-bind-currentpalette-to-theme-provider branch November 25, 2025 15:36
@danielchalmers danielchalmers changed the title MudThemeProvider: Add bind-CurrentPalette parameter ThemeProvider: Add bind-CurrentPalette parameter Dec 20, 2025
This was referenced Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add bind-CurrentPalette to theme provider

3 participants