A collapsible is a region whose content expands and collapses: details under a summary, a “show more”, a section of advanced settings. It renders its children always and its content after them while open. The app owns open and the trigger that flips it (one of the children), and the look is the app's: a Collapsible has no trigger, chevron, motion or label of its own.
use nsh_base::{collapsible, theme}; use nsh_core::{AccessibilityState, ui::prelude::*}; fn storage() -> impl IntoElement { let open = signal(false); collapsible() .open(open) .gap_2() .child( view() .role(AccessibilityRole::Button) .accessibility_state(move || AccessibilityState { expanded: Some(open.get()), ..AccessibilityState::default() }) .on_press(move || open.update(|o| *o = !*o)) .flex_row() .items_center() .justify_between() .child(text("Storage")) .child(text("›").transition_transform().rotate(move || { if open.get() { deg(90.0) } else { deg(0.0) } })), ) .content(text("Photos 120 GB · Apps 54 GB · System 38 GB").text_color(move || theme().muted_foreground)) }
import { mount, signal, Text, View } from "nsh"; import { Collapsible } from "nsh/base"; function App() { const open = signal(false); return ( <Collapsible open={open()} class="gap-2" content={<Text class="text-muted-foreground">Photos 120 GB · Apps 54 GB · System 38 GB</Text>} > <View role="button" accessibilityState={{ expanded: open() }} onPress={() => open.set(!open())} class="flex-row items-center justify-between active:opacity-70" > <Text class="font-medium">Storage</Text> <Text class={`transition-transform ${open() ? "rotate-90" : ""}`}>›</Text> </View> </Collapsible> ); } mount(App);
Collapsible comes from nsh/base, the nsh-base crate in Rust. The value is the app's: its trigger flips open, and the collapsible shows the value the app passes. In Rust, collapsible() makes one with a setter per prop (.open(..), .content(..)), the typed style methods for the region and .child(..) for what shows always.
| Prop | Rust | Type | Default | Description |
|---|---|---|---|---|
open | .open(..) | boolean | false | Expanded: the content is in the tree, after the children. The app owns the value. |
content | .content(..) | JSX.Element | none | What expands. It is made once and kept while collapsed, so what is inside keeps its state (an input's text, a scroll position, a switch) from one opening to the next. |
children | .child(..) | JSX.Element | none | What shows either way: a summary, the trigger that flips open. |
class, style | style methods | string, StyleProp<ViewStyle> | none | The region around the children and the content (gap-2 spaces them). |
A collapsible reports nothing of its own: the trigger is the app's, and its press (onPress of the View with role="button") is where the app flips open.
| Part | Inside | Styled with |
|---|---|---|
| The region | class, style | |
| The children | the region, first | their own elements |
| The content | the region, after the children, while open | its own elements (content) |
The content comes and goes at once. A transition of the app‘s shows the change: the trigger’s chevron turns (transition-transform), and the content can fade in with its own classes. Each platform lays the region out again when the content arrives or leaves.
accessibilityState={{ expanded: open }} and a button role (role="button" on a View), and screen readers announce “expanded” or “collapsed” with its label.See Accessibility for labels, roles and announcements in general.
open, and leaves it out of the layout otherwise.