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

Keep Tabs and Pills active on refresh #20

Open
semaf opened this issue Nov 12, 2022 · 0 comments
Open

Keep Tabs and Pills active on refresh #20

semaf opened this issue Nov 12, 2022 · 0 comments
Assignees

Comments

@semaf
Copy link

semaf commented Nov 12, 2022

Just want to share this feature to get tabs or pills active after refresh the page. Maybe you will build-in it.

const pillsTab = document.querySelector('#pills-tab');
const pills = pillsTab.querySelectorAll('a[data-bs-toggle="pill"]');

pills.forEach(pill => {
  pill.addEventListener('shown.bs.tab', (event) => {
    const { target } = event;
    const { id: targetId } = target;
    
    savePillId(targetId);
  });
});

const savePillId = (selector) => {
  localStorage.setItem('activePillId', selector);
};

const getPillId = () => {
  const activePillId = localStorage.getItem('activePillId');
  
  // if local storage item is null, show default tab
  if (!activePillId) return;
  
  // call 'show' function
  const someTabTriggerEl = document.querySelector(`#${activePillId}`)
  const tab = new bootstrap.Tab(someTabTriggerEl);

  tab.show();
};

// get pill id on load
getPillId();

Source: https://codepen.io/yaroslav-trach/pen/wvyrJJx?editors=1111

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

2 participants