Skip to content

Commit

Permalink
Merge pull request #8 from DanielvdSpoel/feature/filament-3x-support
Browse files Browse the repository at this point in the history
Migrate to filament v3
  • Loading branch information
pxlrbt authored Aug 3, 2023
2 parents df61bc4 + 2181e10 commit 08aa092
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 156 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"php": "^8.0",
"filament/filament": "^2.9.15"
"filament/filament": "^3.0-stable"
},
"require-dev": {
"laravel/pint": "^1.10"
Expand Down
66 changes: 43 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,33 @@ Never confuse your tabs with different Filament environments again.

## Installation via Composer

**Requires PHP > 8.0 and Filament > 2.9.15**
| Plugin Version | Filament Version | PHP Version |
|----------------|-----------------|-------------|
| 1.x | ^2.9.15 | \> 8.0 |
| 2.x | 3.x | \> 8.1 |

```bash
composer require pxlrbt/filament-environment-indicator
```

## Usage

To use this plugin register it in your panel configuration:

```php
use pxlrbt\FilamentEnvironmentIndicator\EnvironmentIndicatorPlugin;

$panel
->plugins([
EnvironmentIndicatorPlugin::make(),
]);
```

## Configuration

Out of the box, this plugin adds a colored border to the top of the admin panel and a badge next to the search bar.


You can customize any behaviour, by using Filament's `::configureUsing()` syntax inside your ServiceProviders `boot()` method.
You can customize any behaviour via the plugin object.

### Customizing the view
Use `php artisan vendor:publish --tag="filament-environment-indicator-views"` to publish the view to the `resources/views/vendor/filament-environment-indicator` folder. After this you can customize it as you wish!
Expand All @@ -35,40 +50,45 @@ Use `php artisan vendor:publish --tag="filament-environment-indicator-views"` to
By default, the package checks whether you have Spatie permissions plugin installed and checks for a role called `super_admin`. You can further customize whether the indicators should be shown.

```php
use pxlrbt\FilamentEnvironmentIndicator\FilamentEnvironmentIndicator;
use pxlrbt\FilamentEnvironmentIndicator\EnvironmentIndicatorPlugin;

FilamentEnvironmentIndicator::configureUsing(function (FilamentEnvironmentIndicator $indicator) {
$indicator->visible = fn () => auth()->user()?->can('see_indicator');
}, isImportant: true);
$panel->plugins([
EnvironmentIndicatorPlugin::make()
->visible(fn () => auth()->user()?->can('see_indicator'))
]);
```

### Colors

You can overwrite the default colors if you want your own colors or need to add more. The color accepts any CSS color value.
You can overwrite the default colors if you want your own colors or need to add more. The `->color()`method accepts any Filament's Color object or a closure that returns a color object.

```php
use pxlrbt\FilamentEnvironmentIndicator\FilamentEnvironmentIndicator;

FilamentEnvironmentIndicator::configureUsing(function (FilamentEnvironmentIndicator $indicator) {
$indicator->color = fn () => match (app()->environment()) {
'production' => null,
'staging' => 'orange',
default => 'blue',
};
}, isImportant: true);
use pxlrbt\FilamentEnvironmentIndicator\EnvironmentIndicatorPlugin;
use Filament\Support\Colors\Color;

$panel->plugins([
EnvironmentIndicatorPlugin::make()
->color(fn () => match (app()->environment()) {
'production' => null,
'staging' => Color::Orange,
default => Color::Blue,
})
]);
```

### Indicators

By default, both indicators are displayed. You can turn them off separately.

```php
use pxlrbt\FilamentEnvironmentIndicator\FilamentEnvironmentIndicator;

FilamentEnvironmentIndicator::configureUsing(function (FilamentEnvironmentIndicator $indicator) {
$indicator->showBadge = fn () => false;
$indicator->showBorder = fn () => true;
}, isImportant: true);
use pxlrbt\FilamentEnvironmentIndicator\EnvironmentIndicatorPlugin;
use Filament\Support\Colors\Color;

$panel->plugins([
EnvironmentIndicatorPlugin::make()
->showBadge(false)
->showBorder(true)
]);
```

## Contributing
Expand Down
15 changes: 13 additions & 2 deletions resources/views/badge.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<div
class="environment-indicator hidden sm:flex items-center h-10 rounded-lg px-3 text-white text-sm font-medium"
style="background-color: {{ $color }}; margin-right: 1rem;"
class="
environment-indicator hidden sm:flex items-center h-9 mr-4 px-3 text-sm font-medium
rounded-lg shadow-sm ring-1
ring-custom-600/20 bg-custom-50 text-custom-600
dark:ring-custom-400/30 dark:bg-custom-400/10 dark:text-custom-400
"
style="
--c-50: {{ $color[50] }};
--c-300: {{ $color[300] }};
--c-400: {{ $color[400] }};
--c-600: {{ $color[600] }};
margin-right: 1rem;
"
>
{{ $environment }}
</div>
143 changes: 143 additions & 0 deletions src/EnvironmentIndicatorPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace pxlrbt\FilamentEnvironmentIndicator;

use Closure;
use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Colors\Color;
use Filament\Support\Concerns\EvaluatesClosures;
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;

class EnvironmentIndicatorPlugin implements Plugin
{
use EvaluatesClosures;

public bool | Closure | null $visible = null;
public bool | Closure | null $showBadge = null;
public bool | Closure | null $showBorder = null;

public array | Closure | null $color = null;

public static function make(): static
{
$plugin = app(static::class);

// Defaults
$plugin->visible(function () {
if (($user = auth()->user()) === null) {
return false;
}

if (method_exists($user, 'hasRole')) {
return $user->hasRole('super_admin');
}

return true;
});

$plugin->color(fn () => match (app()->environment()) {
'production' => Color::Red,
'staging' => Color::Orange,
'development' => Color::Blue,
default => Color::Pink,
});

$plugin->showBadge(fn () => match (app()->environment()) {
'production', 'prod' => false,
default => true,
});

$plugin->showBorder(fn () => match (app()->environment()) {
'production', 'prod' => false,
default => true,
});

return $plugin;
}

public function getId(): string
{
return 'environment-indicator';
}

public function boot(Panel $panel): void
{
//
}

public function register(Panel $panel): void
{
$panel->renderHook('panels::global-search.start', function () {
if (! $this->evaluate($this->visible)) {
return '';
}

if (! $this->evaluate($this->showBadge)) {
return '';
}

return View::make('filament-environment-indicator::badge', [
'color' => $this->getColor(),
'environment' => ucfirst(app()->environment()),
]);
});

$panel->renderHook('panels::styles.after', function () {
if (! $this->evaluate($this->visible)) {
return '';
}

if (! $this->evaluate($this->showBorder)) {
return '';
}

return new HtmlString("
<style>
.fi-topbar,
.fi-sidebar {
border-top: 5px solid rgb({$this->getColor()['500']}) !important;
}
.fi-topbar {
height: calc(4rem + 5px) !important;
}
</style>
");
});
}

public function visible(bool|Closure $visible): static
{
$this->visible = $visible;

return $this;
}

public function showBadge(bool|Closure $showBadge = true): static
{
$this->showBadge = $showBadge;

return $this;
}

public function showBorder(bool|Closure $showBorder = true): static
{
$this->showBorder = $showBorder;

return $this;
}

public function color(array|Closure $color = Color::Pink): static
{
$this->color = $color;

return $this;
}

protected function getColor(): array
{
return $this->evaluate($this->color);
}
}
122 changes: 0 additions & 122 deletions src/FilamentEnvironmentIndicator.php

This file was deleted.

Loading

0 comments on commit 08aa092

Please sign in to comment.