아코디언
아코디언 (Accordion)
아코디언(Accordion)은 접고 펼 수 있는 패널을 세로로 쌓아 보여주는 컴포넌트예요. 여러 항목 중 필요한 내용만 골라 펼쳐볼 수 있어 자주 쓰는 UI 구성 요소랍니다. items 배열만 넘기면 손쉽게 만들어 볼 수 있어요.
출처: Nuxt UI
본문
접고 펼 수 있는 패널들을 세로로 쌓아 놓은 컴포넌트예요.
사용법 (Usage)
아코디언 컴포넌트로 접고 펼 수 있는 항목들의 목록을 보여줄 수 있어요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = ref<AccordionItem[]>([
{
label: "Is Nuxt UI free to use?",
content: "Yes! Nuxt UI is completely free and open source under the MIT license. All 125+ components are available to everyone."
},
{
label: "Can I use Nuxt UI with Vue without Nuxt?",
content: "Yes! While optimized for Nuxt, Nuxt UI works perfectly with standalone Vue projects via our Vite plugin. You can follow the [installation guide](/docs/getting-started/installation/vue) to get started."
},
{
label: "Is Nuxt UI production-ready?",
content: "Yes! Nuxt UI is used in production by thousands of applications with extensive tests, regular updates, and active maintenance."
}
])
</script>
<template>
<UAccordion :items="items" />
</template>
Items (항목들)
items prop에는 아래 속성들을 가진 객체 배열을 전달해요:
label?: stringicon?: stringtrailingIcon?: stringcontent?: stringvalue?: stringdisabled?: booleanslot?: stringclass?: anyui?: { item?: ClassNameValue, header?: ClassNameValue, trigger?: ClassNameValue, leadingIcon?: ClassNameValue, label?: ClassNameValue, trailingIcon?: ClassNameValue, content?: ClassNameValue, body?: ClassNameValue }
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = ref<AccordionItem[]>([
{
label: "Icons",
icon: "i-lucide-smile",
content: "You have nothing to do, @nuxt/icon will handle it automatically."
},
{
label: "Colors",
icon: "i-lucide-swatch-book",
content: "Choose a primary and a neutral color from your Tailwind CSS theme."
},
{
label: "Components",
icon: "i-lucide-box",
content: "You can customize components by using the `class` / `ui` props or in your app.config.ts."
}
])
</script>
<template>
<UAccordion :items="items" />
</template>
Multiple (여러 개 열기)
type prop을 multiple로 설정하면 여러 항목을 동시에 활성화할 수 있어요. 기본값은 single이에요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = ref<AccordionItem[]>([
{
label: "Icons",
icon: "i-lucide-smile",
content: "You have nothing to do, @nuxt/icon will handle it automatically."
},
{
label: "Colors",
icon: "i-lucide-swatch-book",
content: "Choose a primary and a neutral color from your Tailwind CSS theme."
},
{
label: "Components",
icon: "i-lucide-box",
content: "You can customize components by using the `class` / `ui` props or in your app.config.ts."
}
])
</script>
<template>
<UAccordion type="multiple" :items="items" />
</template>
Collapsible (접기 가능 여부)
type이 single일 때 collapsible prop을 false로 설정하면 활성 항목이 접히지 않도록 막을 수 있어요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = ref<AccordionItem[]>([
{
label: "Icons",
icon: "i-lucide-smile",
content: "You have nothing to do, @nuxt/icon will handle it automatically."
},
{
label: "Colors",
icon: "i-lucide-swatch-book",
content: "Choose a primary and a neutral color from your Tailwind CSS theme."
},
{
label: "Components",
icon: "i-lucide-box",
content: "You can customize components by using the `class` / `ui` props or in your app.config.ts."
}
])
</script>
<template>
<UAccordion :collapsible="false" :items="items" />
</template>
Unmount (내용 해제)
unmount-on-hide prop을 사용하면 아코디언이 접혔을 때 내용이 (DOM에서) 해제되는 걸 막을 수 있어요. 기본값은 true예요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = ref<AccordionItem[]>([
{
label: "Icons",
icon: "i-lucide-smile",
content: "You have nothing to do, @nuxt/icon will handle it automatically."
},
{
label: "Colors",
icon: "i-lucide-swatch-book",
content: "Choose a primary and a neutral color from your Tailwind CSS theme."
},
{
label: "Components",
icon: "i-lucide-box",
content: "You can customize components by using the `class` / `ui` props or in your app.config.ts."
}
])
</script>
<template>
<UAccordion :unmount-on-hide="false" :items="items" />
</template>
[!NOTE]
DOM을 직접 살펴보면 각 항목의 내용이 렌더링되고 있는 걸 확인할 수 있어요.
Disabled (비활성화)
disabled 속성으로 아코디언 전체를 비활성화할 수 있어요.
또한 각 항목 객체에 disabled 속성을 넣으면 특정 항목만 비활성화할 수도 있어요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = ref<AccordionItem[]>([
{
label: "Icons",
icon: "i-lucide-smile",
content: "You have nothing to do, @nuxt/icon will handle it automatically."
},
{
label: "Colors",
icon: "i-lucide-swatch-book",
content: "Choose a primary and a neutral color from your Tailwind CSS theme.",
disabled: true
},
{
label: "Components",
icon: "i-lucide-box",
content: "You can customize components by using the `class` / `ui` props or in your app.config.ts."
}
])
</script>
<template>
<UAccordion disabled :items="items" />
</template>
Trailing Icon (오른쪽 아이콘)
trailing-icon prop으로 각 항목 오른쪽에 표시되는 Icon을 바꿀 수 있어요. 기본값은 i-lucide-chevron-down이에요.
[!TIP]
항목 객체에
trailingIcon속성을 넣으면 특정 항목에만 아이콘을 설정할 수도 있어요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = ref<AccordionItem[]>([
{
label: "Icons",
icon: "i-lucide-smile",
content: "You have nothing to do, @nuxt/icon will handle it automatically.",
trailingIcon: "i-lucide-plus"
},
{
label: "Colors",
icon: "i-lucide-swatch-book",
content: "Choose a primary and a neutral color from your Tailwind CSS theme."
},
{
label: "Components",
icon: "i-lucide-box",
content: "You can customize components by using the `class` / `ui` props or in your app.config.ts."
}
])
</script>
<template>
<UAccordion trailing-icon="i-lucide-arrow-down" :items="items" />
</template>
Nuxt:
[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme
You can customize this icon globally in your
app.config.tsunderui.icons.chevronDownkey.
Vue:
[!TIP] See: /docs/getting-started/integrations/icons/vue#theme
You can customize this icon globally in your
vite.config.tsunderui.icons.chevronDownkey.
예시 (Examples)
활성 항목 제어하기
default-value prop이나 v-model 디렉티브에 항목의 value를 넣어 활성 항목을 제어할 수 있어요. value를 지정하지 않으면 기본적으로 인덱스가 문자열로 들어가요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items: AccordionItem[] = [
{
label: 'Icons',
icon: 'i-lucide-smile',
content: 'You have nothing to do, @nuxt/icon will handle it automatically.'
},
{
label: 'Colors',
icon: 'i-lucide-swatch-book',
content: 'Choose a primary and a neutral color from your Tailwind CSS theme.'
},
{
label: 'Components',
icon: 'i-lucide-box',
content: 'You can customize components by using the `class` / `ui` props or in your app.config.ts.'
}
]
const active = ref('0')
// Note: This is for demonstration purposes only. Don't do this at home.
onMounted(() => {
setInterval(() => {
active.value = String((Number(active.value) + 1) % items.length)
}, 2000)
})
</script>
<template>
<UAccordion v-model="active" :items="items" />
</template>
[!TIP]
v-model이나default-value를 제공할 때 항목을 매칭하는 키를 바꾸려면value-keyprop을 사용해요.
[!CAUTION]
type="multiple"일 때는default-valueprop이나v-model디렉티브에 배열을 전달해야 해요.
드래그 앤 드롭 적용하기
@vueuse/integrations의 useSortable 컴포저블로 아코디언에 드래그 앤 드롭 기능을 더할 수 있어요. 이 통합은 Sortable.js를 감싸서 매끄러운 드래그 앤 드롭 경험을 제공해요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
import { useSortable } from '@vueuse/integrations/useSortable'
const items = shallowRef<AccordionItem[]>([
{
label: 'Icons',
icon: 'i-lucide-smile',
content: 'You have nothing to do, @nuxt/icon will handle it automatically.'
},
{
label: 'Colors',
icon: 'i-lucide-swatch-book',
content: 'Choose a primary and a neutral color from your Tailwind CSS theme.'
},
{
label: 'Components',
icon: 'i-lucide-box',
content: 'You can customize components by using the `class` / `ui` props or in your app.config.ts.'
}
])
const accordion = useTemplateRef<HTMLElement>('accordion')
useSortable(accordion, items, {
animation: 150
})
</script>
<template>
<UAccordion ref="accordion" :items="items" />
</template>
body 슬롯 사용하기
#body 슬롯으로 각 항목의 본문을 자유롭게 꾸밀 수 있어요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items: AccordionItem[] = [
{
label: 'Icons',
icon: 'i-lucide-smile'
},
{
label: 'Colors',
icon: 'i-lucide-swatch-book'
},
{
label: 'Components',
icon: 'i-lucide-box'
}
]
</script>
<template>
<UAccordion :items="items">
<template #body="{ item }">
This is the {{ item.label }} panel.
</template>
</UAccordion>
</template>
[!TIP]
#body슬롯에는 미리 정의된 스타일이 들어 있어요. 처음부터 자유롭게 만들고 싶다면#content슬롯을 사용해요.
content 슬롯 사용하기
#content 슬롯으로 각 항목의 내용을 꾸밀 수 있어요.
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items: AccordionItem[] = [
{
label: 'Icons',
icon: 'i-lucide-smile'
},
{
label: 'Colors',
icon: 'i-lucide-swatch-book'
},
{
label: 'Components',
icon: 'i-lucide-box'
}
]
</script>
<template>
<UAccordion :items="items">
<template #content="{ item }">
<p class="pb-3.5 text-sm text-muted">
This is the {{ item.label }} panel.
</p>
</template>
</UAccordion>
</template>
커스텀 슬롯 사용하기
slot 속성으로 특정 항목만 다르게 꾸밀 수 있어요.
다음 슬롯들을 사용할 수 있어요:
#{{ item.slot }}#{{ item.slot }}-body
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = [
{
label: 'Icons',
icon: 'i-lucide-smile',
content: 'You have nothing to do, @nuxt/icon will handle it automatically.'
},
{
label: 'Colors',
icon: 'i-lucide-swatch-book',
slot: 'colors' as const,
content: 'Choose a primary and a neutral color from your Tailwind CSS theme.'
},
{
label: 'Components',
icon: 'i-lucide-box',
content: 'You can customize components by using the `class` / `ui` props or in your app.config.ts.'
}
] satisfies AccordionItem[]
</script>
<template>
<UAccordion :items="items">
<template #colors="{ item }">
<p class="text-sm pb-3.5 text-primary">
{{ item.content }}
</p>
</template>
</UAccordion>
</template>
마크다운 내용 렌더링하기
@comark/vue의 Markdown 컴포넌트로 아코디언 항목 안에 마크다운을 렌더링할 수 있어요.
<script setup lang="ts">
import { Markdown } from '@comark/vue'
const items = [
{
label: 'Is Nuxt UI free to use?',
icon: 'i-lucide-circle-help',
content: 'Yes! Nuxt UI is completely free and open source under the MIT license. All 125+ components are available to everyone.'
},
{
label: 'Can I use Nuxt UI with Vue without Nuxt?',
icon: 'i-lucide-circle-help',
content: 'Yes! While optimized for Nuxt, Nuxt UI works perfectly with standalone Vue projects via our Vite plugin. You can follow the [installation guide](/docs/getting-started/installation/vue) to get started.'
},
{
label: 'Will Nuxt UI work with other CSS frameworks like UnoCSS?',
icon: 'i-lucide-circle-help',
content: 'No. Nuxt UI is designed exclusively for Tailwind CSS. UnoCSS support would require significant architecture changes due to different class naming conventions.'
},
{
label: 'How does Nuxt UI handle accessibility?',
icon: 'i-lucide-circle-help',
content: 'Through [Reka UI](https://reka-ui.com/docs/overview/accessibility) integration, Nuxt UI provides **automatic ARIA attributes**, **keyboard navigation**, **focus management**, and **screen reader support**. While offering a strong foundation, testing in your specific use case remains important.'
},
{
label: 'How is Nuxt UI tested?',
icon: 'i-lucide-circle-help',
content: 'Nuxt UI ensures reliability with 1000+ Vitest tests covering core functionality and accessibility.'
},
{
label: 'Is Nuxt UI production-ready?',
icon: 'i-lucide-circle-help',
content: 'Yes! Nuxt UI is used in production by thousands of applications with extensive tests, regular updates, and active maintenance.'
}
]
</script>
<template>
<UAccordion
type="multiple"
:items="items"
:unmount-on-hide="false"
:default-value="['3']"
:ui="{
trigger: 'text-base',
body: 'text-base text-muted'
}"
>
<template #body="{ item }">
<Markdown :value="item.content" unwrap="p" />
</template>
</UAccordion>
</template>
API
Props
/**
* Props for the Accordion component
*/
interface AccordionProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
items?: T[] | undefined;
/**
* The icon displayed on the right side of the trigger.
* @default appConfig.ui.icons.chevronDown
*/
trailingIcon?: any;
/**
* The key used to get the value from the item.
* @default 'value'
*/
valueKey?: keyof Extract<NestedItem<T>, object> & string | DotPathKeys<Extract<NestedItem<T>, object>> | undefined;
/**
* The key used to get the label from the item.
* @default 'label'
*/
labelKey?: keyof Extract<NestedItem<T>, object> & string | DotPathKeys<Extract<NestedItem<T>, object>> | undefined;
ui?: { root?: SlotClass; item?: SlotClass; header?: SlotClass; trigger?: SlotClass; content?: SlotClass; body?: SlotClass; leadingIcon?: SlotClass; trailingIcon?: SlotClass; label?: SlotClass; } | undefined;
/**
* When type is "single", allows closing content when clicking trigger for an open item.
* When type is "multiple", this prop has no effect.
* @default true
*/
collapsible?: boolean | undefined;
/**
* The default active value of the item(s).
*
* Use when you do not need to control the state of the item(s).
*/
defaultValue?: string | string[] | undefined;
/**
* The controlled value of the active item(s).
*
* Use this when you need to control the state of the items. Can be binded with `v-model`
*/
modelValue?: string | string[] | undefined;
/**
* Determines whether a "single" or "multiple" items can be selected at a time.
*
* This prop will overwrite the inferred type from `modelValue` and `defaultValue`.
* @default 'single'
*/
type?: "single" | "multiple" | undefined;
/**
* When `true`, prevents the user from interacting with the accordion and all its items
* @default false
*/
disabled?: boolean | undefined;
/**
* When `true`, the element will be unmounted on closed state.
* @default true
*/
unmountOnHide?: boolean | undefined;
}
Slots
/**
* Slots for the Accordion component
*/
interface AccordionSlots {
default(): any;
leading(): any;
trailing(): any;
content(): any;
body(): any;
}
Emits
/**
* Emitted events for the Accordion component
*/
interface AccordionEmits {
update:modelValue: (payload: [value: string | string[] | undefined]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
accordion: {
slots: {
root: 'w-full',
item: 'border-b border-default last:border-b-0',
header: 'flex',
trigger: 'group flex-1 flex items-center gap-1.5 font-medium text-sm py-3.5 outline-primary/25 focus-visible:outline-3 min-w-0 rounded-md',
content: 'data-[state=open]:animate-[accordion-down_200ms_var(--ease-out)] data-[state=closed]:animate-[accordion-up_200ms_var(--ease-out)] data-[state=closed]:overflow-hidden focus:outline-none',
body: 'text-sm pb-3.5',
leadingIcon: 'shrink-0 size-5',
trailingIcon: 'shrink-0 size-5 ms-auto group-data-[state=open]:rotate-180 transition-transform duration-200 ease-out motion-reduce:transition-none',
label: 'text-start break-words'
},
variants: {
disabled: {
true: {
trigger: 'cursor-not-allowed opacity-75'
}
}
}
}
}
})