Add ui::ComponentPreview
(#20319)
The `ComponentPreview` trait enables rendering storybook-like previews of components inside of Zed.  This initial version will work for any component that doesn't return a view. Example impl: ```rust impl ComponentPreview for Checkbox { fn description() -> impl Into<Option<&'static str>> { "A checkbox lets people choose between opposing..." } fn examples() -> Vec<ComponentExampleGroup<Self>> { vec![ example_group( "Default", vec![ single_example( "Unselected", Checkbox::new("checkbox_unselected", Selection::Unselected), ), // ... more examples ], ), // ... more examples ] } } ``` Example usage: ```rust fn render_components_page(&self, cx: &ViewContext<Self>) -> impl IntoElement { v_flex() .gap_2() .child(Checkbox::render_component_previews(cx)) .child(Icon::render_component_previews(cx)) } } ``` Release Notes: - N/A
This commit is contained in:
parent
a409123342
commit
f6fbf662b4
6 changed files with 249 additions and 26 deletions
|
@ -115,3 +115,51 @@ impl RenderOnce for Checkbox {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl ComponentPreview for Checkbox {
|
||||
fn description() -> impl Into<Option<&'static str>> {
|
||||
"A checkbox lets people choose between a pair of opposing states, like enabled and disabled, using a different appearance to indicate each state."
|
||||
}
|
||||
|
||||
fn examples() -> Vec<ComponentExampleGroup<Self>> {
|
||||
vec![
|
||||
example_group(
|
||||
"Default",
|
||||
vec![
|
||||
single_example(
|
||||
"Unselected",
|
||||
Checkbox::new("checkbox_unselected", Selection::Unselected),
|
||||
),
|
||||
single_example(
|
||||
"Indeterminate",
|
||||
Checkbox::new("checkbox_indeterminate", Selection::Indeterminate),
|
||||
),
|
||||
single_example(
|
||||
"Selected",
|
||||
Checkbox::new("checkbox_selected", Selection::Selected),
|
||||
),
|
||||
],
|
||||
),
|
||||
example_group(
|
||||
"Disabled",
|
||||
vec![
|
||||
single_example(
|
||||
"Unselected",
|
||||
Checkbox::new("checkbox_disabled_unselected", Selection::Unselected)
|
||||
.disabled(true),
|
||||
),
|
||||
single_example(
|
||||
"Indeterminate",
|
||||
Checkbox::new("checkbox_disabled_indeterminate", Selection::Indeterminate)
|
||||
.disabled(true),
|
||||
),
|
||||
single_example(
|
||||
"Selected",
|
||||
Checkbox::new("checkbox_disabled_selected", Selection::Selected)
|
||||
.disabled(true),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,11 @@ use serde::{Deserialize, Serialize};
|
|||
use strum::{EnumIter, EnumString, IntoStaticStr};
|
||||
use ui_macros::DerivePathStr;
|
||||
|
||||
use crate::{prelude::*, Indicator};
|
||||
use crate::{
|
||||
prelude::*,
|
||||
traits::component_preview::{example_group, ComponentExample, ComponentPreview},
|
||||
Indicator,
|
||||
};
|
||||
|
||||
#[derive(IntoElement)]
|
||||
pub enum AnyIcon {
|
||||
|
@ -494,3 +498,26 @@ impl RenderOnce for IconWithIndicator {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ComponentPreview for Icon {
|
||||
fn examples() -> Vec<ComponentExampleGroup<Icon>> {
|
||||
let arrow_icons = vec![
|
||||
IconName::ArrowDown,
|
||||
IconName::ArrowLeft,
|
||||
IconName::ArrowRight,
|
||||
IconName::ArrowUp,
|
||||
IconName::ArrowCircle,
|
||||
];
|
||||
|
||||
vec![example_group(
|
||||
"Arrow Icons",
|
||||
arrow_icons
|
||||
.into_iter()
|
||||
.map(|icon| {
|
||||
let name = format!("{:?}", icon).to_string();
|
||||
ComponentExample::new(name, Icon::new(icon))
|
||||
})
|
||||
.collect(),
|
||||
)]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue