Skip to content

Commit

Permalink
♻️ Use theme context instead of hard-code HTML template.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Oct 29, 2024
1 parent ace9d4d commit c50c05b
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 198 deletions.
52 changes: 26 additions & 26 deletions packages/components/src/form/button/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Props {

#[styled_component]
pub fn Button(props: &Props) -> Html {
let radius_type = use_context::<ContextProviderType>();
let radius_type = use_context::<ComponentContextProviderType>();
let radius_type = match &radius_type {
Some(ctx) => ctx.border_radius_type,
None => BorderRadiusType::Default,
Expand Down Expand Up @@ -127,33 +127,33 @@ pub fn Button(props: &Props) -> Html {
height: 100%;
border: 1px solid transparent;
box-shadow: 1px 1px 4px 0 var(--color-shadow-rgba);
box-shadow: 1px 1px 4px 0 var(--color-shadow);
transition: all 0.3s;
&[data-color="primary"] {
background-color: rgba(var(--color-primary), 0.8);
border-color: rgba(var(--color-primary), 0.8);
background-color: var(--color-primary-most);
border-color: var(--color-primary-most);
}
&[data-color="secondary"] {
background-color: rgba(var(--color-secondary), 0.8);
border-color: rgba(var(--color-secondary), 0.8);
background-color: var(--color-secondary-most);
border-color: var(--color-secondary-most);
}
&[data-color="success"] {
background-color: rgba(var(--color-success), 0.8);
border-color: rgba(var(--color-success), 0.8);
background-color: var(--color-success-most);
border-color: var(--color-success-most);
}
&[data-color="error"] {
background-color: rgba(var(--color-error), 0.8);
border-color: rgba(var(--color-error), 0.8);
background-color: var(--color-error-most);
border-color: var(--color-error-most);
}
&[data-color="info"] {
background-color: rgba(var(--color-info), 0.8);
border-color: rgba(var(--color-info), 0.8);
background-color: var(--color-info-most);
border-color: var(--color-info-most);
}
&[data-color="warning"] {
background-color: rgba(var(--color-warning), 0.8);
border-color: rgba(var(--color-warning), 0.8);
background-color: var(--color-warning-most);
border-color: var(--color-warning-most);
}
&[data-border-radius-type="default"] {
Expand All @@ -172,8 +172,8 @@ pub fn Button(props: &Props) -> Html {
&[data-style="outlined"] {
backdrop-filter: blur(4px);
}
&[data-style="outlined"]&[data-state="none"],
&[data-style="outlined"]&[data-state="hover"] {
&[data-style="outlined"] &[data-state="none"],
&[data-style="outlined"] &[data-state="hover"] {
background-color: transparent;
}
&[data-style="basic"] {
Expand Down Expand Up @@ -220,7 +220,7 @@ pub fn Button(props: &Props) -> Html {
width: 100%;
height: 100%;
border-radius: 4px;
box-shadow: 1px 1px 4px 0 var(--color-shadow-rgba);
box-shadow: 1px 1px 4px 0 var(--color-shadow);
opacity: 0;
z-index: -1;
Expand Down Expand Up @@ -276,22 +276,22 @@ pub fn Button(props: &Props) -> Html {
transition: color 0.3s;
&[data-color="primary"]&[data-style="outlined"] {
color: rgb(var(--color-primary));
color: var(--color-primary);
}
&[data-color="secondary"]&[data-style="outlined"] {
color: rgb(var(--color-secondary));
color: var(--color-secondary);
}
&[data-color="success"]&[data-style="outlined"] {
color: rgb(var(--color-success));
color: var(--color-success);
}
&[data-color="error"]&[data-style="outlined"] {
color: rgb(var(--color-error));
color: var(--color-error);
}
&[data-color="info"]&[data-style="outlined"] {
color: rgb(var(--color-info));
color: var(--color-info);
}
&[data-color="warning"]&[data-style="outlined"] {
color: rgb(var(--color-warning));
color: var(--color-warning);
}
&[data-size="small"] {
Expand All @@ -305,13 +305,13 @@ pub fn Button(props: &Props) -> Html {
}
&[data-style="outlined"] {
color: rgb(var(--color-primary));
color: var(--color-primary);
}
&[data-style="outlined"]&[data-state="active"] {
color: rgb(var(--color-button-text));
color: var(--color-button-text);
}
&[data-style="basic"] {
color: rgb(var(--color-button-text));
color: var(--color-button-text);
}
"#)}

Expand Down
12 changes: 6 additions & 6 deletions packages/components/src/form/button/button_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn ButtonGroup(props: &Props) -> Html {
display: flex;
flex-direction: row;
"#)}>
<ContextShell
<ComponentContextShell
border_radius_type={BorderRadiusType::OnlyLeft}
>
{
Expand All @@ -61,20 +61,20 @@ pub fn ButtonGroup(props: &Props) -> Html {
html! {}
}
}
</ContextShell>
</ComponentContextShell>

{props.children.iter().skip(1).take(props.children.len() - 2).map(|child| html! {
<ContextShell
<ComponentContextShell
border_radius_type={BorderRadiusType::None}
>
{{
let child: Html = child.clone().into();
child
}}
</ContextShell>
</ComponentContextShell>
}).collect::<Html>()}

<ContextShell
<ComponentContextShell
border_radius_type={BorderRadiusType::OnlyRight}
>
{
Expand All @@ -84,7 +84,7 @@ pub fn ButtonGroup(props: &Props) -> Html {
html! {}
}
}
</ContextShell>
</ComponentContextShell>
</div>
},
}
Expand Down
2 changes: 1 addition & 1 deletion packages/macro-types/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where
<textarea id='ssr_data' style='display: none;'>{state}</textarea>
<div id='app'>{html_raw}</div>
<script src='/a.js'></script>
<script>(async () => {{await wasm_vendor_entry('/a.wasm');(await (new wasm_vendor_entry.WebHandle())).start();}})()</script>
<script>(async () => {{await __wasm_vendor_entry('/a.wasm');(await (new __wasm_vendor_entry.WebHandle())).start();}})()</script>
</body>
</html>
"))
Expand Down
14 changes: 7 additions & 7 deletions packages/theme/src/components/form/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub enum BorderRadiusType {
}

#[derive(Debug, PartialEq, Clone)]
pub struct ContextState {
pub struct ComponentContextState {
pub border_radius_type: BorderRadiusType,
}

#[derive(Properties, Debug, PartialEq)]
pub struct ContextProps {
pub struct ComponentContextProps {
#[prop_or(SizeType::Medium)]
pub size: SizeType,
#[prop_or(ColorType::Primary)]
Expand All @@ -31,17 +31,17 @@ pub struct ContextProps {
pub children: Children,
}

pub type ContextProviderType = UseStateHandle<ContextState>;
pub type ComponentContextProviderType = UseStateHandle<ComponentContextState>;

#[function_component]
pub fn ContextShell(props: &ContextProps) -> Html {
let ctx = use_state(|| ContextState {
pub fn ComponentContextShell(props: &ComponentContextProps) -> Html {
let ctx = use_state(|| ComponentContextState {
border_radius_type: props.border_radius_type,
});

html! {
<ContextProvider<ContextProviderType> context={ctx.clone()}>
<ContextProvider<ComponentContextProviderType> context={ctx.clone()}>
{props.children.clone() }
</ContextProvider<ContextProviderType>>
</ContextProvider<ComponentContextProviderType>>
}
}
111 changes: 111 additions & 0 deletions packages/theme/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use yuuka::derive_struct;

use yew::prelude::*;

use crate::{
prelude::element::designs::color::COLOR_MAP,
types::{ColorLevel, ColorMap},
};

derive_struct!(pub Theme { color: ColorMap = COLOR_MAP.clone() });

#[derive(Properties, Debug, PartialEq)]
pub struct ThemeContextProviderProps {
pub context: Theme,

#[prop_or_default]
pub children: Children,
}

#[function_component]
pub fn ThemeContextShell(props: &ThemeContextProviderProps) -> Html {
html! {
<ContextProvider<Theme> context={props.context.clone()}>
<style>
{format!(r#"
:root {{
--color-primary: {};
--color-secondary: {};
--color-success: {};
--color-warning: {};
--color-error: {};
--color-info: {};
--color-primary-most: {};
--color-secondary-most: {};
--color-success-most: {};
--color-warning-most: {};
--color-error-most: {};
--color-info-most: {};
--color-primary-half: {};
--color-secondary-half: {};
--color-success-half: {};
--color-warning-half: {};
--color-error-half: {};
--color-info-half: {};
--color-primary-less: {};
--color-secondary-less: {};
--color-success-less: {};
--color-warning-less: {};
--color-error-less: {};
--color-info-less: {};
--color-light: rgb(255, 255, 255);
--color-dark: rgb(0, 0, 0);
--color-light-most: rgba(255, 255, 255, 0.8);
--color-dark-most: rgba(0, 0, 0, 0.8);
--color-light-half: rgba(255, 255, 255, 0.5);
--color-dark-half: rgba(0, 0, 0, 0.5);
--color-light-less: rgba(255, 255, 255, 0.2);
--color-dark-less: rgba(0, 0, 0, 0.2);
--color-button-text: var(--color-light-most);
--color-text: var(--color-dark-most);
--color-shadow: var(--color-dark-less);
}}
* {{
margin: 0;
padding: 0;
box-sizing: border-box;
}}
body {{
font-family: 'PingFang SC', 'Helvetica Neue', 'Microsoft YaHei', sans-serif;
}}"#,
props.context.color.primary.to_rgb_str(),
props.context.color.secondary.to_rgb_str(),
props.context.color.success.to_rgb_str(),
props.context.color.warning.to_rgb_str(),
props.context.color.error.to_rgb_str(),
props.context.color.info.to_rgb_str(),
props.context.color.primary.to_rgba_str(ColorLevel::Most),
props.context.color.secondary.to_rgba_str(ColorLevel::Most),
props.context.color.success.to_rgba_str(ColorLevel::Most),
props.context.color.warning.to_rgba_str(ColorLevel::Most),
props.context.color.error.to_rgba_str(ColorLevel::Most),
props.context.color.info.to_rgba_str(ColorLevel::Most),
props.context.color.primary.to_rgba_str(ColorLevel::Half),
props.context.color.secondary.to_rgba_str(ColorLevel::Half),
props.context.color.success.to_rgba_str(ColorLevel::Half),
props.context.color.warning.to_rgba_str(ColorLevel::Half),
props.context.color.error.to_rgba_str(ColorLevel::Half),
props.context.color.info.to_rgba_str(ColorLevel::Half),
props.context.color.primary.to_rgba_str(ColorLevel::Less),
props.context.color.secondary.to_rgba_str(ColorLevel::Less),
props.context.color.success.to_rgba_str(ColorLevel::Less),
props.context.color.warning.to_rgba_str(ColorLevel::Less),
props.context.color.error.to_rgba_str(ColorLevel::Less),
props.context.color.info.to_rgba_str(ColorLevel::Less)
)}
</style>

{props.children.clone() }
</ContextProvider<Theme>>
}
}
Loading

0 comments on commit c50c05b

Please sign in to comment.