From 3f88d8e6263f768cf6d276837a3eb60d72735371 Mon Sep 17 00:00:00 2001 From: Adam Weston Date: Thu, 21 May 2026 14:43:27 -0400 Subject: [PATCH] Feat: add disabledOn() method to skip sticky header on custom pages Accepts an array or Closure of page class names and checks them against the current route in shouldStick(). Co-Authored-By: Claude Sonnet 4.6 --- README.md | 30 ++++++++++++++++++++++++++++++ rector.php | 30 +++++++++++++++++------------- src/StickyHeaderPlugin.php | 28 +++++++++++++++++++++++++++- tests/src/Unit/PluginTest.php | 20 ++++++++++++++++++++ 4 files changed, 94 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 2003aa5..4b401fc 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,36 @@ public function panel(Panel $panel): Panel } ``` +### Disabling on Custom Pages + +To disable the sticky header on specific custom pages, pass an array of page class names (or a Closure returning one) to the `disabledOn()` method. + +```php +use Awcodes\StickyHeader\StickyHeaderPlugin; +use App\Filament\Pages\MyCustomPage; +use App\Filament\Pages\AnotherPage; + +public function panel(Panel $panel): Panel +{ + return $panel + ->plugins([ + StickyHeaderPlugin::make() + ->disabledOn([ + MyCustomPage::class, + AnotherPage::class, + ]) + ]) + ]); +} +``` + +A Closure is also accepted, which allows the list to be determined at runtime. + +```php +StickyHeaderPlugin::make() + ->disabledOn(fn () => [MyCustomPage::class]) +``` + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. diff --git a/rector.php b/rector.php index 00d6221..6b9a2bd 100644 --- a/rector.php +++ b/rector.php @@ -4,16 +4,20 @@ use Rector\Config\RectorConfig; -return RectorConfig::configure() - ->withPaths([ - __DIR__.'/src', - ]) - ->withPreparedSets( - deadCode: true, - codeQuality: true, - typeDeclarations: true, - privatization: true, - earlyReturn: true, - strictBooleans: true, - ) - ->withPhpSets(); +try { + return RectorConfig::configure() + ->withPaths([ + __DIR__.'/src', + ]) + ->withPreparedSets( + deadCode: true, + codeQuality: true, + typeDeclarations: true, + privatization: true, + earlyReturn: true, + ) + ->withPhpSets(); +} catch (Rector\Exception\Configuration\InvalidConfigurationException $e) { + echo 'Rector configuration error: '.$e->getMessage().PHP_EOL; + exit(1); +} diff --git a/src/StickyHeaderPlugin.php b/src/StickyHeaderPlugin.php index 70fc4ff..fe5c031 100644 --- a/src/StickyHeaderPlugin.php +++ b/src/StickyHeaderPlugin.php @@ -15,6 +15,8 @@ class StickyHeaderPlugin implements Plugin { use EvaluatesClosures; + protected array|Closure $disabledOn = []; + protected bool|Closure|null $isColored = null; protected bool|Closure|null $isFloating = null; @@ -43,6 +45,13 @@ public function boot(Panel $panel): void ], 'awcodes-sticky-header'); } + public function disabledOn(array|Closure $pages): static + { + $this->disabledOn = $pages; + + return $this; + } + public function colored(bool|Closure $condition = true): static { $this->isColored = $condition; @@ -64,6 +73,11 @@ public function stickOnListPages(bool|Closure $condition = true): static return $this; } + public function getDisabledPages(): array + { + return $this->evaluate($this->disabledOn) ?? []; + } + public function getId(): string { return 'awcodes-sticky-header'; @@ -105,6 +119,18 @@ public function shouldStickOnListPages(): bool public function shouldStick(): bool { - return ! (str(request()->route()->getAction('as'))->contains('index') && ! $this->shouldStickOnListPages()); + $routeName = str(request()->route()->getAction('as')); + + if ($routeName->contains('index') && ! $this->shouldStickOnListPages()) { + return false; + } + + foreach ($this->getDisabledPages() as $pageClass) { + if (method_exists($pageClass, 'getRouteName') && (string) $routeName === $pageClass::getRouteName()) { + return false; + } + } + + return true; } } diff --git a/tests/src/Unit/PluginTest.php b/tests/src/Unit/PluginTest.php index f8c2eb9..43e1de1 100644 --- a/tests/src/Unit/PluginTest.php +++ b/tests/src/Unit/PluginTest.php @@ -41,3 +41,23 @@ true, fn () => true, ]); + +it('can register disabled pages as array', function () { + $this->panel + ->plugins([ + StickyHeaderPlugin::make()->disabledOn(['App\Filament\Pages\CustomPage']), + ]); + + expect(Filament::getPlugin('awcodes-sticky-header')->getDisabledPages()) + ->toBe(['App\Filament\Pages\CustomPage']); +}); + +it('can register disabled pages as closure', function () { + $this->panel + ->plugins([ + StickyHeaderPlugin::make()->disabledOn(fn () => ['App\Filament\Pages\CustomPage']), + ]); + + expect(Filament::getPlugin('awcodes-sticky-header')->getDisabledPages()) + ->toBe(['App\Filament\Pages\CustomPage']); +});