Skip to content

Commit

Permalink
feat: add flip and angle fields to Label (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamilsan authored Jun 12, 2024
1 parent 33582bb commit 9ffa9e4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub fn HelloWorld() -> impl IntoView {
view! {
<SlideShow>
<Slide background_color=Color::MistyRose>
<Label>"Hello →"</Label>
<Label angle=10>"Hello →"</Label>
</Slide>
<Slide background_color=Color::PaleGreen>
<Label>"← World!"</Label>
<Label angle=-10>"← World!"</Label>
</Slide>
<Counter/>
</SlideShow>
Expand Down
2 changes: 2 additions & 0 deletions src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

mod align;
mod color;
mod flip;

pub use align::{Align, VAlign};
pub use color::Color;
pub use flip::Flip;
11 changes: 11 additions & 0 deletions src/properties/flip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Flip is an enum that represents the flip state of an object.
#[derive(Clone, Default, PartialEq)]
pub enum Flip {
/// No flip.
#[default]
None,
/// Horizontal flip.
Horizontal,
/// Vertical flip.
Vertical,
}
16 changes: 13 additions & 3 deletions src/widgets/label.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use leptos::*;

use crate::{use_frame, Align, Color, Size, VAlign};
use crate::{use_frame, Align, Color, Flip, Size, VAlign};

#[component]
pub fn Label(
Expand All @@ -13,6 +13,8 @@ pub fn Label(
#[prop(default = Color::Transparent.into(), into)] background_color: MaybeSignal<Color>,
#[prop(default = true.into(), into)] visible: MaybeSignal<bool>,
#[prop(default = "all .3s".to_string(), into)] transition: String,
#[prop(optional)] angle: i32,
#[prop(optional)] flip: Flip,
children: Children,
) -> impl IntoView {
let f = use_frame();
Expand All @@ -28,6 +30,13 @@ pub fn Label(
VAlign::Bottom => (f.y + f.height, "text-top"),
};

let mut transform = format!("rotate({angle} {x} {y})");
if flip == Flip::Horizontal {
transform.push_str(" scale(-1 1)");
} else if flip == Flip::Vertical {
transform.push_str(" scale(1 -1)");
}

view! {
<g
style:opacity=move || if visible.get() { 1 } else { 0 }
Expand All @@ -46,8 +55,9 @@ pub fn Label(
<text
class:has-text-weight-bold=bold
style:font-family=font
x=x
y=y
x=if flip == Flip::Horizontal { -x } else { x }
y=if flip == Flip::Vertical { -y } else { y }
transform=transform
font-size=move || font_size.into_pixels(f.height)
text-anchor=anchor
fill=color
Expand Down

0 comments on commit 9ffa9e4

Please sign in to comment.