overleaf/services/web/frontend/js/features/bookmarkable-tab/index-bs5.ts
Davinder Singh 248fc3699a CMS Bootstrap 5 migration - Tabs using bootstrap 5 classes (#20478)
* adding classes according to bootstrap 5 tabs

* adding JS for tabs handeling in BS5

* adding styling for tabs to match the redesign styling

* making sure tabs are being highlighted when active

* adding a scroll-margin-top to have some extra space

* removing extra import, it is not needed here because we already import it in bootstrap-x.js files

* using the media-breakpoint-down for a media query styling

* introducing .nav-tabs-container to make the tab triggers horizontally scrollable

* creating a mixin box-shadow-button-input under scss for bootstrap-5

* moving border-width-base to tabs.scss

* aligning tabs to the left under screen size of 768px

* removing focus-style mixin from scss files becuase it was a duplicate

GitOrigin-RevId: 45780c62681fc9b61961f5436d2d55de66a976b6
2024-09-25 08:05:21 +00:00

30 lines
728 B
TypeScript

import { Tab } from 'bootstrap-5'
function bookmarkableTab(tabEl: HTMLElement) {
tabEl.addEventListener('click', () => {
window.location.hash = tabEl.getAttribute('href') as string
})
}
function handleHashChange() {
const hash = window.location.hash
if (!hash) return
// Find the bookmarkable tab that links to the hash
const tabEl = document.querySelector(
`[data-ol-bookmarkable-tab][href="${hash}"]`
)
if (!tabEl) return
// Select the tab via Bootstrap 5
const tab = new Tab(tabEl)
tab.show()
}
document
.querySelectorAll('[data-ol-bookmarkable-tab]')
.forEach(tabEl => bookmarkableTab(tabEl as HTMLElement))
window.addEventListener('hashchange', handleHashChange)
handleHashChange()