본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

ContentToc

원문 보기 위키 갱신

ContentToc (콘텐츠 목차)

ContentToc 컴포넌트는 현재 활성 앵커 링크를 자동으로 강조 표시하는 고정(sticky) 목차(Table of Contents) 컴포넌트예요. @nuxt/content 모듈이 설치된 경우에만 사용할 수 있어요.

출처: Nuxt UI

본문

[!WARNING] See: /docs/getting-started/integrations/content

이 컴포넌트는 @nuxt/content 모듈이 설치되어 있을 때만 사용할 수 있어요.

사용법 (Usage)

페이지를 가져올 때 얻는 page?.body?.toc?.links를 links prop으로 전달하세요.

<script setup lang="ts">
const route = useRoute()

const { data: page } = await useAsyncData(route.path, () => queryCollection('docs').path(route.path).first())
if (!page.value) {
  throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
}
</script>

<template>
  <UContentToc :links="page?.body?.toc?.links" />
</template>

타이틀 (Title)

title prop으로 목차의 제목을 변경할 수 있어요.

<script setup lang="ts">
import type { ContentTocLink } from '@nuxt/ui'

const links = ref<ContentTocLink[]>([
  {
    id: "usage",
    depth: 2,
    text: "Usage",
    children: [
      {
        id: "title",
        depth: 3,
        text: "Title"
      },
      {
        id: "color",
        depth: 3,
        text: "Color"
      },
      {
        id: "highlight",
        depth: 3,
        text: "Highlight"
      },
      {
        id: "highlight-color",
        depth: 3,
        text: "Highlight Color"
      },
      {
        id: "highlight-variant",
        depth: 3,
        text: "Highlight Variant"
      }
    ]
  }
])
</script>

<template>
  <UContentToc title="On this page" :links="links" />
</template>

색상 (Color)

color prop으로 링크의 색상을 변경할 수 있어요.

<script setup lang="ts">
import type { ContentTocLink } from '@nuxt/ui'

const links = ref<ContentTocLink[]>([
  {
    id: "usage",
    depth: 2,
    text: "Usage",
    children: [
      {
        id: "title",
        depth: 3,
        text: "Title"
      },
      {
        id: "color",
        depth: 3,
        text: "Color"
      },
      {
        id: "highlight",
        depth: 3,
        text: "Highlight"
      },
      {
        id: "highlight-color",
        depth: 3,
        text: "Highlight Color"
      },
      {
        id: "highlight-variant",
        depth: 3,
        text: "Highlight Variant"
      }
    ]
  }
])
</script>

<template>
  <UContentToc color="neutral" :links="links" />
</template>

강조 (Highlight)

highlight prop으로 활성 항목에 강조 테두리를 표시할 수 있어요.

<script setup lang="ts">
import type { ContentTocLink } from '@nuxt/ui'

const links = ref<ContentTocLink[]>([
  {
    id: "usage",
    depth: 2,
    text: "Usage",
    children: [
      {
        id: "title",
        depth: 3,
        text: "Title"
      },
      {
        id: "color",
        depth: 3,
        text: "Color"
      },
      {
        id: "highlight",
        depth: 3,
        text: "Highlight"
      },
      {
        id: "highlight-color",
        depth: 3,
        text: "Highlight Color"
      },
      {
        id: "highlight-variant",
        depth: 3,
        text: "Highlight Variant"
      }
    ]
  }
])
</script>

<template>
  <UContentToc highlight :links="links" />
</template>

강조 색상 (Highlight Color)

highlight-color prop으로 강조 표시의 색상을 변경할 수 있어요. 기본값은 color prop과 같아요.

<script setup lang="ts">
import type { ContentTocLink } from '@nuxt/ui'

const links = ref<ContentTocLink[]>([
  {
    id: "usage",
    depth: 2,
    text: "Usage",
    children: [
      {
        id: "title",
        depth: 3,
        text: "Title"
      },
      {
        id: "color",
        depth: 3,
        text: "Color"
      },
      {
        id: "highlight",
        depth: 3,
        text: "Highlight"
      },
      {
        id: "highlight-color",
        depth: 3,
        text: "Highlight Color"
      },
      {
        id: "highlight-variant",
        depth: 3,
        text: "Highlight Variant"
      }
    ]
  }
])
</script>

<template>
  <UContentToc highlight highlight-color="neutral" :links="links" />
</template>

강조 변형 (Highlight Variant) 4.6+

highlight-variant prop으로 강조 표시의 스타일을 변경할 수 있어요. 기본값은 straight예요.

<script setup lang="ts">
import type { ContentTocLink } from '@nuxt/ui'

