用法
在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-on-hide属性来防止在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>您可以检查DOM以查看正在呈现的内容。
禁用
使用disabled道具禁用可折叠。
<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>
<script setup lang="ts">
import { ref } from 'vue'
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,您可以通过按:kbd{value="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 | 'div' | anyThe element or component this component should render as. |
disabled | boolean When | |
defaultOpen | boolean The open state of the collapsible when it is initially rendered. | |
open | boolean The controlled open state of the collapsible. Can be binded with | |
unmountOnHide | true | boolean When |
ui | { root?: SlotClass; content?: SlotClass; } |
Slots
| Slot | Type |
|---|---|
default | { open: boolean; } |
content | {} |
Emits
| Event | Type |
|---|---|
update:open | [value: boolean] |
Theme
app.config.ts
export default defineAppConfig({
ui: {
collapsible: {
slots: {
root: '',
content: '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'
}
}
}
})vite.config.ts
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_var(--ease-out)] data-[state=closed]:animate-[collapsible-up_200ms_var(--ease-out)] data-[state=closed]:overflow-hidden'
}
}
}
})
]
})Changelog
No recent changes