Laravel

Bootstrap Tabs with Livewire & Laravel [Solved]

Share your learning

“Bootstrap tabs not working properly with livewire.”

Actually, Few days back I designed a modal with bootstrap tabs. Then I faced this issue that bootstrap tabs were not working with livewire. 

See Also: Bootstrap date-picker with Laravel-Livewire


Why does bootstrap tabs navigation not working properly with livewire?

Because when you perform any livewire action on these tabs, Livewire updates the component’s html too and your selected tab will switch to the default active tab.

See, how these tabs navigation work? 

When we click on the tabs nav link, bootstrap js simply adds the “active” class to the nav link. 

Next it also adds the “active and show” class to a particular tab-pane. This way bootstrap switches your tabs.

So, here is the solution,

Simply add the wire:ignore to each nav-item and wire:ignore.self to a single tab-pane. This will prevent your tabs html to lose the active classes and switch to the default tab.

<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item" role="presentation" wire:ignore >
    <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation" wire:ignore >
    <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation" wire:ignore>
    <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
  </li>
</ul>

<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab" wire:ignore.self>...</div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab" wire:ignore.self>...</div>
  <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab" wire:ignore.self>...</div>
</div>

Hope you find this article useful. Please share your suggestions below in the comment section.

See you in the next article.

Reference : Ignoring DOM-changes (using wire:ignore)

Satpal

View Comments

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago