Component
collapsible
dx components add collapsibleA collapsible component for showing and hiding content sections.
Added a new feature to the collapsible component
Installation
Use the CLI command for the common path, or copy the component files manually.
Manual installation files
Copy the component source and CSS into your app. Import the shared theme CSS once near your app root.
use dioxus::prelude::*;
use dioxus_icons::lucide::ChevronsUpDown;
use dioxus_primitives::collapsible::{
self, CollapsibleContentProps, CollapsibleProps, CollapsibleTriggerProps,
};
use dioxus_primitives::dioxus_attributes::attributes;
use dioxus_primitives::merge_attributes;
#[component]
pub fn Collapsible(props: CollapsibleProps) -> Element {
rsx! {
document::Link { rel: "stylesheet", href: asset!("/src/components/collapsible/style.css") }
collapsible::Collapsible {
keep_mounted: props.keep_mounted,
default_open: props.default_open,
disabled: props.disabled,
open: props.open,
on_open_change: props.on_open_change,
as: props.r#as,
attributes: props.attributes,
{props.children}
}
}
}
#[component]
pub fn CollapsibleTrigger(props: CollapsibleTriggerProps) -> Element {
let base = attributes!(button {
class: "dx-collapsible-trigger",
});
let merged = merge_attributes(vec![base, props.attributes]);
let show_icon = props.r#as.is_none();
rsx! {
document::Link { rel: "stylesheet", href: asset!("/src/components/collapsible/style.css") }
collapsible::CollapsibleTrigger { as: props.r#as, attributes: merged,
{props.children}
if show_icon {
ChevronsUpDown {
size: "1rem",
stroke: "var(--secondary-color-3)",
}
}
}
}
}
#[component]
pub fn CollapsibleContent(props: CollapsibleContentProps) -> Element {
rsx! {
document::Link { rel: "stylesheet", href: asset!("/src/components/collapsible/style.css") }
collapsible::CollapsibleContent {
class: "dx-collapsible-content",
id: props.id,
attributes: props.attributes,
{props.children}
}
}
}
#[component]
pub fn CollapsibleItem(
#[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
children: Element,
) -> Element {
rsx! {
document::Link { rel: "stylesheet", href: asset!("/src/components/collapsible/style.css") }
div {
border: "1px solid var(--primary-color-6)",
border_radius: "0.5rem",
padding: "1rem",
..attributes,
{children}
}
}
}
#[component]
pub fn CollapsibleList(
#[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
children: Element,
) -> Element {
rsx! {
document::Link { rel: "stylesheet", href: asset!("/src/components/collapsible/style.css") }
div {
display: "flex",
flex_direction: "column",
gap: "0.5rem",
max_width: "20rem",
color: "var(--secondary-color-3)",
..attributes,
{children}
}
}
}Usage notes
The Collapsible component allows users to create expandable sections that can be toggled open or closed. This is useful for displaying content in a compact manner, such as FAQs, menus, or any content that benefits from being hidden until needed.
Component Structure
// The collapsible component must wrap all collapsible items.
Collapsible {
// The trigger is used to expand or collapse the item.
CollapsibleTrigger {}
// The content that is shown when the item is expanded.
CollapsibleContent {}
}