encre_css/plugins/background/background_position/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias("background", "bg"))]
3use crate::prelude::build_plugin::*;
4
5#[derive(Debug)]
6pub(crate) struct PluginDefinition;
7
8impl Plugin for PluginDefinition {
9    fn can_handle(&self, context: ContextCanHandle) -> bool {
10        match context.modifier {
11            Modifier::Builtin { value, .. } => [
12                "bottom",
13                "center",
14                "left",
15                "left-bottom",
16                "left-top",
17                "right",
18                "right-bottom",
19                "right-top",
20                "top",
21            ]
22            .contains(value),
23            Modifier::Arbitrary { hint, value, .. } => {
24                // TailwindCSS uses a dedicated `preferOnConflict` variable for choosing this
25                // plugin instead of the `background_size` plugin (https://github.com/tailwindlabs/tailwindcss/blob/master/src/corePlugins.js#L1816)
26                // We can't and won't reproduce this behavior, so the `bg-size` will be chosen when
27                // the `position` hint is not specified
28                *hint == "position"
29                    || (hint.is_empty() && value.split(',').all(is_matching_position))
30            }
31        }
32    }
33
34    fn handle(&self, context: &mut ContextHandle) {
35        match context.modifier {
36            Modifier::Builtin { value, .. } => context.buffer.line(format_args!(
37                "background-position: {};",
38                value.replace('-', " ")
39            )),
40            Modifier::Arbitrary { value, .. } => {
41                context
42                    .buffer
43                    .line(format_args!("background-position: {value};"));
44            }
45        }
46    }
47}