kitbash Sign in Connect agent
Components

Two-part bar

A bar of two equal cells, violet for the active one and grey for the other, that sticks to the top for the rest of the page. Under it a centred serif title and statement; choosing the other cell cross-fades to its own title.

Part of the style

Violet Difference

7 parts that belong together, with their tokens, type and rules.

Preview

Code
<style>
/* Goes after the pack's base.css */
@import url("https://fonts.googleapis.com/css2?family=Sofia+Sans+Condensed:wght@700;800;900&family=Inter+Tight:wght@400;500;600;700&family=Nothing+You+Could+Do&display=swap");
:root {
  --paper: #04578b;
  --ink: #fff4df;
  --idle: #3973a1;
  --tray: #185e91;
  --plate: #6090b9;
  --violet: #867359;
  --font-mark: "Sofia Sans Condensed", "Arial Narrow", sans-serif;
  --font-caps: "Sofia Sans Condensed", "Arial Narrow", sans-serif;
  --font-text: "Inter Tight", system-ui, sans-serif;
}
</style>

<section class="tabs">
  <div class="tabs__bar" role="tablist" aria-label="Sections">
    <button class="tabs__cell is-active" type="button" role="tab" aria-selected="true">Quarries</button>
    <button class="tabs__cell" type="button" role="tab" aria-selected="false">Stockists</button>
  </div>
  <div class="tabs__panels">
    <div class="tabs__panel is-active" role="tabpanel">
      <h2 class="tabs__title">Stone with a home address</h2>
      <p class="tabs__statement">A short list of family quarries, each still cutting the hill its own village was built&nbsp;from.</p>
    </div>
    <div class="tabs__panel" role="tabpanel" aria-hidden="true">
      <h2 class="tabs__title">See it before you spec it</h2>
      <p class="tabs__statement">Yards and showrooms that keep our slabs on the floor, where you can walk round them, not in a&nbsp;catalogue.</p>
    </div>
  </div>
</section>

<style>
  /* With more of the page below it, the root makes no box, so the bar stays stuck all the way down instead of leaving with this section. */
  .tabs:has(~ :not(style, script)) {
    display: contents;
  }

  .tabs__bar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  .tabs__cell {
    min-height: var(--bar);
    padding: 0 var(--gutter);
    border: 0;
    border-radius: 0;
    background: var(--idle);
    color: var(--ink);
    font-family: var(--font-caps);
    font-size: var(--caps-m);
    line-height: 1;
    text-transform: uppercase;
    cursor: pointer;
  }

  /* Violet marks the active cell until the pointer enters the bar; then it follows the pointer. The swap is instant. */
  .tabs__bar:not(:hover) .tabs__cell.is-active,
  .tabs__cell:hover,
  .tabs__cell:focus-visible {
    background: var(--violet);
  }
  .tabs__cell:focus-visible {
    outline: var(--rule) solid var(--ink);
    outline-offset: calc(-4 * var(--rule));
  }
  @media (hover: none) {
    .tabs__cell.is-active {
      background: var(--violet);
    }
    .tabs__cell:not(.is-active):hover {
      background: var(--idle);
    }
  }

  .tabs__panels {
    display: grid;
    padding: var(--space-xl) var(--gutter);
    background: var(--paper);
    font-family: var(--font-caps);
    line-height: var(--leading-caps);
    text-align: center;
    text-transform: uppercase;
  }
  .tabs__panel {
    grid-area: 1 / 1;
    align-self: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--time) var(--ease), visibility 0s var(--time);
  }
  .tabs__panel.is-active {
    opacity: 1;
    visibility: visible;
    transition-delay: 0s;
  }
  .tabs__title {
    margin: 0 0 var(--space-m);
    font-size: var(--caps-xxl);
    font-weight: 400;
    line-height: 1;
    text-wrap: balance;
  }
  .tabs__statement {
    max-width: 53rem;
    margin: 0 auto;
    font-size: var(--caps-s);
    line-height: 1.3;
    text-wrap: balance;
  }

  @media (prefers-reduced-motion: reduce) {
    .tabs__panel {
      transition-duration: 0s;
      transition-delay: 0s;
    }
  }
</style>

<script>
(() => {
  document.querySelectorAll(".tabs").forEach((root) => {
    const cells = [...root.querySelectorAll(".tabs__cell")];
    const panels = [...root.querySelectorAll(".tabs__panel")];

    const show = (chosen) => {
      cells.forEach((cell, index) => {
        cell.classList.toggle("is-active", index === chosen);
        cell.setAttribute("aria-selected", String(index === chosen));
      });
      panels.forEach((panel, index) => {
        panel.classList.toggle("is-active", index === chosen);
        panel.toggleAttribute("aria-hidden", index !== chosen);
      });
    };

    cells.forEach((cell, index) => cell.addEventListener("click", () => show(index)));
  });
})();
</script>

Similar components

Dot-grid crafts

Dot-grid crafts

Keycap control bar

Keycap control bar

Segmented showcase

Segmented showcase

Daylight readout

Daylight readout

Sound notice

Sound notice

Screen modes

Screen modes

App screen showcase

App screen showcase

Sky panel with tabs

Sky panel with tabs