-
Notifications
You must be signed in to change notification settings - Fork 22
✨ Add mobile-friendly select dropdown for Bootstrap nav-tabs #818
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
Open
n0ctua
wants to merge
1
commit into
main
Choose a base branch
from
noctua/responsive_tabs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Render a <select> above Bootstrap nav tabs for small screens; keep tabs on md+ | ||
| import { Tab } from 'bootstrap' | ||
|
|
||
| // Collect tab toggles with their targets and labels | ||
| function getToggleTargets(nav: HTMLElement): Array<{ el: HTMLElement; target: string; label: string }> { | ||
| const toggles = Array.from(nav.querySelectorAll<HTMLElement>('.nav-link[data-bs-toggle="tab"]')) | ||
| return toggles | ||
| .map((el) => { | ||
| const target = el.getAttribute('data-bs-target') || (el as HTMLAnchorElement).getAttribute?.('href') || '' | ||
| const label = (el.textContent || '').replace(/\s+/g, ' ').trim() | ||
| return target ? { el, target, label } : null | ||
| }) | ||
| .filter((x): x is { el: HTMLElement; target: string; label: string } => x !== null) | ||
| } | ||
|
|
||
| // Determine tab items for overflow detection | ||
| function getTabItems(nav: HTMLElement): HTMLElement[] { | ||
| const items = Array.from(nav.querySelectorAll<HTMLElement>('li.nav-item')) | ||
| if (items.length > 0) return items | ||
| return Array.from(nav.querySelectorAll<HTMLElement>('.nav-link')) | ||
| } | ||
|
|
||
| // Detect if tabs overflow the container or wrap to a new line | ||
| function isOverflowingOrWrapped(nav: HTMLElement): boolean { | ||
| const wasHidden = nav.classList.contains('d-none') | ||
| if (wasHidden) nav.classList.remove('d-none') | ||
|
|
||
| const items = getTabItems(nav) | ||
| if (items.length <= 1) { | ||
| if (wasHidden) nav.classList.add('d-none') | ||
| return false | ||
| } | ||
|
|
||
| const containerWidth = nav.clientWidth | ||
| const totalWidth = items.reduce((sum, el) => sum + el.offsetWidth, 0) | ||
| const firstTop = items[0].offsetTop | ||
| const wrapped = items.some((el) => el.offsetTop > firstTop) | ||
| const overflowing = totalWidth > containerWidth || nav.scrollWidth > nav.clientWidth | ||
|
|
||
| if (wasHidden) nav.classList.add('d-none') | ||
| return wrapped || overflowing | ||
| } | ||
|
|
||
| // Render <select> and keep it in sync | ||
| function initSelectForNavTabs(nav: HTMLElement): void { | ||
| if (nav.dataset.mobileSelectInitialized === 'true') return | ||
| const items = getToggleTargets(nav) | ||
| if (items.length === 0) return | ||
|
|
||
| nav.dataset.mobileSelectInitialized = 'true' | ||
|
|
||
| const select = document.createElement('select') | ||
| select.className = 'form-select mb-0 rounded-0' | ||
| select.setAttribute('aria-label', 'Select tab') | ||
| select.classList.add('d-none') | ||
|
|
||
| items.forEach(({ el, target, label }) => { | ||
| const opt = document.createElement('option') | ||
| opt.value = target | ||
| opt.textContent = label | ||
| if (el.classList.contains('active')) opt.selected = true | ||
| select.appendChild(opt) | ||
| }) | ||
|
|
||
| select.addEventListener('change', () => { | ||
| const { value } = select | ||
| const match = items.find((i) => i.target === value) | ||
| if (match) { | ||
| Tab.getOrCreateInstance(match.el).show() | ||
| } | ||
| }) | ||
|
|
||
| // Insert select before the tab list | ||
| nav.parentElement?.insertBefore(select, nav) | ||
|
|
||
| // Toggle between select and tab bar based on available space | ||
| function updateVisibility(): void { | ||
| const useSelect = isOverflowingOrWrapped(nav) | ||
| if (useSelect) { | ||
| select.classList.remove('d-none') | ||
| nav.classList.add('d-none') | ||
| } else { | ||
| select.classList.add('d-none') | ||
| nav.classList.remove('d-none') | ||
| } | ||
| } | ||
|
|
||
| updateVisibility() | ||
|
|
||
| let resizeTimer: number | undefined | ||
| window.addEventListener('resize', () => { | ||
| window.clearTimeout(resizeTimer) | ||
| resizeTimer = window.setTimeout(updateVisibility, 100) | ||
| }) | ||
|
|
||
| // Reevaluate on content size changes | ||
| if ('ResizeObserver' in window) { | ||
| const ro = new ResizeObserver(() => updateVisibility()) | ||
| ro.observe(nav) | ||
| if (nav.parentElement) { | ||
| ro.observe(nav.parentElement) | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener('load', updateVisibility) | ||
|
|
||
| // Keep select in sync when changed back to tab bar | ||
| nav.addEventListener('shown.bs.tab' as any, (e: Event) => { | ||
| const active = e.target as HTMLElement | null | ||
| if (!active) return | ||
| const activeTarget = active.getAttribute('data-bs-target') || (active as HTMLAnchorElement).getAttribute?.('href') | ||
| if (!activeTarget) return | ||
| if (select.value !== activeTarget) { | ||
| select.value = activeTarget | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| function initAll(): void { | ||
| document.querySelectorAll<HTMLElement>('ul.nav.nav-tabs').forEach(initSelectForNavTabs) | ||
| } | ||
|
|
||
| if (typeof document !== 'undefined') { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would |
||
| if (document.readyState === 'loading') { | ||
| document.addEventListener('DOMContentLoaded', initAll) | ||
| } else { | ||
| initAll() | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only target browsers with support for
ResizeObserver, so we can omit timer-based workarounds.