Skip to content

Commit

Permalink
Merge pull request #690 from hlxsites/682-page-tabs-on-mobile
Browse files Browse the repository at this point in the history
682 page tabs on mobile
  • Loading branch information
davenichols-DHLS authored Jan 24, 2024
2 parents 4926ef3 + d942bb3 commit 5089f2c
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions blocks/page-tabs/page-tabs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
a, div, li, nav, span, ul,
a, div, li, nav, option, select, span, ul,
} from '../../scripts/dom-builder.js';
import { decorateIcons } from '../../scripts/lib-franklin.js';
import { getProductResponse } from '../../scripts/scripts.js';
Expand Down Expand Up @@ -64,6 +64,23 @@ function createTabList(tabs, currentTab) {
);
}

// For mobile view
function createDropdownList(tabs, currentTab) {
const dropdownWrapper = div(
{ class: 'block w-full px-4 py-2 bg-white md:hidden order-last' },
select(
{ id: 'selectedTab', class: 'block w-auto py-2 pl-4 text-base border border-gray-300 rounded text-danaherblue-600 focus:outline-none' },
...tabs.map((tab) => {
const isSelectedTab = tab.id === currentTab;
const navItem = option({ value: tab.name }, tab.name);
if (isSelectedTab) navItem.setAttribute('selected', isSelectedTab);
return navItem;
}),
),
);
return dropdownWrapper;
}

function hasProducts(productResponse) {
return productResponse?.raw?.objecttype === 'Family' && productResponse?.raw?.numproducts > 0;
}
Expand Down Expand Up @@ -141,16 +158,25 @@ export default async function decorate(block) {
});

const navList = createTabList(filteredTabs, currentTab);

const navElement = nav(
div({ class: 'flex justify-center' }, navList),
);

// For Mobile View
const dropdownList = createDropdownList(tabs, currentTab);
main.prepend(dropdownList);

block.innerHTML = '';
block.append(navElement);
pageTabsContainer.classList.add(...'hidden mb-4 -mt-16 md:block !p-0'.split(' '));
}

const selectEl = document.getElementById('selectedTab');
selectEl.addEventListener('change', (event) => {
const innerText = event.target.value;
window.location.hash = `#${innerText.toLowerCase()}`;
});

window.addEventListener('hashchange', () => {
const currentTab = window.location.hash?.replace('#', '') || tabSections[0].getAttribute('.aria-labelledby');
if (!currentTab) return;
Expand Down

0 comments on commit 5089f2c

Please sign in to comment.