Skip to content

Commit

Permalink
♻️ Use components' enum to support custom router.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 6, 2024
1 parent 6e462dd commit 0147230
Show file tree
Hide file tree
Showing 24 changed files with 117 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ uuid = { version = "^1", features = [
'macro-diagnostics',
'serde',
] }
yuuka = "^0.3"
yuuka = "^0.4"

log = "^0.4"
env_logger = "^0.11"
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod block;
pub mod media;
pub mod paragraph;
pub mod typography;

pub use block::*;
pub use media::*;
pub use paragraph::*;
pub use typography::*;
8 changes: 2 additions & 6 deletions packages/components/src/data/paragraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ mod affix;
mod badge;
mod chip;

mod rich;
mod typography;

mod avatar;
mod icon;
mod rich;

pub use affix::Affix;
pub use badge::Badge;
pub use chip::Chip;

pub use rich::Rich;
pub use typography::*;

pub use avatar::Avatar;
pub use icon::Icon;
pub use rich::Rich;
6 changes: 4 additions & 2 deletions packages/components/src/navigation/aside/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
pub mod bottom_nav;
pub mod breadcrumb;
pub mod nav;
pub mod top_nav;

pub mod pagination;
pub mod steps;
pub mod tabs;

pub use bottom_nav::BottomNav;
pub use breadcrumb::Breadcrumb;
pub use nav::{BottomNav, TopNav};
pub use top_nav::TopNav;

pub use pagination::Pagination;
pub use steps::Steps;
Expand Down
5 changes: 0 additions & 5 deletions packages/components/src/navigation/aside/nav/mod.rs

This file was deleted.

128 changes: 102 additions & 26 deletions packages/theme/src/types/component.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
use anyhow::{anyhow, Result};
use once_cell::sync::Lazy;
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter, EnumString, IntoEnumIterator};
use yuuka::derive_enum;

