Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Aug 11, 2025

Link issues

fixes #6597

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Improve theme load logic by asynchronously initializing theme through a JS module before rendering the base layout and unifying theme retrieval across modules.

Bug Fixes:

Enhancements:

  • Load and initialize theme via a JS module in OnInitializedAsync before rendering layout
  • Guard layout rendering with an initialization flag to wait for theme application
  • Unify theme retrieval by removing getPreferredTheme and defaulting getTheme to use auto theme value
  • Update related JS modules to replace getPreferredTheme with getTheme

@bb-auto bb-auto bot added the enhancement New feature or request label Aug 11, 2025
@bb-auto bb-auto bot added this to the 9.9.0 milestone Aug 11, 2025
Copy link
Contributor

sourcery-ai bot commented Aug 11, 2025

Reviewer's Guide

This PR enhances the layout’s theme initialization by moving theme detection and application to a client-side JS module loaded during BaseLayout initialization, unifying the theme utility functions, and conditionally rendering the layout only after theme setup completes.

Sequence diagram for improved theme initialization in BaseLayout

sequenceDiagram
    participant BaseLayout
    participant JSRuntime
    participant ThemeModule
    participant Browser
    BaseLayout->>JSRuntime: Load JS module (BaseLayout.razor.js)
    JSRuntime->>ThemeModule: Call initTheme()
    ThemeModule->>Browser: getTheme() (from localStorage or attribute)
    ThemeModule->>Browser: setTheme(currentTheme, false)
    Note over BaseLayout: _init set to true, layout rendered
Loading

Flow diagram for conditional layout rendering after theme initialization

flowchart TD
    A[Component Initialization] --> B{_init == false?}
    B -- Yes --> C[Do not render layout]
    B -- No --> D[Render Header and Body]
    D --> E[Layout visible with correct theme]
Loading

File-Level Changes

Change Details Files
Add client-side theme initialization in BaseLayout
  • Inject IJSRuntime and IOptions
  • Override OnInitializedAsync to load the JS module and call initTheme
  • Introduce _init flag and render content only after initialization
BaseLayout.razor.cs
BaseLayout.razor
Refactor theme utility functions to use a single getTheme
  • Remove getPreferredTheme export
  • Extend getTheme to fallback to auto value
  • Update Pre.razor.js and ThemeProvider.razor.js to import and call getTheme instead of getPreferredTheme
utility.js
Pre.razor.js
ThemeProvider.razor.js
Introduce BaseLayout.razor.js for theme setup
  • Create initTheme function to fetch current theme and apply it
BaseLayout.razor.js

Assessment against linked issues

Issue Objective Addressed Explanation
#6596 Add dark mode styling support for keyboard (kbd) elements in MeiliSearch The PR only improves theme loading logic and does not add or modify any CSS or styling for kbd elements.
#6596 Introduce theme-aware CSS variables for kbd component styling No changes to CSS variables or kbd component styling are present in the PR diff.
#6596 Update documentation to reflect dark mode support for kbd elements The PR does not update documentation related to kbd elements or their dark mode support.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang merged commit e6afb4f into main Aug 11, 2025
4 checks passed
@ArgoZhang ArgoZhang deleted the refactor-theme branch August 11, 2025 07:31
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes - here's some feedback:

  • Consider moving the JS interop theme initialization into OnAfterRenderAsync (with a first-render guard) to ensure the DOM is ready and avoid pre-render race conditions.
  • Gating the entire layout behind the _init flag may produce a blank page if the module load fails or is slow—consider adding a simple loading skeleton or fallback to improve perceived performance.
  • Wrap the module loading and initTheme invocation in a try/catch to gracefully handle JS interop failures and avoid the layout getting stuck uninitialized.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider moving the JS interop theme initialization into OnAfterRenderAsync (with a first-render guard) to ensure the DOM is ready and avoid pre-render race conditions.
- Gating the entire layout behind the _init flag may produce a blank page if the module load fails or is slow—consider adding a simple loading skeleton or fallback to improve perceived performance.
- Wrap the module loading and initTheme invocation in a try/catch to gracefully handle JS interop failures and avoid the layout getting stuck uninitialized.

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor:4` </location>
<code_context>
-    <main>
-        @Body
-    </main>
+    @if (_init)
+    {
+        <Header></Header>
+        <main>
+            @Body
+        </main>
</code_context>

<issue_to_address>
Delaying layout rendering until _init is true may impact perceived performance.

If initialization is asynchronous, users may experience a blank screen. Display a loading indicator or fallback UI during this period.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +4 to +5
@if (_init)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Delaying layout rendering until _init is true may impact perceived performance.

If initialization is asynchronous, users may experience a blank screen. Display a loading indicator or fallback UI during this period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

doc(Layout): improve load theme logic
1 participant