Theme
On this page (5)
Component Skill — components/category-menu/component-skill.md

Native basis

A <nav aria-label="Categories"> with a list of links - an icon, the name and a count - and aria-current="page" on the current one.

Web Platform APIs

<nav>aria-currentCSS scroll snap

Classes

.mk-category-menu

Data attributes

data-variant: pills, vertical, tiles.

§Default

Underlined tabs on a rule - links with aria-current="page" - that scroll sideways and snap on small screens.

§Pills

data-variant='pills': pills, the current one filled.

§Vertical

data-variant='vertical': a sidebar list with counts.

§Tiles

data-variant='tiles': icon tiles in a grid - for a shop or help-center front.

§CSS view file

/* -- Category Menu (website · Search & Navigation) ------------------------
   Navigates between content categories - underlined tabs that scroll sideways, pills, a vertical list with counts, or icon tiles. */
@layer components {
  /* every box in the block measures border-box - the same on any page */
  :where(.mk-category-menu, .mk-category-menu *, .mk-category-menu ::before, .mk-category-menu ::after) { box-sizing: border-box; }
  .mk-category-menu { width: 100%;
    & ul { display: flex; gap: 0.25rem; margin: 0; padding: 0; list-style: none; overflow-x: auto; overscroll-behavior-x: contain; scroll-snap-type: x proximity; scrollbar-width: none; border-block-end: 1px solid var(--border); }
    & li { flex: none; scroll-snap-align: start; }
    & a { display: inline-flex; align-items: center; gap: 0.375rem; padding: 0.625rem 0.75rem; margin-block-end: -1px; border-block-end: 2px solid transparent; color: var(--muted-foreground); font-size: 0.875rem; font-weight: 500; text-decoration: none; white-space: nowrap; }
    & a:hover { color: var(--foreground); }
    & a[aria-current] { border-block-end-color: var(--foreground); color: var(--foreground); }
    & a:focus-visible { outline: 2px solid var(--ring); outline-offset: -2px; border-radius: var(--radius-sm); }
    & svg { width: 1rem; height: 1rem; }
    & small { color: var(--muted-foreground); font-size: 0.75rem; font-variant-numeric: tabular-nums; }
  }
  .mk-category-menu[data-variant="pills"] {
    & ul { gap: 0.5rem; border: 0; }
    & a { padding: 0.375rem 0.875rem; margin: 0; border: 1px solid var(--border); border-radius: 999px; }
    & a[aria-current] { border-color: var(--foreground); background: var(--foreground); color: var(--background); }
    & a[aria-current] small { color: inherit; opacity: 0.7; }
  }
  .mk-category-menu[data-variant="vertical"] { max-width: 15rem;
    & ul { flex-direction: column; gap: 0.0625rem; border: 0; overflow: visible; }
    & a { display: flex; margin: 0; padding: 0.5rem 0.75rem; border: 0; border-radius: var(--radius-md); }
    & a span { flex: 1; }
    & a:hover { background: var(--accent); }
    & a[aria-current] { background: var(--accent); font-weight: 600; }
  }
  .mk-category-menu[data-variant="tiles"] {
    & ul { display: grid; grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr)); gap: 0.75rem; border: 0; overflow: visible; }
    & a { display: grid; justify-items: start; gap: 0.25rem; margin: 0; padding: 1rem; border: 1px solid var(--border); border-radius: var(--radius-xl); white-space: normal; }
    & a svg { width: 1.5rem; height: 1.5rem; margin-block-end: 0.5rem; color: var(--primary); }
    & a:hover { border-color: color-mix(in oklch, var(--foreground) 30%, var(--border)); }
    & a[aria-current] { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
  }
  /* -- Accessibility ------------------------------------------- */
  @media (forced-colors: active) {
    .mk-category-menu a[aria-current] { outline: 2px solid Highlight; }
  }
}

Comments, ideas or improvements? Edit this page's source on GitHub