hyaenidae/toolkit/src/button.rs

111 lines
2.4 KiB
Rust
Raw Normal View History

2020-12-16 02:40:41 +00:00
#[derive(Clone, Copy, Debug)]
pub enum ButtonKind {
Primary,
PrimaryOutline,
Outline,
Secondary,
}
#[derive(Clone, Copy, Debug)]
pub enum LinkKind {
CurrentTab,
NewTab,
}
#[derive(Clone, Debug, Default)]
2020-12-16 02:40:41 +00:00
pub struct Button {
pub(crate) label: String,
pub(crate) kind: ButtonKind,
pub(crate) classes: Vec<String>,
pub(crate) href: Option<String>,
pub(crate) link_kind: LinkKind,
pub(crate) action: Option<String>,
2020-12-16 02:40:41 +00:00
}
impl Button {
pub fn primary(label: &str) -> Self {
Button {
label: label.to_owned(),
..Default::default()
2020-12-16 02:40:41 +00:00
}
}
pub fn primary_outline(label: &str) -> Self {
Button {
label: label.to_owned(),
kind: ButtonKind::PrimaryOutline,
..Default::default()
2020-12-16 02:40:41 +00:00
}
}
pub fn outline(label: &str) -> Self {
Button {
label: label.to_owned(),
kind: ButtonKind::Outline,
..Default::default()
2020-12-16 02:40:41 +00:00
}
}
pub fn secondary(label: &str) -> Self {
Button {
label: label.to_owned(),
kind: ButtonKind::Secondary,
..Default::default()
2020-12-16 02:40:41 +00:00
}
}
pub fn kind(mut self, kind: ButtonKind) -> Self {
self.kind = kind;
self
}
pub fn class(mut self, class: &str) -> Self {
self.classes.push(class.to_owned());
2020-12-16 02:40:41 +00:00
self
}
pub fn href(mut self, href: &str) -> Self {
self.href = Some(href.to_owned());
2020-12-16 02:40:41 +00:00
self
}
pub fn new_tab(mut self) -> Self {
self.link_kind = LinkKind::NewTab;
2020-12-16 02:40:41 +00:00
self
}
pub fn form(mut self, action: &str) -> Self {
self.action = Some(action.to_owned());
2020-12-16 02:40:41 +00:00
self
}
pub(crate) fn class_string(&self) -> String {
use ButtonKind::*;
let mut classes = self.classes.clone();
2021-01-09 04:32:02 +00:00
let static_classes = match self.kind {
2020-12-16 02:40:41 +00:00
Primary => "toolkit-button toolkit-button__primary",
PrimaryOutline => "toolkit-button toolkit-button__primary-outline",
Outline => "toolkit-button toolkit-button__outline",
Secondary => "toolkit-button toolkit-button__secondary",
};
2021-01-09 04:32:02 +00:00
classes.push(static_classes.to_owned());
classes.join(" ")
2020-12-16 02:40:41 +00:00
}
}
impl Default for ButtonKind {
fn default() -> Self {
ButtonKind::Primary
}
}
impl Default for LinkKind {
fn default() -> Self {
LinkKind::CurrentTab
}
}