You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/components.md
+101-7Lines changed: 101 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -9,23 +9,117 @@ from django_idom.components import static_css
9
9
@component
10
10
defMyComponent():
11
11
return html.div(
12
-
static_css("/static/css/buttons.css"),
13
-
html.button("My Button!")
12
+
static_css("css/buttons.css"),
13
+
html.button("My Button!"),
14
14
)
15
15
```
16
16
17
17
??? question "Should I put `static_css` at the top of my component?"
18
18
19
-
<!--Yes, to ensure proper load order -->
19
+
Yes, if the stylesheet is contains styling for your component.
20
20
21
-
??? question "Can I load HTML using `html.link`?"
21
+
??? question "Can I load CSS using `html.link` instead?"
22
22
23
-
??? question "What about external stylesheets?"
23
+
While you can load a with `html.link`, keep in mind that loading this way **does not** ensure load order. Thus, your stylesheet will likely be loaded after your component is displayed.
??? question "Why not load my CSS in `#!html <head>`?"
26
59
27
-
<!-- Generally, Django stylesheets are loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag. -->
60
+
Traditionally, stylesheets are loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag.
61
+
62
+
Instead, you can use the `static_css` component to help improve webpage load times to deferring loading stylesheets until they are needed.
28
63
29
64
## Static JavaScript
30
65
31
-
<!-- In progress -->
66
+
Allows you to defer loading JavaScript until a component begins rendering. This JavaScript must be stored within [Django's static files](https://docs.djangoproject.com/en/dev/howto/static-files/).
67
+
68
+
```python
69
+
from idom import component, html
70
+
from django_idom.components import static_js
71
+
72
+
@component
73
+
defMyComponent():
74
+
return html.div(
75
+
html.button("My Button!"),
76
+
static_js("js/scripts.js"),
77
+
)
78
+
```
79
+
80
+
??? question "Should I put `static_js` at the bottom of my component?"
81
+
82
+
Yes, if your scripts are reliant on the contents of the component.
83
+
84
+
??? question "Can I load JavaScript using `html.script` instead?"
85
+
86
+
While you can load with `html.script`, keep in mind that loading this way **does not** ensure load order. Thus, your JavaScript will likely be loaded at an arbitrary time after your component is displayed.
87
+
88
+
Here's an example on this use case:
89
+
90
+
```python
91
+
from idom import component, html
92
+
from django_idom.components import static_js
93
+
from django.templatetags.static import static
94
+
95
+
@component
96
+
def MyComponent():
97
+
return html.div(
98
+
html.script({"src": static("js/scripts.js")}),
99
+
html.button("My Button!"),
100
+
)
101
+
```
102
+
103
+
??? question "How do I load external JS?"
104
+
105
+
`static_js` can only be used with local static files.
106
+
107
+
For external JavaScript, substitute `static_js` with `html.script` as such:
0 commit comments