const links = ref<ContentTocLink[]>([
  {
    id: "usage",
    depth: 2,
    text: "Usage",
    children: [
      {
        id: "title",
        depth: 3,
        text: "Title"
      },
      {
        id: "color",
        depth: 3,
        text: "Color"
      },
      {
        id: "highlight",
        depth: 3,
        text: "Highlight"
      },
      {
        id: "highlight-color",
        depth: 3,
        text: "Highlight Color"
      },
      {
        id: "highlight-variant",
        depth: 3,
        text: "Highlight Variant"
      }
    ]
  },
  {
    id: "examples",
    depth: 2,
    text: "Examples",
    children: [
      {
        id: "within-a-page",
        depth: 3,
        text: "Within a Page"
      }
    ]
  },
  {
    id: "api",
    depth: 2,
    text: "API",
    children: [
      {
        id: "props",
        depth: 3,
        text: "Props"
      },
      {
        id: "slots",
        depth: 3,
        text: "Slots"
      },
      {
        id: "emits",
        depth: 3,
        text: "Emits"
      }
    ]
  },
  {
    id: "theme",
    depth: 2,
    text: "Theme"
  }
])
</script>

<template>
  <UContentToc highlight highlight-color="primary" highlight-variant="circuit" :links="links" />
</template>

예제 (Examples)

페이지 안에서 사용 (Within a page)

페이지에서 ContentToc 컴포넌트를 사용해 목차를 표시하세요:

<script setup lang="ts">
const route = useRoute()

const { data: page } = await useAsyncData(route.path, () => queryCollection('docs').path(route.path).first())
if (!page.value) {
  throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
}
</script>

<template>
  <UPage v-if="page">
    <UPageHeader :title="page.title" />

    <UPageBody>
      <ContentRenderer v-if="page.body" :value="page" />

      <USeparator v-if="surround?.filter(Boolean).length" />

      <UContentSurround :surround="(surround as any)" />
    </UPageBody>

    <template v-if="page?.body?.toc?.links?.length" #right>
      <UContentToc :links="page.body.toc.links" />
    </template>
  </UPage>
</template>

API

Props (속성)

/**
 * Props for the ContentToc component
 */
interface ContentTocProps {
  /**
   * The element or component this component should render as.
   * @default 'nav'
   */
  as?: any;
  /**
   * The icon displayed to collapse the content.
   * @default appConfig.ui.icons.chevronDown
   */
  trailingIcon?: any;
  /**
   * The title of the table of contents.
   * @default t('contentToc.title')
   */
  title?: string | undefined;
  /**
   * @default 'primary'
   */
  color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
  /**
   * Display a line next to the active link.
   * @default false
   */
  highlight?: boolean | undefined;
  /**
   * @default 'primary'
   */
  highlightColor?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
  /**
   * The variant of the highlight indicator.
   * @default 'straight'
   */
  highlightVariant?: "straight" | "circuit" | undefined;
  links?: T[] | undefined;
  ui?: { root?: SlotClass; container?: SlotClass; top?: SlotClass; bottom?: SlotClass; trigger?: SlotClass; title?: SlotClass; trailing?: SlotClass; trailingIcon?: SlotClass; content?: SlotClass; list?: SlotClass; listWithChildren?: SlotClass; item?: SlotClass; itemWithChildren?: SlotClass; link?: SlotClass; linkText?: SlotClass; indicator?: SlotClass; indicatorLine?: SlotClass; indicatorActive?: SlotClass; } | undefined;
  /**
   * The open state of the collapsible when it is initially rendered. <br> Use when you do not need to control its open state.
   */
  defaultOpen?: boolean | undefined;
  /**
   * The controlled open state of the collapsible. Can be binded with `v-model`.
   */
  open?: boolean | undefined;
}

Slots (슬롯)

/**
 * Slots for the ContentToc component
 */
interface ContentTocSlots {
  leading(): any;
  default(): any;
  trailing(): any;
  content(): any;
  link(): any;
  top(): any;
  bottom(): any;
}

Emits (이벤트)

/**
 * Emitted events for the ContentToc component
 */
interface ContentTocEmits {
  update:open: (payload: [value: boolean]) => void;
  move: (payload: [id: string]) => void;
}

테마 (Theme)

