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: README.md
+207-8Lines changed: 207 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,23 +73,23 @@ In `button.html`, create a simple HTML button. Use `{{ slot }}` to indicate wher
73
73
```htmldjango
74
74
{# templates/bird/button.html #}
75
75
<button>
76
-
{{ slot }}
76
+
{{ slot }}
77
77
</button>
78
78
```
79
79
80
80
To use your component in a Django template, use the `{% bird %}` templatetag. The content between `{% bird %}` and `{% endbird %}` becomes the `{{ slot }}` content.
81
81
82
82
```htmldjango
83
83
{% bird button %}
84
-
Click me!
84
+
Click me!
85
85
{% endbird %}
86
86
```
87
87
88
88
django-bird automatically recognizes components in the bird directory, so no manual registration is needed. When Django processes the template, django-bird replaces the `{% bird %}` tag with the component's HTML, inserting the provided content into the slot, resulting in:
89
89
90
90
```html
91
91
<button>
92
-
Click me!
92
+
Click me!
93
93
</button>
94
94
```
95
95
@@ -110,13 +110,118 @@ For a full overview of the features and configuration options, please refer to t
110
110
111
111
## Roadmap
112
112
113
-
Here's a roadmap of features I'm considering for django-bird:
113
+
Below are a bunch of features I'd like to bring to django-bird.
114
+
115
+
I have included code snippets where applicable, but they are back-of-the-napkin sketches of potential APIs -- subject to change if and when the feature is actually introduced.
114
116
115
117
### Static Asset Collection
116
118
117
-
This is table stakes for a modern Django template component library. You should be able to define CSS and JS for a component and have it loaded automatically when you use that component.
119
+
This is table stakes for a modern Django template component library. The goal is to allow you to define CSS and JS for a component and have it loaded automatically when you use that component.
120
+
121
+
Unlike django-components, which uses the Django forms library pattern with a `class Media` declaration, the idea is to allow defining styles and scripts within a single component file, or adjacent to a component. django-bird would then collect and compile these assets, streamlining the whole process.
122
+
123
+
Here's a potential example of how you might define a button component with inline styles and scripts:
124
+
125
+
```htmldjango
126
+
{# templates/bird/button.html #}
127
+
<button>
128
+
{{ slot }}
129
+
</button>
130
+
<style>
131
+
button {
132
+
background-color: red;
133
+
padding: 10px 20px;
134
+
color: white;
135
+
border: none;
136
+
cursor: pointer;
137
+
}
138
+
</style>
139
+
<script>
140
+
$bird.addEventListener('click', () => {
141
+
alert('This specific button was clicked!');
142
+
});
143
+
</script>
144
+
```
145
+
146
+
The `$bird` variable in the JavaScript is a potential special identifier that could be used to scope the script to the specific component instance.
147
+
148
+
Alternatively, you could potentially separate the styles and scripts into their own files:
149
+
150
+
```htmldjango
151
+
{# templates/bird/button.html #}
152
+
<button>
153
+
{{ slot }}
154
+
</button>
155
+
```
156
+
157
+
```css
158
+
/* templates/bird/button.css */
159
+
button {
160
+
background-color: red;
161
+
padding: 10px 20px;
162
+
color: white;
163
+
border: none;
164
+
cursor: pointer;
165
+
}
166
+
```
167
+
168
+
```javascript
169
+
// templates/bird/button.js
170
+
$bird.addEventListener('click', () => {
171
+
alert('This specific button was clicked!');
172
+
});
173
+
```
174
+
175
+
To use this component and include its assets in your template, the API might look something like this:
176
+
177
+
```htmldjango
178
+
<html>
179
+
<head>
180
+
{% django_bird_css %}
181
+
</head>
182
+
<body>
183
+
{% bird button %}
184
+
Click me
185
+
{% endbird %}
186
+
187
+
{% django_bird_js %}
188
+
</body>
189
+
</html>
190
+
```
191
+
192
+
In this conceptual setup, `{% django_bird_css %}` and `{% django_bird_js %}` would automatically include the collected and compiled CSS and JavaScript for all components used in the template.
118
193
119
-
Unlike django-components, which uses the Django forms library pattern with a `class Media` declaration, I want to allow defining styles and scripts within a single component file. django-bird will then collect and compile these assets, streamlining the whole process.
194
+
To give you an idea of what the final compiled output might look like, here's a hypothetical example of the HTML that could be generated:
@@ -126,24 +231,118 @@ If you're new to component islands, think of it as fancy lazy-loading. You can s
126
231
127
232
There's a neat library from the 11ty team called is-land that could work well here. I'll probably start by integrating it directly, but after looking at their source code, I might end up bringing it in and customizing it for django-bird's specific needs.
128
233
234
+
Here's how the API forcomponent islands might lookin django-bird:
235
+
236
+
```htmldjango
237
+
{% bird button on="load" %}
238
+
This button loads... on load
239
+
{% endbird %}
240
+
241
+
{% bird button on="idle" %}
242
+
This button loads after page load
243
+
{% endbird %}
244
+
245
+
{% bird button on="visible" %}
246
+
This button loads when scrolled into view
247
+
{% endbird %}
248
+
```
249
+
250
+
In this example, we're using an `on` attribute to specify when each button's JavaScript should be loaded and executed. This approach could significantly improve page load times and performance, especially for pages with many interactive components.
251
+
129
252
### Custom HTML Tag
130
253
131
254
I'm a huge fan of the approaches taken by libraries like [django-cotton](https://github.com/wrabit/django-cotton), [dj-angles](https://github.com/adamghill/dj-angles), and Laravel's [Flux](https://fluxui.dev). They let you use custom HTML-like elements that compile down to native templatetags during loading.
132
255
133
256
This gives you the full power of Django's template language, but in a much nicer package. Compare [this django-allauth template](https://github.com/pennersr/django-allauth/blob/f03ff4dd48e5b1680a57dca56617bf94c928f2cf/allauth/templates/account/email.html) with [these django-cotton examples](https://github.com/wrabit/django-cotton#walkthrough). The allauth template, while powerful, is a mess of tags that barely resembles HTML. The cotton templates, on the other hand, look like clean, custom web elements.
134
257
135
-
After working with devs from the JavaScript world and using a handful of JavaScript frameworks myself, Django templates can feel ancient compared to JSX. A custom HTML tag approach won't solve all these issues, but it's a step in the right direction.
258
+
After working with devs from the JavaScript world and using a handful of JavaScript frameworks myself, Django templates can feel ancient compared to JSX. A custom HTML tag approach could offer a more familiar and readable syntax for component-based development in Django, bridging the gap between traditional Django templates and modern frontend practices.
259
+
260
+
Here's a comparison of how a button component might be used with the current django-bird syntax and a potential custom HTML tag syntax:
261
+
262
+
```htmldjango
263
+
{% bird button %}
264
+
Click me
265
+
{% endbird %}
266
+
267
+
<bird:button>
268
+
Click me
269
+
</bird:button>
270
+
```
136
271
137
272
### Scoped CSS Styles
138
273
139
274
I love how Svelte and other JS frameworks let you use a simple `<style>` tag with broad selectors (`p` instead of `.card-body`, `button` instead of `.submit-btn`), then scope those styles to just that component. While I'm a Tailwind CSS fan, having this escape hatch for quick style tweaks would be fantastic.
140
275
276
+
Here's how a component with scoped styles might look in django-bird:
277
+
278
+
```htmldjango
279
+
{# templates/bird/button.html #}
280
+
<button>
281
+
{{ slot }}
282
+
</button>
283
+
<style>
284
+
button {
285
+
background-color: red;
286
+
}
287
+
</style>
288
+
```
289
+
290
+
You would use this component in your template like this:
291
+
292
+
```htmldjango
293
+
<html>
294
+
<head>
295
+
{% django_bird_css %}
296
+
</head>
297
+
<body>
298
+
{% bird button %}
299
+
Click me
300
+
{% endbird %}
301
+
</body>
302
+
</html>
303
+
```
304
+
305
+
And here's a potential example of how django-bird might compile this to ensure the styles are scoped to just this component:
306
+
307
+
```htmldjango
308
+
<html>
309
+
<head>
310
+
<style>
311
+
#bird-12fdsa33 {
312
+
button {
313
+
background-color: red;
314
+
}
315
+
}
316
+
</style>
317
+
</head>
318
+
<body>
319
+
<button id="bird-12fdsa33">
320
+
Click me
321
+
</button>
322
+
</body>
323
+
</html>
324
+
```
325
+
141
326
### Integration with Tailwind CSS
142
327
143
-
Hot 🔥 take: ditch most of Tailwind's atomic classes and write your styles in a CSS file (shocking, I know!), but process it with Tailwind. This gives you modern CSS power without the atomic class juggling, plus you still get to use Tailwind's awesome design system -- which in my mind is _the_ reason to use Tailwind CSS. I could take or leave the atomic styles, but that design system I cannot develop without.
328
+
Hot 🔥 take: if you're using Tailwind, you should ditch most of Tailwind's atomic classes and write your styles in a CSS file (shocking, I know!), but process it with Tailwind. This gives you modern CSS power without the atomic class juggling, plus you still get to use Tailwind's awesome design system -- which in my mind is _the_ reason to use Tailwind CSS. I could take or leave the atomic styles, but that design system I cannot develop without.
144
329
145
330
I'd love for django-bird components to support this workflow, letting you write clean, Tailwind-processed styles right in your components.
146
331
332
+
Here's how a button component using Tailwind's design system might look in django-bird:
333
+
334
+
```htmldjango
335
+
{# templates/bird/button.html #}
336
+
<button>
337
+
{{ slot }}
338
+
</button>
339
+
<style>
340
+
button {
341
+
background-color: theme("colors.red.500");
342
+
}
343
+
</style>
344
+
```
345
+
147
346
## License
148
347
149
348
`django-bird` is licensed under the MIT license. See the [`LICENSE`](LICENSE) file for more information.
0 commit comments