From 33d3335d4d4889d0967c961ed287c6773420094d Mon Sep 17 00:00:00 2001 From: Ashwin Chandran Date: Fri, 31 Jul 2026 00:48:49 +0200 Subject: [PATCH] Fix 500 error when picking Mauve, Olive, Mist or Taupe as the accent colour Co-authored-by: Claude --- src/Data/Cachet/ThemeData.php | 20 +++++----- tests/Unit/Data/ThemeDataTest.php | 61 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 tests/Unit/Data/ThemeDataTest.php diff --git a/src/Data/Cachet/ThemeData.php b/src/Data/Cachet/ThemeData.php index fdd04a3a..f52a6074 100644 --- a/src/Data/Cachet/ThemeData.php +++ b/src/Data/Cachet/ThemeData.php @@ -11,7 +11,9 @@ final class ThemeData extends BaseData { - public const GRAYS = ['slate', 'gray', 'zinc', 'neutral', 'stone']; + public const GRAYS = ['slate', 'gray', 'zinc', 'neutral', 'stone', 'mauve', 'olive', 'mist', 'taupe']; + + private const DEFAULT_PAIRING = 'zinc'; private const THEME_PAIRINGS = [ 'cachet' => 'zinc', @@ -308,7 +310,7 @@ public function __construct( protected function generate(): void { $accent = $this->themeSettings->accent; - if ($accent === 'cachet') { + if (! isset(self::THEMES[$accent])) { $primaryColor = FilamentColor::getColors()['cachet']; $theme = [ @@ -328,9 +330,9 @@ protected function generate(): void } if ($this->themeSettings->accent_pairing) { - $pairing = self::THEME_PAIRINGS[$accent]; + $pairing = self::matchPairing($accent); } else { - $pairing = $this->themeSettings->accent_content; + $pairing = $this->themeSettings->accent_content ?? self::matchPairing($accent); } $this->styles = $this->compileCss($theme, $pairing); @@ -341,8 +343,7 @@ protected function generate(): void */ protected function compileCss(array $theme, string $pairing): string { - $pairingKey = ucwords($pairing); - $pairingColor = constant("Filament\Support\Colors\Color::{$pairingKey}"); + $pairingColor = Color::all()[$pairing] ?? Color::all()[self::DEFAULT_PAIRING]; $this->lightColors = [ 'accent' => $theme['light']['accent'], @@ -381,10 +382,11 @@ public function lightColors(): array } /** - * Match the pairing for the given accent. + * Match the pairing for the given accent, falling back to a neutral gray + * for accents Cachet has no explicit pairing for. */ - public static function matchPairing(string $accent): string + public static function matchPairing(?string $accent): string { - return self::THEME_PAIRINGS[$accent]; + return self::THEME_PAIRINGS[$accent] ?? self::DEFAULT_PAIRING; } } diff --git a/tests/Unit/Data/ThemeDataTest.php b/tests/Unit/Data/ThemeDataTest.php new file mode 100644 index 00000000..64f90a5c --- /dev/null +++ b/tests/Unit/Data/ThemeDataTest.php @@ -0,0 +1,61 @@ +except(ThemeData::GRAYS) + ->keys() + ->push('cachet') + ->all(); +} + +it('compiles styles for every selectable accent', function () { + foreach (selectableAccents() as $accent) { + $settings = app(ThemeSettings::class)->fill([ + 'accent' => $accent, + 'accent_content' => null, + 'accent_pairing' => true, + ]); + + expect((new ThemeData($settings))->styles) + ->toContain('--accent:', '--accent-background:'); + } +}); + +it('pairs every selectable accent with a gray', function () { + foreach (selectableAccents() as $accent) { + expect(ThemeData::matchPairing($accent))->toBeIn(ThemeData::GRAYS); + } +}); + +it('compiles styles for every selectable accent content', function () { + foreach (ThemeData::GRAYS as $gray) { + $settings = app(ThemeSettings::class)->fill([ + 'accent' => 'cachet', + 'accent_content' => $gray, + 'accent_pairing' => false, + ]); + + expect((new ThemeData($settings))->styles)->toContain('--accent-background:'); + } +}); + +it('falls back to the cachet accent when the stored accent is unknown', function () { + $settings = app(ThemeSettings::class)->fill([ + 'accent' => 'not-a-real-color', + 'accent_content' => null, + 'accent_pairing' => true, + ]); + + expect((new ThemeData($settings))->styles)->toContain('--accent:'); +});