Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navbar.item :current not working with livewire lazy #685

Open
thierroboto opened this issue Nov 15, 2024 · 5 comments
Open

Navbar.item :current not working with livewire lazy #685

thierroboto opened this issue Nov 15, 2024 · 5 comments

Comments

@thierroboto
Copy link

thierroboto commented Nov 15, 2024

Hi,

In my menu, I have one navbar item that has a badge that is a count of posts. In order to prevent the count from slowing down the menu, i made a livewire component with that item and set it as lazy.

With the same code in the livewire component, the "current" doesn't work and doesnt give it an active background.

The code without livewire that works:

<flux:navlist.item icon="document-magnifying-glass" badge="23" badge-color="blue"
                href="{{ route('documents.index') }}" wire:navigate
                class="menu-item hover:!bg-primary-500/25 hover:!text-white data-[current]:!border-0 data-[current]:!bg-primary-500/25 data-[current]:!text-white">
                {{ __('Documents') }}
            </flux:navlist.item>

The livewire blade component (tried it without the current parameter also):

<div>
    @php
        $currentRoute = request()->route()->getName();
    @endphp
    <flux:navlist.item icon="document-magnifying-glass" badge="{{ $count }}" badge-color="blue"
        href="{{ route('documents.index') }}" wire:navigate :current="$currentRoute === 'documents.index'"
        class="menu-item hover:!bg-primary-500/25 hover:!text-white data-[current]:!border-0 data-[current]:!bg-primary-500/25 data-[current]:!text-white">
        {{ __('Documents') }}
    </flux:navlist.item>
</div>

The livewire component code:

<?php

namespace App\Livewire;

use Illuminate\Support\Facades\Cache;
use Livewire\Component;
use Livewire\Attributes\Locked;

class SidebarDocumentsCount extends Component
{
    #[Locked]
    public $count;

    public function mount()
    {
        $user = auth()->user();
        $this->count = Cache::remember('user-' . $user->id . '-documents-count', 60, function () use ($user) {
            return $user->documents->count();
        });
    }

    public function placeholder(array $params = [])
    {
        return view('livewire.placeholders.sidebar-documents-count', $params);
    }

    public function render()
    {
        return view('livewire.sidebar-documents-count');
    }
}
@ju5t
Copy link

ju5t commented Nov 15, 2024

Can you please check your formatting? It's incredibly difficult to read at the moment.

@thierroboto
Copy link
Author

Sorry about that, i tried a few times, github just wont comply!

@jeffchown
Copy link

jeffchown commented Nov 15, 2024

@thierroboto If you could try to include your code blocks by using 3 backticks instead of 1. e.g.

3 backticks on a line by themselves

...code...

3 backticks on a line by themselves

It should format better.

@thierroboto
Copy link
Author

Thanks for the tip! Done. Much better now.

@joshhanley
Copy link
Member

@thierroboto the problem will be that the lazy component will have a route of /livewire/update when it renders, as it is happening as part of an ajax request.

So you need to ensure the sidebar is rendered on initial load for it to be the correct route when it is rendered.

I would just defer the count by wrapping the query in a boolean, instead of making the whole sidebar lazy, then once rendered use wire:init to call a method to calculate the count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants