Skip to content
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

Support Closures in the registration of Filament Panel Colors #9556

Merged
merged 11 commits into from
Nov 15, 2023

Conversation

coolsam726
Copy link
Contributor

  • Changes have been thoroughly tested to not break existing functionality.
  • New functionality has been documented or existing documentation has been updated to reflect changes.

This PR adds support for using closures in each of the colors passed as an array to the FilamentColor::register() method. This makes it possible to evaluate theme colors during the application's runtime, which is currently only possible using a custom Middleware. A good use case for this is in a multi-tenant app where tenants should be able to set their own theme colors as part of the tenant configuration.

With this change, the following color registration will be possible from any service provider's boot method:

use Filament\Support\Colors\Color;
use Filament\Support\Facades\FilamentColor;

FilamentColor::register([
    'primary' => fn() => tenant()?->primary_color ?: Color::Indigo,
    'info' => fn() => Filament::getTenant()?->info_color ?: Color::Amber,
]);

Additionally, it will also be possible to use closures in the $panel->colors() method as follows:

use Filament\Panel;
use Filament\Support\Colors\Color;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->colors([
            'primary' => fn() => Filament::getTenant()?->danger_color ?: Color::Indigo, // Evaluate the color during runtime
            'info' => fn() => settings()->info_color ?: Color::Amber,
        ]);
}

- Modified the FilamentColor::register() signature to accept an array of closures
- Modified the HasColors::colors() signature to accept an array of closures
- Modified ColorManager::processColor() to accept a closure and evaluate it to a color
- Added documentation under Filament Panels -> Themes
- Added documentation under Core Concepts -> Colors
@coolsam726 coolsam726 marked this pull request as draft November 9, 2023 08:43
Copy link
Member

@zepfietje zepfietje left a comment

Choose a reason for hiding this comment

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

Let's only accept a closure as the top-level argument, instead of per color. Offers the same flexibility and should be more performant.

@danharrin danharrin added the enhancement New feature or request label Nov 9, 2023
@danharrin danharrin added this to the v3 milestone Nov 9, 2023
@coolsam726
Copy link
Contributor Author

Let's only accept a closure as the top-level argument, instead of per color. Offers the same flexibility and should be more performant.

That was my initial consideration.My only worry was: Will this still allow plugins to override only a single color without necessarily setting all the six colors at once especially via $panel->colors()? I have done tests and confirm this is still possible. Submitting the changes shortly.

…at the top level.

- Made changes to the panels -> themes documentation
- Made changes to the support -> colors documentation
@coolsam726 coolsam726 marked this pull request as ready for review November 9, 2023 20:05
@zepfietje zepfietje requested review from danharrin and removed request for zepfietje November 10, 2023 17:19
@danharrin
Copy link
Member

Please run Pint

@coolsam726
Copy link
Contributor Author

Please run Pint

Done.

@danharrin danharrin merged commit 19c3c72 into filamentphp:3.x Nov 15, 2023
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants