Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div role="tabs">
    <button id="btn-move-left" class="controlButton" tabindex="-1" aria-hidden="true">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
        <path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" />
      </svg>
    </button>
    <div class="tablistFrame">
      <div role="tablist">
        <button role="tab" id="cool-tab-1" aria-controls="cool-panel-1" aria-selected="true" data-text="Super Cool Menu 1" tabindex="0">Super Cool Menu 1</button>
        <button role="tab" id="cool-tab-2" aria-controls="cool-panel-2" data-text="Super Cool Menu 2" tabindex="-1">Super Cool Menu 2</button>
        <button role="tab" id="cool-tab-3" aria-controls="cool-panel-3" data-text="Super Cool Menu 3" tabindex="-1">Super Cool Menu 3</button>
        <button role="tab" id="cool-tab-4" data-text="Super Cool Menu 4" aria-controls="cool-panel-4" tabindex="-1">Super Cool Menu 4</button>
        <button role="tab" id="cool-tab-5" data-text="Super Cool Menu 5" aria-controls="cool-panel-5" tabindex="-1">Super Cool Menu 5</button>
        <button role="tab" id="cool-tab-6" data-text="Super Cool Menu 6" aria-controls="cool-panel-6" tabindex="-1">Super Cool Menu 6</button>
        <button role="tab" id="cool-tab-7" data-text="Super Cool Menu 7" aria-controls="cool-panel-7" tabindex="-1">Super Cool Menu 7</button>
      </div>
    </div>
    <button id="btn-move-right" class="controlButton" tabindex="-1" aria-hidden="true">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
        <path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7" />
      </svg>
    </button>
  </div>
  <div class="tabpanelsWrapper">
    <div id="cool-panel-1" aria-labelledby="cool-tab-1" role="tabpanel" tabindex="0">Contents of Super Cool Menu 1</div>
    <div id="cool-panel-2" aria-labelledby="cool-tab-2" role="tabpanel" hidden>Contents of Super Cool Menu 2</div>
    <div id="cool-panel-3" aria-labelledby="cool-tab-3" role="tabpanel" hidden>Contents of Super Cool Menu 3</div>
    <div id="cool-panel-4" aria-labelledby="cool-tab-4" role="tabpanel" hidden>Contents of Super Cool Menu 4</div>
    <div id="cool-panel-5" aria-labelledby="cool-tab-5" role="tabpanel" hidden>Contents of Super Cool Menu 5</div>
    <div id="cool-panel-6" aria-labelledby="cool-tab-6" role="tabpanel" hidden>Contents of Super Cool Menu 6</div>
    <div id="cool-panel-7" aria-labelledby="cool-tab-7" role="tabpanel" hidden>Contents of Super Cool Menu 7</div>
  </div>
</div>
              
            
!

CSS

              
                @import "https://unpkg.com/open-props";
@import "https://unpkg.com/open-props/normalize.min.css";

html {
  background-color: var(--gray-2);
}

/* canceling the dark mode */
:root {
  color-scheme: light;
  --link: var(--indigo-7);
  --link-visited: var(--grape-7);

  --text-1: var(--gray-9);
  --text-2: var(--gray-7);

  --surface-1: var(--gray-0);
  --surface-2: var(--gray-2);
  --surface-3: var(--gray-3);
  --surface-4: var(--gray-7);
}
/* avoiding some Open Props' css rules for showing SVG */
svg {
  stroke: currentColor;
  fill: none;
}

[hidden] {
  display: none;
}


[role="tabs"],
[role="tablist"] {
  display: flex;
  min-width: 0;
  flex-wrap: nowrap;
  flex: 1;
}
[role="tablist"]::-webkit-scrollbar {
  display: none;
}
[role="tablist"] {
  height: 48px;
  width: 100%;
  overflow: auto;
  scrollbar-width: none;
  padding: 4px;
}

[role="tab"] {
  white-space: nowrap;
  background-color: transparent;
  padding-inline: var(--size-3);
  color: var(--text-1);
  border-radius: var(--radius-2);
}
[role="tab"]::before {
  content: attr(data-text);
  font-weight: bold;
  visibility: hidden;
  display: block;
  height: 0;
}
[role="tab"]:not([aria-selected="true"]):hover {
  background-color: var(--gray-3);
}

[role="tab"][aria-selected="true"] {
  background-color: #fff;

  box-shadow: var(--shadow-1);
  font-weight: bold;
}

*:focus-visible {
  transition: none;
  outline-offset: 2px;
  outline-color: var(--cyan-8);
}
button > svg {
  width: 24px;
  height: 24px;
}
.container {
  margin-top: var(--size-4);
}
.tablistFrame {
  position: relative;
  height: 100%;
  flex: 1;
  min-width: 0;
  --display-sahdow-left: none;
  --display-sahdow-right: none;
}

.tablistFrame::before {
  content: "";
  pointer-events: none;
  position: absolute;
  top: 0px;
  left: 0px;
  width: 30px;
  height: 100%;
  background-image: linear-gradient(
    86deg,
    rgba(0, 0, 0, 0.2),
    rgba(0, 0, 0, 0) 6px
  );
  display: var(--display-sahdow-left, none);
}
.tablistFrame::after {
  content: "";
  pointer-events: none;
  position: absolute;
  top: 0px;
  right: 0px;
  width: 30px;
  height: 100%;
  background-image: linear-gradient(
    94deg,
    rgba(0, 0, 0, 0) calc(30px - 4px),
    rgba(0, 0, 0, 0.2)
  );
  display: var(--display-sahdow-right, none);
}

.controlButton {
  padding-block: 0;
  padding-inline: var(--size-1);
  background-color: var(--gray-2);
}
.controlButton:disabled {
  color: var(--gray-4);
}
.tabpanelsWrapper {
  padding-block: var(--size-2);
  padding-inline: var(--size-7);
}

              
            
