Skip to content

Commit

Permalink
feat: Body component
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Nov 18, 2023
1 parent 2c0ee8d commit a18acc1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
54 changes: 54 additions & 0 deletions crates/components/src/body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_hooks::{use_get_theme, BodyTheme};

/// [`Body`] component properties.
#[derive(Props)]
pub struct BodyProps<'a> {
/// Inner children for the Body.
children: Element<'a>,
/// Padding for the Body.
#[props(default = "none".to_string(), into)]
padding: String,
}

/// `Body` component.
///
/// Usually just used one time and as a root component for all the app.
///
/// # Props
/// See [`BodyProps`].
///
/// # Styling
/// Inherits the [`BodyTheme`](freya_hooks::BodyTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// fn app(cx: Scope) -> Element {
/// render!(
/// Body {
/// label {
/// "Click this"
/// }
/// }
/// )
/// }
/// ```
///
#[allow(non_snake_case)]
pub fn Body<'a>(cx: Scope<'a, BodyProps<'a>>) -> Element {
let theme = use_get_theme(cx);
let BodyTheme { background, color } = theme.body;
let BodyProps { children, padding } = cx.props;

render!(rect {
width: "fill",
height: "fill",
color: "{color}",
background: "{background}",
padding: "{padding}",
children
})
}
2 changes: 2 additions & 0 deletions crates/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! A collection of basic components to be used in Freya.
mod accordion;
mod body;
mod button;
mod canvas;
mod cursor_area;
Expand All @@ -22,6 +23,7 @@ mod theme;
mod tooltip;

pub use accordion::*;
pub use body::*;
pub use button::*;
pub use canvas::*;
pub use cursor_area::*;
Expand Down
19 changes: 11 additions & 8 deletions examples/switch_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ fn app(cx: Scope) -> Element {
let is_enabled = if *enabled.get() { "Yes" } else { "No" };

render!(
Switch {
enabled: *enabled.get(),
ontoggled: |_| {
enabled.set(!enabled.get());
Body {
padding: "20",
Switch {
enabled: *enabled.get(),
ontoggled: |_| {
enabled.set(!enabled.get());
}
}
label {
"Is enabled? {is_enabled}"
}
TheOtherSwitch { }
}
label {
"Is enabled? {is_enabled}"
}
TheOtherSwitch { }
)
}

0 comments on commit a18acc1

Please sign in to comment.