encre_css/plugins/layout/placement/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias("layout", "inset"))]
3use crate::prelude::build_plugin::*;
4
5use std::borrow::Cow;
6
7fn placement_can_handle(context: &ContextCanHandle) -> bool {
8    match context.modifier {
9        Modifier::Builtin { value, .. } => {
10            spacing::is_matching_builtin_spacing(value) || *value == "auto" || *value == "full"
11        }
12        Modifier::Arbitrary { value, prefix, .. } => {
13            prefix.is_empty()
14                && (is_matching_length(value) || is_matching_percentage(value) || *value == "auto")
15        }
16    }
17}
18
19fn placement_handle(css_properties: &[&str], context: &mut ContextHandle) {
20    match context.modifier {
21        Modifier::Builtin { is_negative, value } => {
22            for css_prop in css_properties {
23                context.buffer.line(format_args!(
24                    "{}: {};",
25                    css_prop,
26                    if *value == "auto" {
27                        Cow::from("auto")
28                    } else if *value == "full" && *is_negative {
29                        Cow::from("-100%")
30                    } else if *value == "full" {
31                        Cow::from("100%")
32                    } else {
33                        spacing::get(value, *is_negative).unwrap()
34                    },
35                ));
36            }
37        }
38        Modifier::Arbitrary { value, .. } => {
39            for css_prop in css_properties {
40                context.buffer.line(format_args!("{css_prop}: {value};"));
41            }
42        }
43    }
44}
45
46#[derive(Debug)]
47pub(crate) struct PluginInsetDefinition;
48
49impl Plugin for PluginInsetDefinition {
50    fn can_handle(&self, context: ContextCanHandle) -> bool {
51        placement_can_handle(&context)
52    }
53
54    fn handle(&self, context: &mut ContextHandle) {
55        placement_handle(&["inset"], context);
56    }
57}
58
59#[derive(Debug)]
60pub(crate) struct PluginInsetXDefinition;
61
62impl Plugin for PluginInsetXDefinition {
63    fn can_handle(&self, context: ContextCanHandle) -> bool {
64        placement_can_handle(&context)
65    }
66
67    fn handle(&self, context: &mut ContextHandle) {
68        placement_handle(&["inset-inline"], context);
69    }
70}
71
72#[derive(Debug)]
73pub(crate) struct PluginInsetYDefinition;
74
75impl Plugin for PluginInsetYDefinition {
76    fn can_handle(&self, context: ContextCanHandle) -> bool {
77        placement_can_handle(&context)
78    }
79
80    fn handle(&self, context: &mut ContextHandle) {
81        placement_handle(&["inset-block"], context);
82    }
83}
84
85#[derive(Debug)]
86pub(crate) struct PluginStartDefinition;
87
88impl Plugin for PluginStartDefinition {
89    fn can_handle(&self, context: ContextCanHandle) -> bool {
90        placement_can_handle(&context)
91    }
92
93    fn handle(&self, context: &mut ContextHandle) {
94        placement_handle(&["inset-inline-start"], context);
95    }
96}
97
98#[derive(Debug)]
99pub(crate) struct PluginEndDefinition;
100
101impl Plugin for PluginEndDefinition {
102    fn can_handle(&self, context: ContextCanHandle) -> bool {
103        placement_can_handle(&context)
104    }
105
106    fn handle(&self, context: &mut ContextHandle) {
107        placement_handle(&["inset-inline-end"], context);
108    }
109}
110
111#[derive(Debug)]
112pub(crate) struct PluginTopDefinition;
113
114impl Plugin for PluginTopDefinition {
115    fn can_handle(&self, context: ContextCanHandle) -> bool {
116        placement_can_handle(&context)
117    }
118
119    fn handle(&self, context: &mut ContextHandle) {
120        placement_handle(&["top"], context);
121    }
122}
123
124#[derive(Debug)]
125pub(crate) struct PluginBottomDefinition;
126
127impl Plugin for PluginBottomDefinition {
128    fn can_handle(&self, context: ContextCanHandle) -> bool {
129        placement_can_handle(&context)
130    }
131
132    fn handle(&self, context: &mut ContextHandle) {
133        placement_handle(&["bottom"], context);
134    }
135}
136
137#[derive(Debug)]
138pub(crate) struct PluginLeftDefinition;
139
140impl Plugin for PluginLeftDefinition {
141    fn can_handle(&self, context: ContextCanHandle) -> bool {
142        placement_can_handle(&context)
143    }
144
145    fn handle(&self, context: &mut ContextHandle) {
146        placement_handle(&["left"], context);
147    }
148}
149
150#[derive(Debug)]
151pub(crate) struct PluginRightDefinition;
152
153impl Plugin for PluginRightDefinition {
154    fn can_handle(&self, context: ContextCanHandle) -> bool {
155        placement_can_handle(&context)
156    }
157
158    fn handle(&self, context: &mut ContextHandle) {
159        placement_handle(&["right"], context);
160    }
161}