-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): provide a
Button
component
- Loading branch information
Showing
16 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::collections::HashSet; | ||
|
||
use leptos::{component, create_memo, view, Children, IntoView, MaybeProp, SignalGet, TextProp}; | ||
|
||
use super::helper::maybe_children::MaybeChildren; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub enum ButtonVariant { | ||
Text, | ||
#[default] | ||
Contained, | ||
Outlined, | ||
} | ||
|
||
#[component] | ||
pub fn Button( | ||
#[prop(optional, into)] id: TextProp, | ||
#[prop(optional, into)] disabled: MaybeProp<bool>, | ||
#[prop(optional, into)] variant: MaybeProp<ButtonVariant>, | ||
#[prop(optional)] children: Option<Children>, | ||
) -> impl IntoView { | ||
let class_names = create_memo(move |_| { | ||
let mut classes: HashSet<&str> = HashSet::new(); | ||
|
||
// Default Classes | ||
classes.insert("px-4"); | ||
classes.insert("py-2"); | ||
classes.insert("rounded-md"); | ||
classes.insert("cursor-pointer"); | ||
classes.insert("font-semibold"); | ||
classes.insert("text-sm"); | ||
|
||
if let Some(is_disabled) = disabled.get() { | ||
if is_disabled { | ||
classes.insert("opacity-70"); | ||
classes.insert("!cursor-not-allowed"); | ||
} | ||
} | ||
|
||
match variant.get().unwrap_or_default() { | ||
ButtonVariant::Text => { | ||
classes.insert("btn-text"); | ||
classes.insert("bg-transparent"); | ||
} | ||
ButtonVariant::Contained => { | ||
classes.insert("btn-contained"); | ||
classes.insert("bg-purple-500"); | ||
classes.insert("text-white"); | ||
} | ||
ButtonVariant::Outlined => { | ||
classes.insert("btn-outlined"); | ||
classes.insert("bg-transparent"); | ||
classes.insert("border-2"); | ||
classes.insert("border-purple-500"); | ||
} | ||
} | ||
|
||
classes.into_iter().collect::<Vec<&str>>().join(" ") | ||
}); | ||
|
||
view! { | ||
<button id={id} class={class_names} disabled=move || disabled.get()> | ||
<MaybeChildren value=children let:children> | ||
{children()} | ||
</MaybeChildren> | ||
</button> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use leptos::*; | ||
|
||
#[slot] | ||
pub struct Fallback { | ||
children: ChildrenFn, | ||
} | ||
|
||
#[component] | ||
pub fn MaybeChildren<T, CF, IV>( | ||
value: Option<T>, | ||
children: CF, | ||
#[prop(optional)] fallback: Option<Fallback>, | ||
) -> impl IntoView | ||
where | ||
CF: FnOnce(T) -> IV + 'static, | ||
IV: IntoView, | ||
{ | ||
if let Some(value) = value { | ||
children(value).into_view() | ||
} else if let Some(fallback) = fallback { | ||
(fallback.children)().into_view() | ||
} else { | ||
().into_view() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod maybe_children; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod helper; | ||
|
||
pub mod button; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod components; | ||
pub mod views; | ||
|
||
use leptos::{component, view, IntoView}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
use leptos::{component, view, IntoView}; | ||
|
||
use crate::components::button::{Button, ButtonVariant}; | ||
|
||
#[component] | ||
pub fn Home() -> impl IntoView { | ||
view! { | ||
<section class="flex flex-col justify-center items-center"> | ||
<img src="/assets/img/townhall.png" alt="TownHall AI Generated" height="500" width="500" /> | ||
<h1>{"AI Generated Picture of a TownHall"}</h1> | ||
<Button variant={ButtonVariant::Text}>{"Text"}</Button> | ||
<Button variant={ButtonVariant::Contained}>{"Contained"}</Button> | ||
<Button variant={ButtonVariant::Outlined}>{"Outlined"}</Button> | ||
</section> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
export default { | ||
content: ['./src/**/*.rs'], | ||
theme: { | ||
extend: {} | ||
}, | ||
plugins: [] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use leptos::{mount_to, view}; | ||
|
||
use wasm_bindgen::JsCast; | ||
use wasm_bindgen_test::*; | ||
|
||
use web::components::button::{Button, ButtonVariant}; | ||
|
||
wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
#[wasm_bindgen_test] | ||
fn default_text_button_class_names_integrity() { | ||
let document = leptos::document(); | ||
let test_wrapper = document.create_element("div").unwrap(); | ||
let _ = document.body().unwrap().append_child(&test_wrapper); | ||
|
||
mount_to( | ||
test_wrapper.clone().unchecked_into(), | ||
|| view! { <Button id="btn-text" variant={ButtonVariant::Text}>"Click Me!"</Button> }, | ||
); | ||
|
||
let button_el = test_wrapper | ||
.query_selector("#btn-text") | ||
.expect("Failed to access document") | ||
.expect("Button Element not found") | ||
.unchecked_into::<web_sys::HtmlButtonElement>(); | ||
|
||
let want_class_names = vec![ | ||
"px-4", | ||
"py-2", | ||
"rounded-md", | ||
"cursor-pointer", | ||
"font-semibold", | ||
"text-sm", | ||
"btn-text", | ||
"bg-transparent" | ||
]; | ||
|
||
let have_class_names = button_el.get_attribute("class").unwrap(); | ||
|
||
for class_name in want_class_names.iter() { | ||
assert!(have_class_names.contains(class_name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod button; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod components; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"moz:firefoxOptions": { | ||
"args": [ | ||
"-headless" | ||
], | ||
"log": { | ||
"level": "trace" | ||
} | ||
} | ||
} |