tailwind_css/systems/preflight/
mod.rs

1use std::fmt::{Display, Formatter};
2
3/// <https://tailwindcss.com/docs/preflight>
4#[derive(Clone, Debug)]
5pub struct PreflightSystem {
6    /// disable all preflight
7    pub disable: bool,
8    /// ## Default margins are removed
9    /// Preflight removes all of the default margins from elements like headings, blockquotes, paragraphs, etc.
10    /// This makes it harder to accidentally rely on margin values applied by the user-agent stylesheet that are not part of your spacing scale.
11    pub remove_margins: bool,
12    /// ## Headings are unstyled
13    /// All heading elements are completely unstyled by default, and have the same font-size and font-weight as normal text.
14    pub unstyle_head: bool,
15    /// ## Lists are unstyled
16    /// Ordered and unordered lists are unstyled by default, with no bullets/numbers and no margin or padding.
17    pub unstyle_list: bool,
18    /// ## Images are block-level
19    /// Images and other replaced elements (like svg, video, canvas, and others) are display: block by default.
20    pub block_level_image: bool,
21    /// ## Border styles are reset globally
22    /// In order to make it easy to add a border by simply adding the border class, Tailwind overrides the default border styles for all elements with the following rules:
23    pub unstyle_border: bool,
24    /// ## Buttons have a default outline
25    /// To ensure that we provide accessible styles out of the box, we made sure that buttons have a default outline. You can of course override this by applying focus:ring or similar utilities to your buttons.
26    pub button_outline: bool,
27    /// Custom field for preflight
28    pub custom: String,
29}
30
31impl Default for PreflightSystem {
32    fn default() -> Self {
33        Self {
34            disable: false,
35            remove_margins: true,
36            unstyle_head: true,
37            unstyle_list: true,
38            block_level_image: true,
39            unstyle_border: true,
40            button_outline: true,
41            custom: String::new(),
42        }
43    }
44}
45
46impl PreflightSystem {
47    const REMOVE_MARGINS: &'static str = r#"
48p, blockquote, hr, dl, dd, h1, h2, h3, h4, h5, h6, figure, pre {
49    margin: 0;
50}
51"#;
52    const RESET_HEAD: &'static str = r#"
53h1, h2, h3, h4, h5, h6 {
54    font-size: inherit;
55    font-weight: inherit;
56}
57"#;
58    const RESET_LIST: &'static str = r#"
59ol, ul {
60    list-style: none;
61    margin: 0;
62    padding: 0;
63}
64"#;
65    const IMAGE_BLOCK: &'static str = r#"
66img, svg, video, canvas, audio, iframe, embed, object {
67  display: block;
68  vertical-align: middle;
69}
70"#;
71    // TODO: read theme here
72    const RESET_BORDER: &'static str = r#"
73*, ::before, ::after {
74  border-width: 0;
75  border-style: solid;
76  border-color: theme('borderColor.DEFAULT', currentColor);
77}
78"#;
79    const BUTTON_OUTLINE: &'static str = r#"
80button:focus {
81  outline: 1px dotted;
82  outline: 5px auto -webkit-focus-ring-color;
83}
84"#;
85}
86
87impl Display for PreflightSystem {
88    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
89        f.write_str(&self.custom)?;
90        // if self.disable { return Ok(()); }
91        if self.remove_margins {
92            f.write_str(Self::REMOVE_MARGINS.trim())?;
93            writeln!(f)?;
94        }
95        if self.unstyle_head {
96            f.write_str(Self::RESET_HEAD.trim())?;
97            writeln!(f)?;
98        }
99        if self.unstyle_list {
100            f.write_str(Self::RESET_LIST.trim())?;
101            writeln!(f)?;
102        }
103        if self.block_level_image {
104            f.write_str(Self::IMAGE_BLOCK.trim())?;
105            writeln!(f)?;
106        }
107        if self.unstyle_border {
108            f.write_str(Self::RESET_BORDER.trim())?;
109            writeln!(f)?;
110        }
111        if self.button_outline {
112            f.write_str(Self::BUTTON_OUTLINE.trim())?;
113            writeln!(f)?;
114        }
115        Ok(())
116    }
117}