Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Collapsible

可点击的带标签标题,用于切换其内容的可见性

Android
iOS
Web
Included in Expo Go
Recommended version:
~58.0.1

Collapsible 是一种基本组件,点击带标签的标题即可显示或隐藏其内容。通过 isOpen 和 onOpenChange 控制 — 每个 Collapsible 都管理独立的状态。

展开的 About 区域,其中包含一个箭头和描述文本展开的 About 区域,其中包含一个箭头和描述文本

安装

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基本的可折叠区域

CollapsibleExample.tsx
import { useState } from 'react'; import { useColorScheme } from 'react-native'; import { Host, Column, Collapsible, Text } from '@expo/ui'; export default function CollapsibleExample() { const [open, setOpen] = useState(false); const colorScheme = useColorScheme(); const ink = { color: colorScheme === 'dark' ? '#FFFFFF' : '#000000' }; return ( <Host matchContents={{ vertical: true }} style={{ width: '100%' }}> <Column spacing={8} style={{ padding: 16 }}> <Collapsible isOpen={open} onOpenChange={setOpen} label="About"> <Text textStyle={ink}> A primitive that toggles visibility of its content via a labelled tappable header. </Text> </Collapsible> </Column> </Host> ); }

手风琴(一次展开一个区域)

将每个 Collapsible 的 isOpen 连接到共享的父级值。该组件不会强制互斥 — 如何组合由使用者决定。

CollapsibleAccordionExample.tsx
import { useState } from 'react'; import { useColorScheme } from 'react-native'; import { Host, Column, Collapsible, Text } from '@expo/ui'; type Section = 'a' | 'b' | 'c' | null; export default function CollapsibleAccordionExample() { const [openSection, setOpenSection] = useState<Section>('a'); const colorScheme = useColorScheme(); const ink = { color: colorScheme === 'dark' ? '#FFFFFF' : '#000000' }; return ( <Host matchContents={{ vertical: true }} style={{ width: '100%' }}> <Column spacing={8} style={{ padding: 16 }}> <Collapsible isOpen={openSection === 'a'} onOpenChange={open => setOpenSection(open ? 'a' : null)} label="Section A"> <Text textStyle={ink}>Opening B or C closes this one.</Text> </Collapsible> <Collapsible isOpen={openSection === 'b'} onOpenChange={open => setOpenSection(open ? 'b' : null)} label="Section B"> <Text textStyle={ink}>Opening A or C closes this one.</Text> </Collapsible> <Collapsible isOpen={openSection === 'c'} onOpenChange={open => setOpenSection(open ? 'c' : null)} label="Section C"> <Text textStyle={ink}>Opening A or B closes this one.</Text> </Collapsible> </Column> </Host> ); }

API

import { Collapsible } from '@expo/ui';

Component

Collapsible

Android
iOS
Web

Type: React.Element<CollapsibleProps>

A primitive that toggles visibility of its content via a labelled tappable header. Controlled via isOpen + onOpenChange.

Props for the Collapsible component, a primitive that shows or hides its content with a tap on a labelled header.

CollapsibleProps

children

Android
iOS
Web
Optional • Type: ReactNode

Content rendered when isOpen is true.

isOpen

Android
iOS
Web
Type: boolean

Whether the content is currently expanded.

label

Android
iOS
Web
Optional • Type: string

Text rendered in the tappable header.

labelStyle

Android
iOS
Web
Optional • Type: { color: string, fontFamily: string, fontSize: number, fontWeight: 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900', letterSpacing: number, lineHeight: number, textAlign: 'center' | 'left' | 'right' }

Text-specific styling for the tappable header label.

onOpenChange

Android
iOS
Web
Type: (isOpen: boolean) => void

Called when the user taps the header to toggle the open state.