Disclosure
A header that stays visible and content that slides open below it, alone or stacked into an accordion; a style decides how the header looks.
Accordion
DisclosureGroup::new("faq")
.open(&mut state.open)
.multiple(state.multiple)
.bordered(state.bordered)
.disabled(state.disabled)
.show(ui, |group| {
group.add("Is it accessible?", |ui| {
ui.label(
"Yes. Each header is a button that reports whether it is expanded, \
and every header can be reached with the keyboard.",
);
});
group.add("Can it hold any content?", |ui| {
ui.label("An item takes any widgets as its content, not just text.");
ui.horizontal(|ui| {
let _ = ui.button("A button");
ui.checkbox(&mut state.checked, "Or a checkbox");
});
});
group.add("Is it animated?", |ui| {
ui.label(
"Yes. Opening and closing slides the content, and the chevron turns to follow. \
Items below move along with it.",
);
});
group.add(Disclosure::new("Disabled item").disabled(true), |ui| {
ui.label("This item cannot be opened.");
});
});
Accordion sizes
for (name, size) in [
("Extra small", Size::XSmall),
("Small", Size::Small),
("Large", Size::Large),
] {
DisclosureGroup::new(name).size(size).show(ui, |group| {
group.add(format!("{name} item"), |ui| {
ui.label("Padding and text size follow the size.");
});
group.add("Another item", |ui| {
ui.label("Closed by default.");
});
});
}
Header of your own
Disclosure::new("Order #4189")
.open(&mut state.order)
.gap(8.0)
.show_with_header(
ui,
|ui, open| {
ui.horizontal(|ui| {
ui.strong("Order #4189");
ui.with_layout(
egui::Layout::right_to_left(egui::Align::Center),
|ui| {
let label = if *open {
"Hide details"
} else {
"Show details"
};
if ui.add(Button::ghost(label).xsmall()).clicked() {
*open = !*open;
}
},
);
});
},
|ui| {
for (title, value) in [
("Shipping address", "100 Market St, San Francisco, CA"),
("Items", "2x Studio Headphones"),
] {
ui.label(title);
ui.label(egui::RichText::new(value).color(muted));
}
},
);
Default
GroupBox::new().outline().show(ui, |ui| {
Disclosure::new("How do I reset my password?")
.open(&mut state.faq)
.gap(8.0)
.show(ui, |ui| {
ui.label(
egui::RichText::new(
"Click the Forgot Password link on the sign in page, and we will send \
you an email with instructions to create a new one.",
)
.color(muted),
)
});
});
Settings
Disclosure::new("Notification settings")
.open(&mut state.settings)
.gap(8.0)
.show_with_header(
ui,
|ui, open| {
let button = Button::new("Notification settings")
.style(&fastframe_ui::button_styles::Outline)
.min_width(ui.available_width());
if ui.add(button).clicked() {
*open = !*open;
}
},
|ui| {
GroupBox::new().outline().show(ui, |ui| {
let labels = ["Push notifications", "Email digests", "Weekly summary"];
for (checked, label) in state.notifications.iter_mut().zip(labels) {
ui.checkbox(checked, label);
}
});
},
);
Nested
Disclosure::new("components")
.default_open(true)
.show(ui, |ui| {
ui.indent("components-children", |ui| {
Disclosure::new("ui").show(ui, |ui| {
ui.indent("ui-children", |ui| {
for file in ["button.rs", "card.rs", "dialog.rs"] {
ui.label(file);
}
});
});
ui.label("login_form.rs");
});
});
ui.label("main.rs");
Styles
let [row, accordion] = &mut state.styled;
Disclosure::new("Row, the default")
.open(row)
.show(ui, |ui| ui.label("A chevron before the title."));
Disclosure::new("Accordion")
.open(accordion)
.style(&disclosure_styles::Accordion)
.show(ui, |ui| ui.label("The chevron at the end, turning."));
Environment
// A group of rows: the style set around it reaches its items.
ui.disclosure_style(disclosure_styles::Row, |ui| {
DisclosureGroup::new("rows")
.bordered(false)
.default_open([1])
.show(ui, |group| {
group.add("Recent", |ui| ui.label("Nothing yet."));
group.add("Pinned", |ui| ui.label("Three chats."));
});
});
Your own style
ui.disclosure_style(Trailing, |ui| {
Disclosure::new("Advanced")
.default_open(true)
.show(ui, |ui| {
ui.label("Shown under a header of your own design.")
});
Disclosure::new("Experimental").show(ui, |ui| ui.label("Closed by default."));
});
The story above is gallery/src/stories/disclosure.rs, running in your browser. Every snippet on this page is cut from it.