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