탐색

v2.1.0
탐색

목차

페이지 안의 각 제목을 링크로 나열하고 화면에 보이는 섹션을 CSS로 표시하는 목차입니다.

이 페이지의 내용

목차는 페이지 안의 각 제목을 링크로 만들어 가느다란 세로선 옆에 한 줄씩 나열합니다. 화면에 보이는 섹션의 링크는 잉크색으로 표시되며, 세로선 위의 표시가 링크 사이를 부드럽게 이동합니다. 브라우저가 scroll-target-group을 지원하면 페이지가 현재 섹션을 자동으로 표시합니다. 목록이 스크롤 위치를 추적하고, 대상 섹션이 화면에 보이는 링크가 :target-current 선택자와 일치합니다. 옵저버나 스크립트가 필요 없습니다.

목차

<div class="grid grid-cols(2) gap-lg">  <div class="h(240) overflow-y-auto scroll-area text-sm leading-md fg-text-body">    <h3 class="m(0) fg-text-strong" id="toc-install">Install</h3>    <p>      Add the package and import it after the preset. The stylesheet has no build step of its own.    </p>    <p>It ships as plain CSS files, one per component.</p>    <h3 class="fg-text-strong" id="toc-theme">Theme</h3>    <p>      Moonstone and obsidian share one set of tokens. Override a token and every component follows.    </p>    <p>The brand color is one token.</p>    <h3 class="fg-text-strong" id="toc-corners">Corners</h3>    <p>      Corners turn with continuous curvature where the browser can draw them, and nest by their inset.    </p>    <p>Elsewhere they are circular.</p>    <h3 class="fg-text-strong" id="toc-motion">Motion</h3>    <p>      One easing family. Interaction is quick, explanation calm, and reduced motion keeps the fade.    </p>    <p class="mb(160)">That is the whole system.</p>  </div>  <nav class="toc self-start" aria-label="On this page">    <p class="toc-title">On this page</p>    <ul class="toc-list">      <li><a class="toc-link" href="#toc-install">Install</a></li>      <li><a class="toc-link" href="#toc-theme">Theme</a></li>      <li><a class="toc-link" href="#toc-corners">Corners</a></li>      <li><a class="toc-link" href="#toc-motion">Motion</a></li>    </ul>  </nav></div>
<div className="grid grid-cols(2) gap-lg">  <div className="h(240) overflow-y-auto scroll-area text-sm leading-md fg-text-body">    <h3 className="m(0) fg-text-strong" id="toc-install">Install</h3>    <p>      Add the package and import it after the preset. The stylesheet has no build step of its own.    </p>    <p>It ships as plain CSS files, one per component.</p>    <h3 className="fg-text-strong" id="toc-theme">Theme</h3>    <p>      Moonstone and obsidian share one set of tokens. Override a token and every component follows.    </p>    <p>The brand color is one token.</p>    <h3 className="fg-text-strong" id="toc-corners">Corners</h3>    <p>      Corners turn with continuous curvature where the browser can draw them, and nest by their inset.    </p>    <p>Elsewhere they are circular.</p>    <h3 className="fg-text-strong" id="toc-motion">Motion</h3>    <p>      One easing family. Interaction is quick, explanation calm, and reduced motion keeps the fade.    </p>    <p className="mb(160)">That is the whole system.</p>  </div>  <nav className="toc self-start" aria-label="On this page">    <p className="toc-title">On this page</p>    <ul className="toc-list">      <li><a className="toc-link" href="#toc-install">Install</a></li>      <li><a className="toc-link" href="#toc-theme">Theme</a></li>      <li><a className="toc-link" href="#toc-corners">Corners</a></li>      <li><a className="toc-link" href="#toc-motion">Motion</a></li>    </ul>  </nav></div>
<div class="grid grid-cols(2) gap-lg">  <div class="h(240) overflow-y-auto scroll-area text-sm leading-md fg-text-body">    <h3 class="m(0) fg-text-strong" id="toc-install">Install</h3>    <p>      Add the package and import it after the preset. The stylesheet has no build step of its own.    </p>    <p>It ships as plain CSS files, one per component.</p>    <h3 class="fg-text-strong" id="toc-theme">Theme</h3>    <p>      Moonstone and obsidian share one set of tokens. Override a token and every component follows.    </p>    <p>The brand color is one token.</p>    <h3 class="fg-text-strong" id="toc-corners">Corners</h3>    <p>      Corners turn with continuous curvature where the browser can draw them, and nest by their inset.    </p>    <p>Elsewhere they are circular.</p>    <h3 class="fg-text-strong" id="toc-motion">Motion</h3>    <p>      One easing family. Interaction is quick, explanation calm, and reduced motion keeps the fade.    </p>    <p class="mb(160)">That is the whole system.</p>  </div>  <nav class="toc self-start" aria-label="On this page">    <p class="toc-title">On this page</p>    <ul class="toc-list">      <li><a class="toc-link" href="#toc-install">Install</a></li>      <li><a class="toc-link" href="#toc-theme">Theme</a></li>      <li><a class="toc-link" href="#toc-corners">Corners</a></li>      <li><a class="toc-link" href="#toc-motion">Motion</a></li>    </ul>  </nav></div>
<template>  <div class="grid grid-cols(2) gap-lg">    <div class="h(240) overflow-y-auto scroll-area text-sm leading-md fg-text-body">      <h3 class="m(0) fg-text-strong" id="toc-install">Install</h3>      <p>        Add the package and import it after the preset. The stylesheet has no build step of its own.      </p>      <p>It ships as plain CSS files, one per component.</p>      <h3 class="fg-text-strong" id="toc-theme">Theme</h3>      <p>        Moonstone and obsidian share one set of tokens. Override a token and every component follows.      </p>      <p>The brand color is one token.</p>      <h3 class="fg-text-strong" id="toc-corners">Corners</h3>      <p>        Corners turn with continuous curvature where the browser can draw them, and nest by their inset.      </p>      <p>Elsewhere they are circular.</p>      <h3 class="fg-text-strong" id="toc-motion">Motion</h3>      <p>        One easing family. Interaction is quick, explanation calm, and reduced motion keeps the fade.      </p>      <p class="mb(160)">That is the whole system.</p>    </div>    <nav class="toc self-start" aria-label="On this page">      <p class="toc-title">On this page</p>      <ul class="toc-list">        <li><a class="toc-link" href="#toc-install">Install</a></li>        <li><a class="toc-link" href="#toc-theme">Theme</a></li>        <li><a class="toc-link" href="#toc-corners">Corners</a></li>        <li><a class="toc-link" href="#toc-motion">Motion</a></li>      </ul>    </nav>  </div></template>
본문을 스크롤하면 표시가 화면에 보이는 섹션을 따라갑니다. scroll-target-group을 지원하지 않는 브라우저에서는 스크립트가 aria-current를 설정해야 표시가 나타납니다.

