Skip to content

Commit

Permalink
♻️ Move context.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Oct 28, 2024
1 parent 3ff2a28 commit 75cbcd3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 63 deletions.
17 changes: 5 additions & 12 deletions packages/components/src/form/button/button.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use stylist::yew::styled_component;
use yew::prelude::*;

use hikari_theme::types::{ColorType, SizeType};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BorderRadiusType {
#[default]
Default,
None,
OnlyLeft,
OnlyRight,
}
use hikari_theme::{
components::form::button::*,
types::{ColorType, SizeType},
};

#[derive(Properties, Debug, PartialEq)]
pub struct Props {
Expand All @@ -30,8 +24,7 @@ pub struct Props {

#[styled_component]
pub fn Button(props: &Props) -> Html {
let radius_type =
use_context::<super::button_group::group_injector_context::ContextProviderType>();
let radius_type = use_context::<ContextProviderType>();
let radius_type = match &radius_type {
Some(ctx) => ctx.border_radius_type,
None => BorderRadiusType::Default,
Expand Down
62 changes: 11 additions & 51 deletions packages/components/src/form/button/button_group.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,11 @@
use stylist::yew::styled_component;
use yew::{html::ChildrenRenderer, prelude::*, virtual_dom::VChild};

use hikari_theme::types::{ColorType, SizeType};

use super::button::{BorderRadiusType, Button};

pub(crate) mod group_injector_context {
use yew::prelude::*;

use super::BorderRadiusType;
use hikari_theme::types::{ColorType, SizeType};

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

#[derive(Properties, Debug, PartialEq)]
pub struct ContextProviderProps {
#[prop_or(SizeType::Medium)]
pub size: SizeType,
#[prop_or(ColorType::Primary)]
pub color: ColorType,
#[prop_or(false)]
pub outlined: bool,
#[prop_or_default]
pub border_radius_type: BorderRadiusType,

#[prop_or_default]
pub children: Children,
}

pub type ContextProviderType = UseStateHandle<Context>;

#[function_component]
pub fn ContextShell(props: &ContextProviderProps) -> Html {
let ctx = use_state(|| Context {
border_radius_type: props.border_radius_type,
});

html! {
<ContextProvider<ContextProviderType> context={ctx.clone()}>
{props.children.clone() }
</ContextProvider<ContextProviderType>>
}
}
}
use super::button::Button;
use hikari_theme::{
components::form::button::*,
types::{ColorType, SizeType},
};

#[derive(Clone, derive_more::From, PartialEq)]
pub enum ButtonGroupVariant {
Expand Down Expand Up @@ -91,7 +51,7 @@ pub fn ButtonGroup(props: &Props) -> Html {
display: flex;
flex-direction: row;
"#)}>
<group_injector_context::ContextShell
<ContextShell
border_radius_type={BorderRadiusType::OnlyLeft}
>
{
Expand All @@ -101,20 +61,20 @@ pub fn ButtonGroup(props: &Props) -> Html {
html! {}
}
}
</group_injector_context::ContextShell>
</ContextShell>

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

<group_injector_context::ContextShell
<ContextShell
border_radius_type={BorderRadiusType::OnlyRight}
>
{
Expand All @@ -124,7 +84,7 @@ pub fn ButtonGroup(props: &Props) -> Html {
html! {}
}
}
</group_injector_context::ContextShell>
</ContextShell>
</div>
},
}
Expand Down
47 changes: 47 additions & 0 deletions packages/theme/src/components/form/button.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use yew::prelude::*;

use crate::types::{ColorType, SizeType};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BorderRadiusType {
#[default]
Default,
None,
OnlyLeft,
OnlyRight,
}

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

#[derive(Properties, Debug, PartialEq)]
pub struct ContextProps {
#[prop_or(SizeType::Medium)]
pub size: SizeType,
#[prop_or(ColorType::Primary)]
pub color: ColorType,
#[prop_or(false)]
pub outlined: bool,
#[prop_or_default]
pub border_radius_type: BorderRadiusType,

#[prop_or_default]
pub children: Children,
}

pub type ContextProviderType = UseStateHandle<ContextState>;

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

html! {
<ContextProvider<ContextProviderType> context={ctx.clone()}>
{props.children.clone() }
</ContextProvider<ContextProviderType>>
}
}

0 comments on commit 75cbcd3

Please sign in to comment.