export default defineAppConfig({
  ui: {
    contentToc: {
      slots: {
        root: 'sticky top-(--ui-header-height) z-10 bg-default/75 lg:bg-[initial] backdrop-blur-sm -mx-4 px-4 sm:px-6 sm:-mx-6 lg:ms-0 flex flex-col overflow-y-auto max-h-[calc(100vh-var(--ui-header-height))]',
        container: 'pt-4 sm:pt-6 pb-2.5 sm:pb-4.5 lg:py-8 border-b border-dashed border-default lg:border-0 flex flex-col lg:min-h-0',
        top: 'lg:shrink-0',
        bottom: 'hidden lg:flex lg:flex-col lg:shrink-0 gap-6',
        trigger: 'group text-sm font-semibold flex-1 flex items-center gap-1.5 py-1.5 -mt-1.5 rounded-sm outline-primary/25 focus-visible:outline-3 lg:shrink-0',
        title: 'truncate',
        trailing: 'ms-auto inline-flex gap-1.5 items-center',
        trailingIcon: 'size-5 transform transition-transform duration-200 ease-out motion-reduce:transition-none shrink-0 group-data-[state=open]:rotate-180 lg:hidden',
        content: 'relative data-[state=open]:animate-[collapsible-down_200ms_var(--ease-out)] data-[state=closed]:animate-[collapsible-up_200ms_var(--ease-out)] data-[state=closed]:overflow-hidden focus:outline-none lg:min-h-[min(var(--list-height,8rem),16rem)] lg:overflow-y-auto lg:scrollbar-none',
        list: 'min-w-0',
        listWithChildren: 'ms-3',
        item: 'min-w-0',
        itemWithChildren: '',
        link: 'group relative text-sm flex items-center rounded-sm outline-primary/25 focus-visible:outline-3 py-1',
        linkText: 'truncate',
        indicator: '',
        indicatorLine: '',
        indicatorActive: ''
      },
      variants: {
        color: {
          primary: '',
          secondary: '',
          success: '',
          info: '',
          warning: '',
          error: '',
          neutral: ''
        },
        highlightColor: {
          primary: {
            indicatorActive: 'bg-primary'
          },
          secondary: {
            indicatorActive: 'bg-secondary'
          },
          success: {
            indicatorActive: 'bg-success'
          },
          info: {
            indicatorActive: 'bg-info'
          },
          warning: {
            indicatorActive: 'bg-warning'
          },
          error: {
            indicatorActive: 'bg-error'
          },
          neutral: {
            indicatorActive: 'bg-inverted'
          }
        },
        active: {
          false: {
            link: [
              'text-muted hover:text-default',
              'transition-colors'
            ]
          }
        },
        highlight: {
          true: ''
        },
        highlightVariant: {
          straight: '',
          circuit: ''
        },
        body: {
          true: {
            bottom: 'mt-6'
          }
        }
      },
      compoundVariants: [
        {
          color: 'primary',
          active: true,
          class: {
            link: 'text-primary'
          }
        },
        {
          color: 'secondary',
          active: true,
          class: {
            link: 'text-secondary'
          }
        },
        {
          color: 'success',
          active: true,
          class: {
            link: 'text-success'
          }
        },
        {
          color: 'info',
          active: true,
          class: {
            link: 'text-info'
          }
        },
        {
          color: 'warning',
          active: true,
          class: {
            link: 'text-warning'
          }
        },
        {
          color: 'error',
          active: true,
          class: {
            link: 'text-error'
          }
        },
        {
          color: 'neutral',
          active: true,
          class: {
            link: 'text-highlighted'
          }
        },
        {
          highlight: true,
          highlightVariant: 'straight',
          class: {
            list: 'ms-2.5 ps-4 border-s border-default',
            item: '-ms-px',
            indicator: 'absolute ms-2.5 transition-[translate,height] duration-200 ease-out motion-reduce:transition-none h-(--indicator-size) translate-y-(--indicator-position) w-px rounded-full',
            indicatorLine: 'hidden',
            indicatorActive: 'w-full h-full'
          }
        },
        {
          highlight: true,
          highlightVariant: 'circuit',
          class: {
            list: 'ps-6.5',
            item: '-ms-px',
            itemWithChildren: 'ps-px',
            indicator: 'absolute ms-2.5 start-0 top-0 rtl:-scale-x-100',
            indicatorLine: 'absolute inset-0 bg-(--ui-border)',
            indicatorActive: 'absolute w-full h-(--indicator-size) translate-y-(--indicator-position) transition-[translate,height] duration-200 ease-out motion-reduce:transition-none'
          }
        }
      ],
      defaultVariants: {
        color: 'primary',
        highlightColor: 'primary',
        highlightVariant: 'straight'
      }
    }
  }
})

변경 이력 (Changelog)

컴포넌트와 테마의 커밋 기록을 확인하세요.

더 알아보기 (Learn more)