Skip to content

Commit

Permalink
Add hover events to cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Follpvosten committed Jan 27, 2020
1 parent 32b24a7 commit 1c849b5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
32 changes: 22 additions & 10 deletions src/components/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ pub struct Props {
pub outlined: bool,
pub classes: String,
pub oncontextclick: Option<Callback<ContextMenuEvent>>,
pub onhoverenter: Option<Callback<MouseEnterEvent>>,
pub onhoverleave: Option<Callback<MouseLeaveEvent>>,
}

pub enum Msg {
RightClick(ContextMenuEvent),
HoverEnter(MouseEnterEvent),
HoverLeave(MouseLeaveEvent),
}

impl Component for Card {
Expand All @@ -39,15 +43,8 @@ impl Component for Card {
}

fn change(&mut self, props: Props) -> ShouldRender {
if self.props.id != props.id
|| self.props.outlined != props.outlined
|| self.props.classes != props.classes
{
self.props = props;
true
} else {
false
}
self.props = props;
true
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
Expand All @@ -57,6 +54,16 @@ impl Component for Card {
callback.emit(event);
}
}
Msg::HoverEnter(event) => {
if let Some(callback) = &self.props.onhoverenter {
callback.emit(event);
}
}
Msg::HoverLeave(event) => {
if let Some(callback) = &self.props.onhoverleave {
callback.emit(event);
}
}
}
false
}
Expand All @@ -69,8 +76,13 @@ impl Component for Card {
};
let classes = format!("mdc-card {} {}", self.props.classes, outlined);
let emit_contextclick = self.link.callback(Msg::RightClick);
let emit_hoverenter = self.link.callback(Msg::HoverEnter);
let emit_hoverleave = self.link.callback(Msg::HoverLeave);
html! {
<div class=classes id=self.id oncontextmenu=emit_contextclick>
<div class=classes id=self.id
oncontextmenu=emit_contextclick
onmouseenter=emit_hoverenter
onmouseleave=emit_hoverleave>
{ self.props.children.render() }
</div>
}
Expand Down
11 changes: 2 additions & 9 deletions src/components/card/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,8 @@ impl Component for Media {
}

fn change(&mut self, props: Props) -> ShouldRender {
if self.props.id != props.id
|| self.props.style != props.style
|| self.props.classes != props.classes
{
self.props = props;
true
} else {
false
}
self.props = props;
true
}

fn update(&mut self, _msg: Self::Message) -> ShouldRender {
Expand Down
8 changes: 2 additions & 6 deletions src/components/card/primary_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ impl Component for PrimaryAction {
}

fn change(&mut self, props: Props) -> ShouldRender {
if self.props.id != props.id {
self.props = props;
true
} else {
false
}
self.props = props;
true
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
Expand Down

0 comments on commit 1c849b5

Please sign in to comment.