Collapsible
用法
在 Collapsible 的默认插槽中,使用一个 Button 或任何其他组件。
然后,使用 #content 插槽添加当 Collapsible 打开时显示的内容。
<template>
<UCollapsible class="flex flex-col gap-2 w-48">
<UButton
label="Open"
color="neutral"
variant="subtle"
trailing-icon="i-lucide-chevron-down"
block
/>
<template #content>
<Placeholder class="h-48" />
</template>
</UCollapsible>
</template>
卸载 (Unmount)
使用 unmount-on-hide prop,以防止内容在 Collapsible 折叠时被卸载。默认为 true。
<template>
<UCollapsible :unmount-on-hide="false" class="flex flex-col gap-2 w-48">
<UButton
label="Open"
color="neutral"
variant="subtle"
trailing-icon="i-lucide-chevron-down"
block
/>
<template #content>
<Placeholder class="h-48" />
</template>
</UCollapsible>
</template>
禁用 (Disabled)
使用 disabled prop 来禁用 Collapsible。
<template>
<UCollapsible class="flex flex-col gap-2 w-48" disabled>
<UButton
label="Open"
color="neutral"
variant="subtle"
trailing-icon="i-lucide-chevron-down"
block
/>
<template #content>
<Placeholder class="h-48" />
</template>
</UCollapsible>
</template>
示例
控制打开状态
你可以通过使用 default-open prop 或 v-model:open 指令来控制打开状态。
<script setup lang="ts">
const open = ref(true)
defineShortcuts({
o: () => open.value = !open.value
})
</script>
<template>
<UCollapsible v-model:open="open" class="flex flex-col gap-2 w-48">
<UButton
label="Open"
color="neutral"
variant="subtle"
trailing-icon="i-lucide-chevron-down"
block
/>
<template #content>
<Placeholder class="h-48" />
</template>
</UCollapsible>
</template>
defineShortcuts,你可以通过按下 O 来切换 Collapsible。带旋转图标
这是一个带旋转图标的按钮示例,该图标指示 Collapsible 的打开状态。
<template>
<UCollapsible class="flex flex-col gap-2 w-48">
<UButton
class="group"
label="Open"
color="neutral"
variant="subtle"
trailing-icon="i-lucide-chevron-down"
:ui="{
trailingIcon: 'group-data-[state=open]:rotate-180 transition-transform duration-200'
}"
block
/>
<template #content>
<Placeholder class="h-48" />
</template>
</UCollapsible>
</template>
API
Props
| Prop | Default | Type |
|---|---|---|
as |
|
The element or component this component should render as. |
disabled |
When | |
unmountOnHide |
|
When |
defaultOpen |
The open state of the collapsible when it is initially rendered. | |
open |
The controlled open state of the collapsible. Can be binded with | |
ui |
|
Slots
| Slot | Type |
|---|---|
default |
|
content |
|
Emits
| Event | Type |
|---|---|
update:open |
|
Theme
export default defineAppConfig({
ui: {
collapsible: {
slots: {
root: '',
content: 'data-[state=open]:animate-[collapsible-down_200ms_ease-out] data-[state=closed]:animate-[collapsible-up_200ms_ease-out] overflow-hidden'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
collapsible: {
slots: {
root: '',
content: 'data-[state=open]:animate-[collapsible-down_200ms_ease-out] data-[state=closed]:animate-[collapsible-up_200ms_ease-out] overflow-hidden'
}
}
}
})
]
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'
export default defineConfig({
plugins: [
vue(),
uiPro({
ui: {
collapsible: {
slots: {
root: '',
content: 'data-[state=open]:animate-[collapsible-down_200ms_ease-out] data-[state=closed]:animate-[collapsible-up_200ms_ease-out] overflow-hidden'
}
}
}
})
]
})