use blinc_core::Color;
use blinc_theme::{ColorToken, ThemeState};
use crate::div::{div, Div, ElementBuilder};
use crate::element::RenderProps;
use crate::tree::{LayoutNodeId, LayoutTree};
#[derive(Clone, Debug)]
pub struct BlockquoteConfig {
pub border_color: Color,
pub border_width: f32,
pub bg_color: Color,
pub padding: f32,
pub margin_y: f32,
}
impl Default for BlockquoteConfig {
fn default() -> Self {
let theme = ThemeState::get();
Self {
border_color: theme.color(ColorToken::Border),
border_width: 4.0,
bg_color: theme.color(ColorToken::SurfaceOverlay),
padding: 4.0,
margin_y: 2.0,
}
}
}
pub struct Blockquote {
inner: Div,
css_element_id: Option<String>,
css_classes: Vec<String>,
}
impl Blockquote {
pub fn new() -> Self {
Self::with_config(BlockquoteConfig::default())
}
pub fn with_config(config: BlockquoteConfig) -> Self {
let inner = div()
.flex_col()
.w_full()
.my(config.margin_y)
.bg(config.bg_color)
.border_left(config.border_width, config.border_color)
.p(config.padding);
Self {
inner,
css_element_id: None,
css_classes: Vec::new(),
}
}
pub fn child(mut self, child: impl ElementBuilder + 'static) -> Self {
self.inner = self.inner.child(child);
self
}
pub fn id(mut self, id: &str) -> Self {
self.css_element_id = Some(id.to_string());
self
}
pub fn class(mut self, name: &str) -> Self {
self.css_classes.push(name.to_string());
self
}
}
impl Default for Blockquote {
fn default() -> Self {
Self::new()
}
}
impl ElementBuilder for Blockquote {
fn build(&self, tree: &mut LayoutTree) -> LayoutNodeId {
self.inner.build(tree)
}
fn render_props(&self) -> RenderProps {
self.inner.render_props()
}
fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
self.inner.children_builders()
}
fn element_type_id(&self) -> crate::div::ElementTypeId {
crate::div::ElementTypeId::Div
}
fn semantic_type_name(&self) -> Option<&'static str> {
Some("blockquote")
}
fn element_id(&self) -> Option<&str> {
self.css_element_id.as_deref()
}
fn element_classes(&self) -> &[String] {
&self.css_classes
}
}
pub fn blockquote() -> Blockquote {
Blockquote::new()
}
pub fn blockquote_with_config(config: BlockquoteConfig) -> Blockquote {
Blockquote::with_config(config)
}
#[cfg(test)]
mod tests {
use super::*;
fn init_theme() {
let _ = ThemeState::try_get().unwrap_or_else(|| {
ThemeState::init_default();
ThemeState::get()
});
}
#[test]
fn test_blockquote_creates_container() {
init_theme();
let mut tree = LayoutTree::new();
let bq = blockquote();
bq.build(&mut tree);
assert!(!tree.is_empty());
}
#[test]
fn test_blockquote_with_child() {
init_theme();
let mut tree = LayoutTree::new();
let bq = blockquote().child(div());
bq.build(&mut tree);
assert!(tree.len() > 1);
}
}