No notes defined.
<nav class="ds2-secondary-nav">
<div class="ds2-secondary-nav__inner">
<div class="ds2-secondary-nav__arrow-container ds2-secondary-nav__arrow-container--left">
<button class="ds2-secondary-nav__arrow">←</button>
</div>
<ul class="ds2-secondary-nav__links">
{% for link in links %}
<li><a class="ds2-secondary-nav__link {% if link.selected %}ds2-secondary-nav__link--selected{% endif %}" href="{{ link.url }}">{{ link.title }}</a></li>
{% endfor %}
</ul>
<div class="ds2-secondary-nav__arrow-container ds2-secondary-nav__arrow-container--right">
<button class="ds2-secondary-nav__arrow">→</button>
</div>
</div>
</nav>
<nav class="ds2-secondary-nav">
<div class="ds2-secondary-nav__inner">
<div class="ds2-secondary-nav__arrow-container ds2-secondary-nav__arrow-container--left">
<button class="ds2-secondary-nav__arrow">←</button>
</div>
<ul class="ds2-secondary-nav__links">
<li><a class="ds2-secondary-nav__link ds2-secondary-nav__link--selected" href="#">Option 1</a></li>
<li><a class="ds2-secondary-nav__link " href="#">Option 2</a></li>
<li><a class="ds2-secondary-nav__link " href="#">Option 3</a></li>
<li><a class="ds2-secondary-nav__link " href="#">Option 4</a></li>
<li><a class="ds2-secondary-nav__link " href="#">Option 5</a></li>
</ul>
<div class="ds2-secondary-nav__arrow-container ds2-secondary-nav__arrow-container--right">
<button class="ds2-secondary-nav__arrow">→</button>
</div>
</div>
</nav>
.ds2-secondary-nav {
$arrowSize: 32px;
$navButtonSize: 60px;
$secondaryNavGaps: 24px;
color: black;
margin: 0 auto;
display: flex;
justify-content: center;
background: $color-grey--faded;
// to stop scrollbar showing
::-webkit-scrollbar{
width: 0;
height: 0
}
::-webkit-scrollbar-thumb{
background:transparent
}
::-webkit-scrollbar-track {
background-color:transparent
}
&__inner {
width: 100%;
max-width: 1400px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
position: relative;
@include media(">=uol-media-m") {
justify-content: center;
}
}
&__links {
display: flex;
align-items: center;
justify-content: left;
overflow: auto;
white-space: nowrap;
scroll-behavior: smooth;
max-width: calc(100% - 2 * #{$navButtonSize});
margin: 0;
padding: 0;
left: $navButtonSize;
list-style: none;
visibility: hidden;
@include media(">=uol-media-m") {
position: relative;
justify-content: center;
left: 0;
visibility: visible;
}
}
&__link {
font-family: uolInter, Arial, Helvetica, sans-serif;
font-size: 16px;
font-style: normal;
font-weight: 400;
text-decoration: none;
padding: 16px 24px;
border-radius: 6px;
@include media(">=uol-media-m") {
display: flex;
}
&:focus {
outline: thin dotted;
}
&:active,
&:hover {
text-decoration: underline;
}
}
&__arrow-container {
background: $color-grey--faded;
width: $navButtonSize;
height: 80px;
display: flex;
align-items: center;
justify-content: center;
z-index: 10;
&--disabled {
opacity: 0.2;
}
}
&__arrow {
background: inherit;
border: 0;
padding: 0;
appearance: none;
color: black;
font-weight: bold;
height: $arrowSize;
width: $arrowSize;
display: flex;
justify-content: space-between;
align-items: center;
justify-content: center;
font-size: 16px;
z-index: 2;
@include media(">=uol-media-m") {
display: none;
}
}
&__link--selected {
background: $color-brand-teal;
display: flex;
}
}
export const ds2Secondary = () => {
document.addEventListener("DOMContentLoaded", function () {
const secondaryNav = document.querySelectorAll('.ds2-secondary-nav');
secondaryNav.forEach( ( nav ) => {
let currentItem = 0; // tracks which item is showing in furthest left position
/*
Following variable used to stop left/right arrow function running THEN scroll function
Both functions incremement the current position and don't want it running twice
*/
let keyJustPressed = false;
const rightArrow = nav.querySelectorAll('.ds2-secondary-nav__arrow-container--right')[0];
const leftArrow = nav.querySelectorAll('.ds2-secondary-nav__arrow-container--left')[0];
const linksContainer = nav.querySelectorAll('.ds2-secondary-nav__links')[0];
const links = linksContainer.querySelectorAll('.ds2-secondary-nav__link');
// function to determine if arrows are disabled or not
let currentItemCheck = () => {
if (currentItem == 0) {
leftArrow.classList.add('ds2-secondary-nav__arrow-container--disabled');
} else {
leftArrow.classList.remove('ds2-secondary-nav__arrow-container--disabled');
}
if (currentItem == links.length - 1) {
rightArrow.classList.add('ds2-secondary-nav__arrow-container--disabled');
} else {
rightArrow.classList.remove('ds2-secondary-nav__arrow-container--disabled');
}
};
/*
links container is set to not show in CSS until position establised to stop visual jump to selected item
here, set links to show whenever there's a window resize
*/
window.addEventListener('resize', () => {
linksContainer.style.visibility = "visible";
});
if (window.innerWidth < 768) {
/*
Current item on mobile is in left most position
Extend margin of last item to allow this
*/
let lastItemMargin = window.innerWidth - links[links.length - 1].offsetWidth - 120;
links[links.length - 1].style.marginRight = lastItemMargin + "px";
// get left positions of each item (so arrows can move in to position)
const linksX = [];
linksX[0] = 0;
let currentX = 0;
for (let i = 1; i < links.length; i++) {
linksX[i] = currentX + links[i-1].offsetWidth;
currentX += links[i-1].offsetWidth;
/*
by default scroll is at first item
if another item has selected class, move scroll to this item
*/
if (links[i-1].classList.contains("ds2-secondary-nav__link--selected")) {
currentItem = i - 1;
linksContainer.scrollTo({
left: linksX[currentItem],
behavior: 'smooth'
});
}
}
/*
above will have put scroll in to correct position
now can show the links without a visible jump
*/
linksContainer.style.visibility = "visible";
currentItemCheck();
/*
When using scroll bar we need to adjust current item (used by arrows)
to correlate scroll with arrows
*/
linksContainer.addEventListener('scroll', (event) => {
if (keyJustPressed == false) {
const currentLeft = event.target.scrollLeft;
// variable to see if item changed (if yes, then run currentItemCheck function)
let tempCurrentItem = currentItem;
for (let i = 0; i < links.length; i++) {
if (currentLeft > linksX[i]) {
currentItem = i + 1;
}
if (currentLeft == 0) {
currentItem = 0;
}
}
if (tempCurrentItem != currentItem || currentItem == 0) {
currentItemCheck();
}
keyJustPressed = false;
}
keyJustPressed = false;
});
// Right arrow pressed, changes currentItem and scrolls to next item (if not last item)
rightArrow.addEventListener("click", () => {
if (currentItem < links.length - 1) {
currentItem += 1;
keyJustPressed = true;
currentItemCheck();
linksContainer.scrollTo({
left: linksX[currentItem],
behavior: 'smooth'
});
}
});
// Left arrow pressed, changes currentItem and scrolls to previous item (if not first item)
leftArrow.addEventListener("click", () => {
if (currentItem > 0) {
currentItem -= 1;
keyJustPressed = true;
currentItemCheck();
linksContainer.scrollTo({
left: linksX[currentItem],
behavior: 'smooth'
});
}
});
}
});
});
}
{
"links": [
{
"title": "Option 1",
"url": "#",
"selected": true
},
{
"title": "Option 2",
"url": "#"
},
{
"title": "Option 3",
"url": "#"
},
{
"title": "Option 4",
"url": "#"
},
{
"title": "Option 5",
"url": "#"
}
]
}