Skip to content

feat: support host-supplied named theme presets#42

Open
nmiddelmann2106 wants to merge 1 commit intoMudBlazor:mainfrom
nmiddelmann2106:feature/named-theme-presets
Open

feat: support host-supplied named theme presets#42
nmiddelmann2106 wants to merge 1 commit intoMudBlazor:mainfrom
nmiddelmann2106:feature/named-theme-presets

Conversation

@nmiddelmann2106
Copy link
Copy Markdown

Summary

Replaces the long-standing "Not Implemented" preset stub in the
ThemeManager drawer with a working dropdown driven by themes the host
application provides. The library stays storage-agnostic — it just
renders what it is given and emits a selection event.

Motivation

Issue #31 names "Add presets" as a planned improvement. There are at
least two natural shapes this can take: built-in catalog presets
(library-shipped) and host-supplied presets (loaded from app state, a
DB, JSON files, etc.). This PR addresses the second shape with a
small, additive API. It is intentionally unopinionated about
persistence: the host fills the list, the library renders and reports
selection.

Changes

  • ThemeManagerTheme.cs: add optional string? Name. Existing usage
    is unaffected; only consumers that opt into presets need to set it.
  • MudThemeManager.razor.cs: add two parameters
    • IEnumerable<ThemeManagerTheme>? Presets
    • EventCallback<ThemeManagerTheme> PresetSelected
  • MudThemeManager.razor: when Presets is null the existing
    disabled "Not Implemented" stub still renders unchanged. When
    provided, a working MudSelect lists the names and emits the
    selection event.
  • Refactor: theme initialisation moves from OnInitialized into a
    small SyncWithTheme() method called from both OnInitialized and
    OnParametersSet, guarded by a reference check on Theme. Without
    this, swapping the active Theme via the existing parameter (which
    is the natural way to react to a preset selection) would leave the
    manager's internal palette caches pointing at the previous theme,
    so subsequent edits would mutate the wrong instance.

Backward compatibility

  • All new parameters are optional.
  • When Presets is null the rendered drawer is byte-identical to
    before (same disabled MudSelect, same "Not Implemented" item).
  • Name on ThemeManagerTheme is nullable and unused by the library
    unless the consumer opts in.

Usage

<MudThemeManager Theme="@_active"
                 Presets="@_themes"
                 PresetSelected="@OnPresetPicked"
                 ThemeChanged="@(t => _active = t)"
                 ... />
private List<ThemeManagerTheme> _themes = new();
private ThemeManagerTheme _active = new();

private void OnPresetPicked(ThemeManagerTheme picked)
{
    _active = picked;
}

Test plan

  • dotnet build clean (0 errors).
  • Smoke: drawer with no Presets shows the unchanged
    "Not Implemented" stub.
  • Smoke: drawer with Presets shows names and switching emits
    PresetSelected.
  • Smoke: switching active Theme after first render correctly
    re-syncs internal caches (subsequent color edits target the
    newly-active theme).

Notes

Replaces the long-standing "Not Implemented" preset stub with a
working dropdown driven by themes the host application provides.

- Add optional `Name` to `ThemeManagerTheme` so the existing wrapper
  can carry a display name. Default null preserves all existing usage.
- Add `[Parameter] IEnumerable<ThemeManagerTheme>? Presets` and
  `[Parameter] EventCallback<ThemeManagerTheme> PresetSelected` on
  `MudThemeManager`. When `Presets` is null the original disabled
  stub renders unchanged (backward compatible).
- Refactor theme initialisation into `SyncWithTheme()` and run it
  from both `OnInitialized` and `OnParametersSet`, with a reference
  guard. This lets the host swap the active theme via the existing
  `Theme` parameter (e.g. on preset selection) and have the manager
  re-sync its internal palette caches. Without this, switching the
  active theme after first render would leave subsequent edits
  applied to the previous theme.

The library remains storage-agnostic: presets are passed in by the
host, persistence stays the host application concern.
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 7, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant