patternfly_yew/components/
background.rs1use std::borrow::Cow;
3use yew::html::IntoPropValue;
4use yew::prelude::*;
5
6#[derive(Clone, PartialEq, Properties)]
8pub struct BackgroundProperties {
9 #[prop_or_default]
11 pub id: Option<String>,
12
13 #[prop_or_default]
19 pub style: BackgroundStyle,
20
21 #[prop_or_default]
26 pub additional_style: Option<String>,
27}
28
29#[derive(Clone, Default, Debug, PartialEq, Eq)]
30pub enum BackgroundStyle {
31 #[default]
32 Default,
33 Image(Cow<'static, str>),
34 Style(Cow<'static, str>),
35}
36
37impl IntoPropValue<BackgroundStyle> for String {
38 fn into_prop_value(self) -> BackgroundStyle {
39 BackgroundStyle::Image(self.into())
40 }
41}
42
43impl IntoPropValue<BackgroundStyle> for &'static str {
44 fn into_prop_value(self) -> BackgroundStyle {
45 BackgroundStyle::Image(self.into())
46 }
47}
48
49impl IntoPropValue<BackgroundStyle> for Cow<'static, str> {
50 fn into_prop_value(self) -> BackgroundStyle {
51 BackgroundStyle::Image(self)
52 }
53}
54
55#[function_component(Background)]
82pub fn background(props: &BackgroundProperties) -> Html {
83 let mut style = match &props.style {
84 BackgroundStyle::Default => {
85 "--pf-v5-c-background-image--BackgroundImage: url(assets/images/pfbg-icon.svg);"
86 .to_string()
87 }
88 BackgroundStyle::Image(url) => {
89 format!("--pf-v5-c-background-image--BackgroundImage: url({url});")
90 }
91 BackgroundStyle::Style(style) => style.to_string(),
92 };
93
94 if let Some(additional) = &props.additional_style {
95 style.push_str(additional);
96 }
97
98 html!(
99 <div
100 id={props.id.clone()}
101 class="pf-v5-c-background-image"
102 {style}
103 ></div>
104 )
105}