Skip to content

Commit

Permalink
Refreshable navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed May 20, 2024
1 parent f6b3b67 commit c095ccb
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/panels/docs/06-navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ To register new navigation items, you can use the [configuration](configuration)
```php
use Filament\Navigation\NavigationItem;
use Filament\Pages\Dashboard;
use Filament\Panel;
use Filament\Panel;use function Filament\Support\original_request;

public function panel(Panel $panel): Panel
{
Expand All @@ -261,7 +261,7 @@ public function panel(Panel $panel): Panel
NavigationItem::make('dashboard')
->label(fn (): string => __('filament-panels::pages/dashboard.title'))
->url(fn (): string => Dashboard::getUrl())
->isActiveWhen(fn () => request()->routeIs('filament.admin.pages.dashboard')),
->isActiveWhen(fn () => original_request()->routeIs('filament.admin.pages.dashboard')),
// ...
]);
}
Expand Down Expand Up @@ -328,7 +328,7 @@ use App\Filament\Resources\UserResource;
use Filament\Navigation\NavigationBuilder;
use Filament\Navigation\NavigationItem;
use Filament\Pages\Dashboard;
use Filament\Panel;
use Filament\Panel;use function Filament\Support\original_request;

public function panel(Panel $panel): Panel
{
Expand All @@ -338,7 +338,7 @@ public function panel(Panel $panel): Panel
return $builder->items([
NavigationItem::make('Dashboard')
->icon('heroicon-o-home')
->isActiveWhen(fn (): bool => request()->routeIs('filament.admin.pages.dashboard'))
->isActiveWhen(fn (): bool => original_request()->routeIs('filament.admin.pages.dashboard'))
->url(fn (): string => Dashboard::getUrl()),
...UserResource::getNavigationItems(),
...Settings::getNavigationItems(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class="fi-layout flex min-h-screen w-full flex-row-reverse overflow-x-clip"
@if (filament()->hasTopbar())
{{ \Filament\Support\Facades\FilamentView::renderHook(\Filament\View\PanelsRenderHook::TOPBAR_BEFORE, scopes: $livewire->getRenderHookScopes()) }}

<x-filament-panels::topbar :navigation="$navigation" />
@livewire(\Filament\Livewire\Topbar::class)

{{ \Filament\Support\Facades\FilamentView::renderHook(\Filament\View\PanelsRenderHook::TOPBAR_AFTER, scopes: $livewire->getRenderHookScopes()) }}
@endif
Expand Down Expand Up @@ -90,10 +90,7 @@ class="fi-layout flex min-h-screen w-full flex-row-reverse overflow-x-clip"
class="fi-sidebar-close-overlay fixed inset-0 z-30 bg-gray-950/50 transition duration-500 dark:bg-gray-950/75 lg:hidden"
></div>

<x-filament-panels::sidebar
:navigation="$navigation"
class="fi-main-sidebar"
/>
@livewire(\Filament\Livewire\Sidebar::class)

<script>
document.addEventListener('DOMContentLoaded', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/panels/resources/views/livewire/sidebar.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<x-filament-panels::sidebar
:navigation="filament()->getNavigation()"
class="fi-main-sidebar"
/>
3 changes: 3 additions & 0 deletions packages/panels/resources/views/livewire/topbar.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-filament-panels::topbar
:navigation="filament()->getNavigation()"
/>
20 changes: 20 additions & 0 deletions packages/panels/src/Livewire/Sidebar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Filament\Livewire;

use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\Livewire;
use Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware;

class Sidebar extends Component
{
#[On('refresh-sidebar')]
public function refresh(): void {}

public function render(): View
{
return view('filament-panels::livewire.sidebar');
}
}
20 changes: 20 additions & 0 deletions packages/panels/src/Livewire/Topbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Filament\Livewire;

use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\Livewire;
use Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware;

class Topbar extends Component
{
#[On('refresh-topbar')]
public function refresh(): void {}

public function render(): View
{
return view('filament-panels::livewire.topbar');
}
}
3 changes: 2 additions & 1 deletion packages/panels/src/Pages/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Route;
use function Filament\Support\original_request;

abstract class Page extends BasePage
{
Expand Down Expand Up @@ -104,7 +105,7 @@ public static function getNavigationItems(): array
->parentItem(static::getNavigationParentItem())
->icon(static::getNavigationIcon())
->activeIcon(static::getActiveNavigationIcon())
->isActiveWhen(fn (): bool => request()->routeIs(static::getNavigationItemActiveRoutePattern()))
->isActiveWhen(fn (): bool => original_request()->routeIs(static::getNavigationItemActiveRoutePattern()))
->sort(static::getNavigationSort())
->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor())
->badgeTooltip(static::getNavigationBadgeTooltip())
Expand Down
4 changes: 4 additions & 0 deletions packages/panels/src/Panel/Concerns/HasComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Filament\Livewire\DatabaseNotifications;
use Filament\Livewire\GlobalSearch;
use Filament\Livewire\Notifications;
use Filament\Livewire\Sidebar;
use Filament\Livewire\Topbar;
use Filament\Pages\Auth\EditProfile;
use Filament\Pages\Page;
use Filament\Resources\Pages\Page as ResourcePage;
Expand Down Expand Up @@ -486,6 +488,8 @@ protected function registerLivewireComponents(): void
$this->queueLivewireComponentForRegistration(EditProfile::class);
$this->queueLivewireComponentForRegistration(GlobalSearch::class);
$this->queueLivewireComponentForRegistration(Notifications::class);
$this->queueLivewireComponentForRegistration(Sidebar::class);
$this->queueLivewireComponentForRegistration(Topbar::class);

if ($this->hasEmailVerification() && is_subclass_of($emailVerificationRouteAction = $this->getEmailVerificationPromptRouteAction(), Component::class)) {
$this->queueLivewireComponentForRegistration($emailVerificationRouteAction);
Expand Down
3 changes: 2 additions & 1 deletion packages/panels/src/Resources/Pages/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route as RouteFacade;
use function Filament\Support\original_request;

abstract class Page extends BasePage
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public static function getNavigationItems(array $urlParameters = []): array
->parentItem(static::getNavigationParentItem())
->icon(static::getNavigationIcon())
->activeIcon(static::getActiveNavigationIcon())
->isActiveWhen(fn (): bool => request()->routeIs(static::getRouteName()))
->isActiveWhen(fn (): bool => original_request()->routeIs(static::getRouteName()))
->sort(static::getNavigationSort())
->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor())
->url(static::getNavigationUrl($urlParameters)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Pages\Enums\SubNavigationPosition;
use Filament\Resources\Pages\Page;
use Illuminate\Contracts\Support\Htmlable;
use function Filament\Support\original_request;

trait HasNavigation
{
Expand Down Expand Up @@ -66,7 +67,7 @@ public static function getNavigationItems(): array
->parentItem(static::getNavigationParentItem())
->icon(static::getNavigationIcon())
->activeIcon(static::getActiveNavigationIcon())
->isActiveWhen(fn () => request()->routeIs(static::getRouteBaseName() . '.*'))
->isActiveWhen(fn () => original_request()->routeIs(static::getRouteBaseName() . '.*'))
->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor())
->badgeTooltip(static::getNavigationBadgeTooltip())
->sort(static::getNavigationSort())
Expand Down
17 changes: 17 additions & 0 deletions packages/support/src/SupportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use Laravel\Octane\Events\RequestReceived;
use Livewire\Livewire;
use Livewire\Mechanisms\DataStore;
use Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
Expand Down Expand Up @@ -92,6 +94,21 @@ class_exists(RequestReceived::class) && Event::listen(RequestReceived::class, fn
),
);

$this->app->scoped(
'originalRequest',
function () {
if (! Livewire::isLivewireRequest()) {
return request();
}

$persistentMiddleware = app(PersistentMiddleware::class);
$request = invade($persistentMiddleware)->makeFakeRequest();

Check failure on line 105 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.3 - L11.* - prefer-stable

Call to protected method makeFakeRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 105 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.3 - L10.* - prefer-stable

Call to protected method makeFakeRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 105 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.2 - L11.* - prefer-stable

Call to protected method makeFakeRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 105 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.2 - L10.* - prefer-stable

Call to protected method makeFakeRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 105 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-stable

Call to protected method makeFakeRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.
invade($persistentMiddleware)->getRouteFromRequest($request);

Check failure on line 106 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.3 - L11.* - prefer-stable

Call to protected method getRouteFromRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 106 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.3 - L10.* - prefer-stable

Call to protected method getRouteFromRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 106 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.2 - L11.* - prefer-stable

Call to protected method getRouteFromRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 106 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.2 - L10.* - prefer-stable

Call to protected method getRouteFromRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

Check failure on line 106 in packages/support/src/SupportServiceProvider.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-stable

Call to protected method getRouteFromRequest() of class Livewire\Mechanisms\PersistentMiddleware\PersistentMiddleware.

return $request;
},
);

$this->app->bind(DataStore::class, DataStoreOverride::class);
}

Expand Down
8 changes: 8 additions & 0 deletions packages/support/src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Http\Request;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -219,3 +220,10 @@ function generate_search_term_expression(string $search, ?bool $isSearchForcedCa
return Str::lower($search);
}
}

if (! function_exists('Filament\Support\original_request')) {
function original_request(): Request
{
return app('originalRequest');
}
}

0 comments on commit c095ccb

Please sign in to comment.