-
-
Notifications
You must be signed in to change notification settings - Fork 26
feat(theme): introduce a mechanism to toggle light/dark modes #230
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5737865
feat(theme): introduce Theme service to manage and persist theme sett…
desmondinho c43ba00
feat(theme): introduce Theme service to manage and persist theme sett…
desmondinho 39a76ac
docs(*): rename ComponentStatus enum to PageStatus
desmondinho 212efa9
docs(theming): add Dark Mode page
desmondinho e9ca5b7
chore(docs): component rename
desmondinho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 4 additions & 2 deletions
6
...cs.Client/Common/Enums/ComponentStatus.cs → ...UI.Docs.Client/Common/Enums/PageStatus.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| namespace LumexUI.Docs.Client.Common; | ||
|
|
||
| public enum ComponentStatus | ||
| public enum PageStatus | ||
| { | ||
| New, | ||
|
|
||
| Soon, | ||
|
|
||
| Preview | ||
| Preview, | ||
|
|
||
| Updated | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
docs/LumexUI.Docs.Client/Pages/Theming/Dark Mode/DarkMode.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| @page "/docs/theming/dark-mode" | ||
| @layout DocsContentLayout | ||
|
|
||
| <DocsSection Title="Global dark mode"> | ||
| <p> | ||
| LumexUI supports both <code>light</code> and <code>dark</code> modes. | ||
| To enable dark mode globally, simply add the <code>dark</code> class | ||
| to a wrapper element such as <code>html</code> or <code>body</code>. | ||
| </p> | ||
|
|
||
| <CodeSnippet Code="@(new CodeBlock( name: "App.razor", snippet: "darkModeRoot" ))" /> | ||
| </DocsSection> | ||
|
|
||
| <DocsSection Title="Mode toggling"> | ||
| <p>LumexUI provides a simple mechanism for switching themes, which consists of two key parts:</p> | ||
|
|
||
| <ul> | ||
| <li><strong>A theme provider</strong> component that sets the initial theme on first load.</li> | ||
| <li><strong>A theme service</strong> that allows you to change the theme dynamically.</li> | ||
| </ul> | ||
|
|
||
| <DocsSection Title="Add the theme provider"> | ||
| <p>Insert the <code>LumexThemeProvider</code> into your <code>MainLayout.razor</code> file:</p> | ||
|
|
||
| <CodeSnippet Code="@(new CodeBlock( name: "MainLayout.razor", snippet: "darkModeProvider" ))" /> | ||
|
|
||
| <LumexAlert Radius="@Radius.Large"> | ||
| <DescriptionContent> | ||
| <p class="mt-0 mb-3"> | ||
| The <code>LumexThemeProvider</code> component requires interactivity. | ||
| If you're using the Blazor Web App template, make sure to apply the appropriate <code>@@rendermode</code>. | ||
| </p> | ||
| <LumexLink Href="https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0" | ||
| External="@true"> | ||
| Learn more about render modes | ||
| </LumexLink> | ||
| </DescriptionContent> | ||
| </LumexAlert> | ||
| </DocsSection> | ||
|
|
||
| <DocsSection Title="Add a mode toggle"> | ||
| <p>Place a mode toggle on your site to toggle between light and dark mode.</p> | ||
| <ThemeTogglePreview /> | ||
| </DocsSection> | ||
| </DocsSection> | ||
|
|
||
| @code { | ||
| [CascadingParameter] private DocsContentLayout Layout { get; set; } = default!; | ||
|
|
||
| private readonly Heading[] _headings = new Heading[] | ||
| { | ||
| new("Global dark mode"), | ||
| new("Mode toggling", [ | ||
| new("Setup"), | ||
| new("Add a mode toggle"), | ||
| ]), | ||
| }; | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| Layout.Initialize( | ||
| title: "Dark Mode", | ||
| category: "Theming", | ||
| description: "Adding dark mode to your app.", | ||
| _headings | ||
| ); | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
docs/LumexUI.Docs.Client/Pages/Theming/Dark Mode/ThemeToggle.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| @inject ThemeService ThemeService | ||
| @inject IPopoverService PopoverService | ||
|
|
||
| <LumexButton Variant="@Variant.Outlined" | ||
| Class="min-w-10 w-10 h-10 p-0" | ||
| OnClick="@TriggerAsync" | ||
| data-popoverref="@_dropdownId"> | ||
| <LumexIcon Icon="@Icons.Rounded.DarkMode" Size="@new( "20" )" Class="hidden dark:block" /> | ||
| <LumexIcon Icon="@Icons.Rounded.LightMode" Size="@new( "20" )" Class="dark:hidden" /> | ||
| </LumexButton> | ||
|
|
||
| <LumexDropdown Id="@_dropdownId" | ||
| Placement="@PopoverPlacement.BottomEnd" | ||
| Class="min-w-32"> | ||
| <LumexDropdownMenu Variant="@MenuVariant.Flat"> | ||
| <LumexDropdownItem OnClick="@(async () => await ThemeService.SetThemeAsync( Theme.Light ))"> | ||
| Light | ||
| </LumexDropdownItem> | ||
| <LumexDropdownItem OnClick="@(async () => await ThemeService.SetThemeAsync( Theme.Dark ))"> | ||
| Dark | ||
| </LumexDropdownItem> | ||
| <LumexDropdownItem OnClick="@(async () => await ThemeService.SetThemeAsync( Theme.System ))"> | ||
| System | ||
| </LumexDropdownItem> | ||
| </LumexDropdownMenu> | ||
| </LumexDropdown> | ||
|
|
||
| @code { | ||
| private string _dropdownId = "theme-toggle"; | ||
|
|
||
| private Task TriggerAsync() => PopoverService.TriggerAsync( _dropdownId ); | ||
| } |
6 changes: 6 additions & 0 deletions
6
docs/LumexUI.Docs.Client/Pages/Theming/Dark Mode/ThemeTogglePreview.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| @rendermode InteractiveWebAssembly | ||
|
|
||
| <PreviewCode Code="@new( name: null, snippet: "darkModeToggle" )" | ||
| PreviewClasses="@(new() { Preview = "justify-center" })"> | ||
| <LumexUI.Docs.Client.Pages.Theming.Dark_Mode.ThemeToggle /> | ||
| </PreviewCode> |
2 changes: 2 additions & 0 deletions
2
docs/LumexUI.Docs.Client/Samples/Theming/Dark Mode/darkModeProvider.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <pre><code class="language-razor"><LumexThemeProvider @rendermode="@InteractiveWebAssembly" /> | ||
| </code></pre> |
12 changes: 12 additions & 0 deletions
12
docs/LumexUI.Docs.Client/Samples/Theming/Dark Mode/darkModeRoot.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <pre data-line="2"><code class="language-html"><!DOCTYPE html> | ||
| <html class="dark"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link href="app.css" rel="stylesheet" /> | ||
| </head> | ||
| <body> | ||
| <script src="_content/LumexUI/js/LumexUI.js" type="module"></script> | ||
| </body> | ||
| </html> | ||
| </code></pre> |
33 changes: 33 additions & 0 deletions
33
docs/LumexUI.Docs.Client/Samples/Theming/Dark Mode/darkModeToggle.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <pre><code class="language-razor">@inject ThemeService ThemeService | ||
| @inject IPopoverService PopoverService | ||
|
|
||
| <LumexButton Variant="@Variant.Outlined" | ||
| Class="min-w-10 w-10 h-10 p-0" | ||
| OnClick="@TriggerAsync" | ||
| data-popoverref="@_dropdownId"> | ||
| <LumexIcon Icon="@Icons.Rounded.DarkMode" Size="@new( "20" )" Class="hidden dark:block" /> | ||
| <LumexIcon Icon="@Icons.Rounded.LightMode" Size="@new( "20" )" Class="dark:hidden" /> | ||
| </LumexButton> | ||
|
|
||
| <LumexDropdown Id="@_dropdownId" | ||
| Placement="@PopoverPlacement.BottomEnd" | ||
| Class="min-w-32"> | ||
| <LumexDropdownMenu Variant="@MenuVariant.Flat"> | ||
| <LumexDropdownItem OnClick="@(async () => await ThemeService.SetThemeAsync( Theme.Light ))"> | ||
| Light | ||
| </LumexDropdownItem> | ||
| <LumexDropdownItem OnClick="@(async () => await ThemeService.SetThemeAsync( Theme.Dark ))"> | ||
| Dark | ||
| </LumexDropdownItem> | ||
| <LumexDropdownItem OnClick="@(async () => await ThemeService.SetThemeAsync( Theme.System ))"> | ||
| System | ||
| </LumexDropdownItem> | ||
| </LumexDropdownMenu> | ||
| </LumexDropdown> | ||
|
|
||
| @code { | ||
| private string _dropdownId = "theme-toggle"; | ||
|
|
||
| private Task TriggerAsync() => PopoverService.TriggerAsync( _dropdownId ); | ||
| } | ||
| </code></pre> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) LumexUI 2024 | ||
| // LumexUI licenses this file to you under the MIT license | ||
| // See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
|
||
| using System.ComponentModel; | ||
|
|
||
| namespace LumexUI.Common; | ||
|
|
||
| /// <summary> | ||
| /// Specifies the available theme options. | ||
| /// </summary> | ||
| public enum Theme | ||
| { | ||
| /// <summary> | ||
| /// A light theme. | ||
| /// </summary> | ||
| [Description( "light" )] | ||
| Light, | ||
|
|
||
| /// <summary> | ||
| /// A dark theme. | ||
| /// </summary> | ||
| [Description( "dark" )] | ||
| Dark, | ||
|
|
||
| /// <summary> | ||
| /// A theme that matches the system setting. | ||
| /// </summary> | ||
| [Description( "system" )] | ||
| System | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) LumexUI 2024 | ||
| // LumexUI licenses this file to you under the MIT license | ||
| // See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| using LumexUI.Services; | ||
|
|
||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace LumexUI; | ||
|
|
||
| /// <summary> | ||
| /// A component that provides theming support. | ||
| /// </summary> | ||
| [ExcludeFromCodeCoverage] | ||
| public class LumexThemeProvider : ComponentBase | ||
| { | ||
| [Inject] private ThemeService ThemeService { get; set; } = default!; | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override async Task OnAfterRenderAsync( bool firstRender ) | ||
| { | ||
| if( firstRender ) | ||
| { | ||
| await ThemeService.InitializeAsync(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.