Skip to content

Commit 4cc86aa

Browse files
committed
flesh out the docs
1 parent a19508f commit 4cc86aa

File tree

1 file changed

+101
-7
lines changed

1 file changed

+101
-7
lines changed

docs/features/components.md

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,117 @@ from django_idom.components import static_css
99
@component
1010
def MyComponent():
1111
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!"),
1414
)
1515
```
1616

1717
??? question "Should I put `static_css` at the top of my component?"
1818

19-
<!-- Yes, to ensure proper load order -->
19+
Yes, if the stylesheet is contains styling for your component.
2020

21-
??? question "Can I load HTML using `html.link`?"
21+
??? question "Can I load CSS using `html.link` instead?"
2222

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.
24+
25+
Here's an example on this use case:
26+
27+
```python
28+
from idom import component, html
29+
from django_idom.components import static_js
30+
from django.templatetags.static import static
31+
32+
@component
33+
def MyComponent():
34+
return html.div(
35+
html.link({"rel": "stylesheet", "href": static("css/buttons.css")}),
36+
html.button("My Button!"),
37+
)
38+
```
39+
40+
??? question "How do I load external CSS?"
41+
42+
`static_css` can only be used with local static files.
43+
44+
For external CSS, substitute `static_css` with `html.link` as such:
45+
46+
```python
47+
from idom import component, html
48+
from django_idom.components import static_js
49+
50+
@component
51+
def MyComponent():
52+
return html.div(
53+
html.link({"rel": "stylesheet", "href": "https://example.com/external-styles.css"}),
54+
html.button("My Button!"),
55+
)
56+
```
2457

2558
??? question "Why not load my CSS in `#!html <head>`?"
2659

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.
2863

2964
## Static JavaScript
3065

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+
def MyComponent():
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:
108+
109+
```python
110+
from idom import component, html
111+
from django_idom.components import static_js
112+
113+
@component
114+
def MyComponent():
115+
return html.div(
116+
html.script({"src": static("https://example.com/external-scripts.js")}),
117+
html.button("My Button!"),
118+
)
119+
```
120+
121+
??? question "Why not load my JS in `#!html <head>`?"
122+
123+
Traditionally, JavaScript is loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag.
124+
125+
Instead, you can use the `static_js` component to help improve webpage load times to deferring loading scripts until they are needed.

0 commit comments

Comments
 (0)