Skip to content

Collapse - Vant 4

📂 Collapse

Create expandable content sections with smooth animations

🌟 Introduction

Transform your content organization with collapsible panels! Perfect for FAQs, settings menus, and content categorization. Click panel titles to elegantly expand or contract content sections.

📦 Installation

Register component globally via app.use, refer to Component Registration for more registration ways.

js
import { createApp } from'vue'; import { Collapse, CollapseItem } from'vant'; const app = createApp(); app.use(Collapse); app.use(CollapseItem);

🎨 Usage Examples

🌟 Basic Usage

Use v-model to control the name of active panels and create interactive content sections:

html
js
import { ref } from'vue'; exportdefault { setup() { const activeNames = ref(['1']); return { activeNames }; }, };

🎵 Accordion Mode

Create an elegant accordion where only one panel expands at a time - perfect for organized content flow:

html
js
import { ref } from'vue'; exportdefault { setup() { const activeName = ref('1'); return { activeName }; }, };

🚫 Disabled State

Use the disabled prop to disable specific CollapseItem panels when needed:

html

🎨 Custom Title

Using title slot to create beautifully customized panel headers:

html
js
import { ref } from'vue'; exportdefault { setup() { const activeNames = ref(['1']); return { activeNames }; }, };

🔄 Toggle All Panels

Using toggleAll method to programmatically control all panels at once:

html
js
import { ref } from'vue'; exportdefault { setup() { const activeNames = ref(['1']); const collapse = ref(null); constopenAll = () => { collapse.value.toggleAll(true); } consttoggleAll = () => { collapse.value.toggleAll(); }, return { activeNames, openAll, toggleAll, collapse, }; }, };

Tips: The toggleAll method cannot be used in accordion mode.

🔧 API Reference

🎛️ Collapse Props

Configure the main collapse container behavior:

AttributeDescriptionTypeDefault
v-modelNames of current active panelsaccordion mode: *numberstring*non-accordion mode: *(number
accordionWhether to be accordion modebooleanfalse
borderWhether to show outer borderbooleantrue

📡 Collapse Events

Listen to panel state changes:

EventDescriptionArguments
changeEmitted when switching panel*activeNames: string

📦 CollapseItem Props

Customize individual collapse panel appearance and behavior:

AttributeDescriptionTypeDefault
nameName*numberstring*
iconLeft Iconstring-
sizeTitle size, can be set to largestring-
titleTitle*numberstring*
valueRight text*numberstring*
labelDescription below the titlestring-
borderWhether to show inner borderbooleantrue
disabledWhether to disabled collapsebooleanfalse
readonlyWhether to be readonlybooleanfalse
is-linkWhether to show link iconbooleantrue
lazy-renderWhether to lazy render util openedbooleantrue
title-classTitle classNamestring-
value-classValue classNamestring-
label-classLabel classNamestring-

⚙️ Collapse Methods

Use ref to get Collapse instance and call powerful instance methods:

NameDescriptionAttributeReturn value
toggleAllToggle the expanded status of all collapses*options?: booleanobject*

toggleAll Usage

js
import { ref } from'vue'; import type { CollapseInstance } from'vant'; const collapseRef = ref<CollapseInstance>(); // Toggle all collapseRef.value?.toggleAll(); // Expand all collapseRef.value?.toggleAll(true); // UnExpand all collapseRef.value?.toggleAll(false); // Toggle all, skip disabled collapseRef.value?.toggleAll({ skipDisabled: true, }); // Expand all, skip disabled collapseRef.value?.toggleAll({ expanded: true, skipDisabled: true, });

⚙️ CollapseItem Methods

Use ref to get CollapseItem instance and call instance methods:

NameDescriptionAttributeReturn value
toggleToggle expanded statusexpanded: boolean-

📝 Types

Access comprehensive TypeScript definitions for better development experience:

ts
importtype { CollapseProps, CollapseItemProps, CollapseItemInstance, CollapseToggleAllOptions, } from'vant';

CollapseItemInstance is the type of component instance:

ts
import { ref } from'vue'; importtype { CollapseItemInstance } from'vant'; const collapseItemRef = ref<CollapseItemInstance>(); collapseItemRef.value?.toggle();

🎰 CollapseItem Slots

Customize every aspect of your collapse panels with flexible slot options:

NameDescription
defaultContent
titleCustom header title
valueCustom header value
labelCustom header label
iconCustom header left icon
right-iconCustom header right icon

🎨 Theming

🎯 CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-collapse-item-durationvar(--van-duration-base)-
--van-collapse-item-content-paddingvar(--van-padding-sm) var(--van-padding-md)-
--van-collapse-item-content-font-sizevar(--van-font-size-md)-
--van-collapse-item-content-line-height1.5-
--van-collapse-item-content-text-colorvar(--van-text-color-2)-
--van-collapse-item-content-backgroundvar(--van-background-2)-
--van-collapse-item-title-disabled-colorvar(--van-text-color-3)-

Explore more components and resources to enhance your development experience:

Content Organization

  • Tabs - Horizontal content switching
  • List - Scrollable content lists
  • Card - Content cards with styling

Interactive Components

Form Components

  • Field - Input fields with validation
  • Switch - Toggle switches
  • Checkbox - Multiple selection options

Development Resources

Enterprise-level mobile solution based on Vant