CollapseTransition
Animate a content block's height when expanding or collapsing. No wrapper element is added. Place one element controlled by v-show or v-if in the default slot.
Import
import { HuiCollapseTransition } from '@nscode/handy-ui'
import type { CollapseTransitionProps, CollapseTransitionEmits } from '@nscode/handy-ui'
Examples
Toggle content
v-show keeps the content and its internal state. The transition controls the outer element's height. Put padding and borders on an inner element so the collapsed block can reach zero height.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCollapseTransition, HuiSpace } from '@nscode/handy-ui'
const open = ref(true)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiButton :aria-expanded="open" @click="open = !open">{{
open ? 'Collapse content' : 'Expand content'
}}</HuiButton></HuiSpace
>
<HuiCollapseTransition>
<div v-show="open">
<div class="content">
Content of any height. After the animation, automatic height is restored so text wrapping
and content changes lay out normally.
</div>
</div>
</HuiCollapseTransition>
</HuiSpace>
</template>
<style scoped>
.content {
padding: var(--hui-spacing-4);
background: var(--hui-color-primary-bg);
border-radius: var(--hui-border-radius);
}
</style>
Duration
duration is measured in milliseconds and defaults to 300. Compare 150, 300 and 600ms below; 0 completes immediately. All durations complete immediately when the system requests reduced motion.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCollapseTransition, HuiSpace } from '@nscode/handy-ui'
const open = ref(true)
const durations = [150, 300, 600, 0]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace><HuiButton @click="open = !open">Toggle all</HuiButton></HuiSpace>
<div v-for="duration in durations" :key="duration">
<p>{{ duration }}ms</p>
<HuiCollapseTransition :duration="duration">
<div v-show="open">
<div class="content">
This block changes height over {{ duration }}ms.<br />A second line makes the height
change easier to see.
</div>
</div>
</HuiCollapseTransition>
</div>
</HuiSpace>
</template>
<style scoped>
.content {
padding: var(--hui-spacing-3);
background: var(--hui-fill-color-light);
border-radius: var(--hui-border-radius);
}
</style>
More filters
Keep the basic filter and transition in one block, with additional spacing inside the collapsible content. Collapsing leaves no empty grid item. With v-show, values remain when the extra fields are reopened.
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiButton,
HuiCard,
HuiCollapseTransition,
HuiInput,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
const open = ref(false)
const name = ref('')
const department = ref('')
const remark = ref('')
</script>
<template>
<HuiCard title="Filters">
<HuiSpace direction="vertical" fill>
<div>
<HuiInput v-model="name" placeholder="Name" />
<HuiCollapseTransition>
<div v-show="open">
<HuiSpace direction="vertical" fill style="margin-top: var(--hui-spacing-4)"
><HuiInput v-model="department" placeholder="Department" /><HuiInput
v-model="remark"
placeholder="Notes"
/></HuiSpace>
</div>
</HuiCollapseTransition>
</div>
<HuiSpace
><HuiButton
type="primary"
@click="HuiMessage.info(`Name: ${name || 'Any'}; department: ${department || 'Any'}`)"
>Search</HuiButton
><HuiButton variant="link" :aria-expanded="open" @click="open = !open">{{
open ? 'Fewer filters' : 'More filters'
}}</HuiButton></HuiSpace
>
</HuiSpace>
</HuiCard>
</template>
Animate the initial appearance
The initial render does not animate by default. Set appear to enable it. Changing key recreates the transition and its content to replay the initial expansion.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCollapseTransition, HuiSpace } from '@nscode/handy-ui'
const revision = ref(0)
const completed = ref(0)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiButton @click="revision++">Replay appearance</HuiButton
><span>Completed {{ completed }} times</span></HuiSpace
>
<HuiCollapseTransition :key="revision" appear :duration="600" @after-enter="completed++">
<div><div class="content">This block expands from zero height on its first mount.</div></div>
</HuiCollapseTransition>
</HuiSpace>
</template>
<style scoped>
.content {
padding: var(--hui-spacing-4);
background: var(--hui-color-primary-bg);
}
</style>
v-if and completion events
v-if removes the content after the leave animation. Each completed animation emits one afterEnter or afterLeave event. Reversing an animation cancels the previous direction without emitting its completion event.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCollapseTransition, HuiSpace } from '@nscode/handy-ui'
const open = ref(false)
const entered = ref(0)
const left = ref(0)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiButton @click="open = !open">{{ open ? 'Remove content' : 'Create content' }}</HuiButton
><span>Expanded {{ entered }} times; collapsed {{ left }} times</span></HuiSpace
>
<HuiCollapseTransition @after-enter="entered++" @after-leave="left++">
<div v-if="open">
<div class="content">This element is removed only after its leave animation.</div>
</div>
</HuiCollapseTransition>
</HuiSpace>
</template>
<style scoped>
.content {
padding: var(--hui-spacing-4);
background: var(--hui-fill-color-light);
}
</style>
Props
| Prop | Description | Type | Default |
|---|---|---|---|
| duration | Animation duration in ms; 0 completes immediately | number | 300 |
| appear | Animate the initial render | boolean | false |
Events
Public completion events are declared and emitted as afterEnter and afterLeave, once per completed direction. Vue templates may keep @after-enter and @after-leave; render functions use onAfterEnter and onAfterLeave. Old event names are not emitted alongside the new names. Cancelled directions remain silent; height/style restoration and the v-if removal point are unchanged.
| Event | Description | Parameters |
|---|---|---|
| afterEnter | Expansion completed | — |
| afterLeave | Collapse completed | — |
Slots
| Slot | Description | Parameters |
|---|---|---|
| default | One real element controlled by v-show or v-if; a component rendering one element is also supported | — |
Methods
None. Control expansion with v-show or v-if on the slotted content.
Types
interface CollapseTransitionProps {
duration?: number
appear?: boolean
}
interface CollapseTransitionEmits {
afterEnter: []
afterLeave: []
}