Skip to content

Commit

Permalink
Merge pull request #25 from rhiannonjourney/feature/closure-support
Browse files Browse the repository at this point in the history
Support closures in `floating()` and `colored()` methods
  • Loading branch information
awcodes authored Oct 20, 2023
2 parents bc3efbb + 5dc52f7 commit 91ee828
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public function panel(Panel $panel): Panel
}
```

Both the `floating()` and `colored()` methods can receive closure that will be evaluated to determine if the theme should be applied. This allows you to apply the theme conditionally, for instance, based off of user preferences.

```php
use Awcodes\FilamentStickyHeader\StickyHeaderPlugin;

public function panel(Panel $panel): Panel
{
return $panel
->plugins([
StickyHeaderPlugin::make()
->floating(fn():bool => auth()->user()->use_floating_header)
->colored(fn():bool => auth()->user()->use_floating_header)
])
]);
}
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
24 changes: 14 additions & 10 deletions src/StickyHeaderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@

namespace Awcodes\FilamentStickyHeader;

use Closure;
use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;
use Filament\Support\Facades\FilamentAsset;

class StickyHeaderPlugin implements Plugin
{
protected ?bool $isColored = null;
use EvaluatesClosures;

protected ?bool $isFloating = null;
protected bool | Closure | null $isColored = null;

protected bool | Closure | null $isFloating = null;

public function boot(Panel $panel): void
{
//
FilamentAsset::registerScriptData([
'stickyHeaderTheme' => $this->getTheme(),
], 'awcodes-sticky-header');
}

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

return $this;
}

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

Expand All @@ -43,12 +49,12 @@ public function getId(): string

public function isColored(): bool
{
return $this->isColored ?? false;
return $this->evaluate($this->isColored) ?? false;
}

public function isFloating(): bool
{
return $this->isFloating ?? false;
return $this->evaluate($this->isFloating) ?? false;
}

public function getTheme(): string
Expand All @@ -72,8 +78,6 @@ public static function make(): static

public function register(Panel $panel): void
{
FilamentAsset::registerScriptData([
'stickyHeaderTheme' => $this->getTheme(),
], 'awcodes-sticky-header');
//
}
}

0 comments on commit 91ee828

Please sign in to comment.