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

可折叠

一个带标签的可点击标题,可切换其内容的可见性。

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

Collapsible 是一个原件,可以通过点击带标签的标题来显示或隐藏其内容。通过 isOpenonOpenChange 控制——每个 Collapsible 管理独立状态。

安装

🌐 Installation

Terminal
npx expo install @expo/ui

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

用法

🌐 Usage

基本可折叠

🌐 Basic collapsible

CollapsibleExample.tsx
import { useState } from 'react'; import { Host, Column, Collapsible, Text } from '@expo/ui'; export default function CollapsibleExample() { const [open, setOpen] = useState(false); return ( <Host style={{ flex: 1 }}> <Column spacing={8} style={{ padding: 16 }}> <Collapsible isOpen={open} onOpenChange={setOpen} label="About"> <Text> A primitive that toggles visibility of its content via a labelled tappable header. </Text> </Collapsible> </Column> </Host> ); }

手风琴(一次只打开一个部分)

🌐 Accordion (one section open at a time)

将每个 CollapsibleisOpen 连接到共享的父级值。组件不强制排他性 — 组合由使用者决定。

🌐 Wire each Collapsible's isOpen to a shared parent value. The component doesn't enforce exclusivity — composition is up to the consumer.

CollapsibleAccordionExample.tsx
import { useState } from 'react'; import { Host, Column, Collapsible, Text } from '@expo/ui'; type Section = 'a' | 'b' | 'c' | null; export default function CollapsibleAccordionExample() { const [openSection, setOpenSection] = useState<Section>('a'); return ( <Host style={{ flex: 1 }}> <Column spacing={8} style={{ padding: 16 }}> <Collapsible isOpen={openSection === 'a'} onOpenChange={open => setOpenSection(open ? 'a' : null)} label="Section A"> <Text>Opening B or C closes this one.</Text> </Collapsible> <Collapsible isOpen={openSection === 'b'} onOpenChange={open => setOpenSection(open ? 'b' : null)} label="Section B"> <Text>Opening A or C closes this one.</Text> </Collapsible> <Collapsible isOpen={openSection === 'c'} onOpenChange={open => setOpenSection(open ? 'c' : null)} label="Section C"> <Text>Opening A or B closes this one.</Text> </Collapsible> </Column> </Host> ); }

应用接口

🌐 API

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

Component

Collapsible

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

Optional • Type: ReactNode

Content rendered when isOpen is true.

isOpen

Type: boolean

Whether the content is currently expanded.

label

Optional • Type: string

Text rendered in the tappable header.

labelStyle

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

Type: (isOpen: boolean) => void

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