|
| 1 | +## Django CSS |
| 2 | + |
| 3 | +Allows you to defer loading a CSS stylesheet until a component begins rendering. This stylesheet must be stored within [Django's static files](https://docs.djangoproject.com/en/dev/howto/static-files/). |
| 4 | + |
| 5 | +```python title="components.py" |
| 6 | +from idom import component, html |
| 7 | +from django_idom.components import django_css |
| 8 | + |
| 9 | +@component |
| 10 | +def MyComponent(): |
| 11 | + return html.div( |
| 12 | + django_css("css/buttons.css"), |
| 13 | + html.button("My Button!"), |
| 14 | + ) |
| 15 | +``` |
| 16 | + |
| 17 | +??? question "Should I put `django_css` at the top of my component?" |
| 18 | + |
| 19 | + Yes, if the stylesheet contains styling for your component. |
| 20 | + |
| 21 | +??? question "Can I load static CSS using `html.link` instead?" |
| 22 | + |
| 23 | + While you can load stylesheets with `html.link`, keep in mind that loading this way **does not** ensure load order. Thus, your stylesheet will be loaded after your component is displayed. This would likely cause some visual jankiness, so use this at your own discretion. |
| 24 | + |
| 25 | + Here's an example on what you should avoid doing for Django static files: |
| 26 | + |
| 27 | + ```python |
| 28 | + from idom import component, html |
| 29 | + from django.templatetags.static import static |
| 30 | + |
| 31 | + @component |
| 32 | + def MyComponent(): |
| 33 | + return html.div( |
| 34 | + html.link({"rel": "stylesheet", "href": static("css/buttons.css")}), |
| 35 | + html.button("My Button!"), |
| 36 | + ) |
| 37 | + ``` |
| 38 | + |
| 39 | +??? question "How do I load external CSS?" |
| 40 | + |
| 41 | + `django_css` can only be used with local static files. |
| 42 | + |
| 43 | + For external CSS, substitute `django_css` with `html.link`. |
| 44 | + |
| 45 | + ```python |
| 46 | + from idom import component, html |
| 47 | + |
| 48 | + @component |
| 49 | + def MyComponent(): |
| 50 | + return html.div( |
| 51 | + html.link({"rel": "stylesheet", "href": "https://example.com/external-styles.css"}), |
| 52 | + html.button("My Button!"), |
| 53 | + ) |
| 54 | + ``` |
| 55 | + |
| 56 | +??? question "Why not load my CSS in `#!html <head>`?" |
| 57 | + |
| 58 | + Traditionally, stylesheets are loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag. |
| 59 | + |
| 60 | + To help improve webpage load times, you can use the `django_css` component to defer loading your stylesheet until it is needed. |
| 61 | + |
| 62 | +## Django JS |
| 63 | + |
| 64 | +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/). |
| 65 | + |
| 66 | +```python title="components.py" |
| 67 | +from idom import component, html |
| 68 | +from django_idom.components import django_js |
| 69 | + |
| 70 | +@component |
| 71 | +def MyComponent(): |
| 72 | + return html.div( |
| 73 | + html.button("My Button!"), |
| 74 | + django_js("js/scripts.js"), |
| 75 | + ) |
| 76 | +``` |
| 77 | + |
| 78 | +??? question "Should I put `django_js` at the bottom of my component?" |
| 79 | + |
| 80 | + Yes, if your scripts are reliant on the contents of the component. |
| 81 | + |
| 82 | +??? question "Can I load static JavaScript using `html.script` instead?" |
| 83 | + |
| 84 | + While you can load JavaScript 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. |
| 85 | + |
| 86 | + Here's an example on what you should avoid doing for Django static files: |
| 87 | + |
| 88 | + ```python |
| 89 | + from idom import component, html |
| 90 | + from django.templatetags.static import static |
| 91 | + |
| 92 | + @component |
| 93 | + def MyComponent(): |
| 94 | + return html.div( |
| 95 | + html.script({"src": static("js/scripts.js")}), |
| 96 | + html.button("My Button!"), |
| 97 | + ) |
| 98 | + ``` |
| 99 | + |
| 100 | +??? question "How do I load external JS?" |
| 101 | + |
| 102 | + `django_js` can only be used with local static files. |
| 103 | + |
| 104 | + For external JavaScript, substitute `django_js` with `html.script`. |
| 105 | + |
| 106 | + ```python |
| 107 | + from idom import component, html |
| 108 | + |
| 109 | + @component |
| 110 | + def MyComponent(): |
| 111 | + return html.div( |
| 112 | + html.script({"src": static("https://example.com/external-scripts.js")}), |
| 113 | + html.button("My Button!"), |
| 114 | + ) |
| 115 | + ``` |
| 116 | + |
| 117 | +??? question "Why not load my JS in `#!html <head>`?" |
| 118 | + |
| 119 | + Traditionally, JavaScript is loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag. |
| 120 | + |
| 121 | + To help improve webpage load times, you can use the `django_js` component to defer loading your JavaScript until it is needed. |
0 commit comments