11"""Tailwind CSS configuration types for Reflex plugins."""
22
33import dataclasses
4+ from collections .abc import Mapping
45from copy import deepcopy
56from typing import Any , Literal , TypedDict
67
@@ -81,7 +82,7 @@ class TailwindConfig(TypedDict):
8182def tailwind_config_js_template (
8283 * , default_content : list [str ], ** kwargs : Unpack [TailwindConfig ]
8384):
84- """Get the Tailwind config template .
85+ """Generate a Tailwind CSS configuration file in JavaScript format .
8586
8687 Args:
8788 default_content: The default content to use if none is provided.
@@ -107,92 +108,68 @@ def tailwind_config_js_template(
107108 imports = [
108109 plugin ["import" ]
109110 for plugin in plugins
110- if isinstance (plugin , dict ) and "import" in plugin
111+ if isinstance (plugin , Mapping ) and "import" in plugin
111112 ]
112113
113114 # Generate import statements for destructured imports
114- import_lines = [
115- f"import {{ { imp ['name' ]} }} from { json .dumps (imp ['from' ])} ;" for imp in imports
116- ]
115+ import_lines = "\n " .join (
116+ [
117+ f"import {{ { imp ['name' ]} }} from { json .dumps (imp ['from' ])} ;"
118+ for imp in imports
119+ ]
120+ )
117121
118122 # Generate plugin imports
119123 plugin_imports = []
120124 for i , plugin in enumerate (plugins , 1 ):
121- if isinstance (plugin , dict ) and "call" not in plugin :
125+ if isinstance (plugin , Mapping ) and "call" not in plugin :
122126 plugin_imports .append (
123127 f"import plugin{ i } from { json .dumps (plugin ['name' ])} ;"
124128 )
125- elif not isinstance (plugin , dict ):
129+ elif not isinstance (plugin , Mapping ):
126130 plugin_imports .append (f"import plugin{ i } from { json .dumps (plugin )} ;" )
127131
128- # Generate preset imports
129- preset_imports = [
130- f"import preset{ i } from { json .dumps (preset )} ;"
131- for i , preset in enumerate (presets , 1 )
132- ]
132+ plugin_imports_lines = "\n " .join (plugin_imports )
133133
134- # Generate preset array
135- preset_array = ""
136- if presets :
137- preset_list = [f" preset{ i } ," for i in range (1 , len (presets ) + 1 )]
138- preset_array = f""" presets: [
139- { chr (10 ).join (preset_list )}
140- ],"""
134+ presets_imports_lines = "\n " .join (
135+ [
136+ f"import preset{ i } from { json .dumps (preset )} ;"
137+ for i , preset in enumerate (presets , 1 )
138+ ]
139+ )
141140
142141 # Generate plugin array
143142 plugin_list = []
144143 for i , plugin in enumerate (plugins , 1 ):
145- if isinstance (plugin , dict ) and "call" in plugin :
144+ if isinstance (plugin , Mapping ) and "call" in plugin :
146145 args_part = ""
147146 if "args" in plugin :
148147 args_part = json .dumps (plugin ["args" ])
149- plugin_list .append (f" { plugin ['call' ]} ({ args_part } ), " )
148+ plugin_list .append (f"{ plugin ['call' ]} ({ args_part } )" )
150149 else :
151- plugin_list .append (f" plugin{ i } ," )
152-
153- # Build the config
154- all_imports = import_lines + plugin_imports + preset_imports
155- imports_section = "\n " .join (all_imports )
156- if imports_section :
157- imports_section += "\n "
158-
159- content_value = json .dumps (content if content is not None else default_content )
160- theme_part = f"theme: { json .dumps (theme )} ," if theme is not None else "theme: {},"
161- dark_mode_part = (
162- f" darkMode: { json .dumps (dark_mode )} ," if dark_mode is not None else ""
163- )
164- core_plugins_part = (
165- f" corePlugins: { json .dumps (core_plugins )} ,"
166- if core_plugins is not None
167- else ""
168- )
169- important_part = (
170- f" important: { json .dumps (important )} ," if important is not None else ""
171- )
172- prefix_part = f" prefix: { json .dumps (prefix )} ," if prefix is not None else ""
173- separator_part = (
174- f" separator: { json .dumps (separator )} ," if separator is not None else ""
175- )
150+ plugin_list .append (f"plugin{ i } " )
176151
177- plugins_section = "\n " .join (plugin_list )
178-
179- config_parts = [
180- f" content: { content_value } ," ,
181- f" { theme_part } " ,
182- dark_mode_part ,
183- core_plugins_part ,
184- important_part ,
185- prefix_part ,
186- separator_part ,
187- preset_array ,
188- f" plugins: [\n { plugins_section } \n ]" ,
189- ]
152+ plugin_use_str = "," .join (plugin_list )
153+
154+ return rf"""
155+ { import_lines }
156+
157+ { plugin_imports_lines }
190158
191- config_body = " \n " . join ( part for part in config_parts if part . strip ())
159+ { presets_imports_lines }
192160
193- return f"""{ imports_section } export default {{
194- { config_body }
195- }};"""
161+ export default {{
162+ content: { json .dumps (content if content else default_content )} ,
163+ theme: { json .dumps (theme if theme else {})} ,
164+ { f"darkMode: { json .dumps (dark_mode )} ," if dark_mode is not None else "" }
165+ { f"corePlugins: { json .dumps (core_plugins )} ," if core_plugins is not None else "" }
166+ { f"importants: { json .dumps (important )} ," if important is not None else "" }
167+ { f"prefix: { json .dumps (prefix )} ," if prefix is not None else "" }
168+ { f"separator: { json .dumps (separator )} ," if separator is not None else "" }
169+ { f"presets: [{ ', ' .join (f'preset{ i } ' for i in range (1 , len (presets ) + 1 ))} ]," if presets else "" }
170+ plugins: [{ plugin_use_str } ]
171+ }};
172+ """
196173
197174
198175@dataclasses .dataclass
0 commit comments