derive_enum!(
#[derive(PartialEq, Serialize, Deserialize)]
#[derive(PartialEq, Copy, Serialize, Deserialize, Display, EnumIter)]
#[macros_recursive(strum(serialize_all = "snake_case"))]
pub enum ComponentType {
#[derive(EnumString)]
#[strum(prefix = "container/")]
Container(enum Container {
#[derive]
#[strum(prefix = "container/layout/")]
Layout(enum Layout {
Aside,
Container,
Main,
Header,
Footer,
}),
#[derive]
#[strum(prefix = "container/place/")]
Place(enum Place {
Column,
Row,
Expand All @@ -20,6 +32,8 @@ derive_enum!(
Divider,
Skeleton,
}),
#[derive]
#[strum(prefix = "container/system/")]
System(enum System {
Flex,
Anchor,
Expand All @@ -31,7 +45,11 @@ derive_enum!(
Breakpoint,
})
}),
#[derive(EnumString)]
#[strum(prefix = "data/")]
Data(enum Data {
#[derive]
#[strum(prefix = "data/display/")]
Block(enum Block {
List,
Progress,
Expand All @@ -42,59 +60,74 @@ derive_enum!(
Collapse,
Masonry,
}),
#[derive]
#[strum(prefix = "data/media/")]
Media(enum Media {
Image,
Charts,
Scene,
}),
#[derive]
#[strum(prefix = "data/paragraph/")]
Paragraph(enum Paragraph {
Typography(enum Typography {
Sub,
Sup,
Bold,
Code,
Sample,
Delete,
IsolateDirection,
OverrideDirection,
Italic,
Mark,
BlockQuote,
Quote,
Ruby,
Size,
Sizing,
Divider,
Split,
Underline,
}),
Rich,
Affix,
Avatar,
Badge,
Chip,
Icon,
})
}),
#[derive]
#[strum(prefix = "data/typography/")]
Typography(enum Typography {
Sub,
Sup,
Bold,
Code,
Sample,
Delete,
IsolateDirection,
OverrideDirection,
Italic,
Mark,
BlockQuote,
Quote,
Ruby,
Sizing,
Divider,
Split,
Underline,
}),
}),
#[derive(EnumString)]
#[strum(prefix = "form/")]
Form(enum Form {
#[derive]
#[strum(prefix = "form/button/")]
Button(enum Button {
Button,
IconButton,
ButtonGroup,
Tag,
}),
#[derive]
#[strum(prefix = "form/input/")]
Input(enum Input {
Text,
TextArea,
Rich,
}),
#[derive]
#[strum(prefix = "form/picker/")]
Picker(enum Picker {
Color,
File,
Date,
Time,
DateTime,
}),
#[derive]
#[strum(prefix = "form/selector/")]
Selector(enum Selector {
MultiSelect,
CheckBox,
Expand All @@ -106,17 +139,21 @@ derive_enum!(
Rating,
}),
}),
#[derive(EnumString)]
#[strum(prefix = "navigation/")]
Navigation(enum Navigation {
#[derive]
#[strum(prefix = "navigation/aside/")]
Aside(enum Aside {
Nav(enum Nav {
TopNav,
BottomNav,
}),
TopNav,
BottomNav,
Breadcrumb,
Pagination,
Steps,
Tabs,
}),
#[derive]
#[strum(prefix = "navigation/modal/")]
Modal(enum Modal {
Drawer,
Menu,
Expand All @@ -129,3 +166,42 @@ derive_enum!(
})
}
);

macro_rules! iter_component_type {
($ty: ident, $parent: ident) => {
$ty::iter()
.map(|x| (ComponentType::$parent($parent::$ty(x)), x.to_string()))
.collect::<Vec<_>>()
};
}

static MAPPER: Lazy<Vec<(ComponentType, String)>> = Lazy::new(|| {
[
iter_component_type!(Layout, Container),
iter_component_type!(Place, Container),
iter_component_type!(System, Container),
iter_component_type!(Block, Data),
iter_component_type!(Media, Data),
iter_component_type!(Paragraph, Data),
iter_component_type!(Typography, Data),
iter_component_type!(Button, Form),
iter_component_type!(Input, Form),
iter_component_type!(Picker, Form),
iter_component_type!(Selector, Form),
iter_component_type!(Aside, Navigation),
iter_component_type!(Modal, Navigation),
]
.concat()
});

impl FromStr for ComponentType {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some((ty, _)) = MAPPER.iter().find(|(_, x)| x == &s) {
Ok(*ty)
} else {
Err(anyhow!("Invalid component type"))
}
}
}
2 changes: 1 addition & 1 deletion website/examples/dev/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub async fn route() -> Result<Router> {
get(move |Path(id): Path<String>| async move { html(format!("/guide/{}", id)).await }),
)
.route(
"/component/:id",
"/component/*id",
get(move |Path(id): Path<String>| async move { html(format!("/component/{}", id)).await }),
);

Expand Down
4 changes: 2 additions & 2 deletions website/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub enum Routes {
#[at("/guide/:id")]
#[component(Guide)]
Guide { id: String },
#[at("/component/:id")]
#[at("/component/*id")]
#[component(Component)]
Component { id: String },
Component { id: ComponentType },

#[not_found]
#[at("/404")]
Expand Down
6 changes: 3 additions & 3 deletions website/src/pages/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ use stylist::yew::styled_component;
use yew::prelude::*;

use hikari_components::form::Button;
use hikari_theme::types::ColorType;
use hikari_theme::types::{ColorType, ComponentType};

#[derive(Properties, Debug, PartialEq)]
pub struct Props {
#[prop_or_default]
pub id: String,
pub id: ComponentType,
}

#[styled_component]
pub fn Component(props: &Props) -> Html {
html! {
<>
<h1>{"Component"}</h1>
<p>{format!("Component {}", props.id)}</p>
<p>{format!("Component {:?}", props.id)}</p>

<Button
color={ColorType::Primary}
Expand Down

0 comments on commit 0147230

Please sign in to comment.