NAccordion is a component that allows you to display collapsible content.
NAccordion label is wrapped around the NButton component, which means that all the props and slots of NButton are available to use.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
<script setup lang="ts">
const items = [
{
label: 'How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
defaultOpen: true,
},
{
label: 'What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
]
</script>
<template>
<NAccordion
:items="items"
/>
</template>
โก By default, the accordion's content is not rendered until it is opened for performance reasons. To render the content when the page loads, even if the accordion is closed for SEO purposes, use the mounted prop.
mounted - allows you to set the mount state of all items.
item.mount - allows you to set the mount state of a specific item. This will override the mounted prop.
If you have a lot of accordion items, it is not recommended to use the mounted prop because it affects the performance of the page, instead use the item.mount prop to mount the content of a specific item if needed.
<script setup lang="ts">
const items = [
{
label: 'How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'When will I get my refund?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
mounted: false,
},
]
</script>
<template>
<NAccordion
:items="items"
mounted
/>
</template>
multiple - allows you to expand multiple items at the same time.
<script setup lang="ts">
const items = [
{
label: 'How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
]
</script>
<template>
<NAccordion
:items="items"
multiple
/>
</template>
item.defaultOpen - allows you to control the default open state of a specific item.
Use multiple prop when using default-open to open multiple items by default.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
<script setup lang="ts">
const items = [
{
label: 'This is open by default',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'This is open by default',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'This is close by default',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
defaultOpen: false, // priority over defaultOpen prop
},
]
</script>
<template>
<NAccordion
:items="items"
multiple
default-open
/>
</template>
Since we use the NButton component for the accordion label, you can use the btn prop to change the color of the label. See NButton for more information.
btn="text-{color}" prop is basically from NButton component. You can use it to change the color of the label.
<script setup lang="ts">
const items = [
{
label: 'How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
]
</script>
<template>
<div class="flex flex-col space-y-4">
<NAccordion
:items="items"
btn="text-gray"
/>
<NSeparator />
<NAccordion
:items="items"
btn="text-orange"
/>
<NSeparator />
<NAccordion
:items="items"
btn="text-indigo"
/>
</div>
</template>
leading - allows you to add leading icon to the label.
item.leading - allows you to add leading icon to the label of a specific item.
trailingOpen - allows you to customize trailing open icon.
trailingClose - allows you to customize trailing close icon.
If no trailing icon is given, it will be used for both open and close states and will animate upside down automatically.
By default, we use the heroicons and tabler for the icons, you can use any icon provided by Iconify through icones, refer to configuration for more information.
Custom Global leading icons
Custom per item leading icon
Custom Trailing open icon
Custom Trailing open and close icons
<script setup lang="ts">
const items1 = [
{
label: 'How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
]
const items2 = [
{
label: 'How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
leading: 'i-heroicons-information-circle text-info',
},
{
label: 'What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
leading: 'i-heroicons-shield-check text-green-600',
},
{
label: 'Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
leading: 'i-heroicons-shopping-cart text-amber',
},
]
</script>
<template>
<div class="flex flex-col gap-4">
<span class="text-sm font-medium">
Custom Global leading icons
</span>
<NAccordion
:items="items1"
leading="i-heroicons-question-mark-circle"
/>
<NSeparator />
<span class="text-sm font-medium">
Custom per item leading icon
</span>
<NAccordion
:items="items2"
/>
<NSeparator />
<span class="text-sm font-medium">
Custom Trailing open icon
</span>
<NAccordion
:items="items1"
trailing-open="i-heroicons-chevron-left"
:una="{
accordionTrailingClose: '-rotate-90',
accordionTrailingOpen: 'rotate-0',
}"
/>
<NSeparator />
<span class="text-sm font-medium">
Custom Trailing open and close icons
</span>
<NAccordion
:items="items1"
trailing-open="i-twemoji-smiling-face-with-sunglasses"
trailing-close="i-twemoji-loudly-crying-face"
/>
</div>
</template>
reverse - allows you to switch the position of the trailing and leading icons.
item.reverse - allows you to switch the position of the trailing and leading icons of a specific item.
reverse prop is basically from NButton component. You can use it to switch the position of the trailing and leading icons.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
<script setup lang="ts">
const items = [
{
label: 'This will be reversed',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
defaultOpen: true,
},
{
label: 'This will be reversed',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'This will not be reversed',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
reverse: false,
},
]
</script>
<template>
<NAccordion
:items="items"
leading="i-heroicons-question-mark-circle"
reverse
/>
</template>
Customize the accordion with your own styles using una preset configuration.
You can globally customize the accordion preset if want to have a different default style. See Configuration section for more details.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
<script setup lang="ts">
const items = [
{
label: 'How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
defaultOpen: true,
},
{
label: 'What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: 'Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
]
</script>
<template>
<NAccordion
:items="items"
class="rounded-none bg-muted"
reverse
trailing-open="i-heroicons-chevron-right"
:una="{
accordionPanel: 'bg-base',
accordionTrailingClose: 'rotate-90',
accordionTrailingOpen: 'rotate-0',
}"
/>
</template>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
<script setup lang="ts">
const items = [
{
label: '1. How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
defaultOpen: true,
},
{
label: '2. What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: '3. Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
]
</script>
<template>
<NAccordion
:items="items"
unstyle
trailing-open="i-heroicons-plus"
:una="{
accordionButton: 'px-0',
accordionPanel: 'bg-muted rounded-none mt-2',
accordionTrailingClose: '-rotate-45',
accordionTrailing: 'text-md',
}"
/>
</template>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.
<script setup lang="ts">
const items = [
{
label: '1. How do I get started?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
defaultOpen: true,
},
{
label: '2. What is your return policy?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
{
label: '3. Can I exchange an item?',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel urna vitae lectus aliquet mollis et eget risus.',
},
]
</script>
<template>
<NAccordion
:items="items"
unstyle
btn="~"
trailing-close="i-heroicons-plus"
trailing-open="i-heroicons-minus"
:una="{
accordionButton: 'px-0',
accordionTrailing: 'text-md',
}"
/>
</template>
import type { NButtonProps } from './button'
export interface NAccordionProps extends Omit<NButtonProps, 'una'> {
/**
* Allows you to add `UnaUI` accordion preset properties,
* Think of it as a shortcut for adding options or variants to the preset if available.
*
* By default, we don't add any options or variants to the accordion,
* But you can add your own in the configuration file.
*/
accordion?: string
/**
* Update leading icon when accordion button item is open,
* Accepts icon name and utility classes
*/
trailingOpen?: string
/**
* Update leading icon when accordion button item is closed,
* Accepts icon name and utility classes
*/
trailingClose?: string
/**
* Allow multiple accordion items to be open at the same time
*
* @default false
*/
multiple?: boolean
/**
* Allow accordion item to be open by default
*
* @default false
*/
defaultOpen?: boolean
/**
* Removes border and divider from accordion
*
* @default false
*/
unstyle?: boolean
/**
* By default, the accordion is unmounted for performance reasons,
* This means that the accordion will not be rendered until it is opened,
* If you want to render the accordion when the page loads, you can use the `mounted` prop.
*
* @default false
*/
mounted?: boolean
/**
* List of items to be rendered,
* It extends the `NButtonProps` interface
*
* @see https://github.com/una-ui/una-ui/blob/main/packages/nuxt/src/runtime/types/button.ts
*/
items: NAccordionItemProps[]
/**
* `UnaUI` preset configuration
*
* @see https://github.com/una-ui/una-ui/blob/main/packages/preset/src/_shortcuts/accordion.ts
*/
una?: {
accordion?: string
accordionItem?: string
accordionButton?: string
accordionPanel?: string
accordionLeading?: string
accordionTrailing?: string
accordionTrailingOpen?: string
accordionTrailingClose?: string
accordionEnterActive?: string
accordionLeaveActive?: string
} & NButtonProps['una']
}
export interface NAccordionItemProps extends NButtonProps {
/**
* Accordion item content
*/
content?: string
/**
* Update item leading icon when accordion button item is open,
* Accepts icon name and utility classes
*
* @example
* trailingOpen='i-heroicons-information-circle text-info'
*/
trailingOpen?: string
/**
* Update item leading icon when accordion button item is closed,
* Accepts icon name and utility classes
*
* @example
* trailingClose='i-heroicons-information-circle text-info'
*/
trailingClose?: string
/**
* Allow accordion item to be open by default
*
* @default false
*/
defaultOpen?: boolean
/**
* Close other accordion items when item is open
*
* @default false
*/
closeOthers?: boolean
/**
* By default, all the accordion item is unmounted for performance reasons,
* You can use the `mounted` prop to render the accordion specific on item.
*
* @default false
*/
mounted?: boolean
/**
* Allow dynamic attributes to be added to the accordion item,
*
*/
[key: string]: any
}