Skip to content

Commit

Permalink
fix: respect user theme preference (#4033)
Browse files Browse the repository at this point in the history
  • Loading branch information
datlechin authored Sep 28, 2024
1 parent 077363a commit feca3d0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion framework/core/js/src/common/Application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export default class Application {
}

private initColorScheme(forumDefault: string | null = null): void {
forumDefault ??= document.documentElement.getAttribute('data-theme') ?? 'auto';
forumDefault ??= app.forum.attribute('colorScheme') ?? 'auto';
this.allowUserColorScheme = forumDefault === 'auto';
const userConfiguredPreference = this.session.user?.preferences()?.colorScheme;

Expand Down
2 changes: 2 additions & 0 deletions framework/core/src/Api/Resource/ForumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public function fields(): array
->get(fn () => $this->settings->get('theme_primary_color')),
Schema\Str::make('themeSecondaryColor')
->get(fn () => $this->settings->get('theme_secondary_color')),
Schema\Str::make('colorScheme')
->get(fn () => $this->settings->get('color_scheme')),
Schema\Str::make('logoUrl')
->get(fn () => $this->getLogoUrl()),
Schema\Str::make('faviconUrl')
Expand Down
6 changes: 5 additions & 1 deletion framework/core/src/Frontend/FrontendServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public function register(): void
$settings = $container->make(SettingsRepositoryInterface::class);

// Add document classes/attributes for design use cases.
$document->extraAttributes['data-theme'] = $settings->get('color_scheme');
$document->extraAttributes['data-theme'] = function (ServerRequestInterface $request) use ($settings) {
return $settings->get('color_scheme') === 'auto'
? RequestUtil::getActor($request)->getPreference('colorScheme')
: $settings->get('color_scheme');
};
$document->extraAttributes['data-colored-header'] = $settings->get('theme_colored_header') ? 'true' : 'false';
$document->extraAttributes['class'][] = function (ServerRequestInterface $request) {
return RequestUtil::getActor($request)->isGuest() ? 'guest-user' : 'logged-in';
Expand Down

0 comments on commit feca3d0

Please sign in to comment.