layoutcss_parser/components/
center.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use indoc::formatdoc;
use std::collections::HashSet;

const CENTER_STYLE: &str = r#"
center-l{
    box-sizing: content-box;
    max-inline-size: fit-content;
    margin-inline: auto;
    display: block;
    text-align: initial;
  }
"#;

const CENTER_AND_TEXT_STYLE: &str = r#"
  center-l[layout~="and-text"]{
    text-align:center;
  }
"#;

const CENTER_RECURSIVE_STYLE: &str = r#"
  center-l[layout~="recursive"]{
    display:flex;
    flex-direction:column;
    align-items:center;
  }
"#;

fn center_max_width_style(value: &str) -> String {
    formatdoc!(
        r#"
        center-l[layout~="max-width:{value}"]{{
            max-inline-size: {value};
            --center-max-width: {value};
        }}
        "#,
    )
}

pub fn center_css(
    max_width: Option<&str>,
    and_text: bool,
    recursive: bool,
    set: &mut HashSet<String>,
) {
    set.insert(CENTER_STYLE.to_string());
    if let Some(value) = max_width {
        set.insert(center_max_width_style(value));
    }
    if and_text {
        set.insert(CENTER_AND_TEXT_STYLE.to_string());
    }
    if recursive {
        set.insert(CENTER_RECURSIVE_STYLE.to_string());
    }
}