Accordion
Layer: organism · Path:
src/organisms/accordion.rs· Exports:accordion::{Accordion, AccordionCtx}
Stacked collapsible sections (shadcn Accordion). show hands you an AccordionCtx whose section(title, body) appends a Collapsible molecule separated from the previous one by a horizontal Divider. Each section owns its open/closed state in egui memory — the organism keeps no state itself.
Design
-
Purpose / when to use — group related option blocks that should fold away (inspector groups: Transform / Rendering / Physics). Use when only some sections need to be visible at once.
-
Anatomy — a vertical stack; each entry is a
Collapsible(header + body), with aDivider::horizontal()+SPACE_2padding inserted before every section after the first. Optionally wrapped in a cardSurface. -
Variants / states
Variant / state How plain Accordion::new()— bare vertical stackcard .card()— wraps the stack in aSurface::new()cardsection open/closed owned per- Collapsiblein egui memory (not by Accordion) -
Tokens / layout consumed —
core::SPACE_2(inter-section gap); card casing viaSurface. -
Accessibility — folding handled by the
Collapsiblemolecule (click header to toggle).
API
Accordion
| Method | Effect |
|---|---|
Accordion::new() -> Self | Bare (no card). |
Accordion::default() | Same as new(). |
.card() -> Self | Wrap the section group in a card Surface. |
.show(ui, build: impl FnOnce(&mut AccordionCtx)) -> Response | Run build, which adds sections via the ctx. Returns the vertical (or Surface) Response. |
AccordionCtx<'u>
Section builder handed to show. Holds ui: &mut Ui and a first flag internally.
| Method | Effect |
|---|---|
.section(title: impl Into<String>, body: impl FnOnce(&mut Ui)) | Add one collapsible section. Inserts a divider + spacing before all but the first. body paints arbitrary widgets into the section. |
Usage
#![allow(unused)]
fn main() {
use ouroboros_ui::organisms::Accordion;
Accordion::new().show(ui, |acc| {
acc.section("Transform", |ui| { /* fields */ });
acc.section("Rendering", |ui| { /* fields */ });
});
}
#![allow(unused)]
fn main() {
// realistic — card variant with arbitrary widgets per section (from storybook)
use ouroboros_ui::organisms::Accordion;
use ouroboros_ui::molecules::{Field, VectorField};
use ouroboros_ui::atoms::{Switch, Text};
Accordion::new().card().show(ui, |acc| {
acc.section("Transform", |ui| {
let mut p = [1.0_f32, 0.0, -1.0];
VectorField::new(&mut p).speed(0.05).show(ui);
});
acc.section("Rendering", |ui| {
Field::new("Cast shadows")
.horizontal()
.show(ui, |ui| Switch::new(&mut on).show(ui));
});
acc.section("Physics", |ui| {
Text::new("Collider, mass, drag").muted().show(ui);
});
});
}
Composition
Composes the Collapsible molecule (one per section), the Divider atom (separators), and optionally the Surface atom (card casing). Never paints directly — see guards.
Notes
- Open/closed state lives in egui memory, owned by each
Collapsible(keyed by its title) — not byAccordion. Identical section titles within one accordion would collide on memory id. sectiontakesFnOncebodies — the body closure runs immediately duringshow.- The first section never gets a leading divider; ordering of
sectioncalls is the visual order.