hyaenidae/toolkit/src/button.rs
2021-01-21 23:42:19 -06:00

121 lines
2.7 KiB
Rust

#[derive(Clone, Copy, Debug)]
pub enum ButtonKind {
Primary,
PrimaryOutline,
Outline,
Secondary,
}
#[derive(Clone, Copy, Debug)]
pub enum LinkKind {
CurrentTab,
NewTab,
}
#[derive(Clone, Debug, Default)]
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>,
dark: bool,
}
impl Button {
pub fn primary(label: &str) -> Self {
Button {
label: label.to_owned(),
..Default::default()
}
}
pub fn primary_outline(label: &str) -> Self {
Button {
label: label.to_owned(),
kind: ButtonKind::PrimaryOutline,
..Default::default()
}
}
pub fn outline(label: &str) -> Self {
Button {
label: label.to_owned(),
kind: ButtonKind::Outline,
..Default::default()
}
}
pub fn secondary(label: &str) -> Self {
Button {
label: label.to_owned(),
kind: ButtonKind::Secondary,
..Default::default()
}
}
pub fn kind(mut self, kind: ButtonKind) -> Self {
self.kind = kind;
self
}
pub fn classes(mut self, classes: &[&str]) -> Self {
self.classes = classes.into_iter().map(|s| s.to_string()).collect();
self
}
pub fn href(mut self, href: &str) -> Self {
self.href = Some(href.to_owned());
self
}
pub fn new_tab(mut self) -> Self {
self.link_kind = LinkKind::NewTab;
self
}
pub fn form(mut self, action: &str) -> Self {
self.action = Some(action.to_owned());
self
}
pub fn dark(mut self, dark: bool) -> Self {
self.dark = dark;
self
}
pub(crate) fn class_string(&self) -> String {
use ButtonKind::*;
let mut classes = self.classes.clone();
let static_classes = match self.kind {
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",
};
classes.push(static_classes.to_owned());
if self.dark {
classes.push("toolkit-dark".to_owned());
}
classes.join(" ")
}
}
impl Default for ButtonKind {
fn default() -> Self {
ButtonKind::Primary
}
}
impl Default for LinkKind {
fn default() -> Self {
LinkKind::CurrentTab
}
}