From 2aa7ff852c24be6d014b3285e8bea3e0ed147d6f Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 20 Jul 2025 19:52:33 +0100 Subject: [PATCH] simplify and add docs warning --- docs/05-panel-configuration.md | 8 ++++++-- .../panels/src/Panel/Concerns/HasSpaMode.php | 4 ++-- packages/support/src/Facades/FilamentView.php | 8 ++++---- packages/support/src/View/ViewManager.php | 8 ++++---- packages/support/src/helpers.php | 2 +- tests/src/Support/SpaModeTest.php | 16 ---------------- 6 files changed, 17 insertions(+), 29 deletions(-) diff --git a/docs/05-panel-configuration.md b/docs/05-panel-configuration.md index 393b55c977..f436acfa27 100644 --- a/docs/05-panel-configuration.md +++ b/docs/05-panel-configuration.md @@ -222,7 +222,7 @@ public function panel(Panel $panel): Panel SPA prefetching enhances the user experience by automatically prefetching pages when users hover over links, making navigation feel even more responsive. This feature utilizes [Livewire's `wire:navigate.hover` functionality](https://livewire.laravel.com/docs/navigate#prefetching-links). -To enable SPA mode with prefetching, you can pass the `prefetch` parameter to the `spa()` method: +To enable SPA mode with prefetching, you can pass the `hasPrefetching: true` parameter to the `spa()` method: ```php use Filament\Panel; @@ -231,7 +231,7 @@ public function panel(Panel $panel): Panel { return $panel // ... - ->spa(prefetch: true); + ->spa(hasPrefetching: true); } ``` @@ -241,6 +241,10 @@ When prefetching is enabled, all links within your panel will automatically incl Prefetching only works when SPA mode is enabled. If you disable SPA mode, prefetching will also be disabled automatically. + + ## Unsaved changes alerts You may alert users if they attempt to navigate away from a page without saving their changes. This is applied on [Create](resources/creating-records) and [Edit](resources/editing-records) pages of a resource, as well as any open action modals. To enable this feature, you can use the `unsavedChangesAlerts()` method: diff --git a/packages/panels/src/Panel/Concerns/HasSpaMode.php b/packages/panels/src/Panel/Concerns/HasSpaMode.php index ad0aac748b..8bc9014bd4 100644 --- a/packages/panels/src/Panel/Concerns/HasSpaMode.php +++ b/packages/panels/src/Panel/Concerns/HasSpaMode.php @@ -15,10 +15,10 @@ trait HasSpaMode */ protected array | Closure $spaModeUrlExceptions = []; - public function spa(bool | Closure $condition = true, bool | Closure $prefetch = false): static + public function spa(bool | Closure $condition = true, bool | Closure $hasPrefetching = false): static { $this->hasSpaMode = $condition; - $this->hasSpaPrefetching = $prefetch; + $this->hasSpaPrefetching = $hasPrefetching; return $this; } diff --git a/packages/support/src/Facades/FilamentView.php b/packages/support/src/Facades/FilamentView.php index d1c7cc33c6..f59695d2a6 100644 --- a/packages/support/src/Facades/FilamentView.php +++ b/packages/support/src/Facades/FilamentView.php @@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Facade; /** * @method static bool hasSpaMode(?string $url = null) - * @method static bool hasSpaPrefetching(?string $url = null) + * @method static bool hasSpaPrefetching() * @method static Htmlable renderHook(string $name, string | array | null $scopes = null) * * @see ViewManager @@ -31,10 +31,10 @@ class FilamentView extends Facade }); } - public static function spa(bool $condition = true, bool $prefetch = false): void + public static function spa(bool $condition = true, bool $hasPrefetching = false): void { - static::resolved(function (ViewManager $viewManager) use ($condition, $prefetch): void { - $viewManager->spa($condition, $prefetch); + static::resolved(function (ViewManager $viewManager) use ($condition, $hasPrefetching): void { + $viewManager->spa($condition, $hasPrefetching); }); } diff --git a/packages/support/src/View/ViewManager.php b/packages/support/src/View/ViewManager.php index bd37e9ada3..1d5eb328df 100644 --- a/packages/support/src/View/ViewManager.php +++ b/packages/support/src/View/ViewManager.php @@ -78,10 +78,10 @@ class ViewManager return new HtmlString(implode('', $hooks)); } - public function spa(bool $condition = true, bool $prefetch = false): void + public function spa(bool $condition = true, bool $hasPrefetching = false): void { $this->hasSpaMode = $condition; - $this->hasSpaPrefetching = $prefetch; + $this->hasSpaPrefetching = $hasPrefetching; } /** @@ -112,8 +112,8 @@ class ViewManager return is_app_url($url); } - public function hasSpaPrefetching(?string $url = null): bool + public function hasSpaPrefetching(): bool { - return $this->hasSpaMode($url) && $this->hasSpaPrefetching; + return $this->hasSpaPrefetching; } } diff --git a/packages/support/src/helpers.php b/packages/support/src/helpers.php index f915090981..c24ea4c7cd 100644 --- a/packages/support/src/helpers.php +++ b/packages/support/src/helpers.php @@ -134,7 +134,7 @@ if (! function_exists('Filament\Support\generate_href_html')) { } elseif ($shouldOpenInSpaMode ?? (FilamentView::hasSpaMode($url))) { $html .= ' wire:navigate'; - if (FilamentView::hasSpaPrefetching($url)) { + if (FilamentView::hasSpaPrefetching()) { $html .= '.hover'; } } diff --git a/tests/src/Support/SpaModeTest.php b/tests/src/Support/SpaModeTest.php index 7ad7578f05..20375d75d6 100644 --- a/tests/src/Support/SpaModeTest.php +++ b/tests/src/Support/SpaModeTest.php @@ -27,14 +27,6 @@ test('SPA prefetching can be toggled', function (): void { expect(FilamentView::hasSpaPrefetching())->toBeFalse(); }); -test('SPA prefetching requires SPA mode to be enabled', function (): void { - FilamentView::spa(false, true); - expect(FilamentView::hasSpaPrefetching())->toBeFalse(); - - FilamentView::spa(true, true); - expect(FilamentView::hasSpaPrefetching())->toBeTrue(); -}); - test('`href` HTML can be generated with `wire:navigate` based on SPA mode', function (): void { FilamentView::spa(); expect(generate_href_html('http://localhost/page')) @@ -95,14 +87,6 @@ test('SPA URL exceptions work correctly', function (): void { expect(FilamentView::hasSpaMode('http://localhost/dashboard'))->toBeTrue(); }); -test('SPA prefetching respects URL exceptions', function (): void { - FilamentView::spa(true, true); - FilamentView::spaUrlExceptions(['*/admin/*']); - - expect(FilamentView::hasSpaPrefetching('http://localhost/admin/users'))->toBeFalse(); - expect(FilamentView::hasSpaPrefetching('http://localhost/dashboard'))->toBeTrue(); -}); - test('`href` HTML respects SPA URL exceptions for prefetching', function (): void { FilamentView::spa(true, true); FilamentView::spaUrlExceptions(['*/admin/*']);