27
27
from reactpy_django .types import AsyncFormEvent , SyncFormEvent , ViewToComponentConstructor , ViewToIframeConstructor
28
28
29
29
30
- DjangoJS = web .export (
31
- web .module_from_file ("reactpy-django" , file = Path (__file__ ).parent / "static" / "reactpy_django" / "client.js" ),
32
- ("OnlyOnceJS" ),
33
- )
34
-
35
-
36
30
def view_to_component (
37
31
view : Callable | View | str ,
38
32
transforms : Sequence [Callable [[VdomDict ], Any ]] = (),
@@ -92,7 +86,9 @@ def constructor(
92
86
return constructor
93
87
94
88
95
- def django_css (static_path : str , key : Key | None = None ) -> ComponentType :
89
+ def django_css (
90
+ static_path : str , only_once : bool = False , auto_remove : bool = False , key : Key | None = None
91
+ ) -> ComponentType :
96
92
"""Fetches a CSS static file for use within ReactPy. This allows for deferred CSS loading.
97
93
98
94
Args:
@@ -102,11 +98,11 @@ def django_css(static_path: str, key: Key | None = None) -> ComponentType:
102
98
immediate siblings
103
99
"""
104
100
105
- return _django_css (static_path = static_path , key = key )
101
+ return _django_css (static_path = static_path , only_once = only_once , auto_remove = auto_remove , key = key )
106
102
107
103
108
104
def django_js (
109
- static_path : str , only_once : bool = False , only_once_auto_remove : bool = False , key : Key | None = None
105
+ static_path : str , only_once : bool = False , auto_remove : bool = False , key : Key | None = None
110
106
) -> ComponentType :
111
107
"""Fetches a JS static file for use within ReactPy. This allows for deferred JS loading.
112
108
@@ -117,9 +113,7 @@ def django_js(
117
113
immediate siblings
118
114
"""
119
115
120
- return _django_js (
121
- static_path = static_path , only_once = only_once , only_once_auto_remove = only_once_auto_remove , key = key
122
- )
116
+ return _django_js (static_path = static_path , only_once = only_once , auto_remove = auto_remove , key = key )
123
117
124
118
125
119
def django_form (
@@ -285,13 +279,30 @@ def _view_to_iframe(
285
279
286
280
287
281
@component
288
- def _django_css (static_path : str ):
282
+ def _django_css (static_path : str , only_once : bool , auto_remove : bool ):
283
+ if only_once :
284
+ return LoadOnlyOnce ({
285
+ "path" : static (static_path ),
286
+ "nodeName" : "link" ,
287
+ "autoRemove" : auto_remove ,
288
+ })
289
+
289
290
return html .style (cached_static_file (static_path ))
290
291
291
292
292
293
@component
293
- def _django_js (static_path : str , only_once : bool , only_once_auto_remove : bool ):
294
+ def _django_js (static_path : str , only_once : bool , auto_remove : bool ):
294
295
if only_once :
295
- return DjangoJS ({"jsPath" : static (static_path ), "autoRemove" : only_once_auto_remove })
296
+ return LoadOnlyOnce ({
297
+ "path" : static (static_path ),
298
+ "nodeName" : "script" ,
299
+ "autoRemove" : auto_remove ,
300
+ })
296
301
297
302
return html .script (cached_static_file (static_path ))
303
+
304
+
305
+ LoadOnlyOnce = web .export (
306
+ web .module_from_file ("reactpy-django" , file = Path (__file__ ).parent / "static" / "reactpy_django" / "client.js" ),
307
+ ("LoadOnlyOnce" ),
308
+ )
0 commit comments