문서 탐색

UIScrollSpy

현재 읽는 섹션을 알리고 지정한 섹션으로 이동합니다.

기본 사용법

변경 절차 목차와 읽는 위치 연결

절차를 스크롤하면 읽고 있는 항목이 표시됩니다. 목차를 선택하면 해당 절차로 이동하고 제목에 포커스를 줍니다.

요청 접수

변경 요청과 검토 기한을 기록합니다.

담당자 검토

변경 범위와 필요한 검증을 확인합니다.

승인과 반영

승인 후 변경사항을 반영하고 결과를 공유합니다.

<script setup lang="ts">
import { UIButton, UIScrollSpy } from "@connextable/ui";
import { computed, useTemplateRef } from "vue";

const chapters = [
  {
    id: "request",
    title: "요청 접수",
    text: "변경 요청과 검토 기한을 기록합니다.",
  },
  {
    id: "review",
    title: "담당자 검토",
    text: "변경 범위와 필요한 검증을 확인합니다.",
  },
  {
    id: "approval",
    title: "승인과 반영",
    text: "승인 후 변경사항을 반영하고 결과를 공유합니다.",
  },
];
const viewport = useTemplateRef<HTMLElement>("viewport");
const headings = useTemplateRef<HTMLElement[]>("heading");
const sections = computed(() =>
  chapters.flatMap((chapter) => {
    const element = headings.value?.find(
      (heading) => heading.dataset.chapter === chapter.id,
    );
    return element ? [{ id: chapter.id, element }] : [];
  }),
);
</script>

<template>
  <UIScrollSpy
    :root="viewport"
    :sections="sections"
    v-slot="{ activeId, scrollToSection }"
  >
    <div class="review-guide">
      <nav class="ui-data-actions" aria-label="변경 절차 목차">
        <UIButton
          v-for="chapter in chapters"
          :key="chapter.id"
          size="sm"
          :variant="activeId === chapter.id ? 'soft' : 'ghost'"
          :aria-current="activeId === chapter.id ? 'location' : undefined"
          @click="scrollToSection(chapter.id, { focus: true })"
          >{{ chapter.title }}</UIButton
        >
      </nav>
      <div
        ref="viewport"
        class="review-guide__content"
        role="region"
        aria-label="변경 절차"
        tabindex="0"
      >
        <section v-for="chapter in chapters" :key="chapter.id">
          <strong ref="heading" :data-chapter="chapter.id" tabindex="-1">{{
            chapter.title
          }}</strong>
          <p>{{ chapter.text }}</p>
        </section>
      </div>
    </div>
  </UIScrollSpy>
</template>

<style scoped>
.review-guide {
  inline-size: min(100%, 30rem);
}
.review-guide__content {
  block-size: 12rem;
  overflow: auto;
  scroll-padding-top: 1rem;
}
.review-guide__content section {
  min-block-size: 9rem;
  padding: var(--ui-space-md);
}
</style>

주요 API

속성과 모델

sections타입readonly UIScrollSpySection[]

읽는 순서대로 id와 실제 제목 요소를 전달합니다. 표시할 제목과 aria-current는 호출부에서 구성합니다.

변경 절차 목차와 읽는 위치 연결 예제
root / content

root에 안쪽 스크롤 영역을 넘깁니다. 문서 전체를 추적할 때는 생략합니다. 크기가 바뀌는 본문은 content로 추가 관찰할 수 있습니다.

변경 절차 목차와 읽는 위치 연결 예제
track-hash타입boolean | undefined

주소의 해시와 활성 항목을 연결합니다. 섹션 id는 해시에 쓰는 실제 id와 일치해야 합니다.

슬롯

#default타입(state: { activeId: string | null; refresh(): void; markTarget(id: string): void; scrollToSection(id: string, options?: UIScrollToSectionOptions | undefined): boolean; }) => unknown

activeId, scrollToSection, refresh, markTarget을 받습니다. scrollToSection의 focus를 켜려면 대상이 포커스 가능한 요소여야 합니다.

변경 절차 목차와 읽는 위치 연결 예제

컴포저블

useScrollSpy

스크립트에서 읽기 위치와 이동 기능만 연결합니다.

전체 속성, 이벤트와 슬롯

UIScrollSpy

sections필수
readonly UIScrollSpySection[]
root
HTMLElement | null | undefined
content
Element | null | undefined
track-hash
boolean | undefined
#default
(state: { activeId: string | null; refresh(): void; markTarget(id: string): void; scrollToSection(id: string, options?: UIScrollToSectionOptions | undefined): boolean; }) => unknown