!

JS

              
                //-- needed elements --
const btnMoveLeft = document.querySelector("#btn-move-left");
const btnMoveRight = document.querySelector("#btn-move-right");
const tablistFrame = document.querySelector(".tablistFrame");
const tablist = document.querySelector("[role=tablist]");
const tabs = document.querySelectorAll("[role=tab]");
const lastTab = tabs[tabs.length - 1];
const firstTab = tabs[0];

let moveRightTargetRect = null;

//-- binds events --
for (const tab of tabs) {
  tab.addEventListener("click", changeActiveTab);
}

// Keydown events for keyboard-accessibility
tablist.addEventListener("keydown", (ev) => {
  switch (ev.key) {
    case "ArrowLeft":
      changeCurrentTabWithKeyboard(ev.target, "previous");
      break;
    case "ArrowRight":
      changeCurrentTabWithKeyboard(ev.target, "next");
      break;
    default:
      break;
  }
});

// scrolling with buttons
btnMoveRight.addEventListener("click", () => {
  const elemsInViewport = document.querySelectorAll("[data-inviewport=true]");
  const a = tablist.getBoundingClientRect();
  const moveTargetElem =
    elemsInViewport[elemsInViewport.length - 1].nextElementSibling;
  const targetRect = moveTargetElem.getBoundingClientRect();
  const newPos = moveTargetElem.offsetLeft + targetRect.width - a.width;
  tablist.scroll({ top: 0, left: newPos, behavior: "smooth" });
});

btnMoveLeft.addEventListener("click", () => {
  const elemsInViewport = document.querySelectorAll("[data-inviewport=true]");
  const a = tablist.getBoundingClientRect();
  const moveTargetElem = elemsInViewport[0].previousElementSibling;
  if (!moveTargetElem) {
    return;
  }

  const targetRect = moveTargetElem.getBoundingClientRect();
  const newPos = moveTargetElem.offsetLeft;
  tablist.scroll({ top: 0, left: newPos, behavior: "smooth" });
});

// observing the positions of those elements
const intersectionObserver = new IntersectionObserver(detectOnEdges, {
  root: tablist,
  threshold: [0.05, 0.97]
});

for (const tabElem of tabs) {
  intersectionObserver.observe(tabElem);
}

// observing whether it needs to show scrollbar or not
const resizeObserver = new ResizeObserver(detectScrollNeeded);
resizeObserver.observe(tablistFrame);

//-- methods --
function detectOnEdges(entries) {
  for (const entry of entries) {
    if (entry.target === firstTab) {
      if (entry.intersectionRatio >= 0.97) {
        disableMoveLeftButton();
      } else {
        enableMoveLeftButton();
      }
    }
    if (entry.target === lastTab) {
      if (entry.intersectionRatio >= 0.97) {
        disableMoveRightButton();
      } else {
        enableMoveRightButton();
      }
    }
    if (entry.isIntersecting) {
      entry.target.setAttribute("data-inviewport", "true");
    } else {
      entry.target.removeAttribute("data-inviewport");
    }
  }
}

function disableMoveLeftButton() {
  tablistFrame.style.setProperty("--display-sahdow-left", "none");
  btnMoveLeft.setAttribute("disabled", "");
}
function enableMoveLeftButton() {
  tablistFrame.style.setProperty("--display-sahdow-left", "block");
  btnMoveLeft.removeAttribute("disabled");
}
function disableMoveRightButton() {
  tablistFrame.style.setProperty("--display-sahdow-right", "none");
  btnMoveRight.setAttribute("disabled", "");
}
function enableMoveRightButton() {
  tablistFrame.style.setProperty("--display-sahdow-right", "block");
  btnMoveRight.removeAttribute("disabled");
}

function detectScrollNeeded(entries) {
  if (entries[0].contentBoxSize[0].inlineSize == tablist.scrollWidth) {
    setButtonsDisplayAttributes("none");
  } else {
    setButtonsDisplayAttributes("block");
  }
}
function setButtonsDisplayAttributes(display) {
  btnMoveLeft.style.display = display;
  btnMoveRight.style.display = display;
}
function changeActiveTabElement(newTarget, oldTarget) {
  const currentPanelId = oldTarget.getAttribute("aria-controls");
  const currentPanel = document.querySelector(`#${currentPanelId}`);
  currentPanel.setAttribute("hidden", "");
  currentPanel.setAttribute("tabindex", "-1");
  oldTarget.removeAttribute("aria-selected");
  oldTarget.setAttribute("tabindex", "-1");

  newTarget.setAttribute("aria-selected", "true");
  newTarget.setAttribute("tabindex", "0");
  const targetPanelId = newTarget.getAttribute("aria-controls");
  const newPanel = document.querySelector(`#${targetPanelId}`);
  newPanel.setAttribute("tabindex", "0");
  newPanel.removeAttribute("hidden");
}
function changeActiveTab(ev) {
  const current = tablist.querySelector("[aria-selected=true]");
  changeActiveTabElement(ev.target, current);
}

function changeCurrentTabWithKeyboard(targetElem, type) {
  const targetIndex = Array.from(tabs).findIndex((elem) => elem === targetElem);
  let newIndex = 0;
  if (type === "next") {
    newIndex = targetIndex + 1;
    if (newIndex >= tabs.length) {
      newIndex = 0;
    }
  } else {
    newIndex = targetIndex - 1;
    if (newIndex < 0) {
      newIndex = tabs.length - 1;
    }
  }
  changeActiveTabElement(tabs[newIndex], tabs[targetIndex]);
  tabs[newIndex].focus();
}

              
            
!
999px

Console