encre_css/plugins/effect/box_shadow/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias = "effect")]
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                ["2xs", "xs", "sm", "md", "lg", "xl", "2xl", "none"].contains(&&**value)
13            }
14            Modifier::Arbitrary { hint, value, .. } => {
15                *hint == "shadow" || (hint.is_empty() && is_matching_shadow(value))
16            }
17        }
18    }
19
20    fn handle(&self, context: &mut ContextHandle) {
21        match context.modifier {
22            Modifier::Builtin { value, .. } => match *value {
23                "2xs" => {
24                    context.buffer.lines([
25                        "--en-shadow: 0 1px var(--en-shadow-color, rgb(0 0 0 / 0.05));",
26                    ]);
27                }
28                "xs" => {
29                    context.buffer.lines([
30                        "--en-shadow: 0 1px 2px 0 var(--en-shadow-color, rgb(0 0 0 / 0.05));",
31                    ]);
32                }
33                "sm" => {
34                    context.buffer.lines([
35                        "--en-shadow: 0 1px 3px 0 var(--en-shadow-color, rgb(0 0 0 / 0.1)), 0 1px 2px -1px var(--en-shadow-color, rgb(0 0 0 / 0.1));",
36                    ]);
37                }
38                "md" => {
39                    context.buffer.lines([
40                        "--en-shadow: 0 4px 6px -1px var(--en-shadow-color, rgb(0 0 0 / 0.1)), 0 2px 4px -2px var(--en-shadow-color, rgb(0 0 0 / 0.1));",
41                    ]);
42                }
43                "lg" => {
44                    context.buffer.lines([
45                        "--en-shadow: 0 10px 15px -3px var(--en-shadow-color, rgb(0 0 0 / 0.1)), 0 4px 6px -4px var(--en-shadow-color, rgb(0 0 0 / 0.1));",
46                    ]);
47                }
48                "xl" => {
49                    context.buffer.lines([
50                        "--en-shadow: 0 20px 25px -5px var(--en-shadow-color, rgb(0 0 0 / 0.1)), 0 8px 10px -6px var(--en-shadow-color, rgb(0 0 0 / 0.1));",
51                    ]);
52                }
53                "2xl" => {
54                    context.buffer.lines([
55                        "--en-shadow: 0 25px 50px -12px var(--en-shadow-color, rgb(0 0 0 / 0.25));",
56                    ]);
57                }
58                "none" => context.buffer.line("--en-shadow: 0 0 #0000;"),
59                _ => unreachable!(),
60            },
61            Modifier::Arbitrary { value, .. } => {
62                let mut shadow = shadow::ShadowList::parse(value).unwrap();
63                shadow.replace_all_colors("var(--en-shadow-color, {})");
64                context
65                    .buffer
66                    .line(format_args!("--en-shadow: {shadow};"));
67            }
68        }
69
70        context.buffer.line("box-shadow: var(--en-inset-shadow, 0 0 #0000), var(--en-inset-ring-shadow, 0 0 #0000), var(--en-ring-offset-shadow, 0 0 #0000), var(--en-ring-shadow, 0 0 #0000), var(--en-shadow);");
71    }
72}
73
74#[derive(Debug)]
75pub(crate) struct PluginInsetDefinition;
76
77impl Plugin for PluginInsetDefinition {
78    fn can_handle(&self, context: ContextCanHandle) -> bool {
79        match context.modifier {
80            Modifier::Builtin { value, .. } => {
81                ["2xs", "xs", "sm"].contains(&&**value)
82            }
83            Modifier::Arbitrary { hint, value, .. } => {
84                *hint == "shadow" || (hint.is_empty() && is_matching_shadow(value))
85            }
86        }
87    }
88
89    fn handle(&self, context: &mut ContextHandle) {
90        match context.modifier {
91            Modifier::Builtin { value, .. } => match *value {
92                "2xs" => {
93                    context.buffer.lines([
94                        "--en-inset-shadow: inset 0 1px var(--en-inset-shadow-color, rgb(0 0 0 / 0.05));",
95                    ]);
96                }
97                "xs" => {
98                    context.buffer.lines([
99                        "--en-inset-shadow: inset 0 1px 1px var(--en-inset-shadow-color, rgb(0 0 0 / 0.05));",
100                    ]);
101                }
102                "sm" => {
103                    context.buffer.lines([
104                        "--en-inset-shadow: 0 2px 4px var(--en-inset-shadow-color, rgb(0 0 0 / 0.05));",
105                    ]);
106                }
107                "none" => context.buffer.line("--en-inset-shadow: inset 0 0 #0000;"),
108                _ => unreachable!(),
109            },
110            Modifier::Arbitrary { value, .. } => {
111                let mut shadow = shadow::ShadowList::parse(value).unwrap();
112                shadow.replace_all_colors("var(--en-inset-shadow-color, {})");
113                context
114                    .buffer
115                    .line(format_args!("--en-inset-shadow: {shadow};"));
116            }
117        }
118
119        context.buffer.line("box-shadow: var(--en-inset-shadow), var(--en-inset-ring-shadow, 0 0 #0000), var(--en-ring-offset-shadow, 0 0 #0000), var(--en-ring-shadow, 0 0 #0000), var(--en-shadow, 0 0 #0000);");
120    }
121}