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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 17 additions & 13 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
28 changes: 27 additions & 1 deletion src/StickyHeaderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}
}
20 changes: 20 additions & 0 deletions tests/src/Unit/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});