Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Data/Cachet/ThemeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 = [
Expand All @@ -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);
Expand All @@ -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'],
Expand Down Expand Up @@ -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;
}
}
61 changes: 61 additions & 0 deletions tests/Unit/Data/ThemeDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tests\Unit\Data;

use Cachet\Data\Cachet\ThemeData;
use Cachet\Settings\ThemeSettings;
use Filament\Support\Colors\Color;

/**
* The accent select is built from Filament's palette minus the grays, so any
* color Filament adds becomes selectable without Cachet knowing about it.
*/
function selectableAccents(): array
{
return collect(Color::all())
->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:');
});
Loading