Skip to content

Commit

Permalink
Merge pull request #1271 from hlxsites/tab-hash-links
Browse files Browse the repository at this point in the history
tab hash link fix
  • Loading branch information
dev-rajneeshkumar authored Nov 9, 2023
2 parents d1a64a5 + eeea789 commit 2078923
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion blocks/tabs-horizontal/tabs-horizontal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
div, li, ul,
} from '../../scripts/dom-helpers.js';
import { toClassName } from '../../scripts/lib-franklin.js';
import { processEmbedFragment } from '../../scripts/scripts.js';

const classActive = 'active';
Expand All @@ -15,6 +16,21 @@ function handleTabClick(e, idx) {
panes[idx].classList.add(classActive);
}

function redirectedNav() {
const hashUrl = window.location.hash.split('#')[1];
setTimeout(() => {
const tab = document.getElementById(hashUrl);
if (tab) {
const tabSection = tab.closest('.section');
tab.click();
window.scroll({
top: tabSection.getBoundingClientRect().top + window.screenY - 150,
behavior: 'smooth',
});
}
}, 500);
}

function buildNav(block) {
const titles = block.querySelectorAll('.tabs-horizontal > div > div:first-child');
const elemWidth = Math.floor(100 / titles.length);
Expand All @@ -24,6 +40,7 @@ function buildNav(block) {
const listItem = li(
{
class: 'tabs-nav-item',
id: toClassName(tabTitle),
style: `width: ${elemWidth}%;`,
onclick: (e) => { handleTabClick(e, idx); },
'aria-label': tabTitle,
Expand Down Expand Up @@ -56,6 +73,6 @@ export default async function decorate(block) {

block.append(nav);
block.append(tabs);

redirectedNav();
return block;
}

0 comments on commit 2078923

Please sign in to comment.