Coverage for fastblocks / adapters / style / vanilla.py: 84%
57 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-02 20:00 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-02 20:00 -0800
1"""Vanilla CSS adapter implementation for custom stylesheets."""
3from contextlib import suppress
4from typing import Any
5from uuid import UUID
7from oneiric.core.resolution import Resolver
9from ._base import StyleBase, StyleBaseSettings
11# Oneiric resolver for dependency injection
12depends = Resolver()
15class VanillaStyleSettings(StyleBaseSettings):
16 """Vanilla CSS-specific settings."""
18 css_paths: list[str] = ["/static/css/base.css"]
19 custom_properties: dict[str, str] = {}
20 css_variables: dict[str, str] = {}
23class VanillaStyle(StyleBase):
24 """Vanilla CSS adapter for custom stylesheets."""
26 # Required ACB 0.19.0+ metadata
27 MODULE_ID: UUID = UUID("01937d86-4f2a-7b3c-8d9e-f3b4d3c2c1a2") # Static UUID7
28 MODULE_STATUS = "stable"
30 # Default component class mappings for semantic naming
31 COMPONENT_CLASSES = {
32 "button": "btn",
33 "button_primary": "btn btn--primary",
34 "button_secondary": "btn btn--secondary",
35 "button_success": "btn btn--success",
36 "button_danger": "btn btn--danger",
37 "button_warning": "btn btn--warning",
38 "button_info": "btn btn--info",
39 "button_small": "btn btn--small",
40 "button_medium": "btn btn--medium",
41 "button_large": "btn btn--large",
42 "input": "form__input",
43 "textarea": "form__textarea",
44 "select": "form__select",
45 "checkbox": "form__checkbox",
46 "radio": "form__radio",
47 "field": "form__field",
48 "label": "form__label",
49 "control": "form__control",
50 "card": "card",
51 "card_header": "card__header",
52 "card_content": "card__content",
53 "card_footer": "card__footer",
54 "hero": "hero",
55 "hero_body": "hero__body",
56 "section": "section",
57 "container": "container",
58 "columns": "grid",
59 "column": "grid__item",
60 "navbar": "navbar",
61 "navbar_brand": "navbar__brand",
62 "navbar_menu": "navbar__menu",
63 "navbar_item": "navbar__item",
64 "footer": "footer",
65 "modal": "modal",
66 "modal_background": "modal__background",
67 "modal_content": "modal__content",
68 "modal_close": "modal__close",
69 "notification": "notification",
70 "tag": "tag",
71 "title": "title",
72 "subtitle": "subtitle",
73 }
75 def __init__(self) -> None:
76 """Initialize Vanilla CSS adapter."""
77 super().__init__()
78 self.settings = VanillaStyleSettings()
80 # Register with Oneiric resolver (fail gracefully if not supported)
81 with suppress(Exception):
82 depends.set(self)
84 def get_stylesheet_links(self) -> list[str]:
85 """Generate link tags for custom CSS files."""
86 return [
87 f'<link rel="stylesheet" href="{css_path}">'
88 for css_path in self.settings.css_paths
89 ]
91 def get_component_class(self, component: str) -> str:
92 """Get semantic class names for components."""
93 if component is None:
94 return ""
95 return self.COMPONENT_CLASSES.get(component, component)
97 def get_css_variables(self) -> str:
98 """Generate CSS custom properties (variables) style block."""
99 if not self.settings.css_variables:
100 return ""
102 variables = [
103 f" --{prop}: {value};"
104 for prop, value in self.settings.css_variables.items()
105 ]
107 return ":root {\n" + "\n".join(variables) + "\n}"
109 @staticmethod
110 def get_utility_classes() -> dict[str, str]:
111 """Get semantic utility classes for common patterns."""
112 return {
113 "text_center": "text--center",
114 "text_left": "text--left",
115 "text_right": "text--right",
116 "text_weight_bold": "text--bold",
117 "text_weight_light": "text--light",
118 "background_primary": "bg--primary",
119 "background_secondary": "bg--secondary",
120 "text_primary": "text--primary",
121 "text_secondary": "text--secondary",
122 "margin_small": "m--sm",
123 "margin_medium": "m--md",
124 "margin_large": "m--lg",
125 "padding_small": "p--sm",
126 "padding_medium": "p--md",
127 "padding_large": "p--lg",
128 "is_hidden": "hidden",
129 "is_visible": "visible",
130 "is_responsive": "responsive",
131 }
133 def build_component_html(
134 self, component: str, content: str = "", **attributes: Any
135 ) -> str:
136 """Build complete HTML component with semantic classes."""
137 css_class = self.get_component_class(component)
139 # Add any additional classes
140 if "class" in attributes:
141 css_class = f"{css_class} {attributes.pop('class')}"
143 # Build attributes string
144 attr_parts = [f'class="{css_class}"']
145 for key, value in attributes.items():
146 if key not in ("transformations"): # Skip internal attributes
147 attr_parts.append(f'{key}="{value}"')
149 attrs_str = " ".join(attr_parts)
151 # Determine the appropriate HTML tag based on component type
152 if component.startswith("button"):
153 return f"<button {attrs_str}>{content}</button>"
154 elif component in ("input", "textarea", "select"):
155 return f"<{component} {attrs_str}>"
156 elif component == "field":
157 return f"<div {attrs_str}>{content}</div>"
159 return f"<div {attrs_str}>{content}</div>"
161 @staticmethod
162 def generate_base_css() -> str:
163 """Generate a basic CSS foundation for vanilla styling."""
164 return """
165/* FastBlocks Vanilla CSS Base */
166:root {
167 --primary-color: #007bff;
168 --secondary-color: #6c757d;
169 --success-color: #28a745;
170 --danger-color: #dc3545;
171 --warning-color: #ffc107;
172 --info-color: #17a2b8;
173 --light-color: #f8f9fa;
174 --dark-color: #343a40;
175}
177.btn {
178 display: inline-block;
179 padding: 0.375rem 0.75rem;
180 margin-bottom: 0;
181 font-size: 1rem;
182 line-height: 1.5;
183 text-align: center;
184 text-decoration: none;
185 vertical-align: middle;
186 cursor: pointer;
187 border: 1px solid transparent;
188 border-radius: 0.25rem;
189 transition: all 0.15s ease-in-out;
190}
192.btn--primary { background-color: var(--primary-color); color: white; }
193.btn--secondary { background-color: var(--secondary-color); color: white; }
194.btn--success { background-color: var(--success-color); color: white; }
195.btn--danger { background-color: var(--danger-color); color: white; }
197.form__field { margin-bottom: 1rem; }
198.form__input, .form__textarea, .form__select {
199 display: block;
200 width: 100%;
201 padding: 0.375rem 0.75rem;
202 font-size: 1rem;
203 line-height: 1.5;
204 border: 1px solid #ced4da;
205 border-radius: 0.25rem;
206}
208.container { max-width: 1200px; margin: 0 auto; padding: 0 15px; }
209.grid { display: grid; gap: 1rem; }
210.card { border: 1px solid #dee2e6; border-radius: 0.25rem; }
211"""
214StyleSettings = VanillaStyleSettings
215Style = VanillaStyle
217# Register with Oneiric resolver (fail gracefully if not supported)
218with suppress(Exception):
219 depends.set(Style, "vanilla")
221__all__ = ["VanillaStyle", "VanillaStyleSettings", "Style", "StyleSettings"]