#목차 계층

목록 항목 안에 toc-list를 넣으면 한 단계 더 깊은 계층이 됩니다. 같은 세로선을 공유하며 링크는 한 단계 들여씁니다. 한 페이지에는 두 단계면 충분합니다. 세 번째 단계가 필요하다면 페이지를 나눌 시점입니다.

목차 계층

<nav class="toc max-w(220)" aria-label="On this page">  <ul class="toc-list">    <li><a class="toc-link" href="#">Overview</a></li>    <li>      <a class="toc-link" href="#" aria-current="true">Theme</a>      <ul class="toc-list">        <li><a class="toc-link" href="#">Surfaces</a></li>        <li><a class="toc-link" href="#">Lines and shadows</a></li>        <li><a class="toc-link" href="#">The brand color</a></li>      </ul>    </li>    <li><a class="toc-link" href="#">Accessibility</a></li>  </ul></nav>
<nav className="toc max-w(220)" aria-label="On this page">  <ul className="toc-list">    <li><a className="toc-link" href="#">Overview</a></li>    <li>      <a className="toc-link" href="#" aria-current="true">Theme</a>      <ul className="toc-list">        <li><a className="toc-link" href="#">Surfaces</a></li>        <li><a className="toc-link" href="#">Lines and shadows</a></li>        <li><a className="toc-link" href="#">The brand color</a></li>      </ul>    </li>    <li><a className="toc-link" href="#">Accessibility</a></li>  </ul></nav>
<nav class="toc max-w(220)" aria-label="On this page">  <ul class="toc-list">    <li><a class="toc-link" href="#">Overview</a></li>    <li>      <a class="toc-link" href="#" aria-current="true">Theme</a>      <ul class="toc-list">        <li><a class="toc-link" href="#">Surfaces</a></li>        <li><a class="toc-link" href="#">Lines and shadows</a></li>        <li><a class="toc-link" href="#">The brand color</a></li>      </ul>    </li>    <li><a class="toc-link" href="#">Accessibility</a></li>  </ul></nav>
<template>  <nav class="toc max-w(220)" aria-label="On this page">    <ul class="toc-list">      <li><a class="toc-link" href="#">Overview</a></li>      <li>        <a class="toc-link" href="#" aria-current="true">Theme</a>        <ul class="toc-list">          <li><a class="toc-link" href="#">Surfaces</a></li>          <li><a class="toc-link" href="#">Lines and shadows</a></li>          <li><a class="toc-link" href="#">The brand color</a></li>        </ul>      </li>      <li><a class="toc-link" href="#">Accessibility</a></li>    </ul>  </nav></template>

#현재 섹션 직접 표시

링크에 aria-current="true"를 지정하면 :target-current 선택자와 일치할 때와 같은 상태로 표시됩니다. 정적 목록에서는 서버에서 설정하고, scroll-target-group을 지원하지 않는 환경에서는 IntersectionObserver로 설정하세요. 둘 다 적용하면 화면에 보이는 같은 섹션을 기준으로 하므로 표시가 일치합니다.

#접근성

  • 목록은 <nav>이며, ‘이 페이지의 목차’와 같은 레이블을 붙입니다. 이렇게 하면 페이지의 랜드마크에서 목차를 찾고 주 탐색 영역과 구별할 수 있습니다.
  • 링크는 id를 통해 해당 제목으로 이동합니다. 스크롤 영역 상단에 내비게이션 바가 고정되어 있다면, 이동한 제목이 내비게이션 바에 가리지 않도록 스크롤 영역에 scroll-padding-top을 지정하세요.
  • 현재 링크의 잉크색이 바뀌고, 그 옆에 1.5px 두께의 잉크색 표시가 나타납니다. 두 변화 모두 색상 계열의 차이에 의존하지 않습니다. 동작 줄이기가 설정되어 있으면 표시가 애니메이션 없이 이동합니다.

#클래스

클래스설명
toc"이 페이지의 목차"와 같은 레이블을 지정한 nav 요소에 적용하는 컨테이너입니다. 스크롤 대상 그룹으로 동작합니다.
toc-title목록 위의 캡션
toc-list가는 레일을 따라 배치된 링크 목록입니다. 목록 항목 안의 toc-list는 하위 단계입니다. 같은 레일을 공유하고 링크는 한 단계 들여씁니다.
toc-link제목의 id로 연결되는 링크입니다. 현재 화면에 보이는 섹션의 링크는 기본 글자색으로 강조하고 레일에 표시를 붙입니다.

#사용자 지정 속성

속성기본값설명
--toc-indent.875rem레일과 텍스트 사이의 간격이자 한 단계의 들여쓰기 너비

#소스 코드

@master/ui/components/toc.css
/* * Table of contents: the headings of a page as a quiet column of links on a hairline rail. * The link of the section in view is in ink, with a mark on the rail. Where the browser supports * scroll-target-group the page marks it by itself: the list watches the scroll and the link whose target is * in view matches :target-current. A script or a server can set aria-current instead; both draw the same. * With anchor positioning the mark is one piece that glides along the rail. */@layer components.ui {  .toc {    --toc-indent: .875rem;    position: relative;    display: grid;    min-width: 0;    font-size: .8125rem;    line-height: 1.4;    scroll-target-group: auto;  }  /* A caption over the list. */  .toc-title {    margin: 0 0 .625rem;    color: var(--color-text-strong);    font-size: .75rem;    font-weight: var(--font-weight-control);    line-height: 1.4;    text-wrap: balance;  }  /* The list draws the rail; a list inside a list is a deeper level and shares the same rail. */  .toc-list {    display: grid;    min-width: 0;    margin: 0;    padding: 0;    box-shadow: inset 1px 0 0 var(--color-line-divider);    list-style: none;    &:dir(rtl) {      box-shadow: inset -1px 0 0 var(--color-line-divider);    }    .toc-list {      box-shadow: none;    }    .toc-list .toc-link {      padding-inline-start: calc(var(--toc-indent) * 2);    }    /* Each deeper level is one more step in. */    .toc-list .toc-list .toc-link {      padding-inline-start: calc(var(--toc-indent) * 3);    }    /* Shadows are not drawn here, so the rail becomes a rule of its own. */    @media (forced-colors: active) {      border-inline-start: 1px solid CanvasText;      .toc-list {        border-inline-start: 0;      }    }  }  .toc-link {    position: relative;    display: block;    padding-block: .3125rem;    padding-inline: var(--toc-indent) .5rem;    color: var(--color-text-muted);    text-decoration: none;    text-wrap: pretty;    overflow-wrap: anywhere;    -webkit-tap-highlight-color: transparent;    touch-action: manipulation;    /* A row answers the pointer at once: no fade from link to link as the pointer runs down the column. */    @media (hover: hover) {      &:hover {        color: var(--color-text-strong);      }    }    /* Pressed: the strong ink, as under the pointer, so a tap is answered too. */    &:active {      color: var(--color-text-strong);    }    &:is(:target-current, [aria-current]:not([aria-current="false"])) {      color: var(--color-text-strong);    }    /* The mark: a short piece of ink on the rail, with round ends, a few pixels in from the row's edges. */    &:is(:target-current, [aria-current]:not([aria-current="false"]))::before {      content: '';      position: absolute;      inset-block: .3125rem;      inset-inline-start: 0;      width: 1.5px;      border-radius: 999px;      background: var(--color-text-strong);    }    /*     * On touch a link is a full row of 44px, as a nav's rows are: the same words with more room over and under     * them, and the mark keeps to the words.     */    @media (pointer: coarse) {      padding-block: .8125rem;      &:is(:target-current, [aria-current]:not([aria-current="false"]))::before {        inset-block: .8125rem;      }    }    &:focus-visible {      @apply --focus-ring;      outline-offset: -2px;      border-radius: calc(var(--radius-chip) * var(--corner-scale));      corner-shape: var(--corner-shape);    }    @media (forced-colors: active) {      &:is(:target-current, [aria-current]:not([aria-current="false"])) {        color: Highlight;      }      &:is(:target-current, [aria-current]:not([aria-current="false"]))::before {        background: Highlight;      }    }  }  @supports (anchor-scope: --toc-mark) {    .toc {      anchor-scope: --toc-mark;      &:has(.toc-link:is(:target-current, [aria-current]:not([aria-current="false"])))::before {        content: '';        position: absolute;        position-anchor: --toc-mark;        top: calc(anchor(top) + .3125rem);        bottom: calc(anchor(bottom) + .3125rem);        inset-inline-start: 0;        width: 1.5px;        border-radius: 999px;        background: var(--color-text-strong);        pointer-events: none;        transition-property: top, bottom;        transition-duration: var(--duration-base);        transition-timing-function: var(--easing-glide);        @media (pointer: coarse) {          top: calc(anchor(top) + .8125rem);          bottom: calc(anchor(bottom) + .8125rem);        }      }      @media (prefers-reduced-motion: reduce) {        &::before {          transition: none;        }      }      /* As specific as the mark itself, or its ink would turn to Canvas and cut the rail instead of marking it. */      @media (forced-colors: active) {        &:has(.toc-link:is(:target-current, [aria-current]:not([aria-current="false"])))::before {          background: Highlight;        }      }    }    .toc-link:is(:target-current, [aria-current]:not([aria-current="false"])) {      anchor-name: --toc-mark;    }    .toc-link:is(:target-current, [aria-current]:not([aria-current="false"]))::before {      content: none;    }  }}