Skip to content

Commit 02acfa1

Browse files
committed
switch tabs when id and hash is used (#215)
When an ID is set on a block that is part of a tabbed group the tab switching and scroll happens as it does when a user clicks on a tab.
1 parent 7685fcd commit 02acfa1

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

preview-src/drivers-tabs.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ GDS also uses tabs, but they have a different list of tab separators. Rather tha
88
[.tabbed-example]
99
====
1010
[.include-with-macos]
11+
[[mac-os-bit]]
1112
======
1213
Tab for macOS
1314
======
1415
[.include-with-linux]
16+
[[linux-bit]]
1517
======
1618
Tab for Linux
1719
======
@@ -105,6 +107,7 @@ Note that this requires that Go modules are enabled.
105107
======
106108
107109
[.include-with-java]
110+
[[java-bit]]
108111
======
109112

110113
To use the Java driver, it is recommended employing a dependency manager, such as Maven or Gradle.

src/js/08-tabs-block.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,23 @@ document.addEventListener('DOMContentLoaded', function () {
205205

206206
parent.removeChild(originalTab)
207207
})
208+
209+
//
210+
// Make active tab based on url hash
211+
//
212+
function decodeFragment (hash) {
213+
return hash && (~hash.indexOf('%') ? decodeURIComponent(hash) : hash).slice(1)
214+
}
215+
216+
var fragment, target, scrollTo
217+
if ((fragment = decodeFragment(window.location.hash)) && (target = document.getElementById(fragment))) {
218+
const langSelection = target.getAttribute('data-lang')
219+
const tabbed = target.closest('.tabbed')
220+
scrollTo = tabbed.querySelector(`[data-lang=${langSelection}]`)
221+
if (scrollTo) {
222+
switchTab({
223+
target: scrollTo,
224+
})
225+
}
226+
}
208227
})

src/js/12-fragment-jumper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
e.preventDefault()
2525
}
2626
var topOffset = toolbar ? toolbar.getBoundingClientRect().bottom : headerNavigationBar.getBoundingClientRect().bottom
27-
27+
var target = this
28+
var tabs
29+
if ((tabs = target.closest('.tabbed'))) {
30+
target = tabs
31+
}
2832
if (cheatSheet) {
2933
var scrollTarget = this.closest('div')
3034
var selectorsTop = document.querySelector('.nav-container .selectors').querySelector('div').getBoundingClientRect().top
3135
if (this.tagName === 'H3') topOffset = selectorsTop
3236
window.scrollTo(0, computePosition(scrollTarget, 0) - topOffset)
3337
} else {
34-
window.scrollTo(0, computePosition(this, 0) - topOffset)
38+
window.scrollTo(0, computePosition(target, 0) - topOffset)
3539
}
3640
}
3741

0 commit comments

Comments